The node for merging the child node with a set of new function columns over the entire result.
For example, we may want to add an expression a + 10 or coalesce(a, b, 1).
Наследование: SingleQueryPlanNode
Пример #1
0
        protected virtual IQueryPlanNode VisitCreateFunctions(CreateFunctionsNode node)
        {
            var child = node.Child;
            if (child != null)
                child = VisitNode(child);

            return new CreateFunctionsNode(child, node.Functions, node.Names);
        }
Пример #2
0
        protected virtual IQueryPlanNode VisitCreateFunctions(CreateFunctionsNode node)
        {
            var child = node.Child;

            if (child != null)
            {
                child = VisitNode(child);
            }

            return(new CreateFunctionsNode(child, node.Functions, node.Names));
        }
Пример #3
0
        private IQueryPlanNode PlanGroup(IQueryPlanNode node, GroupInfo groupInfo)
        {
            // If there is more than 1 aggregate function or there is a group by
            // clause, then we must add a grouping plan.
            if (groupInfo.Columns.AggregateCount > 0 ||
                groupInfo.GroupByCount > 0)
            {
                // If there is no GROUP BY clause then assume the entire result is the
                // group.
                if (groupInfo.GroupByCount == 0)
                {
                    node = new GroupNode(node, groupInfo.GroupMax, groupInfo.FunctionExpressions, groupInfo.FunctionNames);
                }
                else
                {
                    // Do we have any group by functions that need to be planned first?
                    int gfsz = groupInfo.GroupByExpressions.Length;
                    if (gfsz > 0)
                    {
                        var groupFunList = new SqlExpression[gfsz];
                        var groupFunName = new string[gfsz];
                        for (int i = 0; i < gfsz; ++i)
                        {
                            groupFunList[i] = groupInfo.GroupByExpressions[i];
                            groupFunName[i] = "#GROUPBY-" + i;
                        }

                        node = new CreateFunctionsNode(node, groupFunList, groupFunName);
                    }

                    // Otherwise we provide the 'group_by_list' argument
                    node = new GroupNode(node, groupInfo.GroupByNames, groupInfo.GroupMax, groupInfo.FunctionExpressions, groupInfo.FunctionNames);
                }
            }
            else
            {
                // Otherwise no grouping is occurring.  We simply need create a function
                // node with any functions defined in the SELECT.
                // Plan a FunctionsNode with the functions defined in the SELECT.
                if (groupInfo.FunctionCount > 0)
                {
                    node = new CreateFunctionsNode(node, groupInfo.FunctionExpressions, groupInfo.FunctionNames);
                }
            }

            return(node);
        }
Пример #4
0
        private static IQueryPlanNode PlanForOrderBy(IQueryPlanNode plan, IList<SortColumn> orderBy, QueryExpressionFrom queryFrom, IList<SelectColumn> selectedColumns)
        {
            // Sort on the ORDER BY clause
            if (orderBy.Count > 0) {
                int sz = orderBy.Count;
                var orderList = new ObjectName[sz];
                var ascendingList = new bool[sz];

                var functionOrders = new List<SqlExpression>();

                for (int i = 0; i < sz; ++i) {
                    var column = orderBy[i];
                    SqlExpression exp = column.Expression;
                    ascendingList[i] = column.Ascending;
                    var v = exp.AsReferenceName();

                    if (v != null) {
                        var newV = queryFrom.ResolveReference(v);
                        if (newV == null)
                            throw new InvalidOperationException(String.Format("Could not resolve ORDER BY column '{0}' in expression", v));

                        newV = ReplaceAliasedVariable(newV, selectedColumns);
                        orderList[i] = newV;
                    } else {
                        // Otherwise we must be ordering by an expression such as
                        // '0 - a'.

                        // Resolve the expression,
                        exp = exp.Prepare(queryFrom.ExpressionPreparer);

                        // Make sure we substitute any aliased columns in the order by
                        // columns.
                        exp = ReplaceAliasedVariables(exp, selectedColumns);

                        // The new ordering functions are called 'FUNCTIONTABLE.#ORDER-n'
                        // where n is the number of the ordering expression.
                        orderList[i] = new ObjectName(FunctionTableName, "#ORDER-" + functionOrders.Count);
                        functionOrders.Add(exp);
                    }
                }

                // If there are functional orderings,
                // For this we must define a new FunctionTable with the expressions,
                // then order by those columns, and then use another SubsetNode
                // command node.
                int fsz = functionOrders.Count;
                if (fsz > 0) {
                    var funs = new SqlExpression[fsz];
                    var fnames = new String[fsz];
                    for (int n = 0; n < fsz; ++n) {
                        funs[n] = functionOrders[n];
                        fnames[n] = "#ORDER-" + n;
                    }

                    if (plan is SubsetNode) {
                        // If the top plan is a SubsetNode then we use the
                        //   information from it to create a new SubsetNode that
                        //   doesn't include the functional orders we have attached here.
                        var topSubsetNode = (SubsetNode)plan;
                        var mappedNames = topSubsetNode.AliasColumnNames;

                        // Defines the sort functions
                        plan = new CreateFunctionsNode(plan, funs, fnames);
                        // Then plan the sort
                        plan = new SortNode(plan, orderList, ascendingList);
                        // Then plan the subset
                        plan = new SubsetNode(plan, mappedNames, mappedNames);
                    } else {
                        // Defines the sort functions
                        plan = new CreateFunctionsNode(plan, funs, fnames);
                        // Plan the sort
                        plan = new SortNode(plan, orderList, ascendingList);
                    }

                } else {
                    // No functional orders so we only need to sort by the columns
                    // defined.
                    plan = new SortNode(plan, orderList, ascendingList);
                }
            }

            return plan;
        }
