mihaibudiu commented on code in PR #4692:
URL: https://github.com/apache/calcite/pull/4692#discussion_r2628053084


##########
core/src/main/java/org/apache/calcite/sql/SqlDialect.java:
##########
@@ -1825,4 +1825,7 @@ private ContextImpl(DatabaseProduct databaseProduct,
           conformance, nullCollation, dataTypeSystem, jethroInfo);
     }
   }
+  public boolean mustWrapNestedJoin(RelNode relNode) {

Review Comment:
   Please document this function.



##########
core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java:
##########
@@ -233,7 +297,13 @@ public Result visit(Join e) {
       break;
     }
     final Result leftResult = visitInput(e, 0).resetAlias();
-    final Result rightResult = visitInput(e, 1).resetAlias();
+    Result rightResult = visitInput(e, 1).resetAlias();
+
+    if (dialect.mustWrapNestedJoin(e)) {

Review Comment:
   Can this alias conflict with an existing relation name that is in scope?
   Please add a test for this case.



##########
core/src/main/java/org/apache/calcite/sql/dialect/ClickHouseSqlDialect.java:
##########
@@ -404,4 +411,37 @@ private static void unparseFloor(SqlWriter writer, SqlCall 
call) {
     call.operand(0).unparse(writer, 0, 0);
     writer.endList(frame);
   }
+
+  @Override public boolean mustWrapNestedJoin(RelNode rel) {
+    if (!(rel instanceof Join)) {
+      return false;
+    }
+    Join join = (Join) rel;
+
+    // ClickHouse primarily requires wrapping the right side of a JOIN
+    // when it contains a nested JOIN
+    return containsJoinRecursive(join.getRight());
+  }
+
+  /**
+   * Recursively checks if a RelNode contains a JOIN, looking through
+   * transparent single-input operators.
+   */
+  private static boolean containsJoinRecursive(RelNode rel) {

Review Comment:
   Isn't a visitor a better tool to do this?



##########
core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java:
##########
@@ -11462,4 +11463,196 @@ public Sql schema(CalciteAssert.SchemaSpec 
schemaSpec) {
           relFn, transforms);
     }
   }
+

Review Comment:
   I am assuming you have validated these against clickhouse



-- 
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]

Reply via email to