Пример #1
0
        public override AlgebraNode VisitComputeScalarAlgebraNode(ComputeScalarAlgebraNode node)
        {
            base.VisitComputeScalarAlgebraNode(node);

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

            foreach (ComputedValueDefinition definedValue in node.DefinedValues)
            {
                RowBufferEntryExpression rowBufferEntryExpression = definedValue.Expression as RowBufferEntryExpression;
                if (rowBufferEntryExpression != null)
                {
                    _inliningDictionary[definedValue.Target] = rowBufferEntryExpression.RowBufferEntry;
                }
                else
                {
                    remainingDefinedValues.Add(definedValue);
                }
            }

            if (remainingDefinedValues.Count == 0)
            {
                return(node.Input);
            }

            node.DefinedValues = remainingDefinedValues.ToArray();
            return(node);
        }
Пример #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);
            }
        }
        public override AstElement Clone(Dictionary <AstElement, AstElement> alreadyClonedElements)
        {
            RowBufferEntryExpression result = new RowBufferEntryExpression();

            result.RowBufferEntry = _rowBufferEntry;
            return(result);
        }
Пример #4
0
        public override ExpressionNode VisitColumnExpression(ColumnExpression expression)
        {
            RowBufferEntryExpression rowBufferExpression = new RowBufferEntryExpression();

            rowBufferExpression.RowBufferEntry = expression.Column.ValueDefinition.Target;
            return(rowBufferExpression);
        }
Пример #5
0
            public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
            {
                if (!_rowBufferEntries.Contains(expression.RowBufferEntry))
                {
                    _rowBufferEntries.Add(expression.RowBufferEntry);
                }

                return(base.VisitRowBufferEntryExpression(expression));
            }
Пример #6
0
        public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
        {
            if (expression.RowBufferEntry == _rowBufferEntry)
            {
                _lastExpressionsYieldsNullOrFalse = true;
            }

            return(expression);
        }
Пример #7
0
        public override ExpressionNode VisitAllAnySubselect(AllAnySubselect expression)
        {
            expression.Left = VisitExpression(expression.Left);
            ResultAlgebraNode algebrizedQuery = Algebrizer.Convert(expression.Query);

            ExpressionNode leftExpression = expression.Left;
            RowBufferEntryExpression rightExpression = new RowBufferEntryExpression();
            rightExpression.RowBufferEntry = algebrizedQuery.OutputList[0];

            ExpressionBuilder expressionBuilder = new ExpressionBuilder();
            expressionBuilder.Push(leftExpression);
            expressionBuilder.Push(rightExpression);
            expressionBuilder.PushBinary(expression.Op);

            bool negated = (expression.Type == AllAnySubselect.AllAnyType.All);
            if (negated)
            {
                expressionBuilder.PushUnary(UnaryOperator.LogicalNot);
                expressionBuilder.Push(leftExpression);
                expressionBuilder.PushIsNull();
                expressionBuilder.Push(rightExpression);
                expressionBuilder.PushIsNull();
                expressionBuilder.PushNAry(LogicalOperator.Or);
            }

            ExpressionNode filterPredicate = expressionBuilder.Pop();

            FilterAlgebraNode filterAlgebraNode = new FilterAlgebraNode();
            filterAlgebraNode.Input = algebrizedQuery;
            filterAlgebraNode.Predicate = filterPredicate;

            AlgebraNode input = GetAndResetLastNode();

            if (!negated && !ProbingEnabled && input == null)
            {
                SetLastAlgebraNode(filterAlgebraNode);
                return LiteralExpression.FromBoolean(true);
            }
            else
            {
                if (input == null)
                    input = CreateConstantScan();

                RowBufferEntry probeColumn = CreateProbeColumn();

                JoinAlgebraNode joinAlgebraNode = new JoinAlgebraNode();
                joinAlgebraNode.PassthruPredicate = CurrentPassthruPredicate;
                joinAlgebraNode.ProbeBufferEntry = probeColumn;
                joinAlgebraNode.Left = input;
                joinAlgebraNode.Right = filterAlgebraNode;
                joinAlgebraNode.Op = negated ? JoinAlgebraNode.JoinOperator.LeftAntiSemiJoin : JoinAlgebraNode.JoinOperator.LeftSemiJoin;

                SetLastAlgebraNode(joinAlgebraNode);
                return CreateProbeColumnRef(probeColumn);
            }
        }
