Skip to content

Fix missing rows when GLOBAL IN is pushed down to a shard with a stale local replica - #126084

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-global-in-pushdown-stale-local-replica
Oct 19, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-global-in-pushdown-stale-local-replica

Conversation

@groeneai

@groeneai groeneai commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #120979

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

Fixed missing rows in a query over a Distributed table whose GLOBAL IN or GLOBAL NOT IN filter is pushed down from an outer query (for example SELECT x FROM (SELECT * FROM dist) WHERE x GLOBAL IN (SELECT y FROM t)) when the local replica of a shard lags behind max_replica_delay_for_distributed_queries and the shard is read from another replica. The other replica now receives the initiator's set instead of evaluating the subquery against its own data. Closes #120979.

Description

On 26.8 (measured on the official 26.8.18.2 build), SELECT x FROM (SELECT * FROM dist) WHERE x GLOBAL IN (SELECT y FROM ids) returns fewer rows, with no error, when the initiator's own replica of a shard lags beyond max_replica_delay_for_distributed_queries. All other settings are at their defaults.

Root cause: a stale local replica makes the shard go through the lazy DelayedSource path (ReadFromRemote::addLazyPipe). That path spliced the pushed-down filter inside the lambda with no external tables, so tryBuildAdditionalFilterAST kept the raw GLOBAL IN subquery and the other replica evaluated it against its own ids. The initiator's filter can only remove rows, so the rows the shard dropped were lost. The eager path (addPipe) splices at pipeline-build time with the external tables, so the _data_* temporary table is attached to the set before the set is built and is sent with the query.

Change: addLazyPipe now splices the same way, at pipeline-build time, into a copy of the shard query used only by the lambda's remote branch; the local fallback keeps the unmodified query. The pushdown is kept: the shard now receives globalIn(x, _data_...) instead of the subquery.

Validation: a new test in test_delayed_replica_failover makes the local replica stale with SYSTEM STOP FETCHES and gives each node a different ids table; it also checks in the other replica's query_log that the pushed-down filter arrived. It fails on master and on 26.8.18.2 with ['1'] == ['1', '4'], and passes with the fix (50 of 50 runs, plus the module 3 times with the existing test interleaved). The lazy-source and GLOBAL IN stateless tests pass 10 times each with randomized settings.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.175-master (included in 26.11 and later)

groeneai and others added 2 commits October 15, 2026 16:13
…e local replica

When the initiator's own replica of a shard lags beyond
`max_replica_delay_for_distributed_queries`, the shard is read through
`ReadFromRemote::addLazyPipe` (a `DelayedSource`). That path spliced the
pushed-down filter inside the lambda with `addFilters(nullptr, ...)`, so
`tryBuildAdditionalFilterAST` skipped its GLOBAL IN branch and shipped the
raw `globalIn` subquery. The remote replica then evaluated the subquery
against its own table and pre-filtered with a different set; the
initiator's filter cannot restore the rows it dropped.

The `nullptr` was chosen because the lambda runs after the sets are built,
so a temporary table attached there would be empty. The fix splices at
pipeline-build time instead, with the real external tables, exactly as
`addPipe` does, into a copy of the shard query used only by the lambda's
remote branch. The `_data_*` table is then attached to the set before it is
built and sent with the query; the local fallback keeps the unmodified
shard query. `conjunctsLeftOutOfRemoteQuery` keeps calling the converter
with `nullptr`, which it relies on to predict the parallel replicas splice.

The new integration test makes the local replica stale with
`SYSTEM STOP FETCHES` and gives each node a different `ids` table. It
fails on master and on 26.8.18.2 with `['1'] == ['1', '4']`.
`serialize_query_plan` is pinned to 0 because the integration jobs enable
it, and then the shard runs the serialized plan instead of the AST.

