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
13 changes: 0 additions & 13 deletions core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
import org.apache.calcite.rex.RexExecutorImpl;
import org.apache.calcite.rex.RexFieldAccess;
import org.apache.calcite.rex.RexInputRef;
import org.apache.calcite.rex.RexLambda;
import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.rex.RexLocalRef;
import org.apache.calcite.rex.RexNode;
Expand Down Expand Up @@ -3371,12 +3370,6 @@ private static RexShuttle pushShuttle(final Project project) {
@Override public RexNode visitInputRef(RexInputRef ref) {
return project.getProjects().get(ref.getIndex());
}

@Override public RexNode visitLambda(RexLambda lambda) {
// Lambda body references are at a different scope level.
// Do not remap indices inside lambda body against this project.
return lambda;
}
};
}

Expand All @@ -3400,12 +3393,6 @@ private static RexShuttle pushShuttle(final Calc calc) {
@Override public RexNode visitInputRef(RexInputRef ref) {
return projects.get(ref.getIndex());
}

@Override public RexNode visitLambda(RexLambda lambda) {
// Lambda body references are at a different scope level.
// Do not remap indices inside lambda body against this calc.
return lambda;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,34 @@ public static void checkActualAndReferenceFiles() {

}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7718">[CALCITE-7718]
* Lambda capturing ROW field crashes at compilation with assertion
* failure</a>. */
@Test void testLambdaExpressionWithStructCaptureMerge() {
final String sql = "select \"EXISTS\"(array(1, 2), x -> x = t.r.\"EXPR$0\")\n"
+ "from (select ROW(1, 2) as r) as t";
fixture()
.withFactory(c ->
c.withOperatorTable(t -> SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
.withSql(sql)
.ok();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7718">[CALCITE-7718]
* Lambda capturing ROW field crashes at compilation with assertion
* failure</a>. */
@Test void testLambdaExpressionWithStructCaptureMergeOverScan() {
final String sql = "select \"EXISTS\"(array(1, 2), x -> x = t.r.\"EXPR$0\")\n"
+ "from (select ROW(deptno, sal) as r from emp) as t";
fixture()
.withFactory(c ->
c.withOperatorTable(t -> SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
.withSql(sql)
.ok();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-3679">[CALCITE-3679]
* Allow lambda expressions in SQL queries</a>. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5151,6 +5151,30 @@ LogicalProject(EXPR$0=[HIGHER_ORDER_FUNCTION($7, (X, DEPTNO) -> +(DEPTNO, 1))])
<![CDATA[
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3, 4), (N) -> OR(=(N, 1), =(N, 3)))])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
</TestCase>
<TestCase name="testLambdaExpressionWithStructCaptureMerge">
<Resource name="sql">
<![CDATA[select "EXISTS"(array(1, 2), x -> x = t.r."EXPR$0")
from (select ROW(1, 2) as r) as t]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2), (X) -> =(X, ROW(1, 2).EXPR$0))])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
</TestCase>
<TestCase name="testLambdaExpressionWithStructCaptureMergeOverScan">
<Resource name="sql">
<![CDATA[select "EXISTS"(array(1, 2), x -> x = t.r."EXPR$0")
from (select ROW(deptno, sal) as r from emp) as t]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2), (X) -> =(X, ROW($7, $5).EXPR$0))])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down
26 changes: 26 additions & 0 deletions core/src/test/resources/sql/lambda.iq
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,29 @@ select "EXISTS"(array(1, 2, 3), x -> "EXISTS"(array(1, 2, 3), y -> x + y = 4)) a
(1 row)

!ok

# [CALCITE-7718] Lambda capturing ROW field crashes at compilation with
# assertion failure.
# Lambda captures a field of a struct value from the enclosing query.
select "EXISTS"(array(1, 2), x -> x = t.r."EXPR$0") from (select ROW(1, 2) as r) as t;
+--------+
| EXPR$0 |
+--------+
| true |
+--------+
(1 row)

!ok

# Same, but the captured struct is built from table columns
# Jane is in dept 10
select "EXISTS"(array(5, 10), x -> x = t.r."EXPR$0")
from (select ROW(deptno, 1) as r from emp where ename = 'Jane') as t;
+--------+
| EXPR$0 |
+--------+
| true |
+--------+
(1 row)

!ok
Loading