Пример #8
0
        public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
        {
            ILParameterDeclaration rowBufferParameter = _ilEmitContext.GetParameters(expression)[0];

            EmitLoadParameter(rowBufferParameter);
            _ilEmitContext.ILGenerator.Emit(OpCodes.Castclass, typeof(object[]));
            _ilEmitContext.ILGenerator.Emit(OpCodes.Ldc_I4, expression.RowBufferIndex);
            _ilEmitContext.ILGenerator.Emit(OpCodes.Ldelem_Ref);
            return(expression);
        }
Пример #9
0
			public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
			{
				foreach (ComputedValueDefinition valueDefinition in _definedValues)
				{
					if (valueDefinition.Target == expression.RowBufferEntry)
						return valueDefinition.Expression;
				}

				return base.VisitRowBufferEntryExpression(expression);
			}
		public override ExpressionNode VisitAggregagateExpression(AggregateExpression expression)
		{
			expression.Argument = VisitExpression(expression.Argument);

			expression.ValueDefinition.Argument = expression.Argument;

			RowBufferEntryExpression rowBufferExpression = new RowBufferEntryExpression();
			rowBufferExpression.RowBufferEntry = expression.ValueDefinition.Target;
			return rowBufferExpression;
		}
Пример #11
0
        private ExpressionNode CreateProbeColumnRef(RowBufferEntry probeColumn)
        {
            if (!ProbingEnabled)
            {
                return(LiteralExpression.FromBoolean(true));
            }

            RowBufferEntryExpression probeColumnRef = new RowBufferEntryExpression(probeColumn);

            return(probeColumnRef);
        }
Пример #12
0
        public override ExpressionNode VisitAggregagateExpression(AggregateExpression expression)
        {
            expression.Argument = VisitExpression(expression.Argument);

            expression.ValueDefinition.Argument = expression.Argument;

            RowBufferEntryExpression rowBufferExpression = new RowBufferEntryExpression();

            rowBufferExpression.RowBufferEntry = expression.ValueDefinition.Target;
            return(rowBufferExpression);
        }
Пример #13
0
            public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
            {
                foreach (ComputedValueDefinition valueDefinition in _definedValues)
                {
                    if (valueDefinition.Target == expression.RowBufferEntry)
                    {
                        return(valueDefinition.Expression);
                    }
                }

                return(base.VisitRowBufferEntryExpression(expression));
            }
Пример #14
0
            public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
            {
                foreach (UnitedValueDefinition unitedValue in _unitedValues)
                {
                    if (expression.RowBufferEntry == unitedValue.Target)
                    {
                        expression.RowBufferEntry = unitedValue.DependendEntries[_inputIndex];
                    }
                }

                return(expression);
            }
Пример #15
0
			public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
			{
				foreach (BoundRowBufferEntrySet boundRowBufferEntrySet in _bufferEntrySets)
				{
					int rowBufferIndex = Array.IndexOf(boundRowBufferEntrySet.Entries, expression.RowBufferEntry);
					if (rowBufferIndex >= 0)
					{
						expression.RowBuffer = boundRowBufferEntrySet.RowBuffer;
						expression.RowBufferIndex = rowBufferIndex;
						break;
					}
				}

				return expression;
			}
Пример #16
0
            public override AstNode Visit(AstNode node)
            {
                ExpressionNode expression = node as ExpressionNode;

                if (expression != null)
                {
                    foreach (ComputedValueDefinition computedBufferedValue in _computedValues)
                    {
                        if (expression.IsStructuralEqualTo(computedBufferedValue.Expression))
                        {
                            RowBufferEntryExpression rowBufferExpression = new RowBufferEntryExpression();
                            rowBufferExpression.RowBufferEntry = computedBufferedValue.Target;
                            return(rowBufferExpression);
                        }
                    }
                }

                return(base.Visit(node));
            }
