Skip to content

Fix arrayJoin producing duplicate rows with JOIN and WHERE - #96989

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-arrayjoin-duplicate-rows-with-join-where
Feb 15, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-arrayjoin-duplicate-rows-with-join-where

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

Summary

Closes #96398

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):

Fix arrayJoin function producing duplicate rows when used with INNER JOIN and WHERE clause, caused by the partial predicate push-down optimization incorrectly pushing filters containing arrayJoin below a JOIN.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

No documentation changes needed.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.2.1.744
  • Backported to: 26.1.3.40

The `onlyDependsOnAvailableColumns` function in the partial predicate
push-down for JOINs did not check for `ARRAY_JOIN` nodes. This caused
predicates containing `arrayJoin` to be incorrectly pushed down below
a JOIN as a partial (speculative) filter, while the original filter
above the JOIN was kept. The `arrayJoin` therefore executed twice --
once in the pushed-down filter and once in the original filter --
producing duplicate rows.

The fix rejects any subgraph containing `ARRAY_JOIN` from being
considered for partial predicate push-down.

The bug was introduced in #84735 and exposed when the optimization
was enabled by default in #89313.

Closes #96398

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

clickhouse-gh Bot commented Feb 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dcf9040]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Feb 15, 2026
@alexey-milovidov alexey-milovidov self-assigned this Feb 15, 2026
@alexey-milovidov
alexey-milovidov merged commit 43263be into master Feb 15, 2026
130 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-arrayjoin-duplicate-rows-with-join-where branch February 15, 2026 15:04
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 15, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Feb 15, 2026
robot-clickhouse added a commit that referenced this pull request Feb 15, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Feb 15, 2026
clickhouse-gh Bot added a commit that referenced this pull request Feb 15, 2026
Backport #96989 to 26.1: Fix arrayJoin producing duplicate rows with JOIN and WHERE
@KochetovNicolai KochetovNicolai added the post-approved Approved, but after the PR is merged. label Mar 5, 2026
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

post-approved Approved, but after the PR is merged. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default 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.

arrayJoin() function causes duplicated rows when used with INNER JOIN and WHERE in ClickHouse 26.1

5 participants