Exemplo n.º 1
0
        public override AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
        {
            base.VisitResultAlgebraNode(node);
            ReplaceRowBufferEntries(node.OutputList);

            return node;
        }
Exemplo n.º 2
0
        public override QueryNode VisitSortedQuery(SortedQuery query)
        {
            ResultAlgebraNode input = (ResultAlgebraNode)ConvertAstNode(query.Input);

            List <RowBufferEntry> sortColumns = new List <RowBufferEntry>();
            List <SortOrder>      sortOrders  = new List <SortOrder>();

            foreach (OrderByColumn orderByColumn in query.OrderByColumns)
            {
                RowBufferEntry sortColumn = input.OutputList[orderByColumn.ColumnIndex];
                sortColumns.Add(sortColumn);
                sortOrders.Add(orderByColumn.SortOrder);
            }

            SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();

            sortAlgebraNode.Input       = input.Input;
            sortAlgebraNode.SortEntries = sortColumns.ToArray();
            sortAlgebraNode.SortOrders  = sortOrders.ToArray();
            input.Input = sortAlgebraNode;

            SetLastAlgebraNode(input);

            return(query);
        }
Exemplo n.º 3
0
        public override AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
        {
            base.VisitResultAlgebraNode(node);
            ReplaceRowBufferEntries(node.OutputList);

            return(node);
        }
Exemplo n.º 4
0
        public override AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
        {
            if (IsSemiJoinContext)
                return VisitAlgebraNode(node.Input);

            return base.VisitResultAlgebraNode(node);
        }
Exemplo n.º 5
0
        public override AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
        {
            ShowPlanElement inputElement = ConvertNode(node.Input);

            PropertyListBuilder propertyListBuilder = new PropertyListBuilder();

            propertyListBuilder.Begin(Resources.ShowPlanGroupOutputList);
            for (int i = 0; i < node.OutputList.Length; i++)
            {
                propertyListBuilder.Begin();
                propertyListBuilder.SetGroupValue(String.Format(CultureInfo.InvariantCulture, "{0} AS {1}", node.OutputList[i].Name, node.ColumnNames[i]));
                WriteRowBufferEntry(propertyListBuilder, node.OutputList[i]);
                propertyListBuilder.Write(Resources.ShowPlanKeyOutputName, node.ColumnNames[i]);
                propertyListBuilder.End();
            }
            propertyListBuilder.End();

            IList <ShowPlanProperty> properties = propertyListBuilder.ToList();

            ShowPlanElement element = new ShowPlanElement(ShowPlanOperator.Select, properties, inputElement);

            _currentElement = element;

            return(node);
        }
Exemplo n.º 6
0
        public override AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
        {
            if (IsSemiJoinContext)
            {
                return(VisitAlgebraNode(node.Input));
            }

            return(base.VisitResultAlgebraNode(node));
        }
Exemplo n.º 7
0
		public override AstElement Clone(Dictionary<AstElement, AstElement> alreadyClonedElements)
		{
			ResultAlgebraNode result = new ResultAlgebraNode();
			result.StatisticsIterator = StatisticsIterator;
			result.OutputList = ArrayHelpers.Clone(OutputList);
			result.Input = (AlgebraNode)Input.Clone(alreadyClonedElements);
			result.ColumnNames = ArrayHelpers.Clone(_columnNames);
			return result;
		}
Exemplo n.º 8
0
        public override AstElement Clone(Dictionary <AstElement, AstElement> alreadyClonedElements)
        {
            ResultAlgebraNode result = new ResultAlgebraNode();

            result.StatisticsIterator = StatisticsIterator;
            result.OutputList         = ArrayHelpers.Clone(OutputList);
            result.Input       = (AlgebraNode)Input.Clone(alreadyClonedElements);
            result.ColumnNames = ArrayHelpers.Clone(_columnNames);
            return(result);
        }