Пример #17
0
		public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
		{
			_writer.Write(expression.RowBufferEntry.Name);
			return base.VisitRowBufferEntryExpression(expression);
		}
Пример #18
0
 private static bool VisitRowBufferExpression(RowBufferEntryExpression node1, RowBufferEntryExpression node2)
 {
     return(node2 != null &&
            node1.RowBufferEntry == node2.RowBufferEntry);
 }
Пример #19
0
        public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
        {
            expression.RowBufferEntry = ReplaceRowBufferEntry(expression.RowBufferEntry);

            return expression;
        }
Пример #20
0
        private static ResultAlgebraNode CreateAssertedSubquery(ResultAlgebraNode inputNode)
        {
            if (AstUtil.WillProduceAtMostOneRow(inputNode))
            {
                return(inputNode);
            }

            RowBufferEntry inputEntry = inputNode.OutputList[0];

            AggregatedValueDefinition countDefinedValue = new AggregatedValueDefinition();

            countDefinedValue.Aggregate  = new CountAggregateBinding("COUNT");
            countDefinedValue.Aggregator = countDefinedValue.Aggregate.CreateAggregator(typeof(int));
            countDefinedValue.Argument   = LiteralExpression.FromInt32(0);

            RowBufferEntry countDefinedValueEntry = new RowBufferEntry(countDefinedValue.Aggregator.ReturnType);

            countDefinedValue.Target = countDefinedValueEntry;

            RowBufferEntryExpression anyAggregateArgument = new RowBufferEntryExpression();

            anyAggregateArgument.RowBufferEntry = inputEntry;

            AggregatedValueDefinition anyDefinedValue = new AggregatedValueDefinition();

            anyDefinedValue.Aggregate  = new FirstAggregateBinding("ANY");
            anyDefinedValue.Aggregator = anyDefinedValue.Aggregate.CreateAggregator(inputEntry.DataType);
            anyDefinedValue.Argument   = anyAggregateArgument;

            RowBufferEntry anyDefinedValueEntry = new RowBufferEntry(inputEntry.DataType);

            anyDefinedValue.Target = anyDefinedValueEntry;

            AggregateAlgebraNode aggregateAlgebraNode = new AggregateAlgebraNode();

            aggregateAlgebraNode.Input         = inputNode.Input;
            aggregateAlgebraNode.DefinedValues = new AggregatedValueDefinition[] { countDefinedValue, anyDefinedValue };

            // CASE WHEN SubqueryCount > 1 THEN 0 ELSE NULL END

            ExpressionBuilder expressionBuilder = new ExpressionBuilder();

            expressionBuilder.Push(new RowBufferEntryExpression(countDefinedValueEntry));
            expressionBuilder.Push(LiteralExpression.FromInt32(1));
            expressionBuilder.PushBinary(BinaryOperator.Greater);
            ExpressionNode whenExpression = expressionBuilder.Pop();
            ExpressionNode thenExpression = LiteralExpression.FromInt32(0);

            CaseExpression caseExpression = new CaseExpression();

            caseExpression.WhenExpressions = new ExpressionNode[] { whenExpression };
            caseExpression.ThenExpressions = new ExpressionNode[] { thenExpression };

            expressionBuilder.Push(caseExpression);
            ExpressionNode predicate = expressionBuilder.Pop();

            AssertAlgebraNode assertAlgebraNode = new AssertAlgebraNode();

            assertAlgebraNode.Input         = aggregateAlgebraNode;
            assertAlgebraNode.Predicate     = predicate;
            assertAlgebraNode.AssertionType = AssertionType.MaxOneRow;

            ResultAlgebraNode resultAlgebraNode = new ResultAlgebraNode();

            resultAlgebraNode.Input       = assertAlgebraNode;
            resultAlgebraNode.OutputList  = new RowBufferEntry[] { anyDefinedValueEntry };
            resultAlgebraNode.ColumnNames = inputNode.ColumnNames;

            return(resultAlgebraNode);
        }
Пример #21
0
			public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
			{
				return new RowBufferEntryExpression(ReplaceRowBuffer(expression.RowBufferEntry));
			}
Пример #22
0
		public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
		{
			if (expression.RowBufferEntry == _rowBufferEntry)
				_lastExpressionsYieldsNullOrFalse = true;

			return expression;
		}
