Skip to content
Open
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 @@ -194,6 +194,8 @@
public final Result visitRoot(RelNode r) {
List<RelOptRule> rules = new ArrayList<>();
if (!this.dialect.supportsGroupByLiteral()) {
rules.add(CoreRules.PROJECT_MERGE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not obvious to me why this solves the problem. Can you explain?

rules.add(CoreRules.PROJECT_REMOVE);
rules.add(AggregateProjectConstantToDummyJoinRule.Config.DEFAULT.toRule());
}

Expand Down Expand Up @@ -727,7 +729,7 @@
* @param program Required only if {@code rex} contains {@link RexLocalRef}
* @param rex Expression to convert
*/
public SqlNode toSql(@Nullable RexProgram program, RexNode rex) {

Check warning on line 732 in core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 178 to 64, Complexity from 39 to 14, Nesting Level from 3 to 2, Number of Variables from 46 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AZ-pcXErN7uvEWkOcSd2&open=AZ-pcXErN7uvEWkOcSd2&pullRequest=5130
rex = dialect.prepareUnparse(rex);
final RexSubQuery subQuery;
final SqlNode sqlSubQuery;
Expand Down Expand Up @@ -1583,7 +1585,7 @@
}

/** Converts a {@link RexLiteral} to a {@link SqlLiteral}. */
public static SqlNode toSql(RexLiteral literal) {

Check warning on line 1588 in core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 91 to 64, Complexity from 25 to 14, Nesting Level from 3 to 2, Number of Variables from 13 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AZ-pcXErN7uvEWkOcSd3&open=AZ-pcXErN7uvEWkOcSd3&pullRequest=5130
SqlTypeName typeName = literal.getTypeName();
switch (typeName) {
case SYMBOL:
Expand Down Expand Up @@ -2164,7 +2166,7 @@
}

/** Returns whether a new sub-query is required. */
private boolean needNewSubQuery(

Check warning on line 2169 in core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 73 to 64, Complexity from 34 to 14, Nesting Level from 3 to 2, Number of Variables from 15 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AZ-pcXErN7uvEWkOcSd4&open=AZ-pcXErN7uvEWkOcSd4&pullRequest=5130
@UnknownInitialization Result this,
RelNode rel, List<Clause> clauses,
Set<Clause> expectedClauses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,31 @@ private static String toSql(RelNode root, SqlDialect dialect,
.withInformix().ok(expectedInformix);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7679">[CCALCITE-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 \"t\".\"id\"\n"
+ "FROM \"foodmart\".\"employee\",\n"
+ "(VALUES (NULL)) AS \"t\" (\"id\")\n"
+ "GROUP BY \"t\".\"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 Expand Up @@ -8123,12 +8148,11 @@ private void checkLiteral2(String expression, String expected) {
@Test void testThreeValues() {
final String sql = "select * from (values (1), (2), (3)) as t(\"a\")\n";
sql(sql)
.withRedshift().ok("SELECT *\n"
+ "FROM (SELECT 1 AS \"a\"\n"
.withRedshift().ok("SELECT 1 AS \"a\"\n"
+ "UNION ALL\n"
+ "SELECT 2 AS \"a\"\n"
+ "UNION ALL\n"
+ "SELECT 3 AS \"a\")");
+ "SELECT 3 AS \"a\"");
}

@Test void testValuesEmpty() {
Expand Down Expand Up @@ -9805,13 +9829,12 @@ private void checkLiteral2(String expression, String expected) {
+ "ON t1.DEPTNO = t2.c0";
final String expectedPostgres = "SELECT *\n"
+ "FROM \"SCOTT\".\"DEPT\"\n"
+ "LEFT JOIN (SELECT \"t\".\"C0\", \"t0\".\"C0\" AS \"C00\"\n"
+ "FROM (SELECT \"DEPTNO\" AS \"C0\"\n"
+ "FROM \"SCOTT\".\"DEPT\") AS \"t\"\n"
+ "LEFT JOIN ((SELECT \"DEPTNO\" AS \"C0\"\n"
+ "FROM \"SCOTT\".\"DEPT\") AS \"t\" "
+ "INNER JOIN (SELECT \"DEPTNO\" AS \"C0\"\n"
+ "FROM \"SCOTT\".\"DEPT\") AS \"t0\""
+ " ON \"t\".\"C0\" = \"t0\".\"C0\") AS \"t1\""
+ " ON \"DEPT\".\"DEPTNO\" = \"t1\".\"C0\"";
+ " ON \"t\".\"C0\" = \"t0\".\"C0\")"
+ " ON \"DEPT\".\"DEPTNO\" = \"t\".\"C0\"";
sql(sql)
.schema(CalciteAssert.SchemaSpec.JDBC_SCOTT)
.withPostgresql().ok(expectedPostgres);
Expand Down
Loading