Esempio n. 1
0
        public override TableReference VisitDerivedTableReference(DerivedTableReference node)
        {
            AlgebraNode algebrizedQuery = Convert(node.Query);

            List <ComputedValueDefinition> definedValues = new List <ComputedValueDefinition>();

            for (int i = 0; i < node.DerivedTableBinding.ColumnRefs.Length; i++)
            {
                RowBufferEntry targetRowBufferEntry = node.DerivedTableBinding.ColumnRefs[i].ValueDefinition.Target;
                RowBufferEntry sourceRowBufferEntry = algebrizedQuery.OutputList[i];

                ComputedValueDefinition definedValue = new ComputedValueDefinition();
                definedValue.Target     = targetRowBufferEntry;
                definedValue.Expression = new RowBufferEntryExpression(sourceRowBufferEntry);
                definedValues.Add(definedValue);
            }

            ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();

            computeScalarAlgebraNode.Input         = algebrizedQuery;
            computeScalarAlgebraNode.DefinedValues = definedValues.ToArray();
            SetLastAlgebraNode(computeScalarAlgebraNode);

            return(node);
        }
Esempio n. 2
0
        private static void CreateBufferedValue(ExpressionNode expression, ICollection <ComputedValueDefinition> computedColumnList, ICollection <RowBufferEntry> columnList, IEnumerable <ComputedValueDefinition> alreadyComputedBufferedValues)
        {
            if (alreadyComputedBufferedValues != null)
            {
                expression = ReplaceAlreadyComputedSubsequences(expression, alreadyComputedBufferedValues);
            }

            foreach (ComputedValueDefinition computedBufferedValue in computedColumnList)
            {
                if (expression.IsStructuralEqualTo(computedBufferedValue.Expression))
                {
                    columnList.Add(computedBufferedValue.Target);
                    return;
                }
            }

            RowBufferEntryExpression rowBufferExpression = expression as RowBufferEntryExpression;

            if (rowBufferExpression != null)
            {
                columnList.Add(rowBufferExpression.RowBufferEntry);
            }
            else
            {
                RowBufferEntry rowBufferEntry = new RowBufferEntry(expression.ExpressionType);
                columnList.Add(rowBufferEntry);

                ComputedValueDefinition computedValue = new ComputedValueDefinition();
                computedValue.Target     = rowBufferEntry;
                computedValue.Expression = expression;
                computedColumnList.Add(computedValue);
            }
        }
Esempio n. 3
0
		private static AlgebraNode PadLeftWithNullsOnRightSide(JoinAlgebraNode node, RowBufferCreationMode rowBufferCreationMode)
		{
			ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
			computeScalarAlgebraNode.Input = node.Left;

			List<RowBufferEntry> outputList = new List<RowBufferEntry>();
			outputList.AddRange(node.Left.OutputList);

			List<ComputedValueDefinition> definedValues = new List<ComputedValueDefinition>();
			foreach (RowBufferEntry rowBufferEntry in node.Right.OutputList)
			{
				ComputedValueDefinition definedValue = new ComputedValueDefinition();

				if (rowBufferCreationMode == RowBufferCreationMode.KeepExisting)
				{
					definedValue.Target = rowBufferEntry;
				}
				else
				{
					definedValue.Target = new RowBufferEntry(rowBufferEntry.DataType);
				}

				definedValue.Expression = LiteralExpression.FromTypedNull(rowBufferEntry.DataType);
				definedValues.Add(definedValue);
				outputList.Add(definedValue.Target);
			}

			computeScalarAlgebraNode.DefinedValues = definedValues.ToArray();
			computeScalarAlgebraNode.OutputList = outputList.ToArray();

			return computeScalarAlgebraNode;
		}
Esempio n. 4
0
        public override AlgebraNode VisitFilterAlgebraNode(FilterAlgebraNode node)
        {
            node.Input = VisitAlgebraNode(node.Input);

            ExpressionNode originalPredicate = (ExpressionNode) node.Predicate.Clone();
            SpoolExpressionExtractor spoolExpressionExtractor = new SpoolExpressionExtractor(_outerReferences);
            // HACK: This hack ensures that TRUE literals introduced by SpoolExpressionExtractor are removed.
            node.Predicate = AstUtil.CombineConditions(LogicalOperator.And, spoolExpressionExtractor.VisitExpression(node.Predicate));
            SpoolExpression[] spoolExpressions = spoolExpressionExtractor.GetSpoolExpressions();

            // Now we must check that the remaining filter incl. input to the filter don't reference any other
            // outer reference.

            bool remainingFilterHasDependenciesToOuterReferences = CheckIfNodeHasDependenciesToOuterReferences(node);

            if (remainingFilterHasDependenciesToOuterReferences)
            {
                // OK; we cannot insert a spool operation here. Undo the expression replacement.
                node.Predicate = originalPredicate;
            }
            else if (spoolExpressions.Length > 0)
            {
                SpoolExpression spoolExpression = spoolExpressions[0];

                AlgebraNode currentInput;

                if (node.Predicate is ConstantExpression)
                    currentInput = node.Input;
                else
                    currentInput = node;

                RowBufferEntry indexEntry;
                RowBufferEntryExpression indexExpressionAsRowBufferEntryExpression = spoolExpression.IndexExpression as RowBufferEntryExpression;

                if (indexExpressionAsRowBufferEntryExpression != null)
                {
                    indexEntry = indexExpressionAsRowBufferEntryExpression.RowBufferEntry;
                }
                else
                {
                    indexEntry = new RowBufferEntry(spoolExpression.IndexExpression.ExpressionType);
                    ComputedValueDefinition definedValue = new ComputedValueDefinition();
                    definedValue.Target = indexEntry;
                    definedValue.Expression = spoolExpression.IndexExpression;

                    ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
                    computeScalarAlgebraNode.Input = currentInput;
                    computeScalarAlgebraNode.DefinedValues = new ComputedValueDefinition[] { definedValue };
                    currentInput = computeScalarAlgebraNode;
                }

                IndexSpoolAlgebraNode indexSpoolAlgebraNode = new IndexSpoolAlgebraNode();
                indexSpoolAlgebraNode.Input = currentInput;
                indexSpoolAlgebraNode.IndexEntry = indexEntry;
                indexSpoolAlgebraNode.ProbeExpression = spoolExpression.ProbeExpression;
                return indexSpoolAlgebraNode;
            }

            return node;
        }
        public override AlgebraNode VisitJoinAlgebraNode(JoinAlgebraNode node)
        {
            base.VisitJoinAlgebraNode(node);

            if (node.Op == JoinAlgebraNode.JoinOperator.FullOuterJoin)
            {
                // TODO: Check if we could represent this join condition by an hash match operator

                JoinAlgebraNode leftOuterJoinNode = new JoinAlgebraNode();
                leftOuterJoinNode.Op         = JoinAlgebraNode.JoinOperator.LeftOuterJoin;
                leftOuterJoinNode.Predicate  = (ExpressionNode)node.Predicate.Clone();
                leftOuterJoinNode.Left       = (AlgebraNode)node.Left.Clone();
                leftOuterJoinNode.Right      = (AlgebraNode)node.Right.Clone();
                leftOuterJoinNode.OutputList = ArrayHelpers.Clone(node.OutputList);

                List <RowBufferEntry> swappedOutputList = new List <RowBufferEntry>();
                swappedOutputList.AddRange(node.Right.OutputList);
                swappedOutputList.AddRange(node.Left.OutputList);

                JoinAlgebraNode leftAntiSemiJoinNode = new JoinAlgebraNode();
                leftAntiSemiJoinNode.Op         = JoinAlgebraNode.JoinOperator.LeftAntiSemiJoin;
                leftAntiSemiJoinNode.Predicate  = (ExpressionNode)node.Predicate.Clone();
                leftAntiSemiJoinNode.Left       = (AlgebraNode)node.Right.Clone();
                leftAntiSemiJoinNode.Right      = (AlgebraNode)node.Left.Clone();
                leftAntiSemiJoinNode.OutputList = swappedOutputList.ToArray();

                List <ComputedValueDefinition> computeScalareDefinedValues = new List <ComputedValueDefinition>();
                foreach (RowBufferEntry rowBufferEntry in node.Left.OutputList)
                {
                    ComputedValueDefinition definedValue = new ComputedValueDefinition();
                    definedValue.Target     = rowBufferEntry;
                    definedValue.Expression = LiteralExpression.FromTypedNull(rowBufferEntry.DataType);
                    computeScalareDefinedValues.Add(definedValue);
                }

                ComputeScalarAlgebraNode computeScalarNode = new ComputeScalarAlgebraNode();
                computeScalarNode.Input         = leftAntiSemiJoinNode;
                computeScalarNode.DefinedValues = computeScalareDefinedValues.ToArray();
                computeScalarNode.OutputList    = swappedOutputList.ToArray();

                List <UnitedValueDefinition> concatDefinedValues = new List <UnitedValueDefinition>();
                for (int i = 0; i < node.OutputList.Length; i++)
                {
                    RowBufferEntry        rowBufferEntry     = node.OutputList[i];
                    UnitedValueDefinition concatDefinedValue = new UnitedValueDefinition();
                    concatDefinedValue.Target           = rowBufferEntry;
                    concatDefinedValue.DependendEntries = new RowBufferEntry[] { node.OutputList[i], node.OutputList[i] };
                    concatDefinedValues.Add(concatDefinedValue);
                }

                ConcatAlgebraNode concatenationNode = new ConcatAlgebraNode();
                concatenationNode.Inputs        = new AlgebraNode[] { leftOuterJoinNode, computeScalarNode };
                concatenationNode.DefinedValues = concatDefinedValues.ToArray();
                concatenationNode.OutputList    = swappedOutputList.ToArray();

                return(concatenationNode);
            }

            return(node);
        }