Пример #23
0
        public override QueryNode VisitBinaryQuery(BinaryQuery query)
        {
            switch (query.Op)
            {
            case BinaryQueryOperator.Intersect:
            case BinaryQueryOperator.Except:
            {
                ResultAlgebraNode left  = ((ResultAlgebraNode)ConvertAstNode(query.Left));
                ResultAlgebraNode right = ((ResultAlgebraNode)ConvertAstNode(query.Right));

                // Create distinct sort

                SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
                sortAlgebraNode.Distinct    = true;
                sortAlgebraNode.Input       = left;
                sortAlgebraNode.SortEntries = left.OutputList;
                sortAlgebraNode.SortOrders  = CreateAscendingSortOrders(sortAlgebraNode.SortEntries.Length);

                // Insert left (anti) semi join to (except) intersect left and right.

                ExpressionBuilder expressionBuilder = new ExpressionBuilder();
                for (int i = 0; i < left.OutputList.Length; i++)
                {
                    RowBufferEntryExpression leftExpr = new RowBufferEntryExpression();
                    leftExpr.RowBufferEntry = left.OutputList[i];

                    RowBufferEntryExpression rightExpr = new RowBufferEntryExpression();
                    rightExpr.RowBufferEntry = right.OutputList[i];

                    expressionBuilder.Push(leftExpr);
                    expressionBuilder.Push(rightExpr);
                    expressionBuilder.PushBinary(BinaryOperator.Equal);
                    expressionBuilder.Push(leftExpr);
                    expressionBuilder.PushIsNull();
                    expressionBuilder.Push(rightExpr);
                    expressionBuilder.PushIsNull();
                    expressionBuilder.PushBinary(BinaryOperator.LogicalAnd);
                    expressionBuilder.PushBinary(BinaryOperator.LogicalOr);
                }
                expressionBuilder.PushNAry(LogicalOperator.And);
                ExpressionNode joinCondition = expressionBuilder.Pop();

                JoinAlgebraNode joinAlgebraNode = new JoinAlgebraNode();
                if (query.Op == BinaryQueryOperator.Intersect)
                {
                    joinAlgebraNode.Op = JoinAlgebraNode.JoinOperator.LeftSemiJoin;
                }
                else
                {
                    joinAlgebraNode.Op = JoinAlgebraNode.JoinOperator.LeftAntiSemiJoin;
                }
                joinAlgebraNode.Left      = sortAlgebraNode;
                joinAlgebraNode.Right     = right;
                joinAlgebraNode.Predicate = joinCondition;
                SetLastAlgebraNode(joinAlgebraNode);

                ResultAlgebraNode resultAlgebraNode = new ResultAlgebraNode();
                resultAlgebraNode.Input       = GetLastAlgebraNode();
                resultAlgebraNode.OutputList  = left.OutputList;
                resultAlgebraNode.ColumnNames = left.ColumnNames;
                SetLastAlgebraNode(resultAlgebraNode);
                break;
            }

            case BinaryQueryOperator.Union:
            case BinaryQueryOperator.UnionAll:
            {
                // Build a flat list with all inputs.

                List <QueryNode> inputList = AstUtil.FlattenBinaryQuery(query);

                AlgebraNode[] inputs = new AlgebraNode[inputList.Count];
                for (int i = 0; i < inputs.Length; i++)
                {
                    inputs[i] = ConvertAstNode(inputList[i]);
                }

                int outputColumnCount = inputs[0].OutputList.Length;

                UnitedValueDefinition[] definedValues       = new UnitedValueDefinition[outputColumnCount];
                List <RowBufferEntry>   definedValueEntries = new List <RowBufferEntry>();

                for (int i = 0; i < outputColumnCount; i++)
                {
                    RowBufferEntry rowBufferEntry = new RowBufferEntry(inputs[0].OutputList[i].DataType);
                    definedValueEntries.Add(rowBufferEntry);

                    UnitedValueDefinition definedValue = new UnitedValueDefinition();
                    definedValue.Target = rowBufferEntry;

                    List <RowBufferEntry> dependencies = new List <RowBufferEntry>();
                    foreach (ResultAlgebraNode node in inputs)
                    {
                        dependencies.Add(node.OutputList[i]);
                    }
                    definedValue.DependendEntries = dependencies.ToArray();

                    definedValues[i] = definedValue;
                }

                ConcatAlgebraNode concatAlgebraNode = new ConcatAlgebraNode();
                concatAlgebraNode.Inputs        = inputs;
                concatAlgebraNode.DefinedValues = definedValues;
                SetLastAlgebraNode(concatAlgebraNode);

                if (query.Op == BinaryQueryOperator.Union)
                {
                    SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
                    sortAlgebraNode.Distinct    = true;
                    sortAlgebraNode.Input       = GetLastAlgebraNode();
                    sortAlgebraNode.SortEntries = definedValueEntries.ToArray();
                    sortAlgebraNode.SortOrders  = CreateAscendingSortOrders(sortAlgebraNode.SortEntries.Length);
                    SetLastAlgebraNode(sortAlgebraNode);
                }

                ResultAlgebraNode unionResultAlgebraNode = new ResultAlgebraNode();
                unionResultAlgebraNode.Input       = GetLastAlgebraNode();
                unionResultAlgebraNode.ColumnNames = ((ResultAlgebraNode)inputs[0]).ColumnNames;
                unionResultAlgebraNode.OutputList  = definedValueEntries.ToArray();
                SetLastAlgebraNode(unionResultAlgebraNode);

                break;
            }
            }

            return(query);
        }
