Skip to content

Keep a lambda on its side of ARRAY JOIN - #119186

Merged
yariks5s merged 6 commits into
masterfrom
yarik/keep-lambda-above-array-join
Sep 14, 2026
Merged

yariks5s merged 6 commits into
masterfrom
yarik/keep-lambda-above-array-join

Conversation

@yariks5s

@yariks5s yariks5s commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

Repro: https://fiddle.clickhouse.com/971f4ccb-4ebf-48b9-b68b-31ab879d5120
Related: #112241

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixes a case where lambda is pushed up through the ARRAY JOIN step (lambda that captures nothing was moved below the ARRAY JOIN step, away from the higher-order function that applies it)


Workflow [PR]
Sync PR [sync-upstream/pr/119186]

Version info

  • Merged into: 26.9.1.1457 (included in 26.9 and later)

@yariks5s
yariks5s requested a review from nihalzp September 10, 2026 10:22
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b3a2cfd]

Summary: ✅


AI Review

Summary

This PR fixes the ARRAY JOIN boundary issue by reclassifying a capture-free lambda constant from FUNCTION to COLUMN and adding a regression test. That representation change is not carried through the shared query-scope non-determinism check, so several optimizers can now treat higher-order expressions with rand()-style lambda bodies as deterministic; I would block until that is fixed.

Findings

❌ Blockers

  • [src/Interpreters/ActionsDAG.cpp:971] [dismissed by author -- https://github.com/Keep a lambda on its side of ARRAY JOIN #119186#discussion_r3981449835] Reclassifying a capture-free lambda to COLUMN hides it from dagContainsNonDeterministicFunction, which still scans only FUNCTION nodes in src/Processors/QueryPlan/Optimizations/Utils.cpp. Higher-order wrappers such as FunctionArrayMapped report isDeterministicInScopeOfQuery() == true on their own, so an expression like arrayMap(x -> rand(), arr) now slips past mergeExpressions, canHoistGatherThroughStep, join-conversion dry runs, and replicated-subplan planning even though moving those steps changes how many times rand() is drawn and can change query results. This still holds in the current code because the new node becomes COLUMN, while the helper that is supposed to catch query-scope non-determinism was not updated to recurse through ColumnFunctions. Suggested fix: either keep these lambdas as FUNCTION nodes until the generic helper is updated, or make dagContainsNonDeterministicFunction use allNodeFunctions / equivalent ColumnFunction recursion, as the ARRAY JOIN splitter and mergeFilterIntoJoinCondition already do.
Tests
  • ⚠️ Add a focused regression for a capture-free lambda whose body contains a query-scope non-deterministic function, for example arrayMap(x -> rand(), arr) or arrayExists(x -> rand() % 2, arr), and run it through at least one dagContainsNonDeterministicFunction user such as mergeExpressions or canHoistGatherThroughStep. The new test only covers deterministic lambdas around ARRAY JOIN, so it would not fail if folded lambda COLUMNs remain invisible to that helper.
Final Verdict
  • Status: ❌ Block
  • Minimum required actions: teach dagContainsNonDeterministicFunction and its callers to see folded lambda COLUMNs, or narrow this retyping so those lambdas remain FUNCTIONs; then add a regression that proves a query-scope non-deterministic lambda still blocks the affected rewrite.

LLVM Coverage Report

Measured on commit b3a2cfd.

Metric Baseline Current Δ
Lines 89.00% 89.00% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 4/4 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 10, 2026
@nihalzp nihalzp self-assigned this Sep 10, 2026
Comment thread src/Interpreters/ActionsDAG.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Sep 10, 2026
Comment thread src/Interpreters/ActionsDAG.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing b3a2cfd08 with master d86d52562 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 16 s compile time in total, 7 of them have a recent master baseline.

Job report

Comment thread src/Interpreters/ActionsDAG.cpp Resolved
@yariks5s
yariks5s added this pull request to the merge queue Sep 14, 2026
Merged via the queue into master with commit 1f59f0f Sep 14, 2026
175 checks passed
@yariks5s
yariks5s deleted the yarik/keep-lambda-above-array-join branch September 14, 2026 11:21
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 14, 2026
EmeraldShift pushed a commit to EmeraldShift/ClickHouse that referenced this pull request Oct 20, 2026
ExpressionActions::linearizeActions gives a node one argument per child plus
one extra argument only when the node is an INPUT, and its final loop then
assumes every entry of ActionsDAG::inputs has that extra argument. Constant
folding in removeUnusedActions retyped a childless Function-typed node to
COLUMN in place without removing it from inputs, so such an entry had no
arguments at all and arguments.front() ran on an empty vector: hardened libcxx
turns that into __libcpp_verbose_abort, i.e. the server dies with SIGABRT.

The folded value reaches that state through a header: a lambda that captures
only an IN subquery set folds to a ColumnConst holding a ColumnFunction, that
constant lands in the shard header, and ReadFromRemote::addConvertingActions
builds the converting DAG from the header, where addInput keeps the ColumnConst
on the input node.

The disjunct that let a childless node be folded was added by ClickHouse#119186 to fold a
capture-free lambda; before it, !children.empty() excluded every childless node,
so an input could never be retyped. Restricting the disjunct to FUNCTION nodes
keeps that fold (addFunctionImpl sets type = FUNCTION and
tryFoldFunctionToConstant only sets node.column, so a folded lambda is still a
FUNCTION node here) while restoring the invariant, which
ActionsDAG::deserialize already states and enforces with INCORRECT_DATA.

Guarding the crash site was rejected: it would leave a COLUMN node in inputs, so
required_columns and input_positions would silently disagree with the pipeline
header. Dropping the node from inputs was rejected too, because the pipeline
still delivers that column and arity would break in executeOnColumns.

Reachable from plain SQL with default settings, on a stable release:

  SELECT arrayExists(x -> (x IN (SELECT 2)), [2])
  FROM remote('127.0.0.{2,3}', system.one) LIMIT UNTIL 1;

The residual NOT_IMPLEMENTED that this query then returns (a Function(...)
column cannot be serialized to a shard) is pre-existing: a binary with the
pre-ClickHouse#119186 guard returns exactly the same error, with the server alive.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants