Test GROUP BY matching of join columns that are type-coerced by USING.
authorTom Lane
Thu, 2 Jan 2020 00:31:41 +0000 (19:31 -0500)
committerTom Lane
Thu, 2 Jan 2020 00:31:41 +0000 (19:31 -0500)
If we have, say, an int column that is left-joined to a bigint column
with USING, the merged column is the int column promoted to bigint.
GROUP BY's tests for whether grouping on the merged column allows a
reference to the underlying column, or vice versa, should know about
that relationship --- and they do.  But I nearly broke this case with
an ill-advised optimization, so the lack of any test coverage for it
seems like a bad idea.

src/test/regress/expected/aggregates.out
src/test/regress/sql/aggregates.sql

index d091ae4c6e44775ebf8753eea0529787146a668f..f457b5b150ff7f1764c8e1da62074470ea1e1da0 100644 (file)
@@ -1194,6 +1194,32 @@ drop table t2;
 drop table t3;
 drop table p_t1;
 --
+-- Test GROUP BY matching of join columns that are type-coerced due to USING
+--
+create temp table t1(f1 int, f2 bigint);
+create temp table t2(f1 bigint, f22 bigint);
+select f1 from t1 left join t2 using (f1) group by f1;
+ f1 
+----
+(0 rows)
+
+select f1 from t1 left join t2 using (f1) group by t1.f1;
+ f1 
+----
+(0 rows)
+
+select t1.f1 from t1 left join t2 using (f1) group by t1.f1;
+ f1 
+----
+(0 rows)
+
+-- only this one should fail:
+select t1.f1 from t1 left join t2 using (f1) group by f1;
+ERROR:  column "t1.f1" must appear in the GROUP BY clause or be used in an aggregate function
+LINE 1: select t1.f1 from t1 left join t2 using (f1) group by f1;
+               ^
+drop table t1, t2;
+--
 -- Test combinations of DISTINCT and/or ORDER BY
 --
 select array_agg(a order by b)
index 17fb256aec5cc62d1933d26a96a1e8e9d776e0aa..3e593f2d615e080c2c5541a271ba19dd603a1f98 100644 (file)
@@ -432,6 +432,21 @@ drop table t2;
 drop table t3;
 drop table p_t1;
 
+--
+-- Test GROUP BY matching of join columns that are type-coerced due to USING
+--
+
+create temp table t1(f1 int, f2 bigint);
+create temp table t2(f1 bigint, f22 bigint);
+
+select f1 from t1 left join t2 using (f1) group by f1;
+select f1 from t1 left join t2 using (f1) group by t1.f1;
+select t1.f1 from t1 left join t2 using (f1) group by t1.f1;
+-- only this one should fail:
+select t1.f1 from t1 left join t2 using (f1) group by f1;
+
+drop table t1, t2;
+
 --
 -- Test combinations of DISTINCT and/or ORDER BY
 --