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 01d0be765b [CALCITE-7945] Lambda next to a window function produces an 
assertion failure
01d0be765b is described below

commit 01d0be765b4f682b3e1f05b38c595b485766bdfd
Author: Mihai Budiu <[email protected]>
AuthorDate: Tue Nov 17 10:42:52 2026 -0800

    [CALCITE-7945] Lambda next to a window function produces an assertion 
failure
---
 .../apache/calcite/rel/rules/CalcRelSplitter.java  | 14 ++++
 .../calcite/rel/rules/ProjectToWindowRule.java     |  5 ++
 .../org/apache/calcite/rex/RexProgramBuilder.java  | 24 +++++-
 core/src/test/resources/sql/lambda.iq              | 89 ++++++++++++++++++++++
 4 files changed, 129 insertions(+), 3 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/rel/rules/CalcRelSplitter.java 
b/core/src/main/java/org/apache/calcite/rel/rules/CalcRelSplitter.java
index 17e6333dc2..9c974d2344 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/CalcRelSplitter.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/CalcRelSplitter.java
@@ -30,6 +30,7 @@
 import org.apache.calcite.rex.RexDynamicParam;
 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;
@@ -827,6 +828,12 @@ protected RelType(String name) {
 
     protected abstract boolean canImplement(RexCall call);
 
+    /** Returns whether this type can compute a lambda, such as
+     * {@code (e) -> e = x}. The body of the lambda is computed with it. */
+    protected boolean canImplement(RexLambda lambda) {
+      return true;
+    }
+
     protected boolean supportsCondition() {
       return true;
     }
@@ -934,6 +941,13 @@ private static class ImplementTester extends 
RexVisitorImpl<Void> {
       }
       return null;
     }
+
+    @Override public Void visitLambda(RexLambda lambda) {
+      if (!relType.canImplement(lambda)) {
+        throw CannotImplement.INSTANCE;
+      }
+      return null;
+    }
   }
 
   /**
diff --git 
a/core/src/main/java/org/apache/calcite/rel/rules/ProjectToWindowRule.java 
b/core/src/main/java/org/apache/calcite/rel/rules/ProjectToWindowRule.java
index 14a333aace..8d2089bd14 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/ProjectToWindowRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/ProjectToWindowRule.java
@@ -32,6 +32,7 @@
 import org.apache.calcite.rex.RexCall;
 import org.apache.calcite.rex.RexDynamicParam;
 import org.apache.calcite.rex.RexFieldAccess;
+import org.apache.calcite.rex.RexLambda;
 import org.apache.calcite.rex.RexLiteral;
 import org.apache.calcite.rex.RexLocalRef;
 import org.apache.calcite.rex.RexNode;
@@ -256,6 +257,10 @@ static class WindowedAggRelSplitter extends 
CalcRelSplitter {
             return call instanceof RexOver;
           }
 
+          @Override protected boolean canImplement(RexLambda lambda) {
+            return false;
+          }
+
           @Override protected boolean supportsCondition() {
             return false;
           }
diff --git a/core/src/main/java/org/apache/calcite/rex/RexProgramBuilder.java 
b/core/src/main/java/org/apache/calcite/rex/RexProgramBuilder.java
index ebeb12aa60..7b498d63bd 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexProgramBuilder.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexProgramBuilder.java
@@ -910,9 +910,27 @@ private abstract class RegisterShuttle extends RexShuttle {
     }
 
     @Override public RexNode visitLambda(RexLambda lambda) {
-      // Lambda body references are at a different scope level.
-      // Do not validate or register lambda body indices against this 
program's input.
-      return lambda;
+      // The body reads the lambda parameters, thus it stays in the lambda.
+      final RegisterShuttle outer = this;
+      final RexNode body =
+          lambda.getExpression().accept(new RexShuttle() {
+            // A reference to an external value is rewritten:
+            // "e -> e = x" becomes "(E) -> =(E, $t0)".
+            @Override public RexNode visitInputRef(RexInputRef inputRef) {
+              return outer.visitInputRef(inputRef);
+            }
+
+            @Override public RexNode visitLocalRef(RexLocalRef localRef) {
+              return outer.visitLocalRef(localRef);
+            }
+
+            // A window aggregate cannot read the parameters, thus it is
+            // computed outside the lambda.
+            @Override public RexNode visitOver(RexOver over) {
+              return outer.visitOver(over);
+            }
+          });
+      return registerInternal(new RexLambda(lambda.getParameters(), body));
     }
   }
 
diff --git a/core/src/test/resources/sql/lambda.iq 
b/core/src/test/resources/sql/lambda.iq
index 693279fe28..593d96e347 100644
--- a/core/src/test/resources/sql/lambda.iq
+++ b/core/src/test/resources/sql/lambda.iq
@@ -559,3 +559,92 @@ order by "EXISTS"(a, e -> e = 2) desc, "EXISTS"(array[1, 
2], e -> e = 1);
 (2 rows)
 
 !ok
+
+# [CALCITE-7945] Lambda next to a window function produces an assertion
+# failure
+#
+# The results of the tests in this section were validated with Spark 3.5.9
+# and DuckDB 2.0.0, with the translation described for CALCITE-7933.
+
+# The lambda reads the column X, and the query has a window function.
+# Result validated with Spark 3.5.9 and DuckDB 2.0.0.
+select x, row_number() over (order by x) as rn,
+  "EXISTS"(array[2, 3], e -> e = x) as r
+from (values (1), (3), (2)) as t(x);
++---+----+-------+
+| X | RN | R     |
++---+----+-------+
+| 1 |  1 | false |
+| 2 |  2 | true  |
+| 3 |  3 | true  |
++---+----+-------+
+(3 rows)
+
+!ok
+
+# The array argument of the lambda reads the window result, and the lambda
+# reads the column Z, which no other expression reads.
+# Result validated with Spark 3.5.9 and DuckDB 2.0.0.
+select x,
+  "EXISTS"(array[row_number() over (order by x)], e -> e * 100 = z) as r
+from (values (1, 100), (2, 300), (3, 300)) as t(x, z);
++---+-------+
+| X | R     |
++---+-------+
+| 1 | true  |
+| 2 | false |
+| 3 | true  |
++---+-------+
+(3 rows)
+
+!ok
+
+# The same, but the inner lambda of a nested lambda reads the column Z.
+# Result validated with Spark 3.5.9 and DuckDB 2.0.0.
+select x,
+  "EXISTS"(array[row_number() over (order by x)],
+    e -> "EXISTS"(array[100], f -> f = e * 100 and f = z)) as r
+from (values (1, 100), (2, 300), (3, 300)) as t(x, z);
++---+-------+
+| X | R     |
++---+-------+
+| 1 | true  |
+| 2 | false |
+| 3 | false |
++---+-------+
+(3 rows)
+
+!ok
+
+# The lambda reads the window result RN of a sub-query.
+# Result validated with Spark 3.5.9 and DuckDB 2.0.0.
+select x, "EXISTS"(array[1, 2], e -> e = rn) as r
+from (select x, row_number() over (order by x) as rn
+      from (values (1), (3), (2)) as t(x)) as s;
++---+-------+
+| X | R     |
++---+-------+
+| 1 | true  |
+| 2 | true  |
+| 3 | false |
++---+-------+
+(3 rows)
+
+!ok
+
+# The same, but the inner lambda of a nested lambda reads RN.
+# Result validated with Spark 3.5.9 and DuckDB 2.0.0.
+select x,
+  "EXISTS"(array[1, 2], e -> "EXISTS"(array[0], f -> e + f = rn)) as r
+from (select x, row_number() over (order by x) as rn
+      from (values (1), (3), (2)) as t(x)) as s;
++---+-------+
+| X | R     |
++---+-------+
+| 1 | true  |
+| 2 | true  |
+| 3 | false |
++---+-------+
+(3 rows)
+
+!ok

Reply via email to