Exemplo n.º 9
0
        public override AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
        {
            foreach (RowBufferEntry column in node.OutputList)
            {
                AddNeededRowBufferEntry(column);
            }

            node.Input = VisitAlgebraNode(node.Input);

            return(node);
        }
Exemplo n.º 10
0
		public static ResultIterator Convert(MetadataContext metadataContext, bool includeStatistics, ResultAlgebraNode resultAlgebraNode)
		{
			IteratorCreator iteratorCreator = new IteratorCreator(metadataContext, includeStatistics);
			Iterator iterator = iteratorCreator.ConvertAlgebraNode(resultAlgebraNode);

			ILEmitContext.CompleteILCompilation();

			if (includeStatistics)
				return (ResultIterator) ((StatisticsIterator) iterator).Input;

			return (ResultIterator) iterator;
		}
Exemplo n.º 11
0
        public override ExpressionNode VisitSingleRowSubselect(SingleRowSubselect expression)
        {
            AlgebraNode       inputNode          = GetOrCreateInput();
            ResultAlgebraNode algebrizedSubquery = Algebrizer.Convert(expression.Query);
            ResultAlgebraNode assertedSubquery   = CreateAssertedSubquery(algebrizedSubquery);

            JoinAlgebraNode joinAlgebraNode = new JoinAlgebraNode();

            joinAlgebraNode.PassthruPredicate = CurrentPassthruPredicate;
            joinAlgebraNode.Op    = JoinAlgebraNode.JoinOperator.LeftOuterJoin;
            joinAlgebraNode.Left  = inputNode;
            joinAlgebraNode.Right = assertedSubquery;
            SetLastAlgebraNode(joinAlgebraNode);

            return(new RowBufferEntryExpression(assertedSubquery.OutputList[0]));
        }
Exemplo n.º 12
0
        public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
        {
            AlgebraNode       input           = GetAndResetLastNode();
            ResultAlgebraNode algebrizedQuery = Algebrizer.Convert(expression.Query);

            if (!expression.Negated && AstUtil.WillProduceAtLeastOneRow(algebrizedQuery))
            {
                if (input == null)
                {
                    SetLastAlgebraNode(CreateConstantScan());
                }
                else
                {
                    SetLastAlgebraNode(input);
                }

                return(LiteralExpression.FromBoolean(true));
            }


            if (!expression.Negated && !ProbingEnabled && input == null)
            {
                SetLastAlgebraNode(algebrizedQuery);
                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 = algebrizedQuery;
                joinAlgebraNode.Op    = expression.Negated ? JoinAlgebraNode.JoinOperator.LeftAntiSemiJoin : JoinAlgebraNode.JoinOperator.LeftSemiJoin;
                SetLastAlgebraNode(joinAlgebraNode);

                return(CreateProbeColumnRef(probeColumn));
            }
        }
Exemplo n.º 13
0
        public override AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
        {
            bool wasFirstResultNode = _isFirstResultNode;

            if (_isFirstResultNode)
            {
                _isFirstResultNode = false;
            }

            node.Input = VisitAlgebraNode(node.Input);

            if (wasFirstResultNode)
            {
                return(node);
            }

            return(node.Input);
        }
Exemplo n.º 14
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));
            }
        }
Exemplo n.º 15
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;
        }
