-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-6242] Enhance lambda closure #4926
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
base: main
Are you sure you want to change the base?
Changes from all commits
b81a5ad
265a32d
ca02265
a705f79
7170bf6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8115,7 +8115,10 @@ void testGroupExpressionEquivalenceParams() { | |
|
|
||
| /** Test case for | ||
| * <a href="https://issues.apache.org/jira/browse/CALCITE-3679">[CALCITE-3679] | ||
| * Allow lambda expressions in SQL queries</a>. */ | ||
| * Allow lambda expressions in SQL queries</a>. | ||
| * <a href="https://issues.apache.org/jira/browse/CALCITE-6242">[CALCITE-6242] | ||
| * Enhance lambda closure parsing</a>. | ||
| * */ | ||
| @Test void testHigherOrderFunction() { | ||
| final SqlValidatorFixture s = fixture() | ||
| .withOperatorTable(MockSqlOperatorTable.standard().extend()); | ||
|
|
@@ -8129,6 +8132,10 @@ void testGroupExpressionEquivalenceParams() { | |
| .type("RecordType(INTEGER NOT NULL EXPR$0) NOT NULL"); | ||
| s.withSql("select HIGHER_ORDER_FUNCTION2(1, () -> 0.1)") | ||
| .type("RecordType(INTEGER NOT NULL EXPR$0) NOT NULL"); | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1 + ^emp.deptno^) from emp") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add jira message in test.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| .ok(); | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1 + ^deptno^) from emp") | ||
| .ok(); | ||
|
|
||
| // test for type check | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> ^x + 1^)") | ||
|
|
@@ -8146,13 +8153,70 @@ void testGroupExpressionEquivalenceParams() { | |
| .fails("Cannot apply '(?s).*HIGHER_ORDER_FUNCTION' to arguments of type " | ||
| + "'HIGHER_ORDER_FUNCTION\\(<INTEGER>, <FUNCTION\\(ANY, ANY, ANY\\) -> ANY>\\)'.*"); | ||
|
|
||
| // test for illegal parameters | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1 + ^emp.deptno^) from emp") | ||
| .fails("Param 'EMP\\.DEPTNO' not found in lambda expression " | ||
| + "'\\(`X`, `Y`\\) -> `X` \\+ 1 \\+ `EMP`\\.`DEPTNO`'"); | ||
| } | ||
|
|
||
| /** Test case for lambda closure conformance checking. | ||
| * Tests that lambda expressions can or cannot access variables from enclosing | ||
| * scopes based on the SQL conformance level. | ||
| * <a href="https://issues.apache.org/jira/browse/CALCITE-6242">[CALCITE-6242] | ||
| * Enhance lambda closure parsing</a>. | ||
| * */ | ||
| @Test void testLambdaClosureConformance() { | ||
| final SqlValidatorFixture s = fixture() | ||
| .withOperatorTable(MockSqlOperatorTable.standard().extend()); | ||
|
|
||
| // Lambda accessing outer scope variable (closure) | ||
| // In DEFAULT conformance, closure is allowed | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1 + deptno) from emp") | ||
| .withConformance(SqlConformanceEnum.DEFAULT) | ||
| .ok(); | ||
|
|
||
| // In STRICT_92, closure is NOT allowed | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1 + ^deptno^) from emp") | ||
| .withConformance(SqlConformanceEnum.STRICT_92) | ||
| .fails("Lambda closure is not allowed in this conformance: " | ||
| + "reference to 'DEPTNO' from enclosing scope"); | ||
|
|
||
| // In STRICT_99, closure is NOT allowed | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1 + ^deptno^) from emp") | ||
| .fails("Param 'DEPTNO' not found in lambda expression " | ||
| + "'\\(`X`, `Y`\\) -> `X` \\+ 1 \\+ `DEPTNO`'"); | ||
| .withConformance(SqlConformanceEnum.STRICT_99) | ||
| .fails("Lambda closure is not allowed in this conformance: " | ||
| + "reference to 'DEPTNO' from enclosing scope"); | ||
|
|
||
| // In STRICT_2003, closure is NOT allowed | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1 + ^deptno^) from emp") | ||
| .withConformance(SqlConformanceEnum.STRICT_2003) | ||
| .fails("Lambda closure is not allowed in this conformance: " | ||
| + "reference to 'DEPTNO' from enclosing scope"); | ||
|
|
||
| // In BABEL conformance, closure is allowed | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1 + deptno) from emp") | ||
| .withConformance(SqlConformanceEnum.BABEL) | ||
| .ok(); | ||
|
|
||
| // In LENIENT conformance, closure is allowed | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1 + deptno) from emp") | ||
| .withConformance(SqlConformanceEnum.LENIENT) | ||
| .ok(); | ||
|
|
||
| // Lambda using only its own parameters (no closure) - should always work | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1) from emp") | ||
| .withConformance(SqlConformanceEnum.STRICT_92) | ||
| .ok(); | ||
|
|
||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> y) from emp") | ||
| .withConformance(SqlConformanceEnum.STRICT_92) | ||
| .ok(); | ||
|
|
||
| // Test with qualified column name in closure | ||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1 + emp.deptno) from emp") | ||
| .withConformance(SqlConformanceEnum.DEFAULT) | ||
| .ok(); | ||
|
|
||
| s.withSql("select HIGHER_ORDER_FUNCTION(1, (x, y) -> x + 1 + ^emp.deptno^) from emp") | ||
| .withConformance(SqlConformanceEnum.STRICT_92) | ||
| .fails("Lambda closure is not allowed in this conformance: " | ||
| + "reference to 'EMP.DEPTNO' from enclosing scope"); | ||
| } | ||
|
|
||
| /** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-7193">[CALCITE-7193] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,3 +102,16 @@ select "EXISTS"(array[array[1, 2], array[3, 4]], x -> x[1] = 1); | |
| (1 row) | ||
|
|
||
| !ok | ||
|
|
||
| # [CALCITE-6242] Enhance lambda closure parsing | ||
| select * | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here. |
||
| from (select array(1, 2, 3) as arr) as t1 inner join | ||
| (select 1 as v) as t2 on "EXISTS"(arr, x -> x = t2.v); | ||
| +-----------+---+ | ||
| | ARR | V | | ||
| +-----------+---+ | ||
| | [1, 2, 3] | 1 | | ||
| +-----------+---+ | ||
| (1 row) | ||
|
|
||
| !ok | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could add some comments here to explain the reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, thanks