Skip to content
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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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,42 @@ public class SqlQuantifyOperator extends SqlInOperator {
}
return null;
}

@Override public SqlOperator not() {
Copy link
Contributor

Choose a reason for hiding this comment

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

how is this change related to the issue?

Copy link
Author

Choose a reason for hiding this comment

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

When <All convertTo NOT(>= SOME), it is used for toSql.
e.g.
The test on RelToSqlConverterTest#testAll this commit
SQL: SELECT 1, "gross_weight" < ALL(SELECT "gross_weight" FROM "foodmart"."product") AS "t" FROM "foodmart"."product"
Converted rel:

rel#10:LogicalProject.NONE.[](input=JdbcTableScan#7,exprs=[1, NOT(>= SOME($6, {
LogicalProject(gross_weight=[$6])
  JdbcTableScan(table=[[foodmart, product]])
}))])

ToSql:
call SqlImplementor.Context#toSql(RexProgram,RexNode):

 case NOT:
        RexNode operand = ((RexCall) rex).operands.get(0);
        final SqlNode node = toSql(program, operand);
        final SqlOperator inverseOperator = getInverseOperator(operand); //line 802

getInverseOperator use the function not.

If not done, it will throw unexpected SOME.

java.lang.AssertionError: unexpected SOME
	at org.apache.calcite.sql.fun.SqlInOperator.of(SqlInOperator.java:98)
	at org.apache.calcite.sql.fun.SqlInOperator.not(SqlInOperator.java:84)
	at org.apache.calcite.rel.rel2sql.SqlImplementor$Context.getInverseOperator(SqlImplementor.java:914)
	at org.apache.calcite.rel.rel2sql.SqlImplementor$Context.toSql(SqlImplementor.java:802)
	at org.apache.calcite.rel.rel2sql.RelToSqlConverter.visit(RelToSqlConverter.java:460)

if (kind == SqlKind.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);
}
} else {
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9307,6 +9307,24 @@ private void checkLiteral2(String expression, String expected) {
}


@Test void testSome() {
final String sql = "SELECT 1, \"gross_weight\" < SOME(SELECT \"gross_weight\" "
Copy link
Contributor

Choose a reason for hiding this comment

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

Test cases should include ANY. Can we directly rewrite SqlQuantifyOpenator?

+ "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