linrrzqqq opened a new pull request, #71442:
URL: https://github.com/apache/doris/pull/71442
### Background
Higher-order functions in Nereids receive a `Lambda` expression followed by
its input columns. Before function signature matching, `ExpressionAnalyzer`
extracts the input columns into `ArrayItemReference` nodes inside the `Lambda`
closure and replaces the function children with the bound `Lambda` itself, so
`FunctionSignature` only sees a single `LambdaType` argument.
### What problem does this PR solve?
Issue Number: None
Related PR: #66968
PR Type: Bugfix
Problem Summary:
On `branch-4.2`, `Lambda.makeArguments()` assumes zip-style binding whenever
`inputs.size() == lambda.getLambdaArgumentNames().size()` and only enters the
`array_sort` comparator branch when those counts differ. Calling `array_sort`
with a binary comparator `(x, y) -> ...` and two array arguments bypasses arity
validation, binds `y` to the extra array, and propagates the extra array's
nullability to the return type while BE `ArraySortFunction::execute` reads the
outer null map only from the first array—crashing BE with `SIGSEGV` when the
extra array is nullable (e.g., `CAST(NULL AS ARRAY<INT>)`).
This PR cherry-picks the `LambdaBindingSpec` registration framework from
#66968 (commit `a10c799e787`) so each higher-order builtin registers and
enforces its own SQL-level binding rule at analysis time:
```text
Before (branch-4.2):
array_sort((x, y) -> f(x, y), arr1, extra_arr)
--> inputs.size() == 2 == lambda.args.size()
--> skips array_sort arity check, binds y -> extra_arr
--> ArraySort.nullable() = arr1.nullable || extra_arr.nullable
--> extra_arr is Nullable, arr1 is non-Nullable -> BE SIGSEGV at
ColumnNullable::create
After (this PR):
BuiltinScalarFunctions registers array_sort with
LambdaBindingSpecs.ARRAY_COMPARATOR
--> ExpressionAnalyzer looks up FunctionBuilder.getLambdaBindingSpec()
before binding lambda
--> ARRAY_COMPARATOR enforces: inputs.size() == 1 && lambda.args.size()
== 2
--> rejects extra arguments in FE with AnalysisException
```
#### Scope & Compatibility Notes
- **Partial backport scope**: #66968 introduced both the `LambdaBindingSpec`
binding framework and six `MAP` lambda functions (`map_filter`, `map_exists`,
`map_all`, `map_apply`, `transform_keys`, `transform_values`). Because the
`MAP` lambda feature still has unresolved issues on `master` (e.g., join
cross-side `MAP` lambda NPEs and `DECIMALV3` K/V coercion issues), this PR
backports only the `LambdaBindingSpec` framework and converts the existing 14
`ARRAY` higher-order functions to `lambdaScalar(...)`, excluding all `MAP`
lambda functions and BE changes.
- **SQL compatibility**: Valid `ARRAY` higher-order queries preserve
identical semantics and `ExpressionAnalyzer`'s lexical scope resolution
(including `shouldPrioritizeRelationQualifier()`). Two classes of previously
unchecked invalid SQL now fail fast during FE analysis:
1. Calling `array_sort` with a binary comparator lambda and more than one
array argument now raises `AnalysisException: the lambda must be a binary
comparator lambda`.
2. Passing a lambda expression to a non-higher-order function now fails at
binding time with `AnalysisException: Function <name> does not support lambda
arguments`.
- **Forward pick compatibility**: Future backporting of the `MAP` lambda
portion of #66968 can directly add `MAP_ENTRIES` in `LambdaBindingSpecs` and
register the six `MAP` functions in `BuiltinScalarFunctions` without touching
`FunctionBuilder`, `BuiltinFunctionBuilder`, `FunctionHelper`, `Lambda`, or
`ExpressionAnalyzer` again.
### Details
Reproducer SQL executed against `branch-4.2` before and after this PR:
```sql
-- 1. Valid binary comparator call
SELECT ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2]);
-- 2. Invalid extra non-null array argument
SELECT ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2], [9, 8]);
-- 3. Invalid extra nullable array argument (crashes BE on unpatched
branch-4.2)
SELECT ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2], CAST(NULL AS
ARRAY<INT>));
```
Before (unpatched `branch-4.2` FE + latest `branch-4.2` BE):
```text
mysql> SELECT ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2]);
+----------------------------------------------+
| ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2]) |
+----------------------------------------------+
| [1, 2, 3] |
+----------------------------------------------+
mysql> SELECT ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2], [9, 8]);
+------------------------------------------------------+
| ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2], [9, 8]) |
+------------------------------------------------------+
| [1, 2, 3] |
+------------------------------------------------------+
mysql> SELECT ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2], CAST(NULL AS
ARRAY<INT>));
ERROR 1105 (HY000) at line 1: RpcException, msg: send fragments failed.
io.grpc.StatusRuntimeException: UNAVAILABLE: io exception, host: 127.0.0.1
```
BE `be.out` on Before query 3:
```text
*** SIGSEGV address not mapped to object (@0x0) received by PID 2871421 (TID
2874176 OR 0x14d9c020a640) from PID 0; stack trace: ***
4# doris::(anonymous
namespace)::check_nullable_null_map_column(doris::IColumn const&) at
be/src/core/column/column_nullable.cpp:36
5# doris::(anonymous namespace)::check_nullable_sizes(doris::IColumn
const&, doris::IColumn const&) at be/src/core/column/column_nullable.cpp:52
6# doris::ColumnNullable::ColumnNullable(...) at
be/src/core/column/column_nullable.cpp:97
8# doris::ArraySortFunction::execute(...) at
be/src/exprs/lambda_function/varray_sort_function.cpp:271
```
After (this PR):
```text
mysql> SELECT ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2]);
+----------------------------------------------+
| ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2]) |
+----------------------------------------------+
| [1, 2, 3] |
+----------------------------------------------+
mysql> SELECT ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2], [9, 8]);
ERROR 1105 (HY000) at line 1: errCode = 2, detailMessage = the lambda must
be a binary comparator lambda
mysql> SELECT ARRAY_SORT((x, y) -> SIGN(x - y), [3, 1, 2], CAST(NULL AS
ARRAY<INT>));
ERROR 1105 (HY000) at line 1: errCode = 2, detailMessage = the lambda must
be a binary comparator lambda
```
### Testing
- Built FE (`bash build.sh --fe -j50`) with Checkstyle enabled and verified
the SQL reproducer on a local 1-FE/1-BE cluster (`branch-4.2` BE + patched FE).
- Ran FE unit tests (`./run-fe-ut.sh --run
org.apache.doris.nereids.rules.analysis.FunctionRegistryTest`): 7 tests run, 0
failures, 0 errors, 0 skipped.
### Release note
Reject extra or mismatched arguments to lambda-based higher-order functions
such as `array_sort` during Nereids query analysis instead of misbinding them
and crashing BE on nullable inputs.
### Check List (For Author)
- Behavior changed: Yes (see Details)
- Does this need documentation: No
- Pre-submission status: Ready
### AI Disclosure
- Tool: Antigravity
- Generated: Partial cherry-pick adaptation, unit test case, and PR
description draft
- Human review: Reviewed code diff, conflict resolution on
`ExpressionAnalyzer`, FE unit tests, and cluster reproducer outputs
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]