Esempio n. 6
0
        private static AlgebraNode PadLeftWithNullsOnRightSide(JoinAlgebraNode node, RowBufferCreationMode rowBufferCreationMode)
        {
            ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();

            computeScalarAlgebraNode.Input = node.Left;

            List <RowBufferEntry> outputList = new List <RowBufferEntry>();

            outputList.AddRange(node.Left.OutputList);

            List <ComputedValueDefinition> definedValues = new List <ComputedValueDefinition>();

            foreach (RowBufferEntry rowBufferEntry in node.Right.OutputList)
            {
                ComputedValueDefinition definedValue = new ComputedValueDefinition();

                if (rowBufferCreationMode == RowBufferCreationMode.KeepExisting)
                {
                    definedValue.Target = rowBufferEntry;
                }
                else
                {
                    definedValue.Target = new RowBufferEntry(rowBufferEntry.DataType);
                }

                definedValue.Expression = LiteralExpression.FromTypedNull(rowBufferEntry.DataType);
                definedValues.Add(definedValue);
                outputList.Add(definedValue.Target);
            }

            computeScalarAlgebraNode.DefinedValues = definedValues.ToArray();
            computeScalarAlgebraNode.OutputList    = outputList.ToArray();

            return(computeScalarAlgebraNode);
        }
Esempio n. 7
0
		public override AstElement Clone(Dictionary<AstElement, AstElement> alreadyClonedElements)
		{
			ComputedValueDefinition result = new ComputedValueDefinition();
			result.Target = Target;
			result.Expression = (ExpressionNode)_expression.Clone(alreadyClonedElements);
			return result;
		}
Esempio n. 8
0
		public override AlgebraNode VisitJoinAlgebraNode(JoinAlgebraNode node)
		{
			base.VisitJoinAlgebraNode(node);

			if (node.Op == JoinAlgebraNode.JoinOperator.FullOuterJoin)
			{
				// TODO: Check if we could represent this join condition by an hash match operator

				JoinAlgebraNode leftOuterJoinNode = new JoinAlgebraNode();
				leftOuterJoinNode.Op = JoinAlgebraNode.JoinOperator.LeftOuterJoin;
				leftOuterJoinNode.Predicate = (ExpressionNode)node.Predicate.Clone();
				leftOuterJoinNode.Left = (AlgebraNode) node.Left.Clone();
				leftOuterJoinNode.Right = (AlgebraNode) node.Right.Clone();
				leftOuterJoinNode.OutputList = ArrayHelpers.Clone(node.OutputList);

				List<RowBufferEntry> swappedOutputList = new List<RowBufferEntry>();
				swappedOutputList.AddRange(node.Right.OutputList);
				swappedOutputList.AddRange(node.Left.OutputList);

				JoinAlgebraNode leftAntiSemiJoinNode = new JoinAlgebraNode();
				leftAntiSemiJoinNode.Op = JoinAlgebraNode.JoinOperator.LeftAntiSemiJoin;
				leftAntiSemiJoinNode.Predicate = (ExpressionNode) node.Predicate.Clone();
				leftAntiSemiJoinNode.Left = (AlgebraNode) node.Right.Clone();
				leftAntiSemiJoinNode.Right = (AlgebraNode) node.Left.Clone();
				leftAntiSemiJoinNode.OutputList = swappedOutputList.ToArray();

				List<ComputedValueDefinition> computeScalareDefinedValues = new List<ComputedValueDefinition>();
				foreach (RowBufferEntry rowBufferEntry in node.Left.OutputList)
				{
					ComputedValueDefinition definedValue = new ComputedValueDefinition();
					definedValue.Target = rowBufferEntry;
					definedValue.Expression = LiteralExpression.FromTypedNull(rowBufferEntry.DataType);
					computeScalareDefinedValues.Add(definedValue);
				}

				ComputeScalarAlgebraNode computeScalarNode = new ComputeScalarAlgebraNode();
				computeScalarNode.Input = leftAntiSemiJoinNode;
				computeScalarNode.DefinedValues = computeScalareDefinedValues.ToArray();
				computeScalarNode.OutputList = swappedOutputList.ToArray();

				List<UnitedValueDefinition> concatDefinedValues = new List<UnitedValueDefinition>();
				for (int i = 0; i < node.OutputList.Length; i++)
				{
					RowBufferEntry rowBufferEntry = node.OutputList[i];
					UnitedValueDefinition concatDefinedValue = new UnitedValueDefinition();
					concatDefinedValue.Target = rowBufferEntry;
					concatDefinedValue.DependendEntries = new RowBufferEntry[] { node.OutputList[i], node.OutputList[i] };
					concatDefinedValues.Add(concatDefinedValue);
				}

				ConcatAlgebraNode concatenationNode = new ConcatAlgebraNode();
				concatenationNode.Inputs = new AlgebraNode[] {leftOuterJoinNode, computeScalarNode};
				concatenationNode.DefinedValues = concatDefinedValues.ToArray();
				concatenationNode.OutputList = swappedOutputList.ToArray();

				return concatenationNode;
			}

			return node;
		}
Esempio n. 9
0
        public override AstElement Clone(Dictionary <AstElement, AstElement> alreadyClonedElements)
        {
            ComputedValueDefinition result = new ComputedValueDefinition();

            result.Target     = Target;
            result.Expression = (ExpressionNode)_expression.Clone(alreadyClonedElements);
            return(result);
        }
Esempio n. 10
0
		private void EmitComputeScalarIfNeeded(ComputedValueDefinition[] definedValues)
		{
			if (definedValues != null && definedValues.Length > 0)
			{
				ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
				computeScalarAlgebraNode.Input = GetLastAlgebraNode();
				computeScalarAlgebraNode.DefinedValues = definedValues;
				SetLastAlgebraNode(computeScalarAlgebraNode);
			}
		}
