Esempio n. 1
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);
        }
Esempio n. 2
0
		public override AstElement Clone(Dictionary<AstElement, AstElement> alreadyClonedElements)
		{
			SortAlgebraNode result = new SortAlgebraNode();
			result.StatisticsIterator = StatisticsIterator;
			result.OutputList = ArrayHelpers.Clone(OutputList);
			result.Input = (AlgebraNode)Input.Clone(alreadyClonedElements);
			result.Distinct = _distinct;
			result.SortEntries = ArrayHelpers.Clone(_sortEntries);
			result.SortOrders = ArrayHelpers.Clone(_sortOrders);
			return result;
		}
Esempio n. 3
0
        public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
        {
            node.OutputList = RemovedUnneededRowBufferColumns(node.OutputList);

            foreach (RowBufferEntry sortEntry in node.SortEntries)
            {
                AddNeededRowBufferEntry(sortEntry);
            }

            return(base.VisitSortAlgebraNode(node));
        }
Esempio n. 4
0
        public override AstElement Clone(Dictionary <AstElement, AstElement> alreadyClonedElements)
        {
            SortAlgebraNode result = new SortAlgebraNode();

            result.StatisticsIterator = StatisticsIterator;
            result.OutputList         = ArrayHelpers.Clone(OutputList);
            result.Input       = (AlgebraNode)Input.Clone(alreadyClonedElements);
            result.Distinct    = _distinct;
            result.SortEntries = ArrayHelpers.Clone(_sortEntries);
            result.SortOrders  = ArrayHelpers.Clone(_sortOrders);
            return(result);
        }
Esempio n. 5
0
        public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
        {
            _xmlWriter.WriteStartElement("sortAlgebraNode");
            _xmlWriter.WriteAttributeString("distinct", XmlConvert.ToString(node.Distinct));

            //WriteAstNode("input", node.Input);

            //for (int i = 0; i < node.SelectColumns.Length; i++)
            //{
            //    _xmlWriter.WriteStartElement("column");
            //    _xmlWriter.WriteAttributeString("item", node.SelectColumns[i].Name);
            //    _xmlWriter.WriteAttributeString("order", node.SortOrders[i].ToString());
            //    _xmlWriter.WriteEndElement();
            //}

            _xmlWriter.WriteEndElement();

            return(node);
        }
Esempio n. 6
0
        public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
        {
            ShowPlanElement inputElement = ConvertNode(node.Input);

            PropertyListBuilder propertyListBuilder = new PropertyListBuilder();

            AddRowBufferEntries(propertyListBuilder, Resources.ShowPlanGroupOutputList, node.OutputList);
            AddStatistics(propertyListBuilder, node.StatisticsIterator);

            if (node.Distinct)
            {
                propertyListBuilder.Write(Resources.ShowPlanKeyLogicalOperator, Resources.ShowPlanLogicalOperatorDistinctSort);
            }
            else
            {
                propertyListBuilder.Write(Resources.ShowPlanKeyLogicalOperator, Resources.ShowPlanLogicalOperatorSort);
            }

            propertyListBuilder.Begin(Resources.ShowPlanGroupOrderBy);
            for (int i = 0; i < node.SortEntries.Length; i++)
            {
                string sortOrder = SortOrderToString(node.SortOrders[i]);

                propertyListBuilder.Begin();
                propertyListBuilder.SetGroupValue(String.Format(CultureInfo.InvariantCulture, "{0} {1}", node.SortEntries[i].Name, sortOrder));
                WriteRowBufferEntry(propertyListBuilder, node.SortEntries[i]);
                propertyListBuilder.Write(Resources.ShowPlanKeyOrder, sortOrder);
                propertyListBuilder.End();
            }
            propertyListBuilder.End();

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

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

            _currentElement = element;

            return(node);
        }
Esempio n. 7
0
		public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
		{
			return PullFilterUp(node);
		}
Esempio n. 8
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);
        }
Esempio n. 9
0
		public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
		{
			_xmlWriter.WriteStartElement("sortAlgebraNode");
			_xmlWriter.WriteAttributeString("distinct", XmlConvert.ToString(node.Distinct));

			//WriteAstNode("input", node.Input);

			//for (int i = 0; i < node.SelectColumns.Length; i++)
			//{
			//    _xmlWriter.WriteStartElement("column");   
			//    _xmlWriter.WriteAttributeString("item", node.SelectColumns[i].Name);
			//    _xmlWriter.WriteAttributeString("order", node.SortOrders[i].ToString());
			//    _xmlWriter.WriteEndElement();
			//}

			_xmlWriter.WriteEndElement();

			return node;
		}
