Repository navigation
Conversation
… types A lambda used where a concrete value is expected (e.g. as the initial accumulator of arrayFold) is represented in the analyzer by a placeholder DataTypeFunction with no return type. The check at QueryAnalyzer::resolveFunction already rejects null entries in a lambda's argument types, but did not look through nested DataTypeFunction values for the same placeholder shape. When arrayFold copies the accumulator type into the synthetic outer lambda's argument list via getLambdaArgumentTypes, an unresolved init lambda would leak its placeholder DataTypeFunction(args=[nullptr...], return=nullptr) into the synthetic lambda's argument types. The follow-up resolveLambda would then type-check the body (e.g. arrayExists(__function_ref_arg_0, ...)) with that placeholder, and FunctionArrayMapped::getReturnTypeImpl would call removeNullable on a null DataTypePtr, segfaulting at DataTypeNullable.cpp:167. Extend the check to also reject nested DataTypeFunction values whose return type is null. The user-visible error is the same ILLEGAL_TYPE_OF_ARGUMENT already raised for the simpler case. Found by AST fuzzer (STID 1719-3ae5) on https://s3.amazonaws.com/clickhouse-test-reports/PRs/106261/6ce05d7b024f9c6b0cc589474e07333c06b7f3ef/ast_fuzzer_amd_debug_targeted/ (PR ClickHouse#106261).
|
cc @novikd @vdimir — could you review this? It extends the analyzer's "lambda where concrete value expected" check to also reject nested |
Pre-PR validation gateSession: (a) Deterministic repro? Yes. (b) Root cause explained? Any (c) Fix matches root cause? The existing check at (d) Test intent preserved / new tests added? A new test (e) Both directions demonstrated? Yes.
(f) Fix is general, not a narrow patch? Yes. The check sits in |
|
Workflow [PR], commit [e5178d2] Summary: ❌
AI ReviewSummaryThis PR rejects unresolved Final VerdictStatus: ✅ Approve |
When an unexpected `LambdaNode` argument's unresolved placeholder `DataTypeFunction` propagates into the expected lambda's argument types via `getLambdaArgumentTypes` (e.g. `arrayFold` copies the accumulator type into its lambda's argument list), the previous inner-types check reported the expected lambda's outer index, not the actual bad argument. Move detection to a pre-pass over `function_lambda_arguments_indexes` that runs immediately after `getLambdaArgumentTypes`. A LambdaNode position whose `DataTypeFunction` still has all-null argument types and no return type after that call is the unexpected one. Its original argument index now appears in the `ILLEGAL_TYPE_OF_ARGUMENT` diagnostic. The pre-pass also runs before `resolveLambda`, preserving the SIGSEGV protection: `FunctionArrayMapped::getReturnTypeImpl` no longer sees the null return type via `removeNullable`. Add `SET enable_analyzer = 1;` to `04307_arrayfold_unresolved_lambda_init.sql` — the fix lives in `QueryAnalyzer::resolveFunction`, and the old analyzer does not do the bare-function-to-lambda rewrite for `arrayFold(arrayExists, ...)` and reports an unrelated `UNKNOWN_IDENTIFIER`.
The pre-pass added in 32a2575 skipped DataTypeFunction values with an empty argument list, which let zero-argument lambda placeholders such as `() -> 1` reach `resolveLambda` and crash in `FunctionArrayMapped::getReturnTypeImpl` with SIGSEGV. Removing the empty-args early-out is sufficient: `std::all_of` returns true on an empty range, so a `DataTypeFunction([], nullptr)` is now classified as unresolved together with the non-empty case. Regression case added to 04307_arrayfold_unresolved_lambda_init.sql.
The new unresolved-lambda-placeholder pre-pass in `QueryAnalyzer::resolveFunction` (commits 14d5ddb and 32a2575) fires earlier than the downstream `NUMBER_OF_ARGUMENTS_DOESNT_MATCH` that the original null-arg-types guard from PR ClickHouse#96479 produced. The third `arrayFold` argument in this regression query is a lambda used as the initial accumulator (a value slot), which is now rejected upstream with the more semantically accurate `ILLEGAL_TYPE_OF_ARGUMENT` error ("does not expect a lambda expression as argument N"). CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106271&sha=02cfedf7885d75af9f9bb520923f28fa6d860a1c PR: ClickHouse#106271
|
Fixed the Fast test failure on |
The previous fixup updated the expected error code on this regression test from NUMBER_OF_ARGUMENTS_DOESNT_MATCH to ILLEGAL_TYPE_OF_ARGUMENT to match the new pre-pass in QueryAnalyzer::resolveFunction added by this PR. That expectation only holds under enable_analyzer = 1. Under enable_analyzer = 0 the query is still rejected by the old ActionsVisitor / FunctionArrayFold::getLambdaArgumentTypes path, which throws NUMBER_OF_ARGUMENTS_DOESNT_MATCH. The functional-test runner runs this test under the --analyzer variant with enable_analyzer = 0, so the previous expectation broke that variant. Pin the test to enable_analyzer = 0 and restore the original error code. This keeps the test focused on the DataTypeFunction::updateHashImpl null-argument regression it was originally written for, on the code path that exhibited the crash. New-analyzer coverage of the unresolved-lambda placeholder shape lives in the parallel test 04307_arrayfold_unresolved_lambda_init.sql. Bot review thread: ClickHouse#106271 Co-Authored-By: Claude Opus 4.7 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 19/22 (86.36%) | Lost baseline coverage: none · Uncovered code |
|
CI has fully finished. The change itself is green; the remaining red checks are unrelated to this PR:
All AST fuzzer variants pass, including Session: |
|
Superseded by #109840 (merged 2026-07-14), which fixes the same root cause more completely. This PR guarded only the new-analyzer path ( Verified on current master (e590300) that both crash families this PR tracked are fixed under both analyzers, returning
Closing in favor of the merged fix rather than rebasing the redundant subset. |
Passing a lambda where a concrete value is expected, for example as the initial accumulator of
arrayFold, used to crash the server withSIGSEGVinremoveNullablecalled fromFunctionArrayMapped::getReturnTypeImpl.Reproducer (found by the AST fuzzer, STID
1719-3ae5):arrayCount,arrayFirst, and the analogousWITH (a -> a + 1) AS f SELECT arrayFold(arrayExists, [[1, 2, 3]], f)shape crash the same way.Root cause: any
LambdaNodeused as a function argument is assigned a placeholderDataTypeFunction(args=[nullptr...], return=nullptr)inQueryAnalyzer::resolveFunction(resolveFunction.cppline ~1322). That placeholder is filled in only when the outer function treats the position as a lambda. ForarrayFold, the initial accumulator is not a lambda position, so its placeholder stays unresolved.arrayFold::getLambdaArgumentTypesthen copies the accumulator type into the synthetic outer lambda's argument list, letting the placeholder leak into another lambda's argument types. WhenresolveLambdatype-checks the body (the syntheticarrayExists(__function_ref_arg_0, __function_ref_arg_1)wrapper),FunctionArrayMapped::getReturnTypeImplcallsremoveNullableon the placeholder's nullreturn_typeand crashes atDataTypeNullable.cpp:167.Fix: the existing check at
resolveFunction.cppalready rejects null entries in a lambda's argument types withILLEGAL_TYPE_OF_ARGUMENT("does not expect a lambda expression as argument N"). Extend it to also reject nestedDataTypeFunctionvalues whose return type is null. The user-visible error matches the simpler case.CI report:
Closes: #109682
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a server crash when a lambda function is passed where a concrete value is expected by a higher-order function (for example
arrayFold(arrayExists, range(number), (acc, x) -> ...)). The query is now rejected with a clearILLEGAL_TYPE_OF_ARGUMENTerror.Documentation entry for user-facing changes