Fix eager aggregation grouping on cross-type join keys

When a Var of the relation to be partially aggregated is known equal
to a GROUP BY expression through an EC, get_expression_sortgroupref()
let it reuse that expression's SortGroupClause.  If the two are of
different types, as with an int2 grouping key joined to an int4
column, the Var was then grouped using the other type's equality
operator, which could put unequal values into the same group and give
wrong results.

To fix, accept only EC members of the same type as the grouping
expression.  A cross-type Var is then treated like any other join key
and gets a SortGroupClause built for its own type.

Reported-by: Robert Haas <[email protected]>
Author: Richard Guo <[email protected]>
Discussion: 
https://postgr.es/m/ca+tgmob7ism9ykrm44vjuduacchw-fy54mv5njptztl9uny...@mail.gmail.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/d764d61af3160f725ac087ccc05d9ccf03af8312

Modified Files
--------------
src/backend/optimizer/util/relnode.c          |  4 +++
src/test/regress/expected/eager_aggregate.out | 44 +++++++++++++++++++++++++++
src/test/regress/sql/eager_aggregate.sql      | 23 ++++++++++++++
3 files changed, 71 insertions(+)

Reply via email to