Postpone aggregate checks until after collation is assigned.
authorAndrew Gierth
Thu, 17 Jan 2019 05:33:01 +0000 (05:33 +0000)
committerAndrew Gierth
Thu, 17 Jan 2019 06:25:55 +0000 (06:25 +0000)
Previously, parseCheckAggregates was run before
assign_query_collations, but this causes problems if any expression
has already had a collation assigned by some transform function (e.g.
transformCaseExpr) before parseCheckAggregates runs. The differing
collations would cause expressions not to be recognized as equal to
the ones in the GROUP BY clause, leading to spurious errors about
unaggregated column references.

The result was that CASE expr WHEN val ... would fail when "expr"
contained a GROUPING() expression or matched one of the group by
expressions, and where collatable types were involved; whereas the
supposedly identical CASE WHEN expr = val ... would succeed.

Backpatch all the way; this appears to have been wrong ever since
collations were introduced.

Per report from Guillaume Lelarge, analysis and patch by me.

Discussion: https://postgr.es/m/CAECtzeVSO_US8C2Khgfv54ZMUOBR4sWq+6_bLrETnWExHT=rFg@mail.gmail.com
Discussion: https://postgr.es/m/[email protected]

src/backend/parser/analyze.c
src/test/regress/expected/aggregates.out
src/test/regress/expected/groupingsets.out
src/test/regress/sql/aggregates.sql
src/test/regress/sql/groupingsets.sql

index f9bcb641efd39d444553f88d5985b63cb30b8c5d..7854078f99b0c67d1c3e8c8fed0c4ef0d9146892 100644 (file)
@@ -442,11 +442,13 @@ transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
    qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
    qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
    qry->hasAggs = pstate->p_hasAggs;
-   if (pstate->p_hasAggs)
-       parseCheckAggregates(pstate, qry);
 
    assign_query_collations(pstate, qry);
 
+   /* this must be done after collations, for reliable comparison of exprs */
+   if (pstate->p_hasAggs)
+       parseCheckAggregates(pstate, qry);
+
    return qry;
 }
 
@@ -1317,8 +1319,6 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
    qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
    qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
    qry->hasAggs = pstate->p_hasAggs;
-   if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
-       parseCheckAggregates(pstate, qry);
 
    foreach(l, stmt->lockingClause)
    {
@@ -1328,6 +1328,10 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
 
    assign_query_collations(pstate, qry);
 
+   /* this must be done after collations, for reliable comparison of exprs */
+   if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
+       parseCheckAggregates(pstate, qry);
+
    return qry;
 }
 
@@ -1789,8 +1793,6 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
    qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
    qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
    qry->hasAggs = pstate->p_hasAggs;
-   if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
-       parseCheckAggregates(pstate, qry);
 
    foreach(l, lockingClause)
    {
@@ -1800,6 +1802,10 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
 
    assign_query_collations(pstate, qry);
 
+   /* this must be done after collations, for reliable comparison of exprs */
+   if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
+       parseCheckAggregates(pstate, qry);
+
    return qry;
 }
 
index 56d5fbb4fbaf7c0122556e388ae3154f0765d548..305ffa8514763ee81b396f9649e2b3988339183a 100644 (file)
@@ -2081,3 +2081,22 @@ SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
 (1 row)
 
 ROLLBACK;
+-- check collation-sensitive matching between grouping expressions
+select v||'a', case v||'a' when 'aa' then 1 else 0 end, count(*)
+  from unnest(array['a','b']) u(v)
+ group by v||'a' order by 1;
+ ?column? | case | count 
+----------+------+-------
+ aa       |    1 |     1
+ ba       |    0 |     1
+(2 rows)
+
+select v||'a', case when v||'a' = 'aa' then 1 else 0 end, count(*)
+  from unnest(array['a','b']) u(v)
+ group by v||'a' order by 1;
+ ?column? | case | count 
+----------+------+-------
+ aa       |    1 |     1
+ ba       |    0 |     1
+(2 rows)
+
index c7deec2ff402667fc2a21e990e4cabddf78c41bb..381ebce8a1e876b1d3fdf5babb95e7f473afa808 100644 (file)
@@ -1540,4 +1540,29 @@ explain (costs off)
          ->  Seq Scan on tenk1
 (12 rows)
 
+-- check collation-sensitive matching between grouping expressions
+-- (similar to a check for aggregates, but there are additional code
+-- paths for GROUPING, so check again here)
+select v||'a', case grouping(v||'a') when 1 then 1 else 0 end, count(*)
+  from unnest(array[1,1], array['a','b']) u(i,v)
+ group by rollup(i, v||'a') order by 1,3;
+ ?column? | case | count 
+----------+------+-------
+ aa       |    0 |     1
+ ba       |    0 |     1
+          |    1 |     2
+          |    1 |     2
+(4 rows)
+
+select v||'a', case when grouping(v||'a') = 1 then 1 else 0 end, count(*)
+  from unnest(array[1,1], array['a','b']) u(i,v)
+ group by rollup(i, v||'a') order by 1,3;
+ ?column? | case | count 
+----------+------+-------
+ aa       |    0 |     1
+ ba       |    0 |     1
+          |    1 |     2
+          |    1 |     2
+(4 rows)
+
 -- end
index 651ca02536c765b070e6c4d87b415a9208dacc16..cf21fca14b8e53ca42f4f652cb9b0b00b9ee8c4c 100644 (file)
@@ -918,3 +918,11 @@ EXPLAIN (COSTS OFF)
 SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
 
 ROLLBACK;
+
+-- check collation-sensitive matching between grouping expressions
+select v||'a', case v||'a' when 'aa' then 1 else 0 end, count(*)
+  from unnest(array['a','b']) u(v)
+ group by v||'a' order by 1;
+select v||'a', case when v||'a' = 'aa' then 1 else 0 end, count(*)
+  from unnest(array['a','b']) u(v)
+ group by v||'a' order by 1;
index c32d23b8d72d9759469c25bf57509c65f5d1a29e..5d6485913b3220954f0846ab43f4059d021842fe 100644 (file)
@@ -415,4 +415,15 @@ explain (costs off)
          count(*)
     from tenk1 group by grouping sets (unique1,twothousand,thousand,hundred,ten,four,two);
 
+-- check collation-sensitive matching between grouping expressions
+-- (similar to a check for aggregates, but there are additional code
+-- paths for GROUPING, so check again here)
+
+select v||'a', case grouping(v||'a') when 1 then 1 else 0 end, count(*)
+  from unnest(array[1,1], array['a','b']) u(i,v)
+ group by rollup(i, v||'a') order by 1,3;
+select v||'a', case when grouping(v||'a') = 1 then 1 else 0 end, count(*)
+  from unnest(array[1,1], array['a','b']) u(i,v)
+ group by rollup(i, v||'a') order by 1,3;
+
 -- end