Пример #5
0
        private IQueryPlanNode PlanGroup(IQueryPlanNode node, GroupInfo groupInfo)
        {
            // If there is more than 1 aggregate function or there is a group by
            // clause, then we must add a grouping plan.
            if (groupInfo.Columns.AggregateCount > 0 ||
                groupInfo.GroupByCount > 0) {
                // If there is no GROUP BY clause then assume the entire result is the
                // group.
                if (groupInfo.GroupByCount == 0) {
                    node = new GroupNode(node, groupInfo.GroupMax, groupInfo.FunctionExpressions, groupInfo.FunctionNames);
                } else {
                    // Do we have any group by functions that need to be planned first?
                    int gfsz = groupInfo.GroupByExpressions.Length;
                    if (gfsz > 0) {
                        var groupFunList = new SqlExpression[gfsz];
                        var groupFunName = new string[gfsz];
                        for (int i = 0; i < gfsz; ++i) {
                            groupFunList[i] = groupInfo.GroupByExpressions[i];
                            groupFunName[i] = "#GROUPBY-" + i;
                        }

                        node = new CreateFunctionsNode(node, groupFunList, groupFunName);
                    }

                    // Otherwise we provide the 'group_by_list' argument
                    node = new GroupNode(node, groupInfo.GroupByNames, groupInfo.GroupMax, groupInfo.FunctionExpressions, groupInfo.FunctionNames);
                }
            } else {
                // Otherwise no grouping is occurring.  We simply need create a function
                // node with any functions defined in the SELECT.
                // Plan a FunctionsNode with the functions defined in the SELECT.
                if (groupInfo.FunctionCount > 0)
                    node = new CreateFunctionsNode(node, groupInfo.FunctionExpressions, groupInfo.FunctionNames);
            }

            return node;
        }
Пример #6
0
        private static IQueryPlanNode PlanForOrderBy(IQueryPlanNode plan, IList <SortColumn> orderBy, QueryExpressionFrom queryFrom, IList <SelectColumn> selectedColumns)
        {
            // Sort on the ORDER BY clause
            if (orderBy.Count > 0)
            {
                int sz            = orderBy.Count;
                var orderList     = new ObjectName[sz];
                var ascendingList = new bool[sz];

                var functionOrders = new List <SqlExpression>();

                for (int i = 0; i < sz; ++i)
                {
                    var           column = orderBy[i];
                    SqlExpression exp    = column.Expression;
                    ascendingList[i] = column.Ascending;
                    var v = exp.AsReferenceName();

                    if (v != null)
                    {
                        var newV = queryFrom.ResolveReference(v);
                        if (newV == null)
                        {
                            throw new InvalidOperationException(String.Format("Could not resolve ORDER BY column '{0}' in expression", v));
                        }

                        newV         = ReplaceAliasedVariable(newV, selectedColumns);
                        orderList[i] = newV;
                    }
                    else
                    {
                        // Otherwise we must be ordering by an expression such as
                        // '0 - a'.

                        // Resolve the expression,
                        exp = exp.Prepare(queryFrom.ExpressionPreparer);

                        // Make sure we substitute any aliased columns in the order by
                        // columns.
                        exp = ReplaceAliasedVariables(exp, selectedColumns);

                        // The new ordering functions are called 'FUNCTIONTABLE.#ORDER-n'
                        // where n is the number of the ordering expression.
                        orderList[i] = new ObjectName(FunctionTableName, "#ORDER-" + functionOrders.Count);
                        functionOrders.Add(exp);
                    }
                }

                // If there are functional orderings,
                // For this we must define a new FunctionTable with the expressions,
                // then order by those columns, and then use another SubsetNode
                // command node.
                int fsz = functionOrders.Count;
                if (fsz > 0)
                {
                    var funs   = new SqlExpression[fsz];
                    var fnames = new String[fsz];
                    for (int n = 0; n < fsz; ++n)
                    {
                        funs[n]   = functionOrders[n];
                        fnames[n] = "#ORDER-" + n;
                    }

                    if (plan is SubsetNode)
                    {
                        // If the top plan is a SubsetNode then we use the
                        //   information from it to create a new SubsetNode that
                        //   doesn't include the functional orders we have attached here.
                        var topSubsetNode = (SubsetNode)plan;
                        var mappedNames   = topSubsetNode.AliasColumnNames;

                        // Defines the sort functions
                        plan = new CreateFunctionsNode(plan, funs, fnames);
                        // Then plan the sort
                        plan = new SortNode(plan, orderList, ascendingList);
                        // Then plan the subset
                        plan = new SubsetNode(plan, mappedNames, mappedNames);
                    }
                    else
                    {
                        // Defines the sort functions
                        plan = new CreateFunctionsNode(plan, funs, fnames);
                        // Plan the sort
                        plan = new SortNode(plan, orderList, ascendingList);
                    }
                }
                else
                {
                    // No functional orders so we only need to sort by the columns
                    // defined.
                    plan = new SortNode(plan, orderList, ascendingList);
                }
            }

            return(plan);
        }