Esempio n. 11
0
        private static AlgebraNode InstantiateCte(AlgebraNode algebrizedCte, CommonTableBinding commonTableBinding, TableRefBinding commonTableRefBinding)
        {
            // Replace row buffers to base tables by new ones. This must be done because a CTE could be referenced multiple times.
            // Since same row buffer entries means that the underlying data will be stored in the same physical data slot this
            // will lead to problems if, for example, two instances of the same CTE are joined together. Any join condition that
            // operates on the same column will always compare data coming from the same join side (and therefor will always
            // evaluate to true).
            //
            // Some notes on the implementation:
            //
            //      1. Note that just replacing references to row buffers of base tables in RowBufferExpression is not enough;
            //         instead they must also be replaced in output lists, defined value references (esp. ConcatAlgebraNode) etc.
            //      2. Also note that although the QueryNodes are re-algebrized every time a CTE is references the expressions
            //         are still copied from the QueryNodes (instead of cloned). Therefore two algrebrized CTEs will share the same
            //         expression AST instances. That means that replacing the row buffers leads to failure.

            // HACK: This is a workaround for issue 2. However,
            //       I am not quite sure how one should implement row buffer entry replacement without cloning the algebrized query.
            algebrizedCte = (AlgebraNode)algebrizedCte.Clone();

            CteTableDefinedValuesReinitializer cteTableDefinedValuesReinitializer = new CteTableDefinedValuesReinitializer();

            cteTableDefinedValuesReinitializer.Visit(algebrizedCte);

            RowBufferEntry[] outputList = algebrizedCte.OutputList;
            int skipRecursionLevel      = commonTableBinding.IsRecursive ? 1 : 0;

            // Rename the query columns to the CTE columns
            List <ComputedValueDefinition> definedValues = new List <ComputedValueDefinition>();

            for (int i = 0; i < commonTableRefBinding.ColumnRefs.Length; i++)
            {
                RowBufferEntry targetRowBufferEntry = commonTableRefBinding.ColumnRefs[i].ValueDefinition.Target;
                RowBufferEntry sourceRowBufferEntry = outputList[i + skipRecursionLevel];

                ComputedValueDefinition definedValue = new ComputedValueDefinition();
                definedValue.Target     = targetRowBufferEntry;
                definedValue.Expression = new RowBufferEntryExpression(sourceRowBufferEntry);
                definedValues.Add(definedValue);
            }

            ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();

            computeScalarAlgebraNode.Input         = algebrizedCte;
            computeScalarAlgebraNode.DefinedValues = definedValues.ToArray();
            return(computeScalarAlgebraNode);
        }
Esempio n. 12
0
        public override AlgebraNode VisitAggregateAlgebraNode(AggregateAlgebraNode node)
        {
            node.Input = VisitAlgebraNode(node.Input);

            if (node.Input is NullScanAlgebraNode)
            {
                if (node.Groups != null && node.Groups.Length > 0)
                {
                    // Grouped queries return nothing on empty input.
                    return(CreateNullScan(node.OutputList));
                }
                else
                {
                    // Non-grouped aggregation. We return the result value of the aggregates
                    // executed against an empty input as a single row.

                    List <RowBufferEntry>          outputList     = new List <RowBufferEntry>();
                    List <ComputedValueDefinition> emptyValueList = new List <ComputedValueDefinition>();
                    foreach (AggregatedValueDefinition definedValue in node.DefinedValues)
                    {
                        IAggregator aggregator = definedValue.Aggregator;
                        aggregator.Init();
                        object emptyValue = aggregator.Terminate();

                        ComputedValueDefinition computedBufferedValue = new ComputedValueDefinition();
                        computedBufferedValue.Target     = definedValue.Target;
                        computedBufferedValue.Expression = LiteralExpression.FromTypedValue(emptyValue, aggregator.ReturnType);
                        emptyValueList.Add(computedBufferedValue);
                        outputList.Add(computedBufferedValue.Target);
                    }

                    ConstantScanAlgebraNode constantScanAlgebraNode = new ConstantScanAlgebraNode();
                    constantScanAlgebraNode.DefinedValues = emptyValueList.ToArray();
                    constantScanAlgebraNode.OutputList    = outputList.ToArray();
                    return(constantScanAlgebraNode);
                }
            }

            return(node);
        }
Esempio n. 13
0
        public override AlgebraNode VisitFilterAlgebraNode(FilterAlgebraNode node)
        {
            node.Input = VisitAlgebraNode(node.Input);

            ExpressionNode           originalPredicate        = (ExpressionNode)node.Predicate.Clone();
            SpoolExpressionExtractor spoolExpressionExtractor = new SpoolExpressionExtractor(_outerReferences);

            // HACK: This hack ensures that TRUE literals introduced by SpoolExpressionExtractor are removed.
            node.Predicate = AstUtil.CombineConditions(LogicalOperator.And, spoolExpressionExtractor.VisitExpression(node.Predicate));
            SpoolExpression[] spoolExpressions = spoolExpressionExtractor.GetSpoolExpressions();

            // Now we must check that the remaining filter incl. input to the filter don't reference any other
            // outer reference.

            bool remainingFilterHasDependenciesToOuterReferences = CheckIfNodeHasDependenciesToOuterReferences(node);

            if (remainingFilterHasDependenciesToOuterReferences)
            {
                // OK; we cannot insert a spool operation here. Undo the expression replacement.
                node.Predicate = originalPredicate;
            }
            else if (spoolExpressions.Length > 0)
            {
                SpoolExpression spoolExpression = spoolExpressions[0];

                AlgebraNode currentInput;

                if (node.Predicate is ConstantExpression)
                {
                    currentInput = node.Input;
                }
                else
                {
                    currentInput = node;
                }

                RowBufferEntry           indexEntry;
                RowBufferEntryExpression indexExpressionAsRowBufferEntryExpression = spoolExpression.IndexExpression as RowBufferEntryExpression;

                if (indexExpressionAsRowBufferEntryExpression != null)
                {
                    indexEntry = indexExpressionAsRowBufferEntryExpression.RowBufferEntry;
                }
                else
                {
                    indexEntry = new RowBufferEntry(spoolExpression.IndexExpression.ExpressionType);
                    ComputedValueDefinition definedValue = new ComputedValueDefinition();
                    definedValue.Target     = indexEntry;
                    definedValue.Expression = spoolExpression.IndexExpression;

                    ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
                    computeScalarAlgebraNode.Input         = currentInput;
                    computeScalarAlgebraNode.DefinedValues = new ComputedValueDefinition[] { definedValue };
                    currentInput = computeScalarAlgebraNode;
                }

                IndexSpoolAlgebraNode indexSpoolAlgebraNode = new IndexSpoolAlgebraNode();
                indexSpoolAlgebraNode.Input           = currentInput;
                indexSpoolAlgebraNode.IndexEntry      = indexEntry;
                indexSpoolAlgebraNode.ProbeExpression = spoolExpression.ProbeExpression;
                return(indexSpoolAlgebraNode);
            }

            return(node);
        }
