Skip to content

Lower ArrayJoin with nondeterministic functions - #122240

Merged
yariks5s merged 10 commits into
masterfrom
yarik/lower-array-join-with-nondeterministic
Sep 30, 2026
Merged

yariks5s merged 10 commits into
masterfrom
yarik/lower-array-join-with-nondeterministic

Conversation

@yariks5s

@yariks5s yariks5s commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Related: #112241
Related: #117882
Related: #120390

Changelog category (leave one):

  • Backward Incompatible Change

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

With query_plan_lower_array_join_function enabled, a non-deterministic function such as rand() or generateUUIDv4() next to arrayJoin in the same SELECT is evaluated once per output row, as with the ARRAY JOIN clause, instead of once per source row. Set arrayjoin_nondeterministic_functions_before_expansion = 1 (or compatibility to 26.9 or older) to keep the previous behaviour.


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

Version info

  • Merged into: 26.10.1.1071-master (included in 26.10 and later)

@yariks5s
yariks5s requested a review from nihalzp September 25, 2026 13:45
@clickhouse-gh

clickhouse-gh Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c02f5b8]

Summary: ✅


AI Review

Summary

This PR changes arrayJoin lowering so non-deterministic expressions next to the function form behave like the ARRAY JOIN clause by default, and adds legacy_array_join_function_nondeterministic_evaluation / compatibility = '26.9' as the migration path. The direct lambda-body case from the earlier review is fixed, but one higher-order alias case still violates the legacy-setting contract, so the change is not ready to merge yet.

PR Metadata

Changelog category is correct: this is a backward-incompatible semantic change now that query_plan_lower_array_join_function is enabled by default in 26.10. Changelog entry is required and otherwise good, but it still tells users to set arrayjoin_nondeterministic_functions_before_expansion = 1, which no longer exists after the rename. Replace that sentence with: Set legacy_array_join_function_nondeterministic_evaluation = 1 (or compatibility to 26.9 or older) to keep the previous behaviour.

Findings

❌ Blockers

  • [src/Interpreters/ActionsDAG.cpp:2433-2437] The legacy-setting contract is still broken for higher-order calls whose lambda arrives through an ALIAS node. isNonDeterministicOrStatefulCall checks only the immediate child node, but analyzer-produced shapes such as arrayMap(f AS g, ...) keep the lambda under ALIAS; allNodeFunctions does not look through that wrapper, so the outer arrayMap / arrayFilter / arrayFold is treated as deterministic and stays after ARRAY JOIN. With legacy_array_join_function_nondeterministic_evaluation = 1, those queries still draw once per expanded row instead of once per source row. Suggested fix: unwrap alias chains before testing lambda children, or reuse ActionsDAG::hasUnsafeHiddenLambdaBody so hidden lambda bodies are classified the same way as in other optimizer guards.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit c02f5b8.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 55/57 (96.49%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-backward-incompatible Pull request with backwards incompatible changes label Sep 25, 2026
@yariks5s yariks5s changed the title Yarik/lower array join with nondeterministic Lower ArrayJoin with nondeterministic functions Sep 25, 2026
@nihalzp nihalzp self-assigned this Sep 25, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Sep 25, 2026
Comment thread src/Interpreters/ActionsDAG.cpp Resolved
Comment thread src/Interpreters/ActionsDAG.cpp Resolved
…oin-with-nondeterministic

Resolve the conflict with the move of the settings changes history into the setting declarations
(ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as
trailing arguments of the `DECLARE` of each setting.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@nihalzp nihalzp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks good. I left a reply on the automatic review thread.

Additionally, I feel the proposed setting name may not be very clear to the users. I propose legacy_array_join_function_nondeterministic_evaluation (default false: gives different value on every output row; previous value true: one value per source row, repeated across that row's expanded rows).

Comment thread src/Interpreters/ActionsDAG.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c02f5b8c5 with master 80508bd5f (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 61fda56c2; 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

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

Job report

@yariks5s
yariks5s added this pull request to the merge queue Sep 30, 2026
Merged via the queue into master with commit d284608 Sep 30, 2026
149 checks passed
@yariks5s
yariks5s deleted the yarik/lower-array-join-with-nondeterministic branch September 30, 2026 12:05
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 30, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 1, 2026
Since ClickHouse#122240, `query_plan_lower_array_join_function` also lowers an `arrayJoin` that sits next to
other conjuncts in a HAVING filter: the plan becomes `ArrayJoin -> Filter(count() > 3) -> Aggregating`,
so the filter directly above the aggregation no longer holds the `arrayJoin` and the pre-filter
legitimately attaches to it. The `array join in having` cell then saw the annotation and failed in the
merge queue (`0` expected, `1` observed).

The cell exists to exercise the `hasArrayJoin` refusal, so it now pins the lowering off, which keeps
the `arrayJoin` inside the filter the pass reads. The lowered shape gets its own applied cell and an
exact skipped-group pair (4000 of 6000 groups skipped on the `on` arm, the same rows as the `off` arm
and as the un-lowered plan).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-ClickHouse__ClickHouse that referenced this pull request Nov 17, 2026
…plication again

No existing query number or text changes: 1 query is disabled and 1 appended as 2.

Since ClickHouse#122240 a non-deterministic function next to `arrayJoin` is evaluated once
per output row, so query 0 builds 10240 strings of 1 MiB (3.41 s amd / 2.91 s arm
on MasterCI, 16 GiB) and replicates none: with `enable_lazy_columns_replication = 0`
it takes the same time. The copy takes the string from a subquery, which is fixed
per source row, so `arrayJoin` replicates it again, as query 0 did before ClickHouse#122240.

On the official amd builds right before ClickHouse#122240, of its merge and of master, the
copy takes 0.002-0.003 s and 3.57-3.69 s (over 1000x) with lazy replication off,
the same eager work as the old query 0 (0.997-1.03); query 0 was over 1000x
slower without lazy replication before ClickHouse#122240 and takes the same time after.
Saves about 40 s of query time per master perf job on amd.

Related: ClickHouse#129258

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

Labels

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-backward-incompatible Pull request with backwards incompatible changes 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.

4 participants