Пример #24
0
        private static ResultAlgebraNode CreateAssertedSubquery(ResultAlgebraNode inputNode)
        {
            if (AstUtil.WillProduceAtMostOneRow(inputNode))
                return inputNode;

            RowBufferEntry inputEntry = inputNode.OutputList[0];

            AggregatedValueDefinition countDefinedValue = new AggregatedValueDefinition();
            countDefinedValue.Aggregate = new CountAggregateBinding("COUNT");
            countDefinedValue.Aggregator = countDefinedValue.Aggregate.CreateAggregator(typeof(int));
            countDefinedValue.Argument = LiteralExpression.FromInt32(0);

            RowBufferEntry countDefinedValueEntry = new RowBufferEntry(countDefinedValue.Aggregator.ReturnType);
            countDefinedValue.Target = countDefinedValueEntry;

            RowBufferEntryExpression anyAggregateArgument = new RowBufferEntryExpression();
            anyAggregateArgument.RowBufferEntry = inputEntry;

            AggregatedValueDefinition anyDefinedValue = new AggregatedValueDefinition();
            anyDefinedValue.Aggregate = new FirstAggregateBinding("ANY");
            anyDefinedValue.Aggregator = anyDefinedValue.Aggregate.CreateAggregator(inputEntry.DataType);
            anyDefinedValue.Argument = anyAggregateArgument;

            RowBufferEntry anyDefinedValueEntry = new RowBufferEntry(inputEntry.DataType);
            anyDefinedValue.Target = anyDefinedValueEntry;

            AggregateAlgebraNode aggregateAlgebraNode = new AggregateAlgebraNode();
            aggregateAlgebraNode.Input = inputNode.Input;
            aggregateAlgebraNode.DefinedValues = new AggregatedValueDefinition[] { countDefinedValue, anyDefinedValue };

            // CASE WHEN SubqueryCount > 1 THEN 0 ELSE NULL END

            ExpressionBuilder expressionBuilder = new ExpressionBuilder();
            expressionBuilder.Push(new RowBufferEntryExpression(countDefinedValueEntry));
            expressionBuilder.Push(LiteralExpression.FromInt32(1));
            expressionBuilder.PushBinary(BinaryOperator.Greater);
            ExpressionNode whenExpression = expressionBuilder.Pop();
            ExpressionNode thenExpression = LiteralExpression.FromInt32(0);

            CaseExpression caseExpression = new CaseExpression();
            caseExpression.WhenExpressions = new ExpressionNode[] { whenExpression };
            caseExpression.ThenExpressions = new ExpressionNode[] { thenExpression };

            expressionBuilder.Push(caseExpression);
            ExpressionNode predicate = expressionBuilder.Pop();

            AssertAlgebraNode assertAlgebraNode = new AssertAlgebraNode();
            assertAlgebraNode.Input = aggregateAlgebraNode;
            assertAlgebraNode.Predicate = predicate;
            assertAlgebraNode.AssertionType = AssertionType.MaxOneRow;

            ResultAlgebraNode resultAlgebraNode = new ResultAlgebraNode();
            resultAlgebraNode.Input = assertAlgebraNode;
            resultAlgebraNode.OutputList = new RowBufferEntry[] { anyDefinedValueEntry };
            resultAlgebraNode.ColumnNames = inputNode.ColumnNames;

            return resultAlgebraNode;
        }
Пример #25
0
		public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
		{
			ILParameterDeclaration rowBufferParameter = _ilEmitContext.GetParameters(expression)[0];
			EmitLoadParameter(rowBufferParameter);
			_ilEmitContext.ILGenerator.Emit(OpCodes.Castclass, typeof (object[]));
			_ilEmitContext.ILGenerator.Emit(OpCodes.Ldc_I4, expression.RowBufferIndex);
			_ilEmitContext.ILGenerator.Emit(OpCodes.Ldelem_Ref);
			return expression;
		}
Пример #26
0
 public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
 {
     return(new RowBufferEntryExpression(ReplaceRowBuffer(expression.RowBufferEntry)));
 }
Пример #27
0
 public virtual ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
 {
     return(expression);
 }
Пример #28
0
		public virtual ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
		{
			return expression;
		}
Пример #29
0
            public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
            {
                foreach (UnitedValueDefinition unitedValue in _unitedValues)
                {
                    if (expression.RowBufferEntry == unitedValue.Target)
                        expression.RowBufferEntry = unitedValue.DependendEntries[_inputIndex];
                }

                return expression;
            }
Пример #30
0
		private static bool VisitRowBufferExpression(RowBufferEntryExpression node1, RowBufferEntryExpression node2)
	    {
			return node2 != null &&
			       node1.RowBufferEntry == node2.RowBufferEntry;
        }
Пример #31
0
 public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
 {
     _writer.Write(expression.RowBufferEntry.Name);
     return(base.VisitRowBufferEntryExpression(expression));
 }
Пример #32
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);
        }
Пример #33
0
 public override AstElement Clone(Dictionary<AstElement, AstElement> alreadyClonedElements)
 {
     RowBufferEntryExpression result = new RowBufferEntryExpression();
     result.RowBufferEntry = _rowBufferEntry;
     return result;
 }
Пример #34
0
        private ExpressionNode CreateProbeColumnRef(RowBufferEntry probeColumn)
        {
            if (!ProbingEnabled)
                return LiteralExpression.FromBoolean(true);

            RowBufferEntryExpression probeColumnRef = new RowBufferEntryExpression(probeColumn);
            return probeColumnRef;
        }
Пример #35
0
 public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
 {
     _ilEmitContext.AddParameter(expression, expression.RowBuffer, typeof(object[]));
     return(expression);
 }
Пример #36
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);
        }
Пример #37
0
		public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
		{
			_ilEmitContext.AddParameter(expression, expression.RowBuffer, typeof(object[]));
			return expression;
		}
		public override ExpressionNode VisitColumnExpression(ColumnExpression expression)
		{
			RowBufferEntryExpression rowBufferExpression = new RowBufferEntryExpression();
			rowBufferExpression.RowBufferEntry = expression.Column.ValueDefinition.Target;
			return rowBufferExpression;
		}
Пример #39
0
        public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
        {
            expression.RowBufferEntry = ReplaceRowBufferEntry(expression.RowBufferEntry);

            return(expression);
        }
Пример #40
0
            public override AstNode Visit(AstNode node)
            {
                ExpressionNode expression = node as ExpressionNode;
                if (expression != null)
                {
                    foreach (ComputedValueDefinition computedBufferedValue in _computedValues)
                    {
                        if (expression.IsStructuralEqualTo(computedBufferedValue.Expression))
                        {
                            RowBufferEntryExpression rowBufferExpression = new RowBufferEntryExpression();
                            rowBufferExpression.RowBufferEntry = computedBufferedValue.Target;
                            return rowBufferExpression;
                        }
                    }
                }

                return base.Visit(node);
            }
