Skip to content

[CALCITE-6825] Add support for ALL, SOME, ANY in RelToSqlConverter #4224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,8 @@ public SqlNode toSql(@Nullable RexProgram program, RexNode rex) {
return new SqlDynamicParam(caseParam.getIndex(), POS);

case IN:
case SOME:
case ALL:
subQuery = (RexSubQuery) rex;
sqlSubQuery = implementor().visitRoot(subQuery.rel).asQueryOrValues();
final List<RexNode> operands = subQuery.operands;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.calcite.sql.validate.SqlValidator;
Expand Down Expand Up @@ -118,4 +119,45 @@ public class SqlQuantifyOperator extends SqlInOperator {
}
return null;
}

@Override public SqlOperator not() {
switch (kind) {
case SOME:
switch (comparisonKind) {
case EQUALS:
return SqlStdOperatorTable.ALL_NE;
case NOT_EQUALS:
return SqlStdOperatorTable.ALL_EQ;
case LESS_THAN_OR_EQUAL:
return SqlStdOperatorTable.ALL_GT;
case LESS_THAN:
return SqlStdOperatorTable.ALL_GE;
case GREATER_THAN_OR_EQUAL:
return SqlStdOperatorTable.ALL_LT;
case GREATER_THAN:
return SqlStdOperatorTable.ALL_LE;
default:
throw new AssertionError("unexpected SOME comparisonKind " + kind);
}
case ALL:
switch (comparisonKind) {
case EQUALS:
return SqlStdOperatorTable.SOME_NE;
case NOT_EQUALS:
return SqlStdOperatorTable.SOME_EQ;
case LESS_THAN_OR_EQUAL:
return SqlStdOperatorTable.SOME_GT;
case LESS_THAN:
return SqlStdOperatorTable.SOME_GE;
case GREATER_THAN_OR_EQUAL:
return SqlStdOperatorTable.SOME_LT;
case GREATER_THAN:
return SqlStdOperatorTable.SOME_LE;
default:
throw new AssertionError("unexpected ALL comparisonKind " + kind);
}
default:
throw new AssertionError("unexpected " + kind);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9458,6 +9458,27 @@ private void checkLiteral2(String expression, String expected) {
assertEquals(project, forceProject);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6825">[CALCITE-6825]
* Add support for ALL, SOME, ANY in RelToSqlConverter</a>. */
@Test void testSome() {
final String sql = "SELECT 1, \"gross_weight\" < SOME(SELECT \"gross_weight\" "
+ "FROM \"foodmart\".\"product\") AS \"t\" "
+ "FROM \"foodmart\".\"product\"";
final String expected = "SELECT 1, \"gross_weight\" < SOME (SELECT \"gross_weight\"\n"
+ "FROM \"foodmart\".\"product\") AS \"t\"\nFROM \"foodmart\".\"product\"";
sql(sql).ok(expected);
}

@Test void testAll() {
final String sql = "SELECT 1, \"gross_weight\" < ALL(SELECT \"gross_weight\" "
+ "FROM \"foodmart\".\"product\") AS \"t\" "
+ "FROM \"foodmart\".\"product\"";
final String expected = "SELECT 1, \"gross_weight\" < ALL (SELECT \"gross_weight\"\n"
+ "FROM \"foodmart\".\"product\") AS \"t\"\nFROM \"foodmart\".\"product\"";
sql(sql).ok(expected);
}

/** Fluid interface to run tests. */
static class Sql {
private final CalciteAssert.SchemaSpec schemaSpec;
Expand Down