This is an automated email from the ASF dual-hosted git repository.

asolimando pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new b056e45696 [CALCITE-7537] Invalid Postgres SQL generated for 
right-deep comma join trees
b056e45696 is described below

commit b056e45696b406565de3a8f183c59493da77a7bf
Author: Alessandro Solimando <[email protected]>
AuthorDate: Wed May 20 18:42:23 2026 +0200

    [CALCITE-7537] Invalid Postgres SQL generated for right-deep comma join 
trees
---
 .../main/java/org/apache/calcite/sql/SqlJoin.java  |  5 +++-
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java | 32 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/SqlJoin.java 
b/core/src/main/java/org/apache/calcite/sql/SqlJoin.java
index 6541afa385..f948a7d668 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlJoin.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlJoin.java
@@ -246,7 +246,10 @@ private SqlJoinOperator(String name, int prec) {
       default:
         throw Util.unexpected(join.getJoinType());
       }
-      join.right.unparse(writer, getRightPrec(), rightPrec);
+      // Comma join is associative, so no parens needed on the right child
+      final int rightChildLeftPrec =
+          join.getJoinType() == JoinType.COMMA ? getLeftPrec() : 
getRightPrec();
+      join.right.unparse(writer, rightChildLeftPrec, rightPrec);
       SqlNode joinCondition = join.condition;
       if (joinCondition != null) {
         switch (join.getConditionType()) {
diff --git 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index d0768b743e..2953f3b186 100644
--- 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -8449,6 +8449,38 @@ private void checkLiteral2(String expression, String 
expected) {
         .withDoris().ok(expectedStarRocks);
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7537";>[CALCITE-7537]
+   * Invalid Postgres SQL generated for right-deep comma join trees</a>.
+   *
+   * <p>As {@link #testCommaCrossJoin3way()}, but with a right-deep join tree.
+   * The SQL parser produces left-deep trees, so we use {@link RelBuilder}
+   * to construct: {@code Join(DEPT, Join(BONUS, SALGRADE))}.
+   *
+   * <p>A right-deep cross join should produce the same flat comma-separated
+   * FROM list as a left-deep one. */
+  @Test void testCommaCrossJoin3wayRightDeep() {
+    // Use tables with no overlapping column names to avoid SELECT * expansion
+    final Function<RelBuilder, RelNode> relFn = b ->
+        b.scan("DEPT")
+            .scan("BONUS")
+            .scan("SALGRADE")
+            .join(JoinRelType.INNER) // Join(BONUS, SALGRADE)
+            .join(JoinRelType.INNER) // Join(DEPT, Join(BONUS, SALGRADE))
+            .build();
+    final String expectedMysql = "SELECT *\n"
+        + "FROM `scott`.`DEPT`,\n"
+        + "`scott`.`BONUS`,\n"
+        + "`scott`.`SALGRADE`";
+    final String expectedPostgresql = "SELECT *\n"
+        + "FROM \"scott\".\"DEPT\",\n"
+        + "\"scott\".\"BONUS\",\n"
+        + "\"scott\".\"SALGRADE\"";
+    relFn(relFn)
+        .withMysql().ok(expectedMysql)
+        .withPostgresql().ok(expectedPostgresql);
+  }
+
   /** As {@link #testCommaCrossJoin3way()}, but shows that if there is a
    * {@code LEFT JOIN} in the FROM clause, we can't use comma-join. */
   @Test void testLeftJoinPreventsCommaJoin() {

Reply via email to