Esempio n. 14
0
        public override AlgebraNode VisitConcatAlgebraNode(ConcatAlgebraNode node)
        {
            base.VisitConcatAlgebraNode(node);

            List <AlgebraNode> nonEmptyInputs = new List <AlgebraNode>();

            foreach (AlgebraNode input in node.Inputs)
            {
                if (input is NullScanAlgebraNode)
                {
                    // removed by not adding to list.
                }
                else
                {
                    nonEmptyInputs.Add(input);
                }
            }

            if (nonEmptyInputs.Count == 0)
            {
                return(CreateNullScan(node.OutputList));
            }

            if (nonEmptyInputs.Count == 1)
            {
                int inputIndex = Array.IndexOf(node.Inputs, nonEmptyInputs[0]);

                List <ComputedValueDefinition> definedValues            = new List <ComputedValueDefinition>();
                ComputeScalarAlgebraNode       computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
                computeScalarAlgebraNode.Input = nonEmptyInputs[0];
                foreach (UnitedValueDefinition unitedValueDefinition in node.DefinedValues)
                {
                    ComputedValueDefinition computedValueDefinition = new ComputedValueDefinition();
                    computedValueDefinition.Target     = unitedValueDefinition.Target;
                    computedValueDefinition.Expression = new RowBufferEntryExpression(unitedValueDefinition.DependendEntries[inputIndex]);
                    definedValues.Add(computedValueDefinition);
                }
                computeScalarAlgebraNode.DefinedValues = definedValues.ToArray();
                computeScalarAlgebraNode.OutputList    = node.OutputList;
                return(computeScalarAlgebraNode);
            }

            node.Inputs = nonEmptyInputs.ToArray();

            // Update dependend entries

            for (int i = 0; i < node.DefinedValues.Length; i++)
            {
                UnitedValueDefinition definition       = node.DefinedValues[i];
                List <RowBufferEntry> dependendEntries = new List <RowBufferEntry>();

                foreach (RowBufferEntry dependendEntry in definition.DependendEntries)
                {
                    bool entryInAnyInput = false;

                    foreach (AlgebraNode input in node.Inputs)
                    {
                        if (ArrayHelpers.Contains(input.OutputList, dependendEntry))
                        {
                            entryInAnyInput = true;
                            break;
                        }
                    }

                    if (entryInAnyInput)
                    {
                        dependendEntries.Add(dependendEntry);
                    }
                }

                definition.DependendEntries = dependendEntries.ToArray();
            }
            return(node);
        }
Esempio n. 15
0
        public override AlgebraNode VisitJoinAlgebraNode(JoinAlgebraNode node)
        {
            node.Left  = VisitAlgebraNode(node.Left);
            node.Right = VisitAlgebraNode(node.Right);

            if (node.Predicate != null &&
                (node.OuterReferences == null || node.OuterReferences.Length == 0) &&
                (
                    node.Op == JoinAlgebraNode.JoinOperator.InnerJoin ||
                    node.Op == JoinAlgebraNode.JoinOperator.LeftOuterJoin ||
                    node.Op == JoinAlgebraNode.JoinOperator.RightOuterJoin ||
                    node.Op == JoinAlgebraNode.JoinOperator.FullOuterJoin)
                )
            {
                RowBufferEntry[] leftDefinedEntries  = AstUtil.GetDefinedValueEntries(node.Left);
                RowBufferEntry[] rightDefinedEntries = AstUtil.GetDefinedValueEntries(node.Right);

                EqualPredicatesExtractor equalPredicatesExtractor = new EqualPredicatesExtractor(leftDefinedEntries, rightDefinedEntries);
                ExpressionNode           probeResidual            = equalPredicatesExtractor.VisitExpression(node.Predicate);
                BinaryExpression[]       equalPredicates          = equalPredicatesExtractor.GetEqualPredicates();

                if (equalPredicates.Length > 0)
                {
                    BinaryExpression equalPredicate = equalPredicates[0];

                    ExpressionBuilder expressionBuilder = new ExpressionBuilder();
                    expressionBuilder.Push(probeResidual);

                    if (equalPredicates.Length > 1)
                    {
                        for (int i = 1; i < equalPredicates.Length; i++)
                        {
                            expressionBuilder.Push(equalPredicates[i]);
                        }
                        expressionBuilder.PushNAry(LogicalOperator.And);
                    }

                    probeResidual = expressionBuilder.Pop();
                    if (probeResidual is ConstantExpression)
                    {
                        probeResidual = null;
                    }

                    AlgebraNode leftInput  = node.Left;
                    AlgebraNode rightInput = node.Right;

                    if (node.Op == JoinAlgebraNode.JoinOperator.LeftOuterJoin)
                    {
                        node.Op    = JoinAlgebraNode.JoinOperator.RightOuterJoin;
                        leftInput  = node.Right;
                        rightInput = node.Left;
                        ExpressionNode oldLeft = equalPredicate.Left;
                        equalPredicate.Left  = equalPredicate.Right;
                        equalPredicate.Right = oldLeft;
                    }

                    RowBufferEntry           leftEntry;
                    RowBufferEntryExpression leftAsRowBufferEntryExpression = equalPredicate.Left as RowBufferEntryExpression;
                    if (leftAsRowBufferEntryExpression != null)
                    {
                        leftEntry = leftAsRowBufferEntryExpression.RowBufferEntry;
                    }
                    else
                    {
                        leftEntry = new RowBufferEntry(equalPredicate.Left.ExpressionType);
                        ComputedValueDefinition definedValue = new ComputedValueDefinition();
                        definedValue.Target     = leftEntry;
                        definedValue.Expression = equalPredicate.Left;

                        ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
                        computeScalarAlgebraNode.Input         = leftInput;
                        computeScalarAlgebraNode.DefinedValues = new ComputedValueDefinition[] { definedValue };
                        leftInput = computeScalarAlgebraNode;
                    }

                    RowBufferEntry           rightEntry;
                    RowBufferEntryExpression rightAsRowBufferEntryExpression = equalPredicate.Right as RowBufferEntryExpression;
                    if (rightAsRowBufferEntryExpression != null)
                    {
                        rightEntry = rightAsRowBufferEntryExpression.RowBufferEntry;
                    }
                    else
                    {
                        rightEntry = new RowBufferEntry(equalPredicate.Right.ExpressionType);
                        ComputedValueDefinition definedValue = new ComputedValueDefinition();
                        definedValue.Target     = rightEntry;
                        definedValue.Expression = equalPredicate.Right;

                        ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
                        computeScalarAlgebraNode.Input         = rightInput;
                        computeScalarAlgebraNode.DefinedValues = new ComputedValueDefinition[] { definedValue };
                        rightInput = computeScalarAlgebraNode;
                    }

                    HashMatchAlgebraNode hashMatchAlgebraNode = new HashMatchAlgebraNode();
                    hashMatchAlgebraNode.Op            = node.Op;
                    hashMatchAlgebraNode.Left          = leftInput;
                    hashMatchAlgebraNode.Right         = rightInput;
                    hashMatchAlgebraNode.BuildKeyEntry = leftEntry;
                    hashMatchAlgebraNode.ProbeEntry    = rightEntry;
                    hashMatchAlgebraNode.ProbeResidual = probeResidual;
                    return(hashMatchAlgebraNode);
                }
            }

            return(node);
        }
