* transformAggregateCall - * Finish initial transformation of an aggregate call * * parse_func.c has recognized the function as an aggregate, and has set up * all the fields of the Aggref except aggargtypes, aggdirectargs, args, * aggorder, aggdistinct and agglevelsup. The passed-in args list has been * through standard expression transformation and type coercion to match the * agg's decl
| 104 | * pstate level. |
| 105 | */ |
| 106 | void |
| 107 | transformAggregateCall(ParseState *pstate, Aggref *agg, |
| 108 | List *args, List *aggorder, bool agg_distinct) |
| 109 | { |
| 110 | List *argtypes = NIL; |
| 111 | List *tlist = NIL; |
| 112 | List *torder = NIL; |
| 113 | List *tdistinct = NIL; |
| 114 | AttrNumber attno = 1; |
| 115 | int save_next_resno; |
| 116 | ListCell *lc; |
| 117 | |
| 118 | if (AGGKIND_IS_ORDERED_SET(agg->aggkind)) |
| 119 | { |
| 120 | /* |
| 121 | * For an ordered-set agg, the args list includes direct args and |
| 122 | * aggregated args; we must split them apart. |
| 123 | */ |
| 124 | int numDirectArgs = list_length(args) - list_length(aggorder); |
| 125 | List *aargs; |
| 126 | ListCell *lc2; |
| 127 | |
| 128 | Assert(numDirectArgs >= 0); |
| 129 | |
| 130 | aargs = list_copy_tail(args, numDirectArgs); |
| 131 | agg->aggdirectargs = list_truncate(args, numDirectArgs); |
| 132 | |
| 133 | /* |
| 134 | * Build a tlist from the aggregated args, and make a sortlist entry |
| 135 | * for each one. Note that the expressions in the SortBy nodes are |
| 136 | * ignored (they are the raw versions of the transformed args); we are |
| 137 | * just looking at the sort information in the SortBy nodes. |
| 138 | */ |
| 139 | forboth(lc, aargs, lc2, aggorder) |
| 140 | { |
| 141 | Expr *arg = (Expr *) lfirst(lc); |
| 142 | SortBy *sortby = (SortBy *) lfirst(lc2); |
| 143 | TargetEntry *tle; |
| 144 | |
| 145 | /* We don't bother to assign column names to the entries */ |
| 146 | tle = makeTargetEntry(arg, attno++, NULL, false); |
| 147 | tlist = lappend(tlist, tle); |
| 148 | |
| 149 | torder = addTargetToSortList(pstate, tle, |
| 150 | torder, tlist, sortby); |
| 151 | } |
| 152 | |
| 153 | /* Never any DISTINCT in an ordered-set agg */ |
| 154 | Assert(!agg_distinct); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | /* Regular aggregate, so it has no direct args */ |
| 159 | agg->aggdirectargs = NIL; |
| 160 | |
| 161 | /* |
| 162 | * Transform the plain list of Exprs into a targetlist. |
| 163 | */ |
no test coverage detected