This is an automated email from the ASF dual-hosted git repository.

morrySnow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 4b517059e86 [fix](subquery) Reject subqueries in lambda expressions 
(#70634)
4b517059e86 is described below

commit 4b517059e86de6c2721ccc0dc82c45ecb3ff3787
Author: morrySnow <[email protected]>
AuthorDate: Fri Nov 20 15:40:25 2026 +0800

    [fix](subquery) Reject subqueries in lambda expressions (#70634)
    
    ### Background
    
    Nereids binds higher-order function bodies in a lambda scope and
    rewrites SQL subqueries into relational plans. Lambda parameters exist
    only while evaluating individual collection elements.
    
    ### What problem does this PR solve?
    
    Issue Number: close #70002
    
    Problem Summary:
    
    Subqueries inside lambda bodies can fail during planning with a
    null-pointer exception and an empty client error, or a misleading
    unknown-column error. Reject all subqueries in the body before binding
    and return `Subqueries are not supported in lambda expressions`.
    Subqueries outside the body, including those supplying array arguments,
    remain accepted.
    
    ### Details
    
    - **Frequency:** Deterministic for affected shapes such as `x IN (SELECT
    ...)` and `EXISTS (... WHERE v = x)` inside a lambda.
    - **Root cause:** The lambda binder lacked a subquery legality check.
    Apply/Join rewriting can move predicates outside the scope that supplies
    the lambda parameter.
    - **Why existing tests missed it:** Existing function-binding tests
    cover nested captures and comparators, but not subqueries inside lambda
    bodies.
    - **Fix:** Check the unbound body for `SubqueryExpr` using the
    expression tree's cached type metadata, before binding, constant
    folding, or name resolution.
    - **Other paths and branches:** The shared entry covers array functions,
    map functions, comparators, nested lambdas, scalar subqueries, and
    IN/EXISTS with their negated forms. No release branch is changed.
    - **Behavior change:** Uncorrelated scalar subqueries that previously
    happened to work inside lambda bodies are also rejected. Ordinary IN
    lists and subqueries used as enclosing array arguments remain accepted.
    
    ### Release note
    
    Subqueries inside lambda bodies now return a clear unsupported-syntax
    error instead of planner errors or inconsistent acceptance. This
    includes uncorrelated scalar subqueries. Subqueries used outside the
    lambda body, such as array input arguments, remain supported.
---
 .../nereids/rules/analysis/ExpressionAnalyzer.java |  6 +++
 .../nereids/rules/analysis/BindFunctionTest.java   | 41 +++++++++++++++
 .../test_lambda_subquery_rejected.groovy           | 58 ++++++++++++++++++++++
 3 files changed, 105 insertions(+)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java
index f67ac12251a..1a2e1fa8af3 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java
@@ -67,6 +67,7 @@ import org.apache.doris.nereids.trees.expressions.Or;
 import org.apache.doris.nereids.trees.expressions.Placeholder;
 import org.apache.doris.nereids.trees.expressions.Slot;
 import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.SubqueryExpr;
 import org.apache.doris.nereids.trees.expressions.Variable;
 import org.apache.doris.nereids.trees.expressions.WhenClause;
 import org.apache.doris.nereids.trees.expressions.WindowExpression;
@@ -476,6 +477,11 @@ public class ExpressionAnalyzer extends 
SubExprAnalyzer<ExpressionRewriteContext
 
     private Expression analyzeLambdaFunction(Expression lambdaFunction,
             List<Slot> boundedSlots, ExpressionRewriteContext context) {
+        // Check the body before binding can fold subqueries or resolve names 
in the lambda scope.
+        // Subqueries supplying the enclosing function's array inputs are 
outside this body.
+        if (lambdaFunction.containsType(SubqueryExpr.class)) {
+            throw new AnalysisException("Subqueries are not supported in 
lambda expressions");
+        }
         ExpressionAnalyzer enclosingAnalyzer = this;
         ExpressionAnalyzer lambdaAnalyzer = new 
ExpressionAnalyzer(currentPlan, new Scope(Optional.of(getScope()),
                 boundedSlots), context == null ? null : 
context.cascadesContext,
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/BindFunctionTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/BindFunctionTest.java
index 2761ebe6e26..eea082fb2eb 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/BindFunctionTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/BindFunctionTest.java
@@ -152,6 +152,47 @@ public class BindFunctionTest extends TestWithFeService 
implements MemoPatternMa
         PlanChecker.from(connectContext).analyze("SELECT array_sort((x, y) -> 
x - y, arr2) FROM t_arr");
     }
 
+    @Test
+    void testRejectSubqueriesInLambda() {
+        List<String> sqls = ImmutableList.of(
+                "SELECT array_exists(x -> x IN (SELECT col2 FROM t1), arr2) 
FROM t_arr",
+                "SELECT array_exists(x -> x NOT IN (SELECT col2 FROM t1), 
arr2) FROM t_arr",
+                "SELECT array_filter(x -> x IN (SELECT col2 FROM t1), [1, 2])",
+                "SELECT array_exists(x -> EXISTS (SELECT 1 FROM t1 WHERE col2 
= x), arr2) FROM t_arr",
+                "SELECT array_exists(x -> NOT EXISTS (SELECT 1 FROM t1 WHERE 
col2 = x), arr2) FROM t_arr",
+                "SELECT array_exists(x -> EXISTS (SELECT 1 FROM t1), arr2) 
FROM t_arr",
+                "SELECT array_exists(x -> EXISTS (SELECT count(*) FROM t1), 
arr2) FROM t_arr",
+                "SELECT array_map(x -> x + (SELECT max(col2) FROM t1), arr2) 
FROM t_arr",
+                "SELECT array_map(x -> (SELECT max(col2) FROM t1 WHERE col2 = 
x), arr2) FROM t_arr",
+                "SELECT array_map(x -> (SELECT max(col2) FROM t1 WHERE col2 > 
t_arr.id), arr2) FROM t_arr",
+                "SELECT array_map(x -> if(false, (SELECT 1), x), [1, 2])",
+                "SELECT array_map(x -> array_map(y -> y + (SELECT 1), [1]), 
[1])",
+                "SELECT array_map(x -> array_map(y -> y, (SELECT [1])), [1])",
+                "SELECT array_sort((x, y) -> (SELECT 1), [1, 2])",
+                "SELECT map_filter((k, v) -> EXISTS (SELECT 1), map(1, 2))",
+                "SELECT * FROM t_arr WHERE array_exists(x -> x IN (SELECT col2 
FROM t1), arr2)"
+        );
+        for (String sql : sqls) {
+            AnalysisException exception = 
Assertions.assertThrows(AnalysisException.class,
+                    () -> PlanChecker.from(connectContext).analyze(sql), sql);
+            Assertions.assertEquals("Subqueries are not supported in lambda 
expressions",
+                    exception.getMessage(), sql);
+        }
+    }
+
+    @Test
+    void testSubqueriesOutsideLambdaBody() {
+        List<String> sqls = ImmutableList.of(
+                "SELECT array_map(x -> x + 1, (SELECT array_agg(col2) FROM 
t1))",
+                "SELECT array_filter(x -> x IN (1, 2), (SELECT array_agg(col2) 
FROM t1))",
+                "SELECT (SELECT array_map(x -> x + 1, [1, 2]))",
+                "SELECT array_map(x -> array_map(y -> x + y, [1]), [1])"
+        );
+        for (String sql : sqls) {
+            PlanChecker.from(connectContext).analyze(sql).rewrite();
+        }
+    }
+
     private static Lambda innermostLambda(Plan plan) {
         List<Lambda> innermostLambdas = plan.<Plan>collect(node -> 
true).stream()
                 .flatMap(node -> node.getExpressions().stream())
diff --git 
a/regression-test/suites/query_p0/sql_functions/array_functions/test_lambda_subquery_rejected.groovy
 
b/regression-test/suites/query_p0/sql_functions/array_functions/test_lambda_subquery_rejected.groovy
new file mode 100644
index 00000000000..dcbd554a213
--- /dev/null
+++ 
b/regression-test/suites/query_p0/sql_functions/array_functions/test_lambda_subquery_rejected.groovy
@@ -0,0 +1,58 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("test_lambda_subquery_rejected") {
+    // Reject every subquery kind before binding or folding the lambda body.
+    def rejectedQueries = [
+        "SELECT array_exists(x -> x IN (SELECT 1), [1, 2])",
+        "SELECT array_exists(x -> x NOT IN (SELECT 1), [1, 2])",
+        "SELECT array_filter(x -> x IN (SELECT 'evil.com'), ['evil.com', 
'ok.com'])",
+        "SELECT array_exists(x -> EXISTS (SELECT 1 WHERE x = 1), [1, 2])",
+        "SELECT array_exists(x -> NOT EXISTS (SELECT 1 WHERE x = 1), [1, 2])",
+        "SELECT array_exists(x -> EXISTS (SELECT 1), [1, 2])",
+        "SELECT array_exists(x -> EXISTS (SELECT count(*) FROM (SELECT 1) t), 
[1, 2])",
+        "SELECT array_map(x -> concat(x, (SELECT 'suffix')), ['a', 'b'])",
+        "SELECT array_map(x -> (SELECT x), [1, 2])",
+        "SELECT array_map(x -> (SELECT 1 WHERE t.s > 0), a) FROM (SELECT 1 AS 
s, [1, 2] AS a) t",
+        "SELECT array_map(x -> if(false, (SELECT 1), x), [1, 2])",
+        "SELECT array_map(x -> array_map(y -> y + (SELECT 1), [1]), [1])",
+        "SELECT array_map(x -> array_map(y -> y, (SELECT [1])), [1])",
+        "SELECT array_sort((x, y) -> (SELECT 1), [1, 2])",
+        "SELECT map_filter((k, v) -> EXISTS (SELECT 1), map(1, 2))",
+        "SELECT 1 WHERE array_exists(x -> x IN (SELECT 1), [1, 2])"
+    ]
+    rejectedQueries.each { query ->
+        test {
+            sql query
+            exception "Subqueries are not supported in lambda expressions"
+        }
+    }
+
+    // A subquery outside the body may still supply the array or contain a 
lambda.
+    explain {
+        sql "SELECT array_map(x -> x + 1, (SELECT [1, 2]))"
+        contains "PLAN FRAGMENT"
+    }
+    explain {
+        sql "SELECT array_filter(x -> x IN (1, 2), (SELECT [1, 2, 3]))"
+        contains "PLAN FRAGMENT"
+    }
+    explain {
+        sql "SELECT (SELECT array_map(x -> x + 1, [1, 2]))"
+        contains "PLAN FRAGMENT"
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to