qfrjava
2025-04-14 b94d4dd2e6bb5be898cda29c7e8a5356de1e60d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/*
 * For PostgreSQL Database Management System:
 * (formerly known as Postgres, then as Postgres95)
 *
 * Portions Copyright (c) 1996-2010, The PostgreSQL Global Development Group
 *
 * Portions Copyright (c) 1994, The Regents of the University of California
 *
 * Permission to use, copy, modify, and distribute this software and its documentation for any purpose,
 * without fee, and without a written agreement is hereby granted, provided that the above copyright notice
 * and this paragraph and the following two paragraphs appear in all copies.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT,
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY
 * OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA
 * HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */
 
#include "postgres.h"
 
#include "catalog/pg_constraint.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/tlist.h"
#include "optimizer/optimizer.h"
#include "parser/cypher_parse_agg.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
 
typedef struct
{
    ParseState *pstate;
    int min_varlevel;
    int min_agglevel;
    int sublevels_up;
} check_agg_arguments_context;
 
typedef struct
{
    ParseState *pstate;
    Query *qry;
    PlannerInfo *root;
    List *groupClauses;
    List *groupClauseCommonVars;
    bool have_non_var_grouping;
    List **func_grouped_rels;
    int sublevels_up;
    bool in_agg_direct_args;
} check_ungrouped_columns_context;
 
static void check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry,
                                    List *groupClauses, List *groupClauseVars,
                                    bool have_non_var_grouping,
                                    List **func_grouped_rels);
static bool check_ungrouped_columns_walker(Node *node,
                                           check_ungrouped_columns_context *context);
static void finalize_grouping_exprs(Node *node, ParseState *pstate, Query *qry,
                                    List *groupClauses, PlannerInfo *root,
                                    bool have_non_var_grouping);
static bool finalize_grouping_exprs_walker(Node *node,
                                           check_ungrouped_columns_context *context);
static List *expand_groupingset_node(GroupingSet *gs);
static List * expand_grouping_sets(List *groupingSets, int limit);
 
/*
 * From PG's parseCheckAggregates
 *
 * Check for aggregates where they shouldn't be and improper grouping.
 * This function should be called after the target list and qualifications
 * are finalized.
 *
 * Misplaced aggregates are now mostly detected in transformAggregateCall,
 * but it seems more robust to check for aggregates in recursive queries
 * only after everything is finalized.  In any case it's hard to detect
 * improper grouping on-the-fly, so we have to make another pass over the
 * query for that.
 */
