private static bool ProcessOuterApplyOverProject(
            RuleProcessingContext context,
            System.Data.Entity.Core.Query.InternalTrees.Node applyNode,
            out System.Data.Entity.Core.Query.InternalTrees.Node newNode)
        {
            newNode = applyNode;
            System.Data.Entity.Core.Query.InternalTrees.Node child1_1 = applyNode.Child1;
            System.Data.Entity.Core.Query.InternalTrees.Node child1_2 = child1_1.Child1;
            TransformationRulesContext transformationRulesContext     = (TransformationRulesContext)context;
            Var computedVar = context.Command.GetExtendedNodeInfo(child1_1.Child0).NonNullableDefinitions.First;

            if (computedVar == null && child1_2.Children.Count == 1 && (child1_2.Child0.Child0.Op.OpType == OpType.InternalConstant || child1_2.Child0.Child0.Op.OpType == OpType.NullSentinel))
            {
                return(false);
            }
            Command command = context.Command;

            System.Data.Entity.Core.Query.InternalTrees.Node node1 = (System.Data.Entity.Core.Query.InternalTrees.Node)null;
            InternalConstantOp internalConstantOp = (InternalConstantOp)null;
            ExtendedNodeInfo   extendedNodeInfo1  = command.GetExtendedNodeInfo(child1_1.Child0);
            bool flag = false;

            foreach (System.Data.Entity.Core.Query.InternalTrees.Node child in child1_2.Children)
            {
                System.Data.Entity.Core.Query.PlanCompiler.PlanCompiler.Assert(child.Op.OpType == OpType.VarDef, "Expected VarDefOp. Found " + (object)child.Op.OpType + " instead");
                VarRefOp op = child.Child0.Op as VarRefOp;
                if (op == null || !extendedNodeInfo1.Definitions.IsSet(op.Var))
                {
                    if (computedVar == null)
                    {
                        internalConstantOp = command.CreateInternalConstantOp(command.IntegerType, (object)1);
                        System.Data.Entity.Core.Query.InternalTrees.Node node2          = command.CreateNode((Op)internalConstantOp);
                        System.Data.Entity.Core.Query.InternalTrees.Node varDefListNode = command.CreateVarDefListNode(node2, out computedVar);
                        ProjectOp projectOp = command.CreateProjectOp(computedVar);
                        projectOp.Outputs.Or(extendedNodeInfo1.Definitions);
                        node1 = command.CreateNode((Op)projectOp, child1_1.Child0, varDefListNode);
                    }
                    System.Data.Entity.Core.Query.InternalTrees.Node node3 = internalConstantOp == null || !internalConstantOp.IsEquivalent(child.Child0.Op) && child.Child0.Op.OpType != OpType.NullSentinel ? transformationRulesContext.BuildNullIfExpression(computedVar, child.Child0) : command.CreateNode((Op)command.CreateVarRefOp(computedVar));
                    child.Child0 = node3;
                    command.RecomputeNodeInfo(child);
                    flag = true;
                }
            }
            if (flag)
            {
                command.RecomputeNodeInfo(child1_2);
            }
            applyNode.Child1 = node1 ?? child1_1.Child0;
            command.RecomputeNodeInfo(applyNode);
            child1_1.Child0 = applyNode;
            ExtendedNodeInfo extendedNodeInfo2 = command.GetExtendedNodeInfo(applyNode.Child0);

            ((ProjectOp)child1_1.Op).Outputs.Or(extendedNodeInfo2.Definitions);
            newNode = child1_1;
            return(true);
        }
Example #2
0
        private static bool ProcessLikeOverConstant(
            RuleProcessingContext context,
            System.Data.Entity.Core.Query.InternalTrees.Node n,
            out System.Data.Entity.Core.Query.InternalTrees.Node newNode)
        {
            newNode = n;
            InternalConstantOp op1  = (InternalConstantOp)n.Child1.Op;
            InternalConstantOp op2  = (InternalConstantOp)n.Child0.Op;
            string             str1 = (string)op2.Value;
            string             str2 = (string)op1.Value;
            bool?nullable           = ScalarOpRules.MatchesPattern((string)op2.Value, (string)op1.Value);

            if (!nullable.HasValue)
            {
                return(false);
            }
            ConstantPredicateOp constantPredicateOp = context.Command.CreateConstantPredicateOp(nullable.Value);

            newNode = context.Command.CreateNode((Op)constantPredicateOp);
            return(true);
        }
Example #3
0
        /// <summary>
        /// Copies an InternalConstantOp
        /// </summary>
        /// <param name="op">The Op to Copy</param>
        /// <param name="n">The Node that references the Op</param>
        /// <returns>A copy of the original Node that references a copy of the original Op</returns>
        public override Node Visit(InternalConstantOp op, Node n)
        {
            InternalConstantOp newOp = m_destCmd.CreateInternalConstantOp(op.Type, op.Value);

            return(m_destCmd.CreateNode(newOp));
        }
 /// <summary>
 ///     Visitor pattern method for InternalConstantOp
 /// </summary>
 /// <param name="op"> The InternalConstantOp being visited </param>
 /// <param name="n"> The Node that references the Op </param>
 public virtual void Visit(InternalConstantOp op, Node n)
 {
     VisitConstantOp(op, n);
 }
