Skip to content
Draft
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 @@ -772,6 +772,7 @@ private Builder visitAggregate(Aggregate e, List<Integer> groupKeyList,
// "select a, b, sum(x) from ( ... ) group by a, b"
final boolean ignoreClauses = e.getInput() instanceof Project;
final Result x = visitInput(e, 0, isAnon(), ignoreClauses, clauseSet);
parseCorrelTable(e, x);
final Builder builder = x.builder(e);
final List<SqlNode> selectList = new ArrayList<>();
final List<SqlNode> groupByList =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1587,9 +1587,17 @@ public static SqlNode toSql(RexLiteral literal) {
}

protected Context getAliasContext(RexCorrelVariable variable) {
return requireNonNull(
correlTableMap.get(variable.id),
() -> "variable " + variable.id + " is not found");
Context context = correlTableMap.get(variable.id);
if (context == null) {
if (correlTableMap.isEmpty()) {
context =
aliasContext(ImmutableMap.of(variable.id.getName(), variable.getType()), true);
}
if (context != null) {
correlTableMap.put(variable.id, context);
}
}
return requireNonNull(context, () -> "variable " + variable.id + " is not found");
}

/** Simple implementation of {@link Context} that cannot handle sub-queries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11887,6 +11887,61 @@ public Sql schema(CalciteAssert.SchemaSpec schemaSpec) {
sql(sql).schema(CalciteAssert.SchemaSpec.JDBC_SCOTT).ok(expected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7440">[CALCITE-7440]
* RelToSqlConverter throws NPE when correlation scope is missing after
* semi-join rewrites.</a>. */
@Test void testPostgresqlRoundTripCorrelatedProject() {
final String query = correlatedProjectQuery7440();
final RuleSet rules = RuleSets.ofList();
final String generated = sql(query).withPostgresql().optimize(rules, null).exec();
try {
sql(generated).withPostgresql().exec();
} catch (Exception e) {
throw new AssertionError(
"Generated SQL failed PostgreSQL round-trip validation:\n"
+ generated,
e);
}
}

@Test void testPostgresqlRoundTripCorrelatedProjectWithSemiJoinRules() {
Copy link
Member

Choose a reason for hiding this comment

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

I ran this test on the main branch, and strangely it passed. Could you check if your case can reproduce the problem you mentioned?

  @Test void testPostgresqlRoundTripCorrelatedProjectWithSemiJoinRules() {
    final String query = "WITH product_keys AS (\n"
        + "  SELECT p.\"product_id\",\n"
        + "         (SELECT MAX(p3.\"product_id\")\n"
        + "          FROM \"foodmart\".\"product\" p3\n"
        + "          WHERE p3.\"product_id\" = p.\"product_id\") AS \"mx\"\n"
        + "  FROM \"foodmart\".\"product\" p\n"
        + ")\n"
        + "SELECT DISTINCT pk.\"product_id\"\n"
        + "FROM product_keys pk\n"
        + "LEFT JOIN \"foodmart\".\"product\" p2 USING (\"product_id\")\n"
        + "WHERE pk.\"product_id\" IN (\n"
        + "  SELECT p4.\"product_id\"\n"
        + "  FROM \"foodmart\".\"product\" p4\n"
        + ")";

    final RuleSet rules = RuleSets.ofList();
    final String generated = sql(query).withPostgresql().optimize(rules, null).exec();
    sql(generated).withPostgresql().exec();
  }

Copy link
Member

Choose a reason for hiding this comment

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

Please take a look at my description; you may not have solved my problem.

final String query = correlatedProjectQuery7440();

final RuleSet rules =
RuleSets.ofList(CoreRules.FILTER_SUB_QUERY_TO_MARK_CORRELATE,
CoreRules.PROJECT_SUB_QUERY_TO_MARK_CORRELATE,
CoreRules.MARK_TO_SEMI_OR_ANTI_JOIN_RULE,
CoreRules.SEMI_JOIN_JOIN_TRANSPOSE);

final String generated = sql(query).withPostgresql().optimize(rules, null).exec();
try {
sql(generated).withPostgresql().exec();
} catch (Exception e) {
throw new AssertionError(
"Generated SQL failed PostgreSQL round-trip validation:\n"
+ generated,
e);
}
}

private static String correlatedProjectQuery7440() {
return "WITH product_keys AS (\n"
+ " SELECT p.\"product_id\",\n"
+ " (SELECT MAX(p3.\"product_id\")\n"
+ " FROM \"foodmart\".\"product\" p3\n"
+ " WHERE p3.\"product_id\" = p.\"product_id\") AS \"mx\"\n"
+ " FROM \"foodmart\".\"product\" p\n"
+ ")\n"
+ "SELECT DISTINCT pk.\"product_id\"\n"
+ "FROM product_keys pk\n"
+ "LEFT JOIN \"foodmart\".\"product\" p2 USING (\"product_id\")\n"
+ "WHERE pk.\"product_id\" IN (\n"
+ " SELECT p4.\"product_id\"\n"
+ " FROM \"foodmart\".\"product\" p4\n"
+ ")";
}

@Test void testNotBetween() {
Sql f = fixture().withConvertletTable(new SqlRexConvertletTable() {
@Override public @Nullable SqlRexConvertlet get(SqlCall call) {
Expand Down
Loading