Exemplo n.º 16
0
		public override QueryNode VisitSelectQuery(SelectQuery query)
		{
			// Calculate needed row buffers

			RowBufferCalculator rowBufferCalculator = query.RowBufferCalculator;

			// Emit FROM

			if (query.TableReferences != null)
			{
				Visit(query.TableReferences);
			}
			else
			{
				ConstantScanAlgebraNode constantScanAlgebraNode = new ConstantScanAlgebraNode();
				constantScanAlgebraNode.DefinedValues = new ComputedValueDefinition[0];
				SetLastAlgebraNode(constantScanAlgebraNode);
			}

			// Emit WHERE

			if (query.WhereClause != null)
			{
				FilterAlgebraNode filterAlgebraNode = new FilterAlgebraNode();
				filterAlgebraNode.Input = GetLastAlgebraNode();
				filterAlgebraNode.Predicate = query.WhereClause;
				SetLastAlgebraNode(filterAlgebraNode);
			}

			// Emit GROUP BY

			if (query.GroupByColumns != null || query.IsAggregated)
			{
				EmitComputeScalarIfNeeded(rowBufferCalculator.ComputedGroupColumns);

				List<AggregatedValueDefinition> definedValues = new List<AggregatedValueDefinition>();
				foreach (AggregateExpression aggregateDependency in query.AggregateDependencies)
					definedValues.Add(aggregateDependency.ValueDefinition);

				if (query.GroupByColumns != null)
				{
					SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
					sortAlgebraNode.Input = GetLastAlgebraNode();
					sortAlgebraNode.SortEntries = rowBufferCalculator.GroupColumns;
					sortAlgebraNode.SortOrders = CreateAscendingSortOrders(sortAlgebraNode.SortEntries.Length);
					SetLastAlgebraNode(sortAlgebraNode);
				}

				AggregateAlgebraNode algebraNode = new AggregateAlgebraNode();
				algebraNode.Input = GetLastAlgebraNode();
				algebraNode.DefinedValues = definedValues.ToArray();
				algebraNode.Groups = rowBufferCalculator.GroupColumns;
				SetLastAlgebraNode(algebraNode);
			}

			// Emit HAVING

			if (query.HavingClause != null)
			{
				FilterAlgebraNode filterAlgebraNode = new FilterAlgebraNode();
				filterAlgebraNode.Input = GetLastAlgebraNode();
				filterAlgebraNode.Predicate = query.HavingClause;
				SetLastAlgebraNode(filterAlgebraNode);
			}

			// Emit compute scalar to calculate expressions needed in SELECT and ORDER BY

			EmitComputeScalarIfNeeded(rowBufferCalculator.ComputedSelectAndOrderColumns);

			// Emit DISTINCT and ORDER BY

			if (query.IsDistinct && query.OrderByColumns != null)
			{
				List<RowBufferEntry> sortEntries = new List<RowBufferEntry>();
				List<SortOrder> sortOrderList = new List<SortOrder>();

				for (int i = 0; i < query.OrderByColumns.Length; i++)
				{
					sortEntries.Add(rowBufferCalculator.OrderColumns[i]);
					sortOrderList.Add(query.OrderByColumns[i].SortOrder);
				}

				foreach (RowBufferEntry selectColumn in rowBufferCalculator.SelectColumns)
				{
					bool selectColumnMustBeSorted = !sortEntries.Contains(selectColumn);
					if (selectColumnMustBeSorted)
					{
						sortEntries.Add(selectColumn);
						sortOrderList.Add(SortOrder.Ascending);
					}
				}

				SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
				sortAlgebraNode.Distinct = true;
				sortAlgebraNode.Input = GetLastAlgebraNode();
				sortAlgebraNode.SortEntries = sortEntries.ToArray();
				sortAlgebraNode.SortOrders = sortOrderList.ToArray();

				SetLastAlgebraNode(sortAlgebraNode);
			}
			else
			{
				if (query.IsDistinct)
				{
					SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
					sortAlgebraNode.Distinct = true;
					sortAlgebraNode.Input = GetLastAlgebraNode();
					sortAlgebraNode.SortEntries = rowBufferCalculator.SelectColumns;
					sortAlgebraNode.SortOrders = CreateAscendingSortOrders(sortAlgebraNode.SortEntries.Length);
					SetLastAlgebraNode(sortAlgebraNode);
				}

				if (query.OrderByColumns != null)
				{
					List<SortOrder> sortOrderList = new List<SortOrder>();
					foreach (OrderByColumn orderByColumn in query.OrderByColumns)
						sortOrderList.Add(orderByColumn.SortOrder);

					SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
					sortAlgebraNode.Input = GetLastAlgebraNode();
					sortAlgebraNode.SortEntries = rowBufferCalculator.OrderColumns;
					sortAlgebraNode.SortOrders = sortOrderList.ToArray();
					SetLastAlgebraNode(sortAlgebraNode);
				}
			}

			// Emit TOP

			if (query.TopClause != null)
			{
				TopAlgebraNode algebraNode = new TopAlgebraNode();
				algebraNode.Input = GetLastAlgebraNode();
				algebraNode.Limit = query.TopClause.Value;

				if (query.TopClause.WithTies)
					algebraNode.TieEntries = rowBufferCalculator.OrderColumns;

				SetLastAlgebraNode(algebraNode);
			}

			// Emit select list

			List<string> columnNames = new List<string>();
			foreach (SelectColumn columnSource in query.SelectColumns)
			{
				if (columnSource.Alias != null)
					columnNames.Add(columnSource.Alias.Text);
				else
					columnNames.Add(null);
			}

			ResultAlgebraNode resultAlgebraNode = new ResultAlgebraNode();
			resultAlgebraNode.Input = GetLastAlgebraNode();
			resultAlgebraNode.OutputList = rowBufferCalculator.SelectColumns;
			resultAlgebraNode.ColumnNames = columnNames.ToArray();
			SetLastAlgebraNode(resultAlgebraNode);

			return query;
		}