void parse_check_aggregates(ParseState *pstate, Query *qry)
{
    List *gset_common = NIL;
    List *groupClauses = NIL;
    List *groupClauseCommonVars = NIL;
    bool have_non_var_grouping;
    List *func_grouped_rels = NIL;
    ListCell *l;
    bool hasJoinRTEs;
    bool hasSelfRefRTEs;
    PlannerInfo *root = NULL;
    Node *clause;
 
    /* This should only be called if we found aggregates or grouping */
    Assert(pstate->p_hasAggs || qry->groupClause || qry->havingQual || qry->groupingSets);
 
    /*
     * If we have grouping sets, expand them and find the intersection of all
     * sets.
     */
    if (qry->groupingSets)
    {
        /*
         * The limit of 4096 is arbitrary and exists simply to avoid resource
         * issues from pathological constructs.
         */
        List *gsets = expand_grouping_sets(qry->groupingSets, 4096);
 
        if (!gsets)
            ereport(ERROR,
                    (errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
                     errmsg("too many grouping sets present (maximum 4096)"),
                     parser_errposition(pstate,
                                        qry->groupClause ?
                                        exprLocation((Node *) qry->groupClause) :
                                        exprLocation((Node *) qry->groupingSets))));
 
        /*
         * The intersection will often be empty, so help things along by
         * seeding the intersect with the smallest set.
         */
        gset_common = linitial(gsets);
 
        if (gset_common)
        {
            for_each_cell(l, gsets, lnext(gsets, list_head(gsets)))
            {
                gset_common = list_intersection_int(gset_common, lfirst(l));
                if (!gset_common)
                    break;
            }
        }
 
        /*
         * If there was only one grouping set in the expansion, AND if the
         * groupClause is non-empty (meaning that the grouping set is not
         * empty either), then we can ditch the grouping set and pretend we
         * just had a normal GROUP BY.
         */
        if (list_length(gsets) == 1 && qry->groupClause)
            qry->groupingSets = NIL;
    }
 
    /*
     * Scan the range table to see if there are JOIN or self-reference CTE
     * entries.  We'll need this info below.
     */
    hasJoinRTEs = hasSelfRefRTEs = false;
    foreach(l, pstate->p_rtable)
    {
        RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
 
        if (rte->rtekind == RTE_JOIN)
            hasJoinRTEs = true;
        else if (rte->rtekind == RTE_CTE && rte->self_reference)
            hasSelfRefRTEs = true;
    }
 
    /*
     * Build a list of the acceptable GROUP BY expressions for use by
     * check_ungrouped_columns().
     *
     * We get the TLE, not just the expr, because GROUPING wants to know the
     * sortgroupref.
     */
    foreach(l, qry->groupClause)
    {
        SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
        TargetEntry *expr;
 
        expr = get_sortgroupclause_tle(grpcl, qry->targetList);
        if (expr == NULL)
            continue; /* probably cannot happen */
 
        groupClauses = lcons(expr, groupClauses);
    }
 
    /*
     * If there are join alias vars involved, we have to flatten them to the
     * underlying vars, so that aliased and unaliased vars will be correctly
     * taken as equal.  We can skip the expense of doing this if no rangetable
     * entries are RTE_JOIN kind. We use the planner's flatten_join_alias_vars
     * routine to do the flattening; it wants a PlannerInfo root node, which
     * fortunately can be mostly dummy.
     */
    if (hasJoinRTEs)
    {
        root = makeNode(PlannerInfo);
        root->parse = qry;
        root->planner_cxt = CurrentMemoryContext;
        root->hasJoinRTEs = true;
 
        groupClauses = (List *) flatten_join_alias_vars((Query*)root,
                                                        (Node *) groupClauses);
    }
 
    /*
     * Detect whether any of the grouping expressions aren't simple Vars; if
     * they're all Vars then we don't have to work so hard in the recursive
     * scans.  (Note we have to flatten aliases before this.)
     *
     * Track Vars that are included in all grouping sets separately in
     * groupClauseCommonVars, since these are the only ones we can use to
     * check for functional dependencies.
     */
    have_non_var_grouping = false;
    foreach(l, groupClauses)
    {
        TargetEntry *tle = lfirst(l);
 
        if (!IsA(tle->expr, Var))
        {
            have_non_var_grouping = true;
        }
        else if (!qry->groupingSets ||
                 list_member_int(gset_common, tle->ressortgroupref))
        {
            groupClauseCommonVars = lappend(groupClauseCommonVars, tle->expr);
        }
    }
 
    /*
     * Check the targetlist and HAVING clause for ungrouped variables.
     *
     * Note: because we check resjunk tlist elements as well as regular ones,
     * this will also find ungrouped variables that came from ORDER BY and
     * WINDOW clauses.  For that matter, it's also going to examine the
     * grouping expressions themselves --- but they'll all pass the test ...
     *
     * We also finalize GROUPING expressions, but for that we need to traverse
     * the original (unflattened) clause in order to modify nodes.
     */
    clause = (Node *) qry->targetList;
    finalize_grouping_exprs(clause, pstate, qry, groupClauses, root,
                            have_non_var_grouping);
    if (hasJoinRTEs)
        clause = flatten_join_alias_vars((Query*)root, clause);
    check_ungrouped_columns(clause, pstate, qry, groupClauses,
                            groupClauseCommonVars, have_non_var_grouping,
                            &func_grouped_rels);
 
    clause = (Node *) qry->havingQual;
    finalize_grouping_exprs(clause, pstate, qry, groupClauses, root,
                            have_non_var_grouping);
    if (hasJoinRTEs)
        clause = flatten_join_alias_vars((Query*)root, clause);
    check_ungrouped_columns(clause, pstate, qry, groupClauses,
                            groupClauseCommonVars, have_non_var_grouping,
                            &func_grouped_rels);
 
    /*
     * Per spec, aggregates can't appear in a recursive term.
     */
    if (pstate->p_hasAggs && hasSelfRefRTEs)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_RECURSION),
                 errmsg("aggregate functions are not allowed in a recursive query's recursive term"),
                 parser_errposition(pstate, locate_agg_of_level((Node *) qry, 0))));
}
 
