Skip to content

[CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses#5017

Merged
mihaibudiu merged 1 commit into
apache:mainfrom
zzwqqq:fix_unparse_on_sqlmerge
Jun 14, 2026
Merged

[CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause without parentheses#5017
mihaibudiu merged 1 commit into
apache:mainfrom
zzwqqq:fix_unparse_on_sqlmerge

Conversation

@zzwqqq

@zzwqqq zzwqqq commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7585

Changes Proposed

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 for unparsing conditions with the existing WHERE_LIST frame. SELECT, UPDATE, DELETE, and MERGE now emit their clause keyword explicitly and then use the shared helper for the condition.

Adds a regression test for MERGE ... ON EXISTS (...).

@mihaibudiu mihaibudiu requested a review from dssysolyatin June 12, 2026 17:17
@mihaibudiu

Copy link
Copy Markdown
Contributor

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


/**
* Unparses a WHERE clause.
* Unparses a condition clause, such as WHERE or ON.

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.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

public static void unparseWhereClause(SqlWriter writer, SqlNode where,
int leftPrec, int rightPrec) {
writer.sep("WHERE");
public static void unparseConditionClause(SqlWriter writer,

@dssysolyatin dssysolyatin Jun 12, 2026

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.

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()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I removed the clauseKeyword parameter and the unparseWhereClause wrapper.

@mihaibudiu mihaibudiu left a comment

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.

Let's see if @dssysolyatin is happy too, but I think this looks good.

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Jun 13, 2026

@dssysolyatin dssysolyatin left a comment

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.

Looks good, thanks @zzwqqq

@xiedeyantu

Copy link
Copy Markdown
Member

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

@zzwqqq zzwqqq force-pushed the fix_unparse_on_sqlmerge branch from d455572 to f2d5e46 Compare June 13, 2026 23:46
@zzwqqq

zzwqqq commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

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.

@mihaibudiu

Copy link
Copy Markdown
Contributor

I will merge once CI completes

@sonarqubecloud

Copy link
Copy Markdown

@mihaibudiu mihaibudiu merged commit fc9af07 into apache:main Jun 14, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants