Skip to content

Fix exception when arrayJoin in WHERE is pushed below JOIN - #97239

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-array-join-filter-push-down-cross-join
Feb 18, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-array-join-filter-push-down-cross-join

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 17, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fix a LOGICAL_ERROR exception ("In Filter cannot be more inputs in the DAG than columns in the input header") that occurs when a WHERE clause contains arrayJoin referencing columns from both sides of a JOIN
  • The tryToExtractPartialPredicate function clones the full filter DAG and calls removeUnusedActions, which unconditionally keeps ARRAY_JOIN nodes as roots (they change row count). This pulls in INPUT nodes from the other side of the JOIN that are not available in the target stream
  • The fix adds a check: if the DAG still contains ARRAY_JOIN nodes after removeUnusedActions, skip the pushdown entirely

Related PRs:

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=e4cb181acf13e2961a7d6a4d5aa215947a6c8ccf&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_tsan%29

Test plan

  • Added stateless test 03832_array_join_filter_push_down_cross_join with 15 query variations covering: cross join, inner join, left join, three-way join, legacy join step, subqueries, and the original fuzzer-found query
  • Verified with clickhouse-local that the fix resolves the exception
  • Verified with EXPLAIN PLAN that filters are no longer incorrectly pushed below the JOIN

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC) or LOGICAL_ERROR

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

Fix LOGICAL_ERROR exception when arrayJoin in WHERE clause references columns from both sides of a JOIN.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.2.1.876
  • Backported to: 26.1.5.30, 25.12.9.35, 25.11.10.4

The `tryToExtractPartialPredicate` function (used for disjunction-based
filter pushdown over JOINs) clones the full filter DAG, extracts a partial
predicate, and calls `removeUnusedActions`. However, `removeUnusedActions`
unconditionally keeps all `ARRAY_JOIN` nodes as roots (because they change
the row count). This pulls in INPUT nodes from the other side of the JOIN
that are not available in the target stream, causing a LOGICAL_ERROR:
"In Filter cannot be more inputs in the DAG than columns in the input header"

The fix adds a check after `removeUnusedActions`: if the resulting DAG
contains any `ARRAY_JOIN` nodes, don't push it down. This is safe because
`extractPartialPredicate` already correctly rejects predicates depending
on `ARRAY_JOIN` (via `onlyDependsOnAvailableColumns`), so any surviving
`ARRAY_JOIN` is an artifact of `removeUnusedActions`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=e4cb181acf13e2961a7d6a4d5aa215947a6c8ccf&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_tsan%29

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [37991ff]

Summary: ❌

job_name test_name status info comment
Performance Comparison (amd_release, master_head, 2/6) failure
Select historical data failure
Performance Comparison (amd_release, master_head, 6/6) failure
Select historical data failure

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Feb 17, 2026
@yariks5s yariks5s self-assigned this Feb 17, 2026
@alexey-milovidov
alexey-milovidov merged commit 081f7cf into master Feb 18, 2026
140 of 143 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-array-join-filter-push-down-cross-join branch February 18, 2026 07:31
robot-clickhouse added a commit that referenced this pull request Feb 18, 2026
robot-clickhouse added a commit that referenced this pull request Feb 18, 2026
robot-clickhouse added a commit that referenced this pull request Feb 18, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 18, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Feb 18, 2026
clickhouse-gh Bot added a commit that referenced this pull request Feb 18, 2026
Backport #97239 to 25.11: Fix exception when arrayJoin in WHERE is pushed below JOIN
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Mar 1, 2026
alexey-milovidov added a commit that referenced this pull request Mar 17, 2026
Backport #97239 to 26.1: Fix exception when arrayJoin in WHERE is pushed below JOIN
alexey-milovidov added a commit that referenced this pull request Mar 17, 2026
Backport #97239 to 25.12: Fix exception when arrayJoin in WHERE is pushed below JOIN
tiandiwonder added a commit to tiandiwonder/ClickHouse that referenced this pull request May 13, 2026
…step

When the WHERE filter is pushed below a `JoinStepLogical` whose ON clause
contains `arrayJoin`, `fix_predicate_for_join_logical_step` merged an
`ARRAY_JOIN` node from the per-side join expression DAG into the
pushed-down filter. `removeUnusedActions` preserved that `ARRAY_JOIN`
unconditionally because it changes the number of rows. The same
`ARRAY_JOIN` is recomputed later in the per-side "Pre Join Actions"
`ExpressionStep`, multiplying row count by the array length twice -- so
on an array of size 300 the expected 60 000 result rows balloon to
18 000 000.

Snapshot the `ARRAY_JOIN` nodes contributed by `pre_filter_dag` by
pointer identity before the merge (`mergeInplace` splices second's
nodes into first's `nodes` list, preserving addresses) and drop them
after the merge via the new narrow API
`ActionsDAG::removeNodes(unordered_set<const Node *>)`. Safety:
- `splitActionsForJOINFilterPushDown` rejects `ARRAY_JOIN` from
  `allowed_nodes`, so `filter_dag` arriving at this helper is guaranteed
  not to contain any `ARRAY_JOIN` of its own; every `ARRAY_JOIN` we see
  after the merge must have come from `pre_filter_dag`.
- `JoinStepLogical::buildPhysicalJoinImpl` independently clones the same
  JOIN-side expression DAG into the per-side "Pre Join Actions"
  `ExpressionStep`, so the dropped expression is recomputed above this
  filter and the result remains correct.

Related fixes for the same root cause (`removeUnusedActions` row-changing
carve-out interacting with merge/clone of `ARRAY_JOIN`-bearing DAGs):
prevention path in PR ClickHouse#96989, bail-out path in PR ClickHouse#97239. This PR
addresses the remaining cleanup path.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Aug 19, 2026
… `arrayJoin` JOIN key

When `tryConvertJoinToIn` rewrote `INNER JOIN ON arrayJoin(L.col) = R.col` and
the SELECT (or any post-JOIN expression) referenced the source array column
`L.col` or `arrayJoin(L.col)` again, the cloned post-JOIN expression DAG
referenced an INPUT (`__table1.tags`) that the rewritten plan no longer
exposed. `left_pre_join_actions` only forwards the JOIN key expressions, so
the downstream `cloneSubDAGWithHeader` left a dangling INPUT, which threw
`Code: 10 ... Not found column __table1.tags in block ... (NOT_FOUND_COLUMN_IN_BLOCK)`
at execution.

Decline the conversion in `tryConvertJoinToIn` whenever an input of the
`join_output_actions` sub-DAG is not present in the outputs that
`left_pre_join_actions` forwards. The bail-out happens before any plan
mutation. The query then runs through the regular JOIN path and produces
the correct result.

Same root-cause family as PR ClickHouse#96989, PR ClickHouse#97239 and PR ClickHouse#104785: a
plan-transformation pass merges or clones an ARRAY_JOIN-bearing DAG into a
context whose stream header does not provide the required INPUT. Each
prior fix took a call-site bail-out or post-merge cleanup; this PR follows
the bail-out pattern (matching PR ClickHouse#97239).

A more invasive follow-up could forward the missing left-side columns
through `left_pre_join_actions` so the optimization stays on; left for a
separate PR once we have data on how often the bail-out fires.

The setting `query_plan_convert_join_to_in` is off by default, so the bug
is reachable only when explicitly enabled.

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

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

5 participants