/*
 * check_ungrouped_columns -
 *
 * Scan the given expression tree for ungrouped variables (variables
 * that are not listed in the groupClauses list and are not within
 * the arguments of aggregate functions).  Emit a suitable error message
 * if any are found.
 *
 * NOTE: we assume that the given clause has been transformed suitably for
 * parser output.  This means we can use expression_tree_walker.
 *
 * NOTE: we recognize grouping expressions in the main query, but only
 * grouping Vars in subqueries.  For example, this will be rejected,
 * although it could be allowed:
 *    SELECT (SELECT x FROM bar where y = (foo.a + foo.b))
 *    FROM foo
 *    GROUP BY a + b;
 * The difficulty is the need to account for different sublevels_up.
 * This appears to require a whole custom version of equal(), which is
 * way more pain than the feature seems worth.
 */
static void check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry,
                                    List *groupClauses,
                                    List *groupClauseCommonVars,
                                    bool have_non_var_grouping,
                                    List **func_grouped_rels)
{
    check_ungrouped_columns_context context;
 
    context.pstate = pstate;
    context.qry = qry;
    context.root = NULL;
    context.groupClauses = groupClauses;
    context.groupClauseCommonVars = groupClauseCommonVars;
    context.have_non_var_grouping = have_non_var_grouping;
    context.func_grouped_rels = func_grouped_rels;
    context.sublevels_up = 0;
    context.in_agg_direct_args = false;
    check_ungrouped_columns_walker(node, &context);
}
 
static bool check_ungrouped_columns_walker(Node *node, check_ungrouped_columns_context *context)
{
    ListCell *gl;
 
    if (node == NULL)
        return false;
 
    if (IsA(node, Const) || IsA(node, Param))
        return false; /* constants are always acceptable */
 
    if (IsA(node, Aggref))
    {
        Aggref *agg = (Aggref *) node;
 
        if ((int) agg->agglevelsup == context->sublevels_up)
        {
            /*
             * If we find an aggregate call of the original level, do not
             * recurse into its normal arguments, ORDER BY arguments, or
             * filter; ungrouped vars there are not an error.  But we should
             * check direct arguments as though they weren't in an aggregate.
             * We set a special flag in the context to help produce a useful
             * error message for ungrouped vars in direct arguments.
             */
            bool result;
 
            Assert(!context->in_agg_direct_args);
            context->in_agg_direct_args = true;
            result = check_ungrouped_columns_walker((Node *) agg->aggdirectargs,
                                                    context);
            context->in_agg_direct_args = false;
            return result;
        }
 
        /*
         * We can skip recursing into aggregates of higher levels altogether,
         * since they could not possibly contain Vars of concern to us (see
         * transformAggregateCall).  We do need to look at aggregates of lower
         * levels, however.
         */
        if ((int) agg->agglevelsup > context->sublevels_up)
            return false;
    }
 
    if (IsA(node, GroupingFunc))
    {
        GroupingFunc *grp = (GroupingFunc *) node;
 
        /* handled GroupingFunc separately, no need to recheck at this level */
 
        if ((int) grp->agglevelsup >= context->sublevels_up)
            return false;
    }
 
    /*
     * If we have any GROUP BY items that are not simple Vars, check to see if
     * subexpression as a whole matches any GROUP BY item. We need to do this
     * at every recursion level so that we recognize GROUPed-BY expressions
     * before reaching variables within them. But this only works at the outer
     * query level, as noted above.
     */
    if (context->have_non_var_grouping && context->sublevels_up == 0)
    {
        foreach(gl, context->groupClauses)
        {
            TargetEntry *tle = lfirst(gl);
 
            if (equal(node, tle->expr))
                return false; /* acceptable, do not descend more */
        }
    }
 
    /*
     * If we have an ungrouped Var of the original query level, we have a
     * failure.  Vars below the original query level are not a problem, and
     * neither are Vars from above it.  (If such Vars are ungrouped as far as
     * their own query level is concerned, that's someone else's problem...)
     */
    if (IsA(node, Var))
    {
        Var *var = (Var *) node;
        RangeTblEntry *rte;
        char *attname;
 
        if (var->varlevelsup != context->sublevels_up)
            return false; /* it's not local to my query, ignore */
 
        /*
         * Check for a match, if we didn't do it above.
         */
        if (!context->have_non_var_grouping || context->sublevels_up != 0)
        {
            foreach(gl, context->groupClauses)
            {
                Var *gvar = (Var *) ((TargetEntry *) lfirst(gl))->expr;
 
                if (IsA(gvar, Var) &&
                    gvar->varno == var->varno &&
                    gvar->varattno == var->varattno &&
                    gvar->varlevelsup == 0)
                    return false; /* acceptable, we're okay */
            }
        }
 
        /*
         * Check whether the Var is known functionally dependent on the GROUP
         * BY columns.  If so, we can allow the Var to be used, because the
         * grouping is really a no-op for this table.  However, this deduction
         * depends on one or more constraints of the table, so we have to add
         * those constraints to the query's constraintDeps list, because it's
         * not semantically valid anymore if the constraint(s) get dropped.
         * (Therefore, this check must be the last-ditch effort before raising
         * error: we don't want to add dependencies unnecessarily.)
         *
         * Because this is a pretty expensive check, and will have the same
         * outcome for all columns of a table, we remember which RTEs we've
         * already proven functional dependency for in the func_grouped_rels
         * list.  This test also prevents us from adding duplicate entries to
         * the constraintDeps list.
         */
        if (list_member_int(*context->func_grouped_rels, var->varno))
            return false; /* previously proven acceptable */
 
        Assert(var->varno > 0 &&
               (int) var->varno <= list_length(context->pstate->p_rtable));
        rte = rt_fetch(var->varno, context->pstate->p_rtable);
        if (rte->rtekind == RTE_RELATION)
        {
            if (check_functional_grouping(rte->relid, var->varno, 0,
                                          context->groupClauseCommonVars,
                                          &context->qry->constraintDeps))
            {
                *context->func_grouped_rels = lappend_int(*context->func_grouped_rels,
                                                          var->varno);
                return false; /* acceptable */
            }
        }
 
        /* Found an ungrouped local variable; generate error message */
        attname = get_rte_attribute_name(rte, var->varattno);
        if (context->sublevels_up == 0)
            ereport(ERROR, (errcode(ERRCODE_GROUPING_ERROR),
                            errmsg("\"%s\" must be either part of an explicitly listed key or used inside an aggregate function",
                                   attname), context->in_agg_direct_args ?
                                       errdetail("Direct arguments of an ordered-set aggregate must use only grouped columns.") :
                                       0, parser_errposition(context->pstate, var->location)));
        else
            ereport(ERROR, (errcode(ERRCODE_GROUPING_ERROR),
                            errmsg("subquery uses ungrouped column \"%s.%s\" from outer query",
                                   rte->eref->aliasname, attname),
                                   parser_errposition(context->pstate, var->location)));
    }
 
    if (IsA(node, Query))
    {
        /* Recurse into subselects */
        bool result;
 
        context->sublevels_up++;
        result = query_tree_walker((Query *) node,
                                   check_ungrouped_columns_walker,
                                   (void *) context, 0);
        context->sublevels_up--;
        return result;
    }
 
    return expression_tree_walker(node, check_ungrouped_columns_walker,
                                  (void *) context);
}
 
/*
 * finalize_grouping_exprs -
 *      Scan the given expression tree for GROUPING() and related calls,
 *      and validate and process their arguments.
 *
 * This is split out from check_ungrouped_columns above because it needs
 * to modify the nodes (which it does in-place, not via a mutator) while
 * check_ungrouped_columns may see only a copy of the original thanks to
 * flattening of join alias vars. So here, we flatten each individual
 * GROUPING argument as we see it before comparing it.
 */
static void finalize_grouping_exprs(Node *node, ParseState *pstate, Query *qry,
                                    List *groupClauses, PlannerInfo *root,
                                    bool have_non_var_grouping)
{
    check_ungrouped_columns_context context;
 
    context.pstate = pstate;
    context.qry = qry;
    context.root = root;
    context.groupClauses = groupClauses;
    context.groupClauseCommonVars = NIL;
    context.have_non_var_grouping = have_non_var_grouping;
    context.func_grouped_rels = NULL;
    context.sublevels_up = 0;
    context.in_agg_direct_args = false;
    finalize_grouping_exprs_walker(node, &context);
}
 
static bool finalize_grouping_exprs_walker(Node *node,
                                           check_ungrouped_columns_context *context)
{
    ListCell *gl;
 
    if (node == NULL)
        return false;
    if (IsA(node, Const) || IsA(node, Param))
        return false; /* constants are always acceptable */
 
    if (IsA(node, Aggref))
    {
        Aggref *agg = (Aggref *) node;
 
        if ((int) agg->agglevelsup == context->sublevels_up)
        {
            /*
             * If we find an aggregate call of the original level, do not
             * recurse into its normal arguments, ORDER BY arguments, or
             * filter; GROUPING exprs of this level are not allowed there. But
             * check direct arguments as though they weren't in an aggregate.
             */
            bool result;
 
            Assert(!context->in_agg_direct_args);
            context->in_agg_direct_args = true;
            result = finalize_grouping_exprs_walker((Node *) agg->aggdirectargs,
                                                context);
            context->in_agg_direct_args = false;
            return result;
        }
 
        /*
         * We can skip recursing into aggregates of higher levels altogether,
         * since they could not possibly contain exprs of concern to us (see
         * transformAggregateCall).  We do need to look at aggregates of lower
         * levels, however.
         */
        if ((int) agg->agglevelsup > context->sublevels_up)
            return false;
    }
 
    if (IsA(node, GroupingFunc))
    {
        GroupingFunc *grp = (GroupingFunc *) node;
 
        /*
         * We only need to check GroupingFunc nodes at the exact level to
         * which they belong, since they cannot mix levels in arguments.
         */
 
        if ((int) grp->agglevelsup == context->sublevels_up)
        {
            ListCell *lc;
            List *ref_list = NIL;
 
            foreach(lc, grp->args)
            {
                Node *expr = lfirst(lc);
                Index ref = 0;
 
                if (context->root)
                    expr = flatten_join_alias_vars((Query*)context->root, expr);
 
                /*
                 * Each expression must match a grouping entry at the current
                 * query level. Unlike the general expression case, we don't
                 * allow functional dependencies or outer references.
                 */
 
                if (IsA(expr, Var))
                {
                    Var *var = (Var *) expr;
 
                    if (var->varlevelsup == context->sublevels_up)
                    {
                        foreach(gl, context->groupClauses)
                        {
                            TargetEntry *tle = lfirst(gl);
                            Var *gvar = (Var *) tle->expr;
 
                            if (IsA(gvar, Var) &&
                                gvar->varno == var->varno &&
                                gvar->varattno == var->varattno &&
                                gvar->varlevelsup == 0)
                            {
                                ref = tle->ressortgroupref;
                                break;
                            }
                        }
                    }
                }
                else if (context->have_non_var_grouping &&
                         context->sublevels_up == 0)
                {
                    foreach(gl, context->groupClauses)
                    {
                        TargetEntry *tle = lfirst(gl);
 
                        if (equal(expr, tle->expr))
                        {
                           ref = tle->ressortgroupref;
                           break;
                        }
                    }
                }
 
                if (ref == 0)
                    ereport(ERROR,
                            (errcode(ERRCODE_GROUPING_ERROR),
                             errmsg("arguments to GROUPING must be grouping expressions of the associated query level"),
                             parser_errposition(context->pstate,
                             exprLocation(expr))));
 
                ref_list = lappend_int(ref_list, ref);
            }
 
            grp->refs = ref_list;
        }
 
        if ((int) grp->agglevelsup > context->sublevels_up)
            return false;
    }
 
    if (IsA(node, Query))
    {
        /* Recurse into subselects */
        bool result;
 
        context->sublevels_up++;
        result = query_tree_walker((Query *) node,
                                   finalize_grouping_exprs_walker,
                                   (void *) context, 0);
        context->sublevels_up--;
        return result;
    }
    return expression_tree_walker(node, finalize_grouping_exprs_walker,
                                  (void *) context);
}
 
 
/*
 * Given a GroupingSet node, expand it and return a list of lists.
 *
 * For EMPTY nodes, return a list of one empty list.
 *
 * For SIMPLE nodes, return a list of one list, which is the node content.
 *
 * For CUBE and ROLLUP nodes, return a list of the expansions.
 *
 * For SET nodes, recursively expand contained CUBE and ROLLUP.
 */
static List * expand_groupingset_node(GroupingSet *gs)
{
    List *result = NIL;
 
    switch (gs->kind)
    {
        case GROUPING_SET_EMPTY:
            result = list_make1(NIL);
            break;
 
        case GROUPING_SET_SIMPLE:
            result = list_make1(gs->content);
            break;
 
        case GROUPING_SET_ROLLUP:
        {
            List *rollup_val = gs->content;
            ListCell *lc;
            int curgroup_size = list_length(gs->content);
 
            while (curgroup_size > 0)
            {
                List *current_result = NIL;
                int i = curgroup_size;
 
                foreach(lc, rollup_val)
                {
                    GroupingSet *gs_current = (GroupingSet *) lfirst(lc);
 
                    Assert(gs_current->kind == GROUPING_SET_SIMPLE);
 
                    current_result = list_concat(current_result,
                                                 list_copy(gs_current->content));
 
                    /* If we are done with making the current group, break */
                    if (--i == 0)
                        break;
                }
 
                result = lappend(result, current_result);
                --curgroup_size;
            }
 
            result = lappend(result, NIL);
        }
            break;
 
        case GROUPING_SET_CUBE:
        {
            List *cube_list = gs->content;
            int number_bits = list_length(cube_list);
            uint32 num_sets;
            uint32 i;
 
            /* parser should cap this much lower */
            Assert(number_bits < 31);
 
            num_sets = (1U << number_bits);
 
            for (i = 0; i < num_sets; i++)
            {
                List *current_result = NIL;
                ListCell *lc;
                uint32 mask = 1U;
 
                foreach(lc, cube_list)
                {
                    GroupingSet *gs_current = (GroupingSet *) lfirst(lc);
 
                    Assert(gs_current->kind == GROUPING_SET_SIMPLE);
 
                    if (mask & i)
                    {
                        current_result = list_concat(current_result,
                                                     list_copy(gs_current->content));
                    }
 
                    mask <<= 1;
                }
 
                result = lappend(result, current_result);
            }
        }
            break;
 
        case GROUPING_SET_SETS:
        {
            ListCell *lc;
 
            foreach(lc, gs->content)
            {
                List *current_result = expand_groupingset_node(lfirst(lc));
 
                result = list_concat(result, current_result);
            }
        }
            break;
    }
 
    return result;
}
 