Esempio n. 16
0
		public override TableReference VisitDerivedTableReference(DerivedTableReference node)
		{
			AlgebraNode algebrizedQuery = Convert(node.Query);

			List<ComputedValueDefinition> definedValues = new List<ComputedValueDefinition>();
			for (int i = 0; i < node.DerivedTableBinding.ColumnRefs.Length; i++)
			{
				RowBufferEntry targetRowBufferEntry = node.DerivedTableBinding.ColumnRefs[i].ValueDefinition.Target;
				RowBufferEntry sourceRowBufferEntry = algebrizedQuery.OutputList[i];

				ComputedValueDefinition definedValue = new ComputedValueDefinition();
				definedValue.Target = targetRowBufferEntry;
				definedValue.Expression = new RowBufferEntryExpression(sourceRowBufferEntry);
				definedValues.Add(definedValue);
			}

			ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
			computeScalarAlgebraNode.Input = algebrizedQuery;
			computeScalarAlgebraNode.DefinedValues = definedValues.ToArray();
			SetLastAlgebraNode(computeScalarAlgebraNode);

			return node;
		}
        public override AlgebraNode VisitJoinAlgebraNode(JoinAlgebraNode node)
        {
            node.Left = VisitAlgebraNode(node.Left);
            node.Right = VisitAlgebraNode(node.Right);

            if (node.Predicate != null &&
                (node.OuterReferences == null || node.OuterReferences.Length == 0) &&
                (
                    node.Op == JoinAlgebraNode.JoinOperator.InnerJoin ||
                    node.Op == JoinAlgebraNode.JoinOperator.LeftOuterJoin ||
                    node.Op == JoinAlgebraNode.JoinOperator.RightOuterJoin ||
                    node.Op == JoinAlgebraNode.JoinOperator.FullOuterJoin)
                )
            {
                RowBufferEntry[] leftDefinedEntries = AstUtil.GetDefinedValueEntries(node.Left);
                RowBufferEntry[] rightDefinedEntries = AstUtil.GetDefinedValueEntries(node.Right);

                EqualPredicatesExtractor equalPredicatesExtractor = new EqualPredicatesExtractor(leftDefinedEntries, rightDefinedEntries);
                ExpressionNode probeResidual = equalPredicatesExtractor.VisitExpression(node.Predicate);
                BinaryExpression[] equalPredicates = equalPredicatesExtractor.GetEqualPredicates();

                if (equalPredicates.Length > 0)
                {
                    BinaryExpression equalPredicate = equalPredicates[0];

                    ExpressionBuilder expressionBuilder = new ExpressionBuilder();
                    expressionBuilder.Push(probeResidual);

                    if (equalPredicates.Length > 1)
                    {
                        for (int i = 1; i < equalPredicates.Length; i++)
                            expressionBuilder.Push(equalPredicates[i]);
                        expressionBuilder.PushNAry(LogicalOperator.And);
                    }

                    probeResidual = expressionBuilder.Pop();
                    if (probeResidual is ConstantExpression)
                        probeResidual = null;

                    AlgebraNode leftInput = node.Left;
                    AlgebraNode rightInput = node.Right;

                    if (node.Op == JoinAlgebraNode.JoinOperator.LeftOuterJoin)
                    {
                        node.Op = JoinAlgebraNode.JoinOperator.RightOuterJoin;
                        leftInput = node.Right;
                        rightInput = node.Left;
                        ExpressionNode oldLeft = equalPredicate.Left;
                        equalPredicate.Left = equalPredicate.Right;
                        equalPredicate.Right = oldLeft;
                    }

                    RowBufferEntry leftEntry;
                    RowBufferEntryExpression leftAsRowBufferEntryExpression = equalPredicate.Left as RowBufferEntryExpression;
                    if (leftAsRowBufferEntryExpression != null)
                    {
                        leftEntry = leftAsRowBufferEntryExpression.RowBufferEntry;
                    }
                    else
                    {
                        leftEntry = new RowBufferEntry(equalPredicate.Left.ExpressionType);
                        ComputedValueDefinition definedValue = new ComputedValueDefinition();
                        definedValue.Target = leftEntry;
                        definedValue.Expression = equalPredicate.Left;

                        ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
                        computeScalarAlgebraNode.Input = leftInput;
                        computeScalarAlgebraNode.DefinedValues = new ComputedValueDefinition[] {definedValue};
                        leftInput = computeScalarAlgebraNode;
                    }

                    RowBufferEntry rightEntry;
                    RowBufferEntryExpression rightAsRowBufferEntryExpression = equalPredicate.Right as RowBufferEntryExpression;
                    if (rightAsRowBufferEntryExpression != null)
                    {
                        rightEntry = rightAsRowBufferEntryExpression.RowBufferEntry;
                    }
                    else
                    {
                        rightEntry = new RowBufferEntry(equalPredicate.Right.ExpressionType);
                        ComputedValueDefinition definedValue = new ComputedValueDefinition();
                        definedValue.Target = rightEntry;
                        definedValue.Expression = equalPredicate.Right;

                        ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
                        computeScalarAlgebraNode.Input = rightInput;
                        computeScalarAlgebraNode.DefinedValues = new ComputedValueDefinition[] {definedValue};
                        rightInput = computeScalarAlgebraNode;
                    }

                    HashMatchAlgebraNode hashMatchAlgebraNode = new HashMatchAlgebraNode();
                    hashMatchAlgebraNode.Op = node.Op;
                    hashMatchAlgebraNode.Left = leftInput;
                    hashMatchAlgebraNode.Right = rightInput;
                    hashMatchAlgebraNode.BuildKeyEntry = leftEntry;
                    hashMatchAlgebraNode.ProbeEntry = rightEntry;
                    hashMatchAlgebraNode.ProbeResidual = probeResidual;
                    return hashMatchAlgebraNode;
                }
            }

            return node;
        }
Esempio n. 18
0
		public override AlgebraNode VisitConcatAlgebraNode(ConcatAlgebraNode node)
		{
			base.VisitConcatAlgebraNode(node);
            
			List<AlgebraNode> nonEmptyInputs = new List<AlgebraNode>();

			foreach (AlgebraNode input in node.Inputs)
			{
				if (input is NullScanAlgebraNode)
				{
					// removed by not adding to list.
				}
				else
				{
					nonEmptyInputs.Add(input);
				}
			}

			if (nonEmptyInputs.Count == 0)
				return CreateNullScan(node.OutputList);

			if (nonEmptyInputs.Count == 1)
			{
				int inputIndex = Array.IndexOf(node.Inputs, nonEmptyInputs[0]);

				List<ComputedValueDefinition> definedValues = new List<ComputedValueDefinition>();
				ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
				computeScalarAlgebraNode.Input = nonEmptyInputs[0];
				foreach (UnitedValueDefinition unitedValueDefinition in node.DefinedValues)
				{
					ComputedValueDefinition computedValueDefinition = new ComputedValueDefinition();
					computedValueDefinition.Target = unitedValueDefinition.Target;
					computedValueDefinition.Expression = new RowBufferEntryExpression(unitedValueDefinition.DependendEntries[inputIndex]);
					definedValues.Add(computedValueDefinition);
				}
				computeScalarAlgebraNode.DefinedValues = definedValues.ToArray();
				computeScalarAlgebraNode.OutputList = node.OutputList;
				return computeScalarAlgebraNode;
			}

			node.Inputs = nonEmptyInputs.ToArray();

			// Update dependend entries

			for (int i = 0; i < node.DefinedValues.Length; i++)
			{
				UnitedValueDefinition definition = node.DefinedValues[i];
				List<RowBufferEntry> dependendEntries = new List<RowBufferEntry>();

				foreach (RowBufferEntry dependendEntry in definition.DependendEntries)
				{
					bool entryInAnyInput = false;

					foreach (AlgebraNode input in node.Inputs)
					{
						if (ArrayHelpers.Contains(input.OutputList, dependendEntry))
						{
							entryInAnyInput = true;
							break;
						}
					}

					if (entryInAnyInput)
						dependendEntries.Add(dependendEntry);
				}

				definition.DependendEntries = dependendEntries.ToArray();
			}
			return node;
		}