Closes ClickHouse#120979

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The row check alone cannot tell the shard using the initiator's set from a
pushdown that silently declined: the initiator's own filter produces the
same rows in both cases. Pin
`allow_push_predicate_ast_for_distributed_subqueries` and assert that the
secondary query on the fresh replica carries `globalIn(` / `globalNotIn(`.
With the setting at 0 the rows still match and the new check fails.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)
Finding Verdict
⚠️ The regression test checked only rows, so it could not tell a shard that pre-filtered with the initiator's set from a pushdown that silently declined. AGREE, fixed: the test pins allow_push_predicate_ast_for_distributed_subqueries=1 and checks that the fresh replica received the pushed-down globalIn/globalNotIn. No analyzer pin: that setting is obsolete since 26.9.
💡 The issue link was inside the template's HTML comment instead of an active Closes: line. AGREE, fixed in the description.
💡 The _data_* table is filled at pipeline-build time even when the lazy shard ends up reading the stale local replica. Noted, not blocking: the same per-query cost the eager path pays.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. pytest test_delayed_replica_failover/test.py -k test_global_in_pushed_down_to_stale_local_replica fails on every run on master and on the official 26.8.18.2 build with assert ['1'] == ['1', '4'].
b Root cause explained? Yes. A stale local replica sends the shard through addLazyPipe, which spliced the pushed-down filter inside the DelayedSource lambda with no external tables, so the shard evaluated the raw globalIn subquery against its own ids.
c Fix matches root cause? Yes. addLazyPipe now splices at pipeline-build time with the real external tables, as addPipe does, so the shard receives globalIn(x, _data_...). No guard, and the pushdown is kept.
d Test intent preserved / new tests added? Yes. New integration test with node-local ids tables covering GLOBAL IN and GLOBAL NOT IN; it also checks the fresh replica's query_log for the pushed-down filter. The existing test is unchanged and passes.
e Both directions demonstrated? Yes. Base FAIL on master and on 26.8.18.2; fix PASS on 50/50 runs and the module 3x interleaved with test.
f Fix is general across code paths? Yes. addPipe, ReadFromParallelRemoteReplicasStep and the distributed index analysis already pass the external tables; conjunctsLeftOutOfRemoteQuery only predicts and never sends a query.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. With use_delayed_remote_source forcing every shard lazy, results were identical with the failpoint on vs off for Nullable, transform_null_in=1, LowCardinality(String), a tuple LHS, an empty set and WITH TOTALS.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, protocol or format change. The lazy path now sends the same _data_* external table that the eager path already sends.
i Invariants and contracts preserved? Yes. A pushed-down global-IN conjunct now reaches a remote node only together with the initiator's set; the splice runs before the sets are built, as in addPipe.

Session id: cron:clickhouse-impl-slot-7:20261015-152132

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4799c6e]

Summary: ✅


AI Review

Summary

PR #126084, reviewed at 4799c6eb7644d4b6715d42eed7979290bd9b0c18, fixes missing rows when a pushed-down GLOBAL IN or GLOBAL NOT IN filter reaches a remote replica through ReadFromRemote::addLazyPipe. It attaches the initiator's external table during pipeline construction while preserving the original query for local fallback. No actionable findings remain; the regression test covers both operators and verifies that pushdown occurs.

Missing context / blind spots
  • ⚠️ Runtime validation relies on the author's reported failing-base/passing-fix runs. No local ClickHouse build was available, and the targeted integration and integration bugfix-validation jobs were still pending in the current CI workflow. Completed focused CI results would independently confirm those runs.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @KochetovNicolai @nickitat, could you review this? On a shard whose local replica is stale, addLazyPipe spliced the pushed-down filter without external tables, so the other replica received the raw GLOBAL IN subquery and filtered with its own data. It now splices at pipeline-build time with the external tables, as addPipe does, and keeps the nullptr prediction in conjunctsLeftOutOfRemoteQuery unchanged.

@clickhouse-gh clickhouse-gh Bot added the comp-distributed Distributed table engine & query routing across shards (sharding/load balancing). label Oct 15, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 15, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 18, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 18, 2026
Merged via the queue into ClickHouse:master with commit 4e9c824 Oct 19, 2026
351 of 353 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 19, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 3, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 3, 2026
Backport #126084 to 26.10: Fix missing rows when `GLOBAL IN` is pushed down to a shard with a stale local replica
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…d down to a shard with a stale local replica
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-distributed Distributed table engine & query routing across shards (sharding/load balancing). 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 v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GLOBAL IN pushdown to lagging local replica silently drops rows

5 participants