You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "zzwqqq (via GitHub)" <gi...@apache.org> on 2026/06/12 09:37:00 UTC

[PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

zzwqqq opened a new pull request, #5017:
URL: https://github.com/apache/calcite/pull/5017

   
   
   <!--
   Thanks for sending a pull request! Here are some critical tips for you:
   
   1. READ THE GUIDE FIRST: https://calcite.apache.org/develop/#contributing
      *For significant contributions, please discuss on the dev mailing list or Jira BEFORE coding.*
   
   2. JIRA IS USUALLY MANDATORY: Ensure you have created an issue on the Calcite Jira:
      https://issues.apache.org/jira/projects/CALCITE/issues
      *Check existing issues first to avoid duplicates.*
      *Note: A Jira is NOT required for typos and cosmetic changes (i.e., changes that are neither bugs nor features).*
   
   3. TIMING: Strongly recommended to create the Jira BEFORE you start writing code (e.g., a day or so before posting a PR).
      This gives others a chance to weigh in on your specification.
   
   4. CRITICAL CONSISTENCY RULE
      The following three items MUST match exactly in wording and meaning:
      (A) The Jira Issue Title
      (B) This Pull Request Title
      (C) Your Git Commit Message
   
      Format: [CALCITE-XXXX] <Description>
      Example: [CALCITE-0000] Add IF NOT EXISTS clause to CREATE TABLE
   
      Guidelines for a good Title:
      - Illustrate using SQL keywords (in ALL-CAPS) rather than Java method names.
      - Focus on the specification and user experience, not the implementation.
      - Make it clear whether it is a bug or a feature.
      - Use words like "should" to indicate desired behavior vs. current behavior (e.g., "Validator should not close model file").
   
   5. REPRODUCTION: If fixing a bug, please provide a concise SQL example or test case to reproduce the issue for a faster review.
   
   6. TESTING: Ensure `./gradlew build` passes and appropriate tests are added/updated.
   -->
   
   ## Jira Link
   
   [CALCITE-7585](https://issues.apache.org/jira/browse/CALCITE-7585)
   
   ## Changes Proposed
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
   If possible, please consider writing useful notes for better and faster reviews in your PR.
   -->
   Fixes unparsing of `EXISTS` predicates in the `ON` clause of `MERGE`.
   
   `SqlMerge` was unparsing the `ON` condition directly. When the condition contains an `EXISTS` subquery, this can produce invalid SQL such as `ON EXISTS SELECT ...`.
   
   This change adds `SqlUtil.unparseConditionClause` and uses it for `MERGE ON`. The helper reuses the existing `WHERE_LIST` frame, matching the behavior already used for `WHERE` clauses in `SELECT`, `UPDATE`, and `DELETE`.
   
   `SqlUtil.unparseWhereClause` remains as a wrapper around the shared helper.
   
   Adds a regression test for `MERGE ... ON EXISTS (...)`.


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #5017:
URL: https://github.com/apache/calcite/pull/5017#issuecomment-4689941535

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5017) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5017&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5017&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=5017&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [96.9% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5017&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5017&metric=new_duplicated_lines_density&view=list)  
     
   <!-- sqra-placement-anchor -->
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5017)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #5017:
URL: https://github.com/apache/calcite/pull/5017#issuecomment-4696756511

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5017) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5017&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5017&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=5017&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5017&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5017&metric=new_duplicated_lines_density&view=list)  
     
   <!-- sqra-placement-anchor -->
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5017)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #5017:
URL: https://github.com/apache/calcite/pull/5017#discussion_r3405065179


##########
core/src/main/java/org/apache/calcite/sql/SqlUtil.java:
##########
@@ -455,48 +455,62 @@ public static void unparseBinarySyntax(
   }
 
   /**
-   * Unparses a WHERE clause.
+   * Unparses a condition clause, such as WHERE or ON.

Review Comment:
   this deserves a bit more elaboration, since both WHERE and ON can be used in several places in a SQL statement.



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "dssysolyatin (via GitHub)" <gi...@apache.org>.
dssysolyatin commented on code in PR #5017:
URL: https://github.com/apache/calcite/pull/5017#discussion_r3405684340


##########
core/src/main/java/org/apache/calcite/sql/SqlUtil.java:
##########
@@ -455,48 +455,62 @@ public static void unparseBinarySyntax(
   }
 
   /**
-   * Unparses a WHERE clause.
+   * Unparses a condition clause, such as WHERE or ON.
    *
    * <p>Unparsing the condition in a {@link SqlWriter.FrameTypeEnum#WHERE_LIST}
    * frame lets sub-queries in predicates recognize that they need
    * parentheses.
    *
-   * @param writer   Writer
-   * @param where WHERE condition
+   * @param writer Writer
+   * @param clauseKeyword Clause keyword
+   * @param condition Clause condition
    * @param leftPrec Left precedence
    * @param rightPrec Right precedence
    */