Esempio n. 19
0
        private static AlgebraNode AlgebrizeRecursiveCte(CommonTableBinding commonTableBinding)
        {
            // It is a recursive query.
            //
            // Create row buffer entry that is used to guard the recursion and the primary table spool
            // that spools the results needed by nested recursion calls.

            ExpressionBuilder            expressionBuilder = new ExpressionBuilder();
            StackedTableSpoolAlgebraNode primaryTableSpool = new StackedTableSpoolAlgebraNode();

            RowBufferEntry anchorRecursionLevel;

            RowBufferEntry[] anchorOutput;
            AlgebraNode      anchorNode;

            #region Anchor member
            {
                // Emit anchor member.

                AlgebraNode algebrizedAnchor = Convert(commonTableBinding.AnchorMember);

                // Emit compute scalar that initializes the recursion level to 0.

                anchorRecursionLevel = new RowBufferEntry(typeof(int));
                ComputedValueDefinition computedValueDefinition1 = new ComputedValueDefinition();
                computedValueDefinition1.Target     = anchorRecursionLevel;
                computedValueDefinition1.Expression = LiteralExpression.FromInt32(0);

                ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
                computeScalarAlgebraNode.Input         = algebrizedAnchor;
                computeScalarAlgebraNode.DefinedValues = new ComputedValueDefinition[] { computedValueDefinition1 };

                anchorOutput = algebrizedAnchor.OutputList;
                anchorNode   = computeScalarAlgebraNode;
            }
            #endregion

            RowBufferEntry   incrementedRecursionLevel;
            RowBufferEntry[] tableSpoolOutput;
            AlgebraNode      tableSpoolNode;

            #region Table spool
            {
                // Emit table spool reference.

                RowBufferEntry recursionLevelRefEntry = new RowBufferEntry(typeof(int));
                tableSpoolOutput = new RowBufferEntry[anchorOutput.Length];
                for (int i = 0; i < tableSpoolOutput.Length; i++)
                {
                    tableSpoolOutput[i] = new RowBufferEntry(anchorOutput[i].DataType);
                }

                StackedTableSpoolRefAlgebraNode tableSpoolReference = new StackedTableSpoolRefAlgebraNode();
                tableSpoolReference.PrimarySpool  = primaryTableSpool;
                tableSpoolReference.DefinedValues = ArrayHelpers.JoinArrays(new RowBufferEntry[] { recursionLevelRefEntry }, tableSpoolOutput);

                // Emit compute scalar that increases the recursion level by one and renames
                // columns from the spool to the CTE column buffer entries.

                expressionBuilder.Push(new RowBufferEntryExpression(recursionLevelRefEntry));
                expressionBuilder.Push(LiteralExpression.FromInt32(1));
                expressionBuilder.PushBinary(BinaryOperator.Add);

                incrementedRecursionLevel = new RowBufferEntry(typeof(int));
                ComputedValueDefinition incremenedRecLevelValueDefinition = new ComputedValueDefinition();
                incremenedRecLevelValueDefinition.Target     = incrementedRecursionLevel;
                incremenedRecLevelValueDefinition.Expression = expressionBuilder.Pop();

                CteColumnMappingFinder cteColumnMappingFinder = new CteColumnMappingFinder(commonTableBinding, tableSpoolOutput);
                foreach (QueryNode recursiveMember in commonTableBinding.RecursiveMembers)
                {
                    cteColumnMappingFinder.Visit(recursiveMember);
                }

                CteColumnMapping[] cteColumnMappings = cteColumnMappingFinder.GetMappings();

                List <ComputedValueDefinition> definedValues = new List <ComputedValueDefinition>();
                definedValues.Add(incremenedRecLevelValueDefinition);
                foreach (CteColumnMapping cteColumnMapping in cteColumnMappings)
                {
                    ComputedValueDefinition definedValue = new ComputedValueDefinition();
                    definedValue.Target     = cteColumnMapping.VirtualBufferEntry;
                    definedValue.Expression = new RowBufferEntryExpression(cteColumnMapping.SpoolBufferEntry);
                    definedValues.Add(definedValue);
                }

                ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
                computeScalarAlgebraNode.Input         = tableSpoolReference;
                computeScalarAlgebraNode.DefinedValues = definedValues.ToArray();

                tableSpoolNode = computeScalarAlgebraNode;
            }
            #endregion

            RowBufferEntry[] recursiveOutput;
            AlgebraNode      recursiveNode;

            #region Recursive member(s)
            {
                // Emit all recursive parts. The join conditions to the recursive part are replaced by simple filters
                // in the nested Convert() call.

                ConcatAlgebraNode concatAlgebraNode = new ConcatAlgebraNode();
                concatAlgebraNode.Inputs = new AlgebraNode[commonTableBinding.RecursiveMembers.Length];
                for (int i = 0; i < commonTableBinding.RecursiveMembers.Length; i++)
                {
                    concatAlgebraNode.Inputs[i] = Convert(commonTableBinding, commonTableBinding.RecursiveMembers[i]);
                }

                concatAlgebraNode.DefinedValues = new UnitedValueDefinition[anchorOutput.Length];
                for (int i = 0; i < anchorOutput.Length; i++)
                {
                    List <RowBufferEntry> dependencies = new List <RowBufferEntry>();
                    foreach (ResultAlgebraNode algebrizedRecursivePart in concatAlgebraNode.Inputs)
                    {
                        dependencies.Add(algebrizedRecursivePart.OutputList[i]);
                    }

                    concatAlgebraNode.DefinedValues[i]                  = new UnitedValueDefinition();
                    concatAlgebraNode.DefinedValues[i].Target           = new RowBufferEntry(anchorOutput[i].DataType);
                    concatAlgebraNode.DefinedValues[i].DependendEntries = dependencies.ToArray();
                }

                // Calculate the recursive output.

                recursiveOutput = new RowBufferEntry[concatAlgebraNode.DefinedValues.Length];
                for (int i = 0; i < concatAlgebraNode.DefinedValues.Length; i++)
                {
                    recursiveOutput[i] = concatAlgebraNode.DefinedValues[i].Target;
                }

                // Emit cross join

                JoinAlgebraNode crossJoinNode = new JoinAlgebraNode();
                crossJoinNode.Left  = tableSpoolNode;
                crossJoinNode.Right = concatAlgebraNode;

                // Emit assert that ensures that the recursion level is <= 100.

                expressionBuilder.Push(new RowBufferEntryExpression(incrementedRecursionLevel));
                expressionBuilder.Push(LiteralExpression.FromInt32(100));
                expressionBuilder.PushBinary(BinaryOperator.Greater);

                CaseExpression caseExpression = new CaseExpression();
                caseExpression.WhenExpressions    = new ExpressionNode[1];
                caseExpression.WhenExpressions[0] = expressionBuilder.Pop();
                caseExpression.ThenExpressions    = new ExpressionNode[1];
                caseExpression.ThenExpressions[0] = LiteralExpression.FromInt32(0);

                AssertAlgebraNode assertAlgebraNode = new AssertAlgebraNode();
                assertAlgebraNode.Input         = crossJoinNode;
                assertAlgebraNode.AssertionType = AssertionType.BelowRecursionLimit;
                assertAlgebraNode.Predicate     = caseExpression;

                recursiveNode = assertAlgebraNode;
            }
            #endregion

            RowBufferEntry[] algebrizedOutput;
            AlgebraNode      algebrizedCte;

            #region Combination
            {
                // Create concat node to combine anchor and recursive part.

                ConcatAlgebraNode concatAlgebraNode = new ConcatAlgebraNode();
                concatAlgebraNode.Inputs    = new AlgebraNode[2];
                concatAlgebraNode.Inputs[0] = anchorNode;
                concatAlgebraNode.Inputs[1] = recursiveNode;

                concatAlgebraNode.DefinedValues                     = new UnitedValueDefinition[anchorOutput.Length + 1];
                concatAlgebraNode.DefinedValues[0]                  = new UnitedValueDefinition();
                concatAlgebraNode.DefinedValues[0].Target           = new RowBufferEntry(anchorRecursionLevel.DataType);
                concatAlgebraNode.DefinedValues[0].DependendEntries = new RowBufferEntry[] { anchorRecursionLevel, incrementedRecursionLevel };

                for (int i = 0; i < anchorOutput.Length; i++)
                {
                    concatAlgebraNode.DefinedValues[i + 1]                  = new UnitedValueDefinition();
                    concatAlgebraNode.DefinedValues[i + 1].Target           = new RowBufferEntry(anchorOutput[i].DataType);
                    concatAlgebraNode.DefinedValues[i + 1].DependendEntries = new RowBufferEntry[] { anchorOutput[i], recursiveOutput[i] };
                }

                algebrizedOutput = new RowBufferEntry[concatAlgebraNode.DefinedValues.Length];
                for (int i = 0; i < concatAlgebraNode.DefinedValues.Length; i++)
                {
                    algebrizedOutput[i] = concatAlgebraNode.DefinedValues[i].Target;
                }

                // Assign the combination as the input to the primray spool

                primaryTableSpool.Input = concatAlgebraNode;

                // The primary spool represents the result of the "inlined" CTE.

                algebrizedCte = primaryTableSpool;
            }
            #endregion

            algebrizedCte.OutputList = algebrizedOutput;
            return(algebrizedCte);
        }
