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

Conversation

wangdiao
Copy link

@wangdiao wangdiao commented Mar 4, 2025

  • Add support for ALL, SOME, ANY on subQuery like IN.
  • Add overrite not in SqlQuantifyOperator to NOT SOME/ALL.

JIRA: https://issues.apache.org/jira/browse/CALCITE-5100

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants