Skip to content

Backport #119317 to 26.6: Fix a crash and a LOGICAL_ERROR when trim(BOTH '' FROM x) is used where a function is expected - #119855

Merged
clickhouse-gh[bot] merged 3 commits into
26.6from
backport/26.6/119317
Sep 14, 2026
Merged

clickhouse-gh[bot] merged 3 commits into
26.6from
backport/26.6/119317

Conversation

@robot-ch-test-poll4

@robot-ch-test-poll4 robot-ch-test-poll4 commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Original pull-request #119317

This pull-request is a last step of an automated backporting.
Treat it as a standard pull-request: look at the checks and resolve conflicts.
Merge it only if you intend to backport changes to the target branch, otherwise just close it.

The PR source

The PR is created in the CI job

Version info

  • Merged into: 26.6.6.16

…BOTH '' FROM x)` is used where a function is expected
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-backport Changes, backported to release branch. Do not use manually - automated use only! pr-critical-bugfix labels Sep 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [BackportPR], commit [142abd1]

Summary: ✅

@groeneai

Copy link
Copy Markdown
Collaborator

05175_trim_empty_chars_is_a_function_call cannot pass on 26.6: it asserts two behaviours this branch predates

The cherry-picked fix itself is complete here: this PR's file list and per-file counts are
byte-identical to the master merge commit of #119317, the pre-image the parser change removes applied
cleanly, and 02864_statistics_ddl (which the same PR extended with the
STATISTICS(trim(BOTH '' FROM 'x')) case that produced the original Bad cast from DB::ASTLiteral to DB::ASTFunction) is green here. Only the new test file is unportable, in exactly two statements:

  1. .sql:19 ALTER TABLE t_alter MODIFY COLUMN c Int64 STATISTICS(trim(BOTH '' FROM 'x')) succeeds
    instead of raising INCORRECT_QUERY. Applying a column-level STATISTICS clause in
    ALTER ... ADD/MODIFY COLUMN arrived with c8f3a4781ae2ef4e995ade921810e00bdedb47f4; before it the
    clause is silently dropped, so no type validation runs and there is nothing to assert.
  2. .sql:47 CREATE TABLE ok (c Int64 STATISTICS(tdigest) CODEC(ZSTD(3)), ...) fails with
    Syntax error: failed at position 99 (CODEC). The order-agnostic column-modifier loop arrived with
    0208555a59626ca66364e1326e992b97b017d340; here the modifiers still have to be written in one fixed
    order, COMMENT, CODEC, STATISTICS, TTL, PRIMARY KEY, SETTINGS.

Both commits belong to #112788, merged to master on 2026-08-14, after the 26.7 branch cut and before
the 26.8 one: 26.8 has both, 26.3/26.6/26.7 have neither. That line also names statistics types that
are not registered everywhere, uniq_v2 on neither 26.3 nor 26.6 and basic not on 26.3, so fixing
the order alone still leaves it failing on the two older branches. The 26.8 arm is the control: the
one active branch with #112788, green on this test and already merged, while the other three fail it
deterministically.

Smallest edit that works identically on 26.3, 26.6 and 26.7, if the test is worth keeping there:

  • delete .sql:19 and .reference:22; line 18, ALTER TABLE t_alter ADD STATISTICS c TYPE trim(BOTH '' FROM 'x'), keeps the ALTER slot covered and passes here;
  • rewrite .sql:47 as CREATE TABLE ok (c Int64 CODEC(ZSTD(3)) STATISTICS(tdigest), d Int64 CODEC(NONE) STATISTICS(uniq, minmax), INDEX i c TYPE minmax) ENGINE = MergeTree ORDER BY c SETTINGS auto_statistics_types = 'tdigest, uniq';, put that same setting text in the fourth position(...)
    of .sql:48, and mirror both lines into .reference:64-65. Expected values stay 1 1 1 1 and the
    other three position(...) arguments stay as they are, since ASTColumnDeclaration::formatImpl here
    already prints CODEC before STATISTICS; tdigest, uniq and minmax are registered on all
    four branches.

If carrying the test here is not worth it, dropping the 05175 pair from this branch still leaves the
02864 case asserting the fix on it. Importing #112788 first, or declining this arm, are the other
two options, and those are yours rather than mine.

I hold triage only on this repository and maintainerCanModify is false on this head, so I cannot
apply the edit myself.

alexey-milovidov and others added 2 commits September 14, 2026 02:36
…ammar

The test was written against master and relied on two behaviours from
#112788 that 26.6 does not have: order-agnostic column modifiers (26.6
requires `CODEC` before `STATISTICS`, so `STATISTICS(tdigest) CODEC(ZSTD(3))`
is a syntax error) and the `uniq_v2` statistics type. Column-level
`STATISTICS` inside `ALTER TABLE ... MODIFY COLUMN` is also silently dropped
on 26.6, so the `INCORRECT_QUERY` it asserted there never fires; that line is
removed, as in the 26.3 and 26.7 backports. `auto_statistics_types` now uses
`tdigest, uniq`, which are registered on every release branch.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=119855&sha=6528da4a5f23523b6bf2410036824ea8132a8cc8&name_0=BackportPR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%2C%201%2F2%29
PR: #119855

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 @groeneai thank you for the exact diagnosis. Applied your smallest edit in be07204, the same shape as on the 26.3 and 26.7 arms: dropped the ALTER TABLE t_alter MODIFY COLUMN c Int64 STATISTICS(...) line, wrote CODEC before STATISTICS in CREATE TABLE ok, and switched auto_statistics_types to 'tdigest, uniq' because uniq_v2 is not registered on 26.6. The branch is also merged with the current 26.6 head (142abd1).

The other red, Stress test (amd_tsan) with Logical error: Not-ready Set is passed as the second argument for function (STID 0250-41a5, reached through a mapFilter lambda dry-run in ColumnFunction::reduce), is the long-running family tracked in #107619 and cannot be caused by a parser change to trim(BOTH '' FROM x). Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=119855&sha=6528da4a5f23523b6bf2410036824ea8132a8cc8&name_0=BackportPR&name_1=Stress%20test%20%28amd_tsan%29

@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=119855&sha=6528da4a5f23523b6bf2410036824ea8132a8cc8&name_0=BackportPR&name_1=Stress%20test%20%28amd_tsan%29 and provide a fix in a separate PR (or tell whether the fix for this carrier already exists and needs a 26.6 backport). If the fix is already in progress, link it here.

@groeneai

Copy link
Copy Markdown
Collaborator

No fix for this route exists on master or in any open PR, so there is nothing to backport to 26.6 yet. I will open one on master and link it here.

It is not a set-build failure but a lost dry-run flag, and it is specific to the map* higher-order functions. One query on master, no settings and no fuzzer:

SELECT mapFilter((k, v) -> k IN (SELECT 1), map(1, 2));
-- Logical error: 'Not-ready Set is passed as the second argument for function 'in''

SELECT arrayFilter(k -> k IN (SELECT 1), [1, 2]);
-- [1]

ActionsDAG::updateHeader folds a node whose arguments are all constant through evaluatePartialResult, which calls the function with dry_run = true. That is what lets FunctionIn::executeImplDryRun return a dummy for a set that is not built yet. FunctionArrayMapped overrides executeImplDryRun and threads the flag into executeImplCommon, which is why arrayFilter is correct. FunctionMapToArrayAdapter (src/Functions/array/FunctionsMapMiscellaneous.cpp) forwards getReturnTypeImpl, getLambdaArgumentTypes, executeImpl and the trait predicates, but not executeImplDryRun, so the default in IFunction.h falls back to the adapter's plain executeImpl, which reaches FunctionArrayMapped::executeImpl with its hardcoded dry_run = false. ColumnFunction::reduce(false) then evaluates the lambda body as a real execution and FunctionIn throws on the deferred set. The frames match the report.

The blast radius is exactly the adapter boundary: mapFilter, mapApply, mapExists, mapAll, mapSort, mapPartialSort and mapReverseSort all abort, while arrayFilter, arrayMap and arraySort return the right answer. A release build returns Code: 49 for that valid input instead of aborting, so the abort is the debug and sanitizer path the stress test hit.

On the carrier: the binary I reproduced on is master at 195780d64, which predates the #119317 merge (formatQuery($$SELECT trim(BOTH '' FROM 'x')$$) there still gives SELECT 'x'), so the failure is reachable without the parser change. #102308 does not cover this route either: there an in-place set build fails and the plan is lost, here the set is merely deferred and a legitimate dry run is misreported as a real execution.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 @groeneai thank you, that root cause (FunctionMapToArrayAdapter not forwarding executeImplDryRun, so the lambda dry-run degrades into a real execution against a deferred set) is convincing and clearly independent of this parser backport. The Stress test (amd_tsan) re-run on 142abd1 is green, so nothing further is needed on this branch. Please link the master fix here once it is open; it can then be backported to 26.6 separately.

@clickhouse-gh
clickhouse-gh Bot merged commit 40676b3 into 26.6 Sep 14, 2026
38 of 39 checks passed
@clickhouse-gh
clickhouse-gh Bot deleted the backport/26.6/119317 branch September 14, 2026 04:51
@groeneai

Copy link
Copy Markdown
Collaborator

Opened on master: #119907

It forwards the dry-run flag in FunctionMapToArrayAdapter, in FunctionArrayFold, and in the FunctionWithLowCardinalityFastPath mixin that inherits from the adapter, so a lambda holding an IN subquery is no longer executed for real against a set the plan has not built.

The override is absent on 26.3 through 26.8, not only 26.6, so once it merges I will ask for the backport labels rather than authoring cherry-picks by hand.

One limitation is stated in the PR body rather than fixed there: a constant WHERE over such a lambda still returns no rows. That is a separate constant-folding defect in ExecutableFunctionCapture, already reachable on master through arrayExists; the review comment on that PR asks whether to fold its fix in or keep it separate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backport Changes, backported to release branch. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants