This is an automated email from the ASF dual-hosted git repository.
mihaibudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new fa951db4b9 [CALCITE-7497] Enable Lambda supports constant folding
fa951db4b9 is described below
commit fa951db4b900f16a74d087688096e98fb7dadf1e
Author: Terran <[email protected]>
AuthorDate: Wed Apr 29 10:21:43 2026 +0800
[CALCITE-7497] Enable Lambda supports constant folding
---
.../calcite/rel/rules/ReduceExpressionsRule.java | 13 +++
.../java/org/apache/calcite/rex/RexShuttle.java | 8 +-
.../org/apache/calcite/test/RelOptRulesTest.java | 89 ++++++++++++++++++
.../org/apache/calcite/test/RelOptRulesTest.xml | 102 +++++++++++++++++++++
4 files changed, 210 insertions(+), 2 deletions(-)
diff --git
a/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java
b/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java
index 88ffaf72fc..db93802118 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java
@@ -1189,6 +1189,19 @@ private void analyzeCall(RexCall call, Constancy
callConstancy) {
}
@Override public Void visitLambda(RexLambda lambda) {
+ // A lambda as a whole is not a constant (it contains lambda parameter
+ // references), but its body may contain constant sub-expressions (e.g.
+ // the "1 + 2" in "x -> x > 1 + 2"). Recursively analyze the body so
+ // that those inner constants are discovered and later replaced by
+ // RexReplacer (which already recurses into lambda bodies via
+ // RexShuttle.visitLambda).
+ final int stackSizeBefore = stack.size();
+ lambda.getExpression().accept(this);
+ // Discard whatever the body analysis pushed onto the stack – the lambda
+ // itself is not a constant from the outer context's point of view.
+ while (stack.size() > stackSizeBefore) {
+ stack.remove(stack.size() - 1);
+ }
return pushVariable();
}
diff --git a/core/src/main/java/org/apache/calcite/rex/RexShuttle.java
b/core/src/main/java/org/apache/calcite/rex/RexShuttle.java
index a3cf2b9d5e..d99ae12630 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexShuttle.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexShuttle.java
@@ -237,8 +237,12 @@ protected List<RexFieldCollation> visitFieldCollations(
}
@Override public RexNode visitLambda(RexLambda lambda) {
- lambda.getExpression().accept(this);
- return lambda;
+ RexNode oldBody = lambda.getExpression();
+ RexNode newBody = oldBody.accept(this);
+ if (newBody == oldBody) {
+ return lambda;
+ }
+ return new RexLambda(lambda.getParameters(), newBody);
}
@Override public RexNode visitLambdaRef(RexLambdaRef lambdaRef) {
diff --git a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
index 1b3b7295f7..e527029749 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -12321,4 +12321,93 @@ private void
checkLoptOptimizeJoinRule(LoptOptimizeJoinRule rule) {
.withRule(CoreRules.PROJECT_FILTER_VALUES_MERGE)
.checkUnchanged();
}
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7497">[CALCITE-7497]
+ * Enable Lambda supports constant folding</a>.
+ */
+ @Test void testReduceLambdaBodyConstantFolding() {
+ final String sql = "select \"EXISTS\"(ARRAY[1, 2, 3], x -> x > 1 + 2)";
+ sql(sql)
+ .withFactory(f ->
+ f.withOperatorTable(opTab ->
+ SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
+ .withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7497">[CALCITE-7497]
+ * Enable Lambda supports constant folding</a>.
+ *
+ * <p>Boolean constant expression {@code 1 < 2} in lambda body should be
+ * folded to {@code true}. */
+ @Test void testReduceLambdaBodyBooleanConstantFolding() {
+ final String sql = "select \"EXISTS\"(ARRAY[1, 2, 3], x -> x > 1 AND 1 <
2)";
+ sql(sql)
+ .withFactory(f ->
+ f.withOperatorTable(opTab ->
+ SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
+ .withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7497">[CALCITE-7497]
+ * Enable Lambda supports constant folding</a>.
+ *
+ * <p>Negative constant arithmetic {@code -1 * -2} in lambda body should be
+ * folded to {@code 2}. */
+ @Test void testReduceLambdaBodyNegativeConstantFolding() {
+ final String sql = "select \"EXISTS\"(ARRAY[1, 2, 3], x -> x > -1 * -2)";
+ sql(sql)
+ .withFactory(f ->
+ f.withOperatorTable(opTab ->
+ SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
+ .withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7497">[CALCITE-7497]
+ * Enable Lambda supports constant folding</a>.
+ *
+ * <p>MOD function with constant arguments {@code MOD(10, 3)} in lambda body
+ * should be folded to {@code 1}. */
+ @Test void testReduceLambdaBodyModConstantFolding() {
+ final String sql = "select \"EXISTS\"(ARRAY[1, 2, 3], x -> x > MOD(10,
3))";
+ sql(sql)
+ .withFactory(f ->
+ f.withOperatorTable(opTab ->
+ SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
+ .withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7497">[CALCITE-7497]
+ * Enable Lambda supports constant folding</a>.
+ *
+ * <p>OR-connected constant expressions {@code 1 + 2} and {@code 10 - 3}
+ * in lambda body should each be folded independently. */
+ @Test void testReduceLambdaBodyOrConstantFolding() {
+ final String sql = "select \"EXISTS\"(ARRAY[1, 2, 3], x -> x > 1 + 2 OR x
< 10 - 3)";
+ sql(sql)
+ .withFactory(f ->
+ f.withOperatorTable(opTab ->
+ SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
+ .withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7497">[CALCITE-7497]
+ * Enable Lambda supports constant folding</a>.
+ *
+ * <p>Deeply nested constant expression {@code (1 + 2) * (3 + 4)} in lambda
+ * body should be fully folded to {@code 21}. */
+ @Test void testReduceLambdaBodyDeepNestedConstantFolding() {
+ final String sql = "select \"EXISTS\"(ARRAY[1, 2, 3], x -> x > (1 + 2) *
(3 + 4))";
+ sql(sql)
+ .withFactory(f ->
+ f.withOperatorTable(opTab ->
+ SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
+ .withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
+ }
+
}
diff --git
a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
index b9add8436a..2d591dbd36 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -16930,6 +16930,108 @@ LogicalProject(DEPTNO=[$7], SAL=[$5])
<![CDATA[
LogicalProject(EXPR$0=[EXISTS(ARRAY(1), (X) -> true)])
LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testReduceLambdaBodyBooleanConstantFolding">
+ <Resource name="sql">
+ <![CDATA[select "EXISTS"(ARRAY[1, 2, 3], x -> x > 1 AND 1 < 2)]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> AND(>(X, 1), <(1, 2)))])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> AND(>(X, 1), true))])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testReduceLambdaBodyConstantFolding">
+ <Resource name="sql">
+ <![CDATA[select "EXISTS"(ARRAY[1, 2, 3], x -> x > 1 + 2)]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, +(1, 2)))])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, 3))])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testReduceLambdaBodyDeepNestedConstantFolding">
+ <Resource name="sql">
+ <![CDATA[select "EXISTS"(ARRAY[1, 2, 3], x -> x > (1 + 2) * (3 + 4))]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, *(+(1, 2), +(3,
4))))])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, 21))])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testReduceLambdaBodyModConstantFolding">
+ <Resource name="sql">
+ <![CDATA[select "EXISTS"(ARRAY[1, 2, 3], x -> x > MOD(10, 3))]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, MOD(10, 3)))])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, 1))])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testReduceLambdaBodyNegativeConstantFolding">
+ <Resource name="sql">
+ <![CDATA[select "EXISTS"(ARRAY[1, 2, 3], x -> x > -1 * -2)]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, *(-1, -2)))])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, 2))])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testReduceLambdaBodyOrConstantFolding">
+ <Resource name="sql">
+ <![CDATA[select "EXISTS"(ARRAY[1, 2, 3], x -> x > 1 + 2 OR x < 10 - 3)]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> OR(>(X, +(1, 2)), <(X,
-(10, 3))))])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> OR(>(X, 3), <(X, 7)))])
+ LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
</TestCase>