-  public static void unparseWhereClause(SqlWriter writer, SqlNode where,
-      int leftPrec, int rightPrec) {
-    writer.sep("WHERE");
+  public static void unparseConditionClause(SqlWriter writer,

Review Comment:
   I would suggest to drop the `clauseKeyword` parameter from this function and just write
   
   ```
   writer.sep("ON");
   SqlUtil.unparseConditionClause()
   ```
   
   and
   
   ```
   write.sep("WHERE")
   SqlUtil.unparseConditionClause()
   ```
   
   



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "xiedeyantu (via GitHub)" <gi...@apache.org>.
xiedeyantu commented on PR #5017:
URL: https://github.com/apache/calcite/pull/5017#issuecomment-4698281508

   You already have two approvals, so you can squash your commits.


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #5017:
URL: https://github.com/apache/calcite/pull/5017#issuecomment-4696749612

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5017) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5017&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5017&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=5017&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5017&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5017&metric=new_duplicated_lines_density&view=list)  
     
   <!-- sqra-placement-anchor -->
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5017)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "dssysolyatin (via GitHub)" <gi...@apache.org>.
dssysolyatin commented on code in PR #5017:
URL: https://github.com/apache/calcite/pull/5017#discussion_r3405684340


##########
core/src/main/java/org/apache/calcite/sql/SqlUtil.java:
##########
@@ -455,48 +455,62 @@ public static void unparseBinarySyntax(
   }
 
   /**
-   * Unparses a WHERE clause.
+   * Unparses a condition clause, such as WHERE or ON.
    *
    * <p>Unparsing the condition in a {@link SqlWriter.FrameTypeEnum#WHERE_LIST}
    * frame lets sub-queries in predicates recognize that they need
    * parentheses.
    *
-   * @param writer   Writer
-   * @param where WHERE condition
+   * @param writer Writer
+   * @param clauseKeyword Clause keyword
+   * @param condition Clause condition
    * @param leftPrec Left precedence
    * @param rightPrec Right precedence
    */
-  public static void unparseWhereClause(SqlWriter writer, SqlNode where,
-      int leftPrec, int rightPrec) {
-    writer.sep("WHERE");
+  public static void unparseConditionClause(SqlWriter writer,

Review Comment:
   I would drop the `clauseKeyword` parameter from this function and just write
   
   ```
   writer.sep("ON");
   SqlUtil.unparseConditionClause()
   ```
   
   and
   
   ```
   write.sep("WHERE")
   SqlUtil.unparseConditionClause()
   ```
   
   



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #5017:
URL: https://github.com/apache/calcite/pull/5017#issuecomment-4700186427

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5017) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5017&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5017&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=5017&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5017&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5017&metric=new_duplicated_lines_density&view=list)  
     
   <!-- sqra-placement-anchor -->
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5017)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on PR #5017:
URL: https://github.com/apache/calcite/pull/5017#issuecomment-4693581980

   @dssysolyatin I have added you are a reviewer since you raised this problem originally


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #5017:
URL: https://github.com/apache/calcite/pull/5017#discussion_r3406869411


##########
core/src/main/java/org/apache/calcite/sql/SqlUtil.java:
##########
@@ -455,48 +455,62 @@ public static void unparseBinarySyntax(
   }
 
   /**
-   * Unparses a WHERE clause.
+   * Unparses a condition clause, such as WHERE or ON.

Review Comment:
   Thanks, I clarified the Javadoc to describe that the helper uses the existing WHERE_LIST frame for predicate-list formatting.



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu merged PR #5017:
URL: https://github.com/apache/calcite/pull/5017


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #5017:
URL: https://github.com/apache/calcite/pull/5017#discussion_r3406872051


##########
core/src/main/java/org/apache/calcite/sql/SqlUtil.java:
##########
@@ -455,48 +455,62 @@ public static void unparseBinarySyntax(
   }
 
   /**
-   * Unparses a WHERE clause.
+   * Unparses a condition clause, such as WHERE or ON.
    *
    * <p>Unparsing the condition in a {@link SqlWriter.FrameTypeEnum#WHERE_LIST}
    * frame lets sub-queries in predicates recognize that they need
    * parentheses.
    *
-   * @param writer   Writer
-   * @param where WHERE condition
+   * @param writer Writer
+   * @param clauseKeyword Clause keyword
+   * @param condition Clause condition
    * @param leftPrec Left precedence
    * @param rightPrec Right precedence
    */
-  public static void unparseWhereClause(SqlWriter writer, SqlNode where,
-      int leftPrec, int rightPrec) {
-    writer.sep("WHERE");
+  public static void unparseConditionClause(SqlWriter writer,

Review Comment:
   Thanks, I removed the clauseKeyword parameter and the unparseWhereClause wrapper.



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on PR #5017:
URL: https://github.com/apache/calcite/pull/5017#issuecomment-4700141301

   > You already have two approvals, so you can squash your commits.
   
   Thanks @xiedeyantu @mihaibudiu @dssysolyatin. I have updated the PR description and squashed the commits.


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on PR #5017:
URL: https://github.com/apache/calcite/pull/5017#issuecomment-4700144170

   I will merge once CI completes
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org