Esempio n. 20
0
		public override AlgebraNode VisitAggregateAlgebraNode(AggregateAlgebraNode node)
		{
			node.Input = VisitAlgebraNode(node.Input);

			if (node.Input is NullScanAlgebraNode)
			{
				if (node.Groups != null && node.Groups.Length > 0)
				{
					// Grouped queries return nothing on empty input.
					return CreateNullScan(node.OutputList);
				}
				else
				{
					// Non-grouped aggregation. We return the result value of the aggregates
					// executed against an empty input as a single row.

					List<RowBufferEntry> outputList = new List<RowBufferEntry>();
					List<ComputedValueDefinition> emptyValueList = new List<ComputedValueDefinition>();
					foreach (AggregatedValueDefinition definedValue in node.DefinedValues)
					{
						IAggregator aggregator = definedValue.Aggregator;
						aggregator.Init();
						object emptyValue = aggregator.Terminate();

						ComputedValueDefinition computedBufferedValue = new ComputedValueDefinition();
						computedBufferedValue.Target = definedValue.Target;
						computedBufferedValue.Expression = LiteralExpression.FromTypedValue(emptyValue, aggregator.ReturnType);
						emptyValueList.Add(computedBufferedValue);
						outputList.Add(computedBufferedValue.Target);
					}

					ConstantScanAlgebraNode constantScanAlgebraNode = new ConstantScanAlgebraNode();
					constantScanAlgebraNode.DefinedValues = emptyValueList.ToArray();
					constantScanAlgebraNode.OutputList = outputList.ToArray();
					return constantScanAlgebraNode;
				}
			}
            
			return node;
		}
Esempio n. 21
0
		private RuntimeComputedValueOutput[] GetDefinedValues(RowBufferEntry[] outputList, ComputedValueDefinition[] definedValues, params BoundRowBufferEntrySet[] boundRowBufferEntrySets)
		{
			RuntimeComputedValueOutput[] result = new RuntimeComputedValueOutput[definedValues.Length];

			for (int i = 0; i < definedValues.Length; i++)
			{
				RuntimeComputedValueOutput definedValue = new RuntimeComputedValueOutput();
				definedValue.Expression = CreateRuntimeExpression(definedValues[i].Expression, boundRowBufferEntrySets);
				definedValue.TargetIndex = Array.IndexOf(outputList, definedValues[i].Target);
				result[i] = definedValue;
			}

			return result;
		}
Esempio n. 22
0
		private static AlgebraNode AlgebrizeRecursiveCte(CommonTableBinding commonTableBinding)
		{
			// It is a recursive query.
			//
			// Create row buffer entry that is used to guard the recursion and the primary table spool
			// that spools the results needed by nested recursion calls.

			ExpressionBuilder expressionBuilder = new ExpressionBuilder();
			StackedTableSpoolAlgebraNode primaryTableSpool = new StackedTableSpoolAlgebraNode();

			RowBufferEntry anchorRecursionLevel;
			RowBufferEntry[] anchorOutput;
			AlgebraNode anchorNode;

			#region Anchor member
			{
				// Emit anchor member.

				AlgebraNode algebrizedAnchor = Convert(commonTableBinding.AnchorMember);

				// Emit compute scalar that initializes the recursion level to 0.

				anchorRecursionLevel = new RowBufferEntry(typeof(int));
				ComputedValueDefinition computedValueDefinition1 = new ComputedValueDefinition();
				computedValueDefinition1.Target = anchorRecursionLevel;
				computedValueDefinition1.Expression = LiteralExpression.FromInt32(0);

				ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
				computeScalarAlgebraNode.Input = algebrizedAnchor;
				computeScalarAlgebraNode.DefinedValues = new ComputedValueDefinition[] { computedValueDefinition1 };

				anchorOutput = algebrizedAnchor.OutputList;
				anchorNode = computeScalarAlgebraNode;
			}
			#endregion

			RowBufferEntry incrementedRecursionLevel;
			RowBufferEntry[] tableSpoolOutput;
			AlgebraNode tableSpoolNode;

			#region Table spool
			{
				// Emit table spool reference.

				RowBufferEntry recursionLevelRefEntry = new RowBufferEntry(typeof(int));
				tableSpoolOutput = new RowBufferEntry[anchorOutput.Length];
				for (int i = 0; i < tableSpoolOutput.Length; i++)
					tableSpoolOutput[i] = new RowBufferEntry(anchorOutput[i].DataType);

				StackedTableSpoolRefAlgebraNode tableSpoolReference = new StackedTableSpoolRefAlgebraNode();
				tableSpoolReference.PrimarySpool = primaryTableSpool;
				tableSpoolReference.DefinedValues = ArrayHelpers.JoinArrays(new RowBufferEntry[] { recursionLevelRefEntry }, tableSpoolOutput);

				// Emit compute scalar that increases the recursion level by one and renames
				// columns from the spool to the CTE column buffer entries.

				expressionBuilder.Push(new RowBufferEntryExpression(recursionLevelRefEntry));
				expressionBuilder.Push(LiteralExpression.FromInt32(1));
				expressionBuilder.PushBinary(BinaryOperator.Add);

				incrementedRecursionLevel = new RowBufferEntry(typeof(int));
				ComputedValueDefinition incremenedRecLevelValueDefinition = new ComputedValueDefinition();
				incremenedRecLevelValueDefinition.Target = incrementedRecursionLevel;
				incremenedRecLevelValueDefinition.Expression = expressionBuilder.Pop();

				CteColumnMappingFinder cteColumnMappingFinder = new CteColumnMappingFinder(commonTableBinding, tableSpoolOutput);
				foreach (QueryNode recursiveMember in commonTableBinding.RecursiveMembers)
					cteColumnMappingFinder.Visit(recursiveMember);

				CteColumnMapping[] cteColumnMappings = cteColumnMappingFinder.GetMappings();

				List<ComputedValueDefinition> definedValues = new List<ComputedValueDefinition>();
				definedValues.Add(incremenedRecLevelValueDefinition);
				foreach (CteColumnMapping cteColumnMapping in cteColumnMappings)
				{
					ComputedValueDefinition definedValue = new ComputedValueDefinition();
					definedValue.Target = cteColumnMapping.VirtualBufferEntry;
					definedValue.Expression = new RowBufferEntryExpression(cteColumnMapping.SpoolBufferEntry);
					definedValues.Add(definedValue);
				}

				ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
				computeScalarAlgebraNode.Input = tableSpoolReference;
				computeScalarAlgebraNode.DefinedValues = definedValues.ToArray();

				tableSpoolNode = computeScalarAlgebraNode;
			}
			#endregion

			RowBufferEntry[] recursiveOutput;
			AlgebraNode recursiveNode;

			#region Recursive member(s)
			{
				// Emit all recursive parts. The join conditions to the recursive part are replaced by simple filters
				// in the nested Convert() call.

				ConcatAlgebraNode concatAlgebraNode = new ConcatAlgebraNode();
				concatAlgebraNode.Inputs = new AlgebraNode[commonTableBinding.RecursiveMembers.Length];
				for (int i = 0; i < commonTableBinding.RecursiveMembers.Length; i++)
					concatAlgebraNode.Inputs[i] = Convert(commonTableBinding, commonTableBinding.RecursiveMembers[i]);

				concatAlgebraNode.DefinedValues = new UnitedValueDefinition[anchorOutput.Length];
				for (int i = 0; i < anchorOutput.Length; i++)
				{
					List<RowBufferEntry> dependencies = new List<RowBufferEntry>();
					foreach (ResultAlgebraNode algebrizedRecursivePart in concatAlgebraNode.Inputs)
						dependencies.Add(algebrizedRecursivePart.OutputList[i]);

					concatAlgebraNode.DefinedValues[i] = new UnitedValueDefinition();
					concatAlgebraNode.DefinedValues[i].Target = new RowBufferEntry(anchorOutput[i].DataType);
					concatAlgebraNode.DefinedValues[i].DependendEntries = dependencies.ToArray();
				}

				// Calculate the recursive output.

				recursiveOutput = new RowBufferEntry[concatAlgebraNode.DefinedValues.Length];
				for (int i = 0; i < concatAlgebraNode.DefinedValues.Length; i++)
					recursiveOutput[i] = concatAlgebraNode.DefinedValues[i].Target;

				// Emit cross join

				JoinAlgebraNode crossJoinNode = new JoinAlgebraNode();
				crossJoinNode.Left = tableSpoolNode;
				crossJoinNode.Right = concatAlgebraNode;

				// Emit assert that ensures that the recursion level is <= 100.

				expressionBuilder.Push(new RowBufferEntryExpression(incrementedRecursionLevel));
				expressionBuilder.Push(LiteralExpression.FromInt32(100));
				expressionBuilder.PushBinary(BinaryOperator.Greater);

				CaseExpression caseExpression = new CaseExpression();
				caseExpression.WhenExpressions = new ExpressionNode[1];
				caseExpression.WhenExpressions[0] = expressionBuilder.Pop();
				caseExpression.ThenExpressions = new ExpressionNode[1];
				caseExpression.ThenExpressions[0] = LiteralExpression.FromInt32(0);

				AssertAlgebraNode assertAlgebraNode = new AssertAlgebraNode();
				assertAlgebraNode.Input = crossJoinNode;
				assertAlgebraNode.AssertionType = AssertionType.BelowRecursionLimit;
				assertAlgebraNode.Predicate = caseExpression;

				recursiveNode = assertAlgebraNode;
			}
			#endregion

			RowBufferEntry[] algebrizedOutput;
			AlgebraNode algebrizedCte;

			#region Combination
			{
				// Create concat node to combine anchor and recursive part.

				ConcatAlgebraNode concatAlgebraNode = new ConcatAlgebraNode();
				concatAlgebraNode.Inputs = new AlgebraNode[2];
				concatAlgebraNode.Inputs[0] = anchorNode;
				concatAlgebraNode.Inputs[1] = recursiveNode;

				concatAlgebraNode.DefinedValues = new UnitedValueDefinition[anchorOutput.Length + 1];
				concatAlgebraNode.DefinedValues[0] = new UnitedValueDefinition();
				concatAlgebraNode.DefinedValues[0].Target = new RowBufferEntry(anchorRecursionLevel.DataType);
				concatAlgebraNode.DefinedValues[0].DependendEntries = new RowBufferEntry[] { anchorRecursionLevel, incrementedRecursionLevel };

				for (int i = 0; i < anchorOutput.Length; i++)
				{
					concatAlgebraNode.DefinedValues[i + 1] = new UnitedValueDefinition();
					concatAlgebraNode.DefinedValues[i + 1].Target = new RowBufferEntry(anchorOutput[i].DataType);
					concatAlgebraNode.DefinedValues[i + 1].DependendEntries = new RowBufferEntry[] { anchorOutput[i], recursiveOutput[i] };
				}

				algebrizedOutput = new RowBufferEntry[concatAlgebraNode.DefinedValues.Length];
				for (int i = 0; i < concatAlgebraNode.DefinedValues.Length; i++)
					algebrizedOutput[i] = concatAlgebraNode.DefinedValues[i].Target;

				// Assign the combination as the input to the primray spool

				primaryTableSpool.Input = concatAlgebraNode;

				// The primary spool represents the result of the "inlined" CTE.

				algebrizedCte = primaryTableSpool;
			}
			#endregion

			algebrizedCte.OutputList = algebrizedOutput;
			return algebrizedCte;
		}
