Skip to content

Commit 18a8aed

Browse files
dssysolyatinmihaibudiu
authored andcommitted
[CALCITE-7268] SqlToRelConverter throws exception if lambda contains IN
1 parent 82cdcc7 commit 18a8aed

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,6 +2100,9 @@ private void findSubQueries(
21002100
}
21012101
final SqlKind kind = node.getKind();
21022102
switch (kind) {
2103+
// A lambda has its own scope, which is not part of the blackboard.
2104+
case LAMBDA:
2105+
return;
21032106
case EXISTS:
21042107
case UNIQUE:
21052108
case SELECT:
@@ -2282,6 +2285,7 @@ private RexNode convertLambda(Blackboard bb, SqlNode node) {
22822285

22832286
final Blackboard lambdaBb = createBlackboard(scope, nameToNodeMap, false);
22842287
lambdaBb.setRoot(castNonNull(bb.inputs));
2288+
replaceSubQueries(lambdaBb, call.getExpression(), RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
22852289
final RexNode expr = lambdaBb.convertExpression(call.getExpression());
22862290
return rexBuilder.makeLambdaCall(expr, parameters);
22872291
}

core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ public static void checkActualAndReferenceFiles() {
143143
.ok();
144144
}
145145

146+
/** Test case for
147+
* <a href="https://issues.apache.org/jira/browse/CALCITE-7268">[CALCITE-7268]
148+
* SqlToRelConverter throws exception if lambda contains IN</a>. */
149+
@Test void testLambdaExpressionContainsIn() {
150+
final String sql = "select \"EXISTS\"(ARRAY[1,2,3,4], (n) -> n IN (1,3))";
151+
fixture()
152+
.withFactory(c ->
153+
c.withOperatorTable(t -> SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
154+
.withSql(sql)
155+
.ok();
156+
157+
}
158+
146159
/** Test case for
147160
* <a href="https://issues.apache.org/jira/browse/CALCITE-3679">[CALCITE-3679]
148161
* Allow lambda expressions in SQL queries</a>. */

core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4694,6 +4694,17 @@ LogicalProject(EXPR$0=[HIGHER_ORDER_FUNCTION2(1, () -> -1)])
46944694
<![CDATA[
46954695
LogicalProject(EXPR$0=[HIGHER_ORDER_FUNCTION($7, (X, DEPTNO) -> +(DEPTNO, 1))])
46964696
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
4697+
]]>
4698+
</Resource>
4699+
</TestCase>
4700+
<TestCase name="testLambdaExpressionContainsIn">
4701+
<Resource name="sql">
4702+
<![CDATA[select "EXISTS"(ARRAY[1,2,3,4], (n) -> n IN (1,3))]]>
4703+
</Resource>
4704+
<Resource name="plan">
4705+
<![CDATA[
4706+
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3, 4), (N) -> OR(=(N, 1), =(N, 3)))])
4707+
LogicalValues(tuples=[[{ 0 }]])
46974708
]]>
46984709
</Resource>
46994710
</TestCase>

0 commit comments

Comments
 (0)