public override QueryNode VisitSelectQuery(SelectQuery query)
		{
			base.VisitSelectQuery(query);

			RowBufferCalculator rowBufferCalculator = new RowBufferCalculator(query);
			rowBufferCalculator.Calculate();
			query.RowBufferCalculator = rowBufferCalculator;

			return query;
		}
Beispiel #2
0
        public override QueryNode VisitSelectQuery(SelectQuery query)
        {
            base.VisitSelectQuery(query);

            RowBufferCalculator rowBufferCalculator = new RowBufferCalculator(query);

            rowBufferCalculator.Calculate();
            query.RowBufferCalculator = rowBufferCalculator;

            return(query);
        }
Beispiel #3
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);
        }