Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2261,8 +2261,41 @@ && hasSortByOrdinal(node)) {
if (groupKeysContainOver(agg)) {
return true;
}

if (!dialect.supportsGroupByLiteral()
&& hasGroupByLiteral(agg)) {
return true;
}
}

return false;
}

/**
* Returns whether any grouping key of {@code aggregate} is represented by
* a literal expression in this result's {@code SELECT} list.
*/
private boolean hasGroupByLiteral(
@UnknownInitialization Result this, Aggregate aggregate) {
if (!(node instanceof SqlSelect)) {
return false;
}

final SqlNodeList selectList = ((SqlSelect) node).getSelectList();
if (selectList.equals(SqlNodeList.SINGLETON_STAR)) {
return false;
}

for (int groupKey : aggregate.getGroupSet()) {
if (groupKey >= selectList.size()) {
return false;
}
final SqlNode expression = SqlUtil.stripAs(selectList.get(groupKey));
// A literal wrapped in a CAST is also considered a literal.
if (SqlUtil.isLiteral(expression, true)) {
return true;
}
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,56 @@ private static String toSql(RelNode root, SqlDialect dialect,
.withInformix().ok(expectedInformix);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7679">[CALCITE-7679]
* RelToSqlConverter generates GROUP BY literals for dialects that do not
* support them when the constant is hidden by nested Projects</a>. */
@Test void testGroupByLiteralWithNestedProjects() {
final String query = "SELECT \"id\"\n"
+ "FROM (\n"
+ " SELECT \"id\"\n"
+ " FROM (\n"
+ " SELECT NULL AS \"id\"\n"
+ " FROM \"employee\"\n"
+ " ) AS \"t1\"\n"
+ ") AS \"t2\"\n"
+ "GROUP BY \"id\"";
final String expectedPostgresql = "SELECT \"id\"\n"
+ "FROM (SELECT NULL AS \"id\"\n"
+ "FROM \"foodmart\".\"employee\") AS \"t0\"\n"
+ "GROUP BY \"id\"";
sql(query)
// Disable RelBuilder's eager Project merging to retain the nested
// Projects that reproduce the constant GROUP BY conversion issue.
.withConfig(c -> c.withRelBuilderConfigTransform(b -> b.withBloat(-1)))
.withPostgresql().ok(expectedPostgresql);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7679">[CALCITE-7679]
* RelToSqlConverter generates GROUP BY literals for dialects that do not
* support them when the constant is hidden by nested Projects</a>. */
@Test void testGroupByLiteralWithReorderedNestedProjects() {
final String query = "SELECT \"id\", \"employee_id\"\n"
+ "FROM (\n"
+ " SELECT \"id\", \"employee_id\"\n"
+ " FROM (\n"
+ " SELECT \"employee_id\", NULL AS \"id\"\n"
+ " FROM \"employee\"\n"
+ " ) AS \"t1\"\n"
+ ") AS \"t2\"\n"
+ "GROUP BY \"id\", \"employee_id\"";
final String expectedPostgresql = "SELECT \"id\", \"employee_id\"\n"
+ "FROM (SELECT NULL AS \"id\", \"employee_id\"\n"
+ "FROM \"foodmart\".\"employee\") AS \"t0\"\n"
+ "GROUP BY \"id\", \"employee_id\"";
sql(query)
// Disable RelBuilder's eager Project merging to retain the nested
// Projects that reproduce the constant GROUP BY conversion issue.
.withConfig(c -> c.withRelBuilderConfigTransform(b -> b.withBloat(-1)))
.withPostgresql().ok(expectedPostgresql);
}

/** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-6910">[CALCITE-6910]
* RelToSql does not handle ASOF joins</a>. */
@Test void testAsofJoin() {
Expand Down
Loading