Exemplo n.º 17
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;
		}
Exemplo n.º 18
0
		protected override void ClearCompiledState()
		{
			_resultAlgebraNode = null;
			_resultIterator = null;
			_errorCollector.Reset();
		}
Exemplo n.º 19
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);
        }
Exemplo n.º 20
0
        public override QueryNode VisitSelectQuery(SelectQuery query)
        {
            // Calculate needed row buffers

            RowBufferCalculator rowBufferCalculator = query.RowBufferCalculator;

            // Emit FROM

            if (query.TableReferences != null)
            {
                Visit(query.TableReferences);
            }
            else
            {
                ConstantScanAlgebraNode constantScanAlgebraNode = new ConstantScanAlgebraNode();
                constantScanAlgebraNode.DefinedValues = new ComputedValueDefinition[0];
                SetLastAlgebraNode(constantScanAlgebraNode);
            }

            // Emit WHERE

            if (query.WhereClause != null)
            {
                FilterAlgebraNode filterAlgebraNode = new FilterAlgebraNode();
                filterAlgebraNode.Input     = GetLastAlgebraNode();
                filterAlgebraNode.Predicate = query.WhereClause;
                SetLastAlgebraNode(filterAlgebraNode);
            }

            // Emit GROUP BY

            if (query.GroupByColumns != null || query.IsAggregated)
            {
                EmitComputeScalarIfNeeded(rowBufferCalculator.ComputedGroupColumns);

                List <AggregatedValueDefinition> definedValues = new List <AggregatedValueDefinition>();
                foreach (AggregateExpression aggregateDependency in query.AggregateDependencies)
                {
                    definedValues.Add(aggregateDependency.ValueDefinition);
                }

                if (query.GroupByColumns != null)
                {
                    SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
                    sortAlgebraNode.Input       = GetLastAlgebraNode();
                    sortAlgebraNode.SortEntries = rowBufferCalculator.GroupColumns;
                    sortAlgebraNode.SortOrders  = CreateAscendingSortOrders(sortAlgebraNode.SortEntries.Length);
                    SetLastAlgebraNode(sortAlgebraNode);
                }

                AggregateAlgebraNode algebraNode = new AggregateAlgebraNode();
                algebraNode.Input         = GetLastAlgebraNode();
                algebraNode.DefinedValues = definedValues.ToArray();
                algebraNode.Groups        = rowBufferCalculator.GroupColumns;
                SetLastAlgebraNode(algebraNode);
            }

            // Emit HAVING

            if (query.HavingClause != null)
            {
                FilterAlgebraNode filterAlgebraNode = new FilterAlgebraNode();
                filterAlgebraNode.Input     = GetLastAlgebraNode();
                filterAlgebraNode.Predicate = query.HavingClause;
                SetLastAlgebraNode(filterAlgebraNode);
            }

            // Emit compute scalar to calculate expressions needed in SELECT and ORDER BY

            EmitComputeScalarIfNeeded(rowBufferCalculator.ComputedSelectAndOrderColumns);

            // Emit DISTINCT and ORDER BY

            if (query.IsDistinct && query.OrderByColumns != null)
            {
                List <RowBufferEntry> sortEntries   = new List <RowBufferEntry>();
                List <SortOrder>      sortOrderList = new List <SortOrder>();

                for (int i = 0; i < query.OrderByColumns.Length; i++)
                {
                    sortEntries.Add(rowBufferCalculator.OrderColumns[i]);
                    sortOrderList.Add(query.OrderByColumns[i].SortOrder);
                }

                foreach (RowBufferEntry selectColumn in rowBufferCalculator.SelectColumns)
                {
                    bool selectColumnMustBeSorted = !sortEntries.Contains(selectColumn);
                    if (selectColumnMustBeSorted)
                    {
                        sortEntries.Add(selectColumn);
                        sortOrderList.Add(SortOrder.Ascending);
                    }
                }

                SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
                sortAlgebraNode.Distinct    = true;
                sortAlgebraNode.Input       = GetLastAlgebraNode();
                sortAlgebraNode.SortEntries = sortEntries.ToArray();
                sortAlgebraNode.SortOrders  = sortOrderList.ToArray();

                SetLastAlgebraNode(sortAlgebraNode);
            }
            else
            {
                if (query.IsDistinct)
                {
                    SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
                    sortAlgebraNode.Distinct    = true;
                    sortAlgebraNode.Input       = GetLastAlgebraNode();
                    sortAlgebraNode.SortEntries = rowBufferCalculator.SelectColumns;
                    sortAlgebraNode.SortOrders  = CreateAscendingSortOrders(sortAlgebraNode.SortEntries.Length);
                    SetLastAlgebraNode(sortAlgebraNode);
                }

                if (query.OrderByColumns != null)
                {
                    List <SortOrder> sortOrderList = new List <SortOrder>();
                    foreach (OrderByColumn orderByColumn in query.OrderByColumns)
                    {
                        sortOrderList.Add(orderByColumn.SortOrder);
                    }

                    SortAlgebraNode sortAlgebraNode = new SortAlgebraNode();
                    sortAlgebraNode.Input       = GetLastAlgebraNode();
                    sortAlgebraNode.SortEntries = rowBufferCalculator.OrderColumns;
                    sortAlgebraNode.SortOrders  = sortOrderList.ToArray();
                    SetLastAlgebraNode(sortAlgebraNode);
                }
            }

            // Emit TOP

            if (query.TopClause != null)
            {
                TopAlgebraNode algebraNode = new TopAlgebraNode();
                algebraNode.Input = GetLastAlgebraNode();
                algebraNode.Limit = query.TopClause.Value;

                if (query.TopClause.WithTies)
                {
                    algebraNode.TieEntries = rowBufferCalculator.OrderColumns;
                }

                SetLastAlgebraNode(algebraNode);
            }

            // Emit select list

            List <string> columnNames = new List <string>();

            foreach (SelectColumn columnSource in query.SelectColumns)
            {
                if (columnSource.Alias != null)
                {
                    columnNames.Add(columnSource.Alias.Text);
                }
                else
                {
                    columnNames.Add(null);
                }
            }

            ResultAlgebraNode resultAlgebraNode = new ResultAlgebraNode();

            resultAlgebraNode.Input       = GetLastAlgebraNode();
            resultAlgebraNode.OutputList  = rowBufferCalculator.SelectColumns;
            resultAlgebraNode.ColumnNames = columnNames.ToArray();
            SetLastAlgebraNode(resultAlgebraNode);

            return(query);
        }
