Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6d85bb1

Browse files
committed
Correctly set up aggregate FILTER expression in partial-aggregation plans.
The aggfilter expression should be removed from the parent (combining) Aggref, since it's not supposed to apply the filter, and indeed cannot because any Vars used in the filter would not be available after the lower-level aggregation step. Per report from Jeff Janes. (This has been broken since the introduction of partial aggregation, I think. The error became obvious after commit 59a3795, when setrefs.c began processing the parent Aggref's fields normally and thus would detect such Vars. The special-case coding previously used in setrefs.c skipped over the parent's aggfilter field without processing it. That was broken in its own way because no other setrefs.c processing got applied either; though since the executor would not execute the filter expression, only initialize it, that oversight might not have had any visible symptoms at present.) Report: <CAMkU=1xfuPf2edAe4ZGXTmJpU7jxuKukKyvNtEXwu35B7dvejg@mail.gmail.com>
1 parent 9d7abca commit 6d85bb1

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/backend/optimizer/plan/setrefs.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,10 @@ convert_combining_aggrefs(Node *node, void *context)
17521752
Aggref *child_agg;
17531753
Aggref *parent_agg;
17541754

1755+
/* Assert we've not chosen to partial-ize any unsupported cases */
1756+
Assert(orig_agg->aggorder == NIL);
1757+
Assert(orig_agg->aggdistinct == NIL);
1758+
17551759
/*
17561760
* Since aggregate calls can't be nested, we needn't recurse into the
17571761
* arguments. But for safety, flat-copy the Aggref node itself rather
@@ -1762,13 +1766,17 @@ convert_combining_aggrefs(Node *node, void *context)
17621766

17631767
/*
17641768
* For the parent Aggref, we want to copy all the fields of the
1765-
* original aggregate *except* the args list. Rather than explicitly
1766-
* knowing what they all are here, we can momentarily modify child_agg
1767-
* to provide a source for copyObject.
1769+
* original aggregate *except* the args list, which we'll replace
1770+
* below, and the aggfilter expression, which should be applied only
1771+
* by the child not the parent. Rather than explicitly knowing about
1772+
* all the other fields here, we can momentarily modify child_agg to
1773+
* provide a suitable source for copyObject.
17681774
*/
17691775
child_agg->args = NIL;
1776+
child_agg->aggfilter = NULL;
17701777
parent_agg = (Aggref *) copyObject(child_agg);
17711778
child_agg->args = orig_agg->args;
1779+
child_agg->aggfilter = orig_agg->aggfilter;
17721780

17731781
/*
17741782
* Now, set up child_agg to represent the first phase of partial

0 commit comments

Comments
 (0)