Example #5
0
        private static bool ProcessOuterApplyOverProject(RuleProcessingContext context, Node applyNode, out Node newNode)
        {
            newNode = applyNode;
            var projectNode    = applyNode.Child1;
            var varDefListNode = projectNode.Child1;

            var trc           = (TransformationRulesContext)context;
            var inputNodeInfo = context.Command.GetExtendedNodeInfo(projectNode.Child0);
            var sentinelVar   = inputNodeInfo.NonNullableDefinitions.First;

            //
            // special case handling first - we'll end up in an infinite loop otherwise.
            // If the ProjectOp is the dummy ProjectOp that we would be building (ie)
            // it defines only 1 var - and the defining expression is simply a constant
            //
            if (sentinelVar == null
                &&
                varDefListNode.Children.Count == 1
                &&
                (varDefListNode.Child0.Child0.Op.OpType == OpType.InternalConstant ||
                 varDefListNode.Child0.Child0.Op.OpType == OpType.NullSentinel))
            {
                return(false);
            }

            var  command          = context.Command;
            Node dummyProjectNode = null;
            InternalConstantOp nullSentinelDefinitionOp = null;

            // get node information for the project's child
            var projectInputNodeInfo = command.GetExtendedNodeInfo(projectNode.Child0);

            //
            // Build up a dummy project node.
            // Walk through each local definition of the current project Node, and convert
            // all expressions into case expressions whose value depends on the var
            // produced by the dummy project node
            //

            // Dev10 #480443: If any of the definitions changes we need to recompute the node info.
            var anyVarDefChanged = false;

            foreach (var varDefNode in varDefListNode.Children)
            {
                PlanCompiler.Assert(varDefNode.Op.OpType == OpType.VarDef, "Expected VarDefOp. Found " + varDefNode.Op.OpType + " instead");
                var varRefOp = varDefNode.Child0.Op as VarRefOp;
                if (varRefOp == null ||
                    !projectInputNodeInfo.Definitions.IsSet(varRefOp.Var))
                {
                    // do we need to build a dummy project node
                    if (sentinelVar == null)
                    {
                        nullSentinelDefinitionOp = command.CreateInternalConstantOp(command.IntegerType, 1);
                        var dummyConstantExpr          = command.CreateNode(nullSentinelDefinitionOp);
                        var dummyProjectVarDefListNode = command.CreateVarDefListNode(dummyConstantExpr, out sentinelVar);
                        var dummyProjectOp             = command.CreateProjectOp(sentinelVar);
                        dummyProjectOp.Outputs.Or(projectInputNodeInfo.Definitions);
                        dummyProjectNode = command.CreateNode(dummyProjectOp, projectNode.Child0, dummyProjectVarDefListNode);
                    }

                    Node currentDefinition;

                    // If the null sentinel was just created, and the local definition of the current project Node
                    // is an internal constant equivalent to the null sentinel, it can be rewritten as a reference
                    // to the null sentinel.
                    if (nullSentinelDefinitionOp != null &&
                        (nullSentinelDefinitionOp.IsEquivalent(varDefNode.Child0.Op) ||
                         //The null sentinel has the same value of 1, thus it is safe.
                         varDefNode.Child0.Op.OpType == OpType.NullSentinel))
                    {
                        currentDefinition = command.CreateNode(command.CreateVarRefOp(sentinelVar));
                    }
                    else
                    {
                        currentDefinition = trc.BuildNullIfExpression(sentinelVar, varDefNode.Child0);
                    }
                    varDefNode.Child0 = currentDefinition;
                    command.RecomputeNodeInfo(varDefNode);
                    anyVarDefChanged = true;
                }
            }

            // Recompute node info if needed
            if (anyVarDefChanged)
            {
                command.RecomputeNodeInfo(varDefListNode);
            }

            //
            // If we've created a dummy project node, make that the new child of the applyOp
            //
            applyNode.Child1 = dummyProjectNode != null ? dummyProjectNode : projectNode.Child0;
            command.RecomputeNodeInfo(applyNode);

            //
            // Pull up the project node above the apply node now. Also, make sure that every Var of
            // the applyNode's definitions actually shows up in the new Project
            //
            projectNode.Child0 = applyNode;
            var applyLeftChildNodeInfo = command.GetExtendedNodeInfo(applyNode.Child0);
            var projectOp = (ProjectOp)projectNode.Op;

            projectOp.Outputs.Or(applyLeftChildNodeInfo.Definitions);

            newNode = projectNode;
            return(true);
        }
Example #6
0
 // <summary>
 // Copies an InternalConstantOp
 // </summary>
 // <param name="op"> The Op to Copy </param>
 // <param name="n"> The Node that references the Op </param>
 // <returns> A copy of the original Node that references a copy of the original Op </returns>
 public override Node Visit(InternalConstantOp op, Node n)
 {
     var newOp = m_destCmd.CreateInternalConstantOp(op.Type, op.Value);
     return m_destCmd.CreateNode(newOp);
 }