Exemplo n.º 21
0
 public virtual AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
 {
     node.Input = VisitAlgebraNode(node.Input);
     return(node);
 }
Exemplo n.º 22
0
		public virtual AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
		{
			node.Input = VisitAlgebraNode(node.Input);
			return node;
		}
Exemplo n.º 23
0
        public override AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
        {
            bool wasFirstResultNode = _isFirstResultNode;
            if (_isFirstResultNode)
                _isFirstResultNode = false;

            node.Input = VisitAlgebraNode(node.Input);

            if (wasFirstResultNode)
                return node;

            return node.Input;
        }
Exemplo n.º 24
0
		private void Compile()
		{
			if (DataContext == null)
				throw ExceptionBuilder.PropertyNotInitialized("DataContext");
			
			if (Text == null || Text.Length == 0)
				throw ExceptionBuilder.PropertyNotInitialized("Text");
			
			// Compile query

			ClearCompiledState();
			Compiler compiler = new Compiler(_errorCollector);
			_resultAlgebraNode = compiler.CompileQuery(Text, Scope);
	
			if (_errorCollector.ErrorsSeen)
			{
				IList<CompilationError> errors = _errorCollector.GetErrors();
				OnCompilationFailed(new CompilationFailedEventArgs(errors));
				throw ExceptionBuilder.QueryCompilationFailed(errors);
			}

			OnCompilationSucceeded(EventArgs.Empty);
			_resultIterator = IteratorCreator.Convert(DataContext.MetadataContext, true, _resultAlgebraNode);
		}
