Skip to content
Open
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: 1 addition & 2 deletions core/src/main/java/org/apache/calcite/sql/SqlDelete.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ public SqlNode getTargetTable() {
}
SqlNode condition = this.condition;
if (condition != null) {
writer.sep("WHERE");
condition.unparse(writer, opLeft, opRight);
SqlUtil.unparseWhereClause(writer, condition, opLeft, opRight);
}
writer.endList(frame);
}
Expand Down
31 changes: 1 addition & 30 deletions core/src/main/java/org/apache/calcite/sql/SqlSelectOperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
*/
package org.apache.calcite.sql;

import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.util.SqlBasicVisitor;
import org.apache.calcite.sql.util.SqlVisitor;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.ArrayList;
import java.util.List;

import static org.apache.calcite.linq4j.Nullness.castNonNull;
Expand Down Expand Up @@ -175,34 +173,7 @@ public SqlSelect createCall(

SqlNode where = select.where;
if (where != null) {
writer.sep("WHERE");

if (!writer.isAlwaysUseParentheses()) {
SqlNode node = where;

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

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

// unparse in a WHERE_LIST frame
writer.list(SqlWriter.FrameTypeEnum.WHERE_LIST, whereSep,
new SqlNodeList(list, where.getParserPosition()));
} else {
where.unparse(writer, 0, 0);
}
SqlUtil.unparseWhereClause(writer, where, 0, 0);
}
if (select.groupBy != null) {
SqlNodeList groupBy =
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/apache/calcite/sql/SqlUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ public void setSourceSelect(SqlSelect sourceSelect) {
writer.endList(setFrame);
SqlNode condition = this.condition;
if (condition != null) {
writer.sep("WHERE");
condition.unparse(writer, opLeft, opRight);
SqlUtil.unparseWhereClause(writer, condition, opLeft, opRight);
}
writer.endList(frame);
}
Expand Down
43 changes: 43 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,49 @@ public static void unparseBinarySyntax(
writer.endList(frame);
}

/**
* Unparses a WHERE clause.
*
* <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 leftPrec Left precedence
* @param rightPrec Right precedence
*/
public static void unparseWhereClause(SqlWriter writer, SqlNode where,
int leftPrec, int rightPrec) {
writer.sep("WHERE");

if (!writer.isAlwaysUseParentheses()) {
SqlNode node = where;

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

// Unroll whereClause.
final List<SqlNode> list = new ArrayList<>(0);
while (node.getKind() == whereSep.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()));
} else {
where.unparse(writer, leftPrec, rightPrec);
}
}

/**
* Concatenates string literals.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10163,6 +10163,30 @@ private void checkLiteral2(String expression, String expected) {
final String expected2 = "UPDATE \"foodmart\".\"product\" SET \"product_name\" = 'calcite', "
+ "\"product_id\" = 10\nWHERE \"product_id\" = 1";
sql(sql2).ok(expected2);

final String sql3 = "update \"foodmart\".\"product\"\n"
+ "set \"product_name\" = 'calcite'\n"
+ "where exists (\n"
+ " select 1 from \"foodmart\".\"product_class\")";
final String expected3 = "UPDATE \"foodmart\".\"product\" SET \"product_name\" = "
+ "'calcite'\nWHERE EXISTS (SELECT *\nFROM \"foodmart\".\"product_class\")";
sql(sql3).ok(expected3);
}

@Test void testDelete() {
final String sql0 = "delete from \"foodmart\".\"product\"\n"
+ "where exists (\n"
+ " select 1 from \"foodmart\".\"product_class\")";
final String expected0 = "DELETE FROM \"foodmart\".\"product\"\n"
+ "WHERE EXISTS (SELECT *\nFROM \"foodmart\".\"product_class\")";
sql(sql0).ok(expected0);

final String sql1 = "delete from \"foodmart\".\"product\"\n"
+ "where not exists (\n"
+ " select 1 from \"foodmart\".\"product_class\")";
final String expected1 = "DELETE FROM \"foodmart\".\"product\"\n"
+ "WHERE NOT EXISTS (SELECT *\nFROM \"foodmart\".\"product_class\")";
sql(sql1).ok(expected1);
}

/**
Expand Down
Loading