Esempio n. 10
0
 public virtual AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
 {
     node.Input = VisitAlgebraNode(node.Input);
     return(node);
 }
Esempio n. 11
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);
        }
Esempio n. 12
0
 public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
 {
     return(ReturnInputIfItProducesJustOneRow(node));
 }
Esempio n. 13
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;
		}
Esempio n. 14
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;
		}
Esempio n. 15
0
		public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
		{
			SortIterator sortIterator;
			if (node.Distinct)
				sortIterator = new DistinctSortIterator();
			else 
				sortIterator = new SortIterator();

			sortIterator.RowBuffer = new object[node.OutputList.Length];
			sortIterator.SortOrders = node.SortOrders;
			sortIterator.SortEntries = GetIteratorInput(node.Input.OutputList, node.SortEntries);
			sortIterator.Comparers = GetComparersFromExpressionTypes(node.SortEntries);
			sortIterator.Input = ConvertAlgebraNode(node.Input);
			sortIterator.InputOutput = GetIteratorOutput(0, node.Input.OutputList, node.OutputList);
			SetLastIterator(node, sortIterator);

			return node;
		}
Esempio n. 16
0
 public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
 {
     node.Input = VisitAlgebraNode(node.Input);
     return(NullScanIfInputIsNullScan(node));
 }
Esempio n. 17
0
 public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
 {
     node.Input = VisitAlgebraNode(node.Input);
     node.OutputList = node.Input.OutputList;
     return node;
 }
Esempio n. 18
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;
		}
Esempio n. 19
0
 public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
 {
     base.VisitSortAlgebraNode(node);
     ReplaceRowBufferEntries(node.SortEntries);
     return node;
 }
Esempio n. 20
0
 public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
 {
     base.VisitSortAlgebraNode(node);
     ReplaceRowBufferEntries(node.SortEntries);
     return(node);
 }
Esempio n. 21
0
		public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
		{
			node.Input = VisitAlgebraNode(node.Input);
			return NullScanIfInputIsNullScan(node);
		}
Esempio n. 22
0
 public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
 {
     node.Input      = VisitAlgebraNode(node.Input);
     node.OutputList = node.Input.OutputList;
     return(node);
 }
Esempio n. 23
0
 public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
 {
     return(PullFilterUp(node));
 }
Esempio n. 24
0
		public virtual AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
		{
			node.Input = VisitAlgebraNode(node.Input);
			return node;
		}
Esempio n. 25
0
		public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
		{
			return ReturnInputIfItProducesJustOneRow(node);
		}
Esempio n. 26
0
        public override AlgebraNode VisitSortAlgebraNode(SortAlgebraNode node)
        {
            ShowPlanElement inputElement = ConvertNode(node.Input);

            PropertyListBuilder propertyListBuilder = new PropertyListBuilder();
            AddRowBufferEntries(propertyListBuilder, Resources.ShowPlanGroupOutputList, node.OutputList);
            AddStatistics(propertyListBuilder, node.StatisticsIterator);

            if (node.Distinct)
                propertyListBuilder.Write(Resources.ShowPlanKeyLogicalOperator, Resources.ShowPlanLogicalOperatorDistinctSort);
            else
                propertyListBuilder.Write(Resources.ShowPlanKeyLogicalOperator, Resources.ShowPlanLogicalOperatorSort);

            propertyListBuilder.Begin(Resources.ShowPlanGroupOrderBy);
            for (int i = 0; i < node.SortEntries.Length; i++)
            {
                string sortOrder = SortOrderToString(node.SortOrders[i]);

                propertyListBuilder.Begin();
                propertyListBuilder.SetGroupValue(String.Format(CultureInfo.InvariantCulture, "{0} {1}", node.SortEntries[i].Name, sortOrder));
                WriteRowBufferEntry(propertyListBuilder, node.SortEntries[i]);
                propertyListBuilder.Write(Resources.ShowPlanKeyOrder, sortOrder);
                propertyListBuilder.End();
            }
            propertyListBuilder.End();

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

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

            return node;
        }