Exemplo n.º 25
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);
        }
Exemplo n.º 26
0
		public override AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
		{
			ResultIterator resultIterator = new ResultIterator();
			resultIterator.RowBuffer = new object[node.OutputList.Length];
			resultIterator.Input = ConvertAlgebraNode(node.Input);
			resultIterator.InputOutput = new IteratorOutput[node.OutputList.Length];
			resultIterator.ColumnNames = new string[node.OutputList.Length];
			resultIterator.ColumnTypes = new Type[node.OutputList.Length];
			for (int i = 0; i < node.OutputList.Length; i++)
			{
				IteratorOutput iteratorOutput = new IteratorOutput();
				iteratorOutput.SourceIndex = Array.IndexOf(node.Input.OutputList, node.OutputList[i]);
				iteratorOutput.TargetIndex = i;
				resultIterator.InputOutput[i] = iteratorOutput;
				resultIterator.ColumnNames[i] = node.ColumnNames[i];
				resultIterator.ColumnTypes[i] = node.OutputList[i].DataType;
			}
			SetLastIterator(node, resultIterator);

			return node;
		}
Exemplo n.º 27
0
        public override AlgebraNode VisitResultAlgebraNode(ResultAlgebraNode node)
        {
            ShowPlanElement inputElement = ConvertNode(node.Input);

            PropertyListBuilder propertyListBuilder = new PropertyListBuilder();
            propertyListBuilder.Begin(Resources.ShowPlanGroupOutputList);
            for (int i = 0; i < node.OutputList.Length; i++)
            {
                propertyListBuilder.Begin();
                propertyListBuilder.SetGroupValue(String.Format(CultureInfo.InvariantCulture, "{0} AS {1}", node.OutputList[i].Name, node.ColumnNames[i]));
                WriteRowBufferEntry(propertyListBuilder, node.OutputList[i]);
                propertyListBuilder.Write(Resources.ShowPlanKeyOutputName, node.ColumnNames[i]);
                propertyListBuilder.End();
            }
            propertyListBuilder.End();

            IList<ShowPlanProperty> properties = propertyListBuilder.ToList();

            ShowPlanElement element = new ShowPlanElement(ShowPlanOperator.Select, properties, inputElement);
            _currentElement = element;

            return node;
        }