Skip to content
Merged
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
3 changes: 2 additions & 1 deletion core/src/main/java/org/apache/calcite/sql/SqlDelete.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public SqlNode getTargetTable() {
}
SqlNode condition = this.condition;
if (condition != null) {
SqlUtil.unparseWhereClause(writer, condition, opLeft, opRight);
writer.sep("WHERE");
SqlUtil.unparseConditionClause(writer, condition, opLeft, opRight);
}
writer.endList(frame);
}
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/org/apache/calcite/sql/SqlMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,8 @@ public void setSourceSelect(SqlSelect sourceSelect) {
writer.keyword("USING");
source.unparse(writer, opLeft, opRight);

writer.newlineAndIndent();
writer.keyword("ON");
condition.unparse(writer, opLeft, opRight);
writer.sep("ON");
SqlUtil.unparseConditionClause(writer, condition, opLeft, opRight);

SqlUpdate updateCall = this.updateCall;
if (updateCall != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ public SqlSelect createCall(

SqlNode where = select.where;
if (where != null) {
SqlUtil.unparseWhereClause(writer, where, 0, 0);
writer.sep("WHERE");
SqlUtil.unparseConditionClause(writer, where, 0, 0);
}
if (select.groupBy != null) {
SqlNodeList groupBy =
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/apache/calcite/sql/SqlUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ public void setSourceSelect(SqlSelect sourceSelect) {
writer.endList(setFrame);
SqlNode condition = this.condition;
if (condition != null) {
SqlUtil.unparseWhereClause(writer, condition, opLeft, opRight);
writer.sep("WHERE");
SqlUtil.unparseConditionClause(writer, condition, opLeft, opRight);
}
writer.endList(frame);
}
Expand Down
34 changes: 15 additions & 19 deletions core/src/main/java/org/apache/calcite/sql/SqlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,45 +455,41 @@ public static void unparseBinarySyntax(
}

/**
* Unparses a WHERE clause.
* Unparses a condition using a
* {@link SqlWriter.FrameTypeEnum#WHERE_LIST} frame, which provides
* predicate-list formatting.
*
* <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 condition 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,

@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.

SqlNode condition, int leftPrec, int rightPrec) {
if (!writer.isAlwaysUseParentheses()) {
SqlNode node = where;
SqlNode node = condition;

// Decide whether to split on ORs or ANDs.
SqlBinaryOperator whereSep = SqlStdOperatorTable.AND;
SqlBinaryOperator conditionSep = SqlStdOperatorTable.AND;
if ((node instanceof SqlCall)
&& node.getKind() == SqlKind.OR) {
whereSep = SqlStdOperatorTable.OR;
conditionSep = SqlStdOperatorTable.OR;
}

// Unroll whereClause.
// Unroll condition.
final List<SqlNode> list = new ArrayList<>(0);
while (node.getKind() == whereSep.kind) {
while (node.getKind() == conditionSep.kind) {
assert node instanceof SqlCall;
final SqlCall call1 = (SqlCall) node;
list.add(0, call1.operand(1));
node = call1.operand(0);
}
list.add(0, node);

writer.list(SqlWriter.FrameTypeEnum.WHERE_LIST, whereSep,
new SqlNodeList(list, where.getParserPosition()));
writer.list(SqlWriter.FrameTypeEnum.WHERE_LIST, conditionSep,
new SqlNodeList(list, condition.getParserPosition()));
} else {
where.unparse(writer, leftPrec, rightPrec);
condition.unparse(writer, leftPrec, rightPrec);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10046,6 +10046,23 @@ private void checkLiteral2(String expression, String expected) {
sql(sql7)
.schema(CalciteAssert.SchemaSpec.JDBC_SCOTT)
.ok(expected7);

// [CALCITE-7585] SqlMerge unparse EXISTS predicates in ON clause
// without parentheses.
final String sql8 = "merge into \"DEPT\" as \"t\"\n"
+ "using \"DEPT\" as \"s\"\n"
+ "on exists (select 1 from \"EMP\" as \"e\" where \"e\".\"DEPTNO\" = \"s\".\"DEPTNO\")\n"
+ "when matched then\n"
+ "update set \"DNAME\" = \"s\".\"DNAME\"";
final String expected8 = "MERGE INTO \"SCOTT\".\"DEPT\" AS \"DEPT0\"\n"
+ "USING \"SCOTT\".\"DEPT\"\n"
+ "ON EXISTS (SELECT *\n"
+ "FROM \"SCOTT\".\"EMP\"\n"
+ "WHERE \"DEPTNO\" = \"DEPT\".\"DEPTNO\")\n"
+ "WHEN MATCHED THEN UPDATE SET \"DNAME\" = \"DEPT\".\"DNAME\"";
sql(sql8)
.schema(CalciteAssert.SchemaSpec.JDBC_SCOTT)
.ok(expected8);
}

/** Test case for
Expand Down
Loading