Пример #41
0
		public override QueryNode VisitBinaryQuery(BinaryQuery query)
		{
			switch (query.Op)
			{
				case BinaryQueryOperator.Intersect:
				case BinaryQueryOperator.Except:
				{
					ResultAlgebraNode left = ((ResultAlgebraNode)ConvertAstNode(query.Left));
					ResultAlgebraNode right = ((ResultAlgebraNode)ConvertAstNode(query.Right));

					// Create distinct sort

					SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
					sortAlgebraNode.Distinct = true;
					sortAlgebraNode.Input = left;
					sortAlgebraNode.SortEntries = left.OutputList;
					sortAlgebraNode.SortOrders = CreateAscendingSortOrders(sortAlgebraNode.SortEntries.Length);

					// Insert left (anti) semi join to (except) intersect left and right.

					ExpressionBuilder expressionBuilder = new ExpressionBuilder();
					for (int i = 0; i < left.OutputList.Length; i++)
					{
						RowBufferEntryExpression leftExpr = new RowBufferEntryExpression();
						leftExpr.RowBufferEntry = left.OutputList[i];

						RowBufferEntryExpression rightExpr = new RowBufferEntryExpression();
						rightExpr.RowBufferEntry = right.OutputList[i];

						expressionBuilder.Push(leftExpr);
						expressionBuilder.Push(rightExpr);
						expressionBuilder.PushBinary(BinaryOperator.Equal);
						expressionBuilder.Push(leftExpr);
						expressionBuilder.PushIsNull();
						expressionBuilder.Push(rightExpr);
						expressionBuilder.PushIsNull();
						expressionBuilder.PushBinary(BinaryOperator.LogicalAnd);
						expressionBuilder.PushBinary(BinaryOperator.LogicalOr);
					}
					expressionBuilder.PushNAry(LogicalOperator.And);
					ExpressionNode joinCondition = expressionBuilder.Pop();

					JoinAlgebraNode joinAlgebraNode = new JoinAlgebraNode();
					if (query.Op == BinaryQueryOperator.Intersect)
						joinAlgebraNode.Op = JoinAlgebraNode.JoinOperator.LeftSemiJoin;
					else
						joinAlgebraNode.Op = JoinAlgebraNode.JoinOperator.LeftAntiSemiJoin;
					joinAlgebraNode.Left = sortAlgebraNode;
					joinAlgebraNode.Right = right;
					joinAlgebraNode.Predicate = joinCondition;
					SetLastAlgebraNode(joinAlgebraNode);

					ResultAlgebraNode resultAlgebraNode = new ResultAlgebraNode();
					resultAlgebraNode.Input = GetLastAlgebraNode();
					resultAlgebraNode.OutputList = left.OutputList;
					resultAlgebraNode.ColumnNames = left.ColumnNames;
					SetLastAlgebraNode(resultAlgebraNode);
					break;
				}

				case BinaryQueryOperator.Union:
				case BinaryQueryOperator.UnionAll:
				{
					// Build a flat list with all inputs.

					List<QueryNode> inputList = AstUtil.FlattenBinaryQuery(query);

					AlgebraNode[] inputs = new AlgebraNode[inputList.Count];
					for (int i = 0; i < inputs.Length; i++)
						inputs[i] = ConvertAstNode(inputList[i]);

					int outputColumnCount = inputs[0].OutputList.Length;

					UnitedValueDefinition[] definedValues = new UnitedValueDefinition[outputColumnCount];
					List<RowBufferEntry> definedValueEntries = new List<RowBufferEntry>();

					for (int i = 0; i < outputColumnCount; i++)
					{
						RowBufferEntry rowBufferEntry = new RowBufferEntry(inputs[0].OutputList[i].DataType);
						definedValueEntries.Add(rowBufferEntry);

						UnitedValueDefinition definedValue = new UnitedValueDefinition();
						definedValue.Target = rowBufferEntry;

						List<RowBufferEntry> dependencies = new List<RowBufferEntry>();
						foreach (ResultAlgebraNode node in inputs)
							dependencies.Add(node.OutputList[i]);
						definedValue.DependendEntries = dependencies.ToArray();

						definedValues[i] = definedValue;
					}

					ConcatAlgebraNode concatAlgebraNode = new ConcatAlgebraNode();
					concatAlgebraNode.Inputs = inputs;
					concatAlgebraNode.DefinedValues = definedValues;
					SetLastAlgebraNode(concatAlgebraNode);

					if (query.Op == BinaryQueryOperator.Union)
					{
						SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
						sortAlgebraNode.Distinct = true;
						sortAlgebraNode.Input = GetLastAlgebraNode();
						sortAlgebraNode.SortEntries = definedValueEntries.ToArray();
						sortAlgebraNode.SortOrders = CreateAscendingSortOrders(sortAlgebraNode.SortEntries.Length);
						SetLastAlgebraNode(sortAlgebraNode);
					}

					ResultAlgebraNode unionResultAlgebraNode = new ResultAlgebraNode();
					unionResultAlgebraNode.Input = GetLastAlgebraNode();
					unionResultAlgebraNode.ColumnNames = ((ResultAlgebraNode)inputs[0]).ColumnNames;
					unionResultAlgebraNode.OutputList = definedValueEntries.ToArray();
					SetLastAlgebraNode(unionResultAlgebraNode);

					break;
				}
			}

			return query;
		}