static int cmp_list_len_asc(const void *a, const void *b)
{
    int la = list_length(*(List *const *) a);
    int lb = list_length(*(List *const *) b);
 
    return (la > lb) ? 1 : (la == lb) ? 0 : -1;
}
 
/*
 * Expand a groupingSets clause to a flat list of grouping sets.
 * The returned list is sorted by length, shortest sets first.
 *
 * This is mainly for the planner, but we use it here too to do
 * some consistency checks.
 */
static List * expand_grouping_sets(List *groupingSets, int limit)
{
    List *expanded_groups = NIL;
    List *result = NIL;
    double numsets = 1;
    ListCell *lc;
 
    if (groupingSets == NIL)
        return NIL;
 
    foreach(lc, groupingSets)
    {
        List *current_result = NIL;
        GroupingSet *gs = lfirst(lc);
 
        current_result = expand_groupingset_node(gs);
 
        Assert(current_result != NIL);
 
        numsets *= list_length(current_result);
 
        if (limit >= 0 && numsets > limit)
            return NIL;
 
        expanded_groups = lappend(expanded_groups, current_result);
    }
 
    /*
     * Do cartesian product between sublists of expanded_groups. While at it,
     * remove any duplicate elements from individual grouping sets (we must
     * NOT change the number of sets though)
     */
 
    foreach(lc, (List *) linitial(expanded_groups))
    {
        result = lappend(result, list_union_int(NIL, (List *) lfirst(lc)));
    }
 
    for_each_cell(lc, expanded_groups, 
                  lnext(expanded_groups, list_head(expanded_groups)))
    {
        List *p = lfirst(lc);
        List *new_result = NIL;
        ListCell *lc2;
 
        foreach(lc2, result)
        {
            List *q = lfirst(lc2);
            ListCell *lc3;
 
            foreach(lc3, p)
            {
                new_result = lappend(new_result,
                                     list_union_int(q,(List *) lfirst(lc3)));
            }
        }
        result = new_result;
    }
 
    if (list_length(result) > 1)
    {
        int            result_len = list_length(result);
        List      **buf = palloc(sizeof(List *) * result_len);
        List      **ptr = buf;
 
        foreach(lc, result)
        {
            *ptr++ = lfirst(lc);
        }
 
        qsort(buf, result_len, sizeof(List *), cmp_list_len_asc);
 
        result = NIL;
        ptr = buf;
 
        while (result_len-- > 0)
            result = lappend(result, *ptr++);
 
        pfree(buf);
    }
 
    return result;
}