Esempio n. 23
0
		private static AlgebraNode InstantiateCte(AlgebraNode algebrizedCte, CommonTableBinding commonTableBinding, TableRefBinding commonTableRefBinding)
		{
			// Replace row buffers to base tables by new ones. This must be done because a CTE could be referenced multiple times.
			// Since same row buffer entries means that the underlying data will be stored in the same physical data slot this
			// will lead to problems if, for example, two instances of the same CTE are joined together. Any join condition that
			// operates on the same column will always compare data coming from the same join side (and therefor will always
			// evaluate to true).
			//
			// Some notes on the implementation:
			//
			//      1. Note that just replacing references to row buffers of base tables in RowBufferExpression is not enough;
			//         instead they must also be replaced in output lists, defined value references (esp. ConcatAlgebraNode) etc.
			//      2. Also note that although the QueryNodes are re-algebrized every time a CTE is references the expressions
			//         are still copied from the QueryNodes (instead of cloned). Therefore two algrebrized CTEs will share the same
			//         expression AST instances. That means that replacing the row buffers leads to failure.

			// HACK: This is a workaround for issue 2. However, 
			//       I am not quite sure how one should implement row buffer entry replacement without cloning the algebrized query.
			algebrizedCte = (AlgebraNode) algebrizedCte.Clone();

			CteTableDefinedValuesReinitializer cteTableDefinedValuesReinitializer = new CteTableDefinedValuesReinitializer();
			cteTableDefinedValuesReinitializer.Visit(algebrizedCte);

			RowBufferEntry[] outputList = algebrizedCte.OutputList;
			int skipRecursionLevel = commonTableBinding.IsRecursive ? 1 : 0;

			// Rename the query columns to the CTE columns
			List<ComputedValueDefinition> definedValues = new List<ComputedValueDefinition>();
			for (int i = 0; i < commonTableRefBinding.ColumnRefs.Length; i++)
			{
				RowBufferEntry targetRowBufferEntry = commonTableRefBinding.ColumnRefs[i].ValueDefinition.Target;
				RowBufferEntry sourceRowBufferEntry = outputList[i + skipRecursionLevel];

				ComputedValueDefinition definedValue = new ComputedValueDefinition();
				definedValue.Target = targetRowBufferEntry;
				definedValue.Expression = new RowBufferEntryExpression(sourceRowBufferEntry);
				definedValues.Add(definedValue);
			}

			ComputeScalarAlgebraNode computeScalarAlgebraNode = new ComputeScalarAlgebraNode();
			computeScalarAlgebraNode.Input = algebrizedCte;
			computeScalarAlgebraNode.DefinedValues = definedValues.ToArray();
			return computeScalarAlgebraNode;
		}
Esempio n. 24
0
        private static void CreateBufferedValue(ExpressionNode expression, ICollection<ComputedValueDefinition> computedColumnList, ICollection<RowBufferEntry> columnList, IEnumerable<ComputedValueDefinition> alreadyComputedBufferedValues)
        {
            if (alreadyComputedBufferedValues != null)
                expression = ReplaceAlreadyComputedSubsequences(expression, alreadyComputedBufferedValues);

            foreach (ComputedValueDefinition computedBufferedValue in computedColumnList)
            {
                if (expression.IsStructuralEqualTo(computedBufferedValue.Expression))
                {
                    columnList.Add(computedBufferedValue.Target);
                    return;
                }
            }

            RowBufferEntryExpression rowBufferExpression = expression as RowBufferEntryExpression;
            if (rowBufferExpression != null)
            {
                columnList.Add(rowBufferExpression.RowBufferEntry);
            }
            else
            {
                RowBufferEntry rowBufferEntry = new RowBufferEntry(expression.ExpressionType);
                columnList.Add(rowBufferEntry);

                ComputedValueDefinition computedValue = new ComputedValueDefinition();
                computedValue.Target = rowBufferEntry;
                computedValue.Expression = expression;
                computedColumnList.Add(computedValue);
            }
        }
Esempio n. 25
0
			public RowBufferEntryReplacer(ComputedValueDefinition[] definedValues)
			{
				_definedValues = definedValues;
			}