tisyabhatia commented on code in PR #5127:
URL: https://github.com/apache/calcite/pull/5127#discussion_r3786494687
##########
core/src/test/java/org/apache/calcite/test/JdbcTest.java:
##########
@@ -1297,15 +1297,23 @@ private void checkResultSetMetaData(Connection
connection, String sql)
+ "c0=1998\n");
}
- /** Test case for [CALCITE-7594] GROUP BY ALL: grouping only by a constant
- * over empty input returns 0 rows. */
+ /** Test case for [CALCITE-7675] GROUP BY ALL over a select list whose only
+ * non-aggregate item is a constant.
+ *
+ * <p>The constant is not a grouping key, so no grouping key remains and the
+ * query is a single-group aggregation. It therefore returns one row even
+ * though the input is empty. Before [CALCITE-7675] the constant was a
+ * grouping key, empty input yielded no groups, and the query returned no
+ * rows. BigQuery documents the behavior asserted here: "If the set of
+ * inferred grouping keys is empty after exclusions are applied, all input
+ * rows are considered a single group for aggregation." */
@Test void testGroupByAllOverEmptyInput() {
CalciteAssert.hr()
.query("select 'x', count(*)\n"
+ "from \"hr\".\"emps\"\n"
+ "where false\n"
+ "group by all")
- .returnsCount(0);
+ .returns("EXPR$0=x; EXPR$1=0\n");
Review Comment:
The expansion is what gets stored as a view definition, so:
SELECT deptno, count(*) as cnt, sum(sal) AS sm, 42 AS constant FROM emp
GROUP BY ALL
unparses to GROUP BY 42. And that would fail with "ordinal out of range."
The in-range case is also invalid behavior because SELECT ... 2 AS constant ...
GROUP BY ALL resolves to GROUP BY deptno, 2, and 2 resolves to COUNT(*) which
is an aggregate and we cannot group by aggregates.
On the empty-input semantics, Calcite's behavior:
SELECT count(*), sum(sal) FROM emp GROUP BY ALL -- 1 row
SELECT count(*), sum(sal), 42 as constant FROM emp GROUP BY ALL -- 0 rows
the first is what is already in CALCITE-7594. Adding a constant should not
change the cardinality so that's why the inferred keyset should exclude it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]