Пример #42
0
			public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
			{
				if (!_rowBufferEntries.Contains(expression.RowBufferEntry))
					_rowBufferEntries.Add(expression.RowBufferEntry);

				return base.VisitRowBufferEntryExpression(expression);
			}
Пример #43
0
        public override ExpressionNode VisitAllAnySubselect(AllAnySubselect expression)
        {
            expression.Left = VisitExpression(expression.Left);
            ResultAlgebraNode algebrizedQuery = Algebrizer.Convert(expression.Query);

            ExpressionNode           leftExpression  = expression.Left;
            RowBufferEntryExpression rightExpression = new RowBufferEntryExpression();

            rightExpression.RowBufferEntry = algebrizedQuery.OutputList[0];

            ExpressionBuilder expressionBuilder = new ExpressionBuilder();

            expressionBuilder.Push(leftExpression);
            expressionBuilder.Push(rightExpression);
            expressionBuilder.PushBinary(expression.Op);

            bool negated = (expression.Type == AllAnySubselect.AllAnyType.All);

            if (negated)
            {
                expressionBuilder.PushUnary(UnaryOperator.LogicalNot);
                expressionBuilder.Push(leftExpression);
                expressionBuilder.PushIsNull();
                expressionBuilder.Push(rightExpression);
                expressionBuilder.PushIsNull();
                expressionBuilder.PushNAry(LogicalOperator.Or);
            }

            ExpressionNode filterPredicate = expressionBuilder.Pop();

            FilterAlgebraNode filterAlgebraNode = new FilterAlgebraNode();

            filterAlgebraNode.Input     = algebrizedQuery;
            filterAlgebraNode.Predicate = filterPredicate;

            AlgebraNode input = GetAndResetLastNode();

            if (!negated && !ProbingEnabled && input == null)
            {
                SetLastAlgebraNode(filterAlgebraNode);
                return(LiteralExpression.FromBoolean(true));
            }
            else
            {
                if (input == null)
                {
                    input = CreateConstantScan();
                }

                RowBufferEntry probeColumn = CreateProbeColumn();

                JoinAlgebraNode joinAlgebraNode = new JoinAlgebraNode();
                joinAlgebraNode.PassthruPredicate = CurrentPassthruPredicate;
                joinAlgebraNode.ProbeBufferEntry  = probeColumn;
                joinAlgebraNode.Left  = input;
                joinAlgebraNode.Right = filterAlgebraNode;
                joinAlgebraNode.Op    = negated ? JoinAlgebraNode.JoinOperator.LeftAntiSemiJoin : JoinAlgebraNode.JoinOperator.LeftSemiJoin;

                SetLastAlgebraNode(joinAlgebraNode);
                return(CreateProbeColumnRef(probeColumn));
            }
        }