Skip to content

Read time series table functions on parallel replicas - #119350

Merged
nickitat merged 45 commits into
masterfrom
pr-table-function-anchor
Nov 1, 2026
Merged

nickitat merged 45 commits into
masterfrom
pr-table-function-anchor

Conversation

@nickitat

@nickitat nickitat commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

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 reading a TimeSeries target table through the timeSeriesSamples, timeSeriesTags and timeSeriesMetricFamilies table functions with parallel replicas, which returned every row once per replica.


Reading a TimeSeries target table through timeSeriesSamples / timeSeriesTags / timeSeriesMetricFamilies with parallel replicas was wrong in two independent ways. This fixes the one that returned every row once per replica, without adding anything specific to the TimeSeries engine beyond one small virtual method of ITableFunction. The other one - an unqualified table name failing on the replicas with UNKNOWN_TABLE - is fixed by #128194, stacked on this one. It lands second because on its own it would make such a read return every row once per replica instead of failing.

Every row came back once per replica. The initiator accepted a table function for parallel reading, because it checks only that the resolved storage is a MergeTree (timeSeriesSamples resolves to the inner target table), so it created the coordinator and sent the query to the followers. But a follower decides which read to coordinate with findTableForParallelReplicas, and that function considered only TableNodes. For a table function it found nothing, so the follower turned parallel replicas off for the read, never contacted the coordinator, and scanned its whole copy of the table. Since no follower announced itself, the coordinator also gave all ranges to the initiator's own read, and the result held one full copy of the data per replica.

The fix lets a follower choose a table function as the coordinated read: the planner works with ITableExpressionNode instead of TableNode. A table function opts in with ITableFunction::getParallelReplicasReadID, an optional identity of the source the call reads. No value, the default, means the call cannot be the coordinated read: a table function that builds a storage of its own (view, merge, s3) is the same table on no two replicas, so it never qualifies, even when that storage is a MergeTree. A value must be computed the same on every replica - independent of anything local to a server, such as the UUID of a hidden inner table, and of the alias the call was written with - and must differ for different sources; the resolved storage is still checked as it is for a table. The TimeSeries target functions build it from their canonical name and the qualified TimeSeries table, e.g. timeSeriesSamples(db.ts). The initiator applies the same rule, so it no longer starts a coordinated read that the followers would not join.

The identity is also the name the replicas announce the read under to the coordinator. Naming it after the storage it resolved to, as for a table, would give every replica whose TimeSeries table was created independently - with a different UUID, so a differently named inner table - a coordinator of its own, and each would read everything. INSERT SELECT with parallel_replicas_local_plan = 0 was exposed to it: it runs no table-status probe and pins no snapshot replica, so each replica inserted the whole source.

All three parallel-replicas modes are covered. The custom-key fan-out needed the same fix, since it addresses the read by its own table id. The plan-based mode (parallel_replicas_plan_based, and serialize_query_plan) sends the replicas a plan fragment instead of query text. The fragment names the storage the call resolved to on the initiator. For timeSeriesSamples that is the inner target table, named after the UUID of the TimeSeries table. That UUID is the same on every server when the table is created with ON CLUSTER or in a Replicated database. When each server created its table on its own, the other replicas cannot find the inner table and fail with UNKNOWN_TABLE instead of reading something else, as for a materialized view (#128254). A shipped read is announced to the coordinator under the inner table's name, by the replicas and by the initiator's own copy alike, including a copy a projection replaced.

A read through a table function is not distributed at all when additional_table_filters is set anywhere in the query sent to the replicas: around the read, in a subquery, on the other side of a join, or in the body of a materialized CTE. The query text the replicas receive makes them match the filters against their own current database, so a filter keyed by a table's short name can apply on the initiator and not on the replicas.

No new wire format is added.

During a rolling upgrade, disable parallel replicas for queries that read through the TimeSeries target table functions, including a distributed INSERT SELECT, until all participating servers include this fix: an older follower can still return or insert duplicate rows.

Related: #118130
Related: #128254
Related: #118518


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

Version info

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

`SELECT * FROM timeSeriesSamples(ts)` was wrong with parallel replicas in two
independent ways, and this fixes both without adding anything specific to the
`TimeSeries` engine beyond one small interface.

The name did not resolve. The `QueryTree -> AST` conversion qualifies a table
name with its database, so a forwarded query is self-contained - but the
arguments of a table function are shipped as written, so the replicas resolved
`ts` against their own current database. Rather than teach each table function
to qualify its own arguments, the initiator now sends its current database to
the replicas as the `database` setting. It was previously stripped by
`stripInitiatorOnlySettings`, for a reason that belongs to the `Distributed`
fan-out: `rewriteSelectQuery` can leave the remote table unqualified there, so a
shard must use its own default database. Parallel replicas have no such
remapping - every replica holds the same tables - so the setting is put back
after the strip. This covers every table function that takes an unqualified
table name, not just the `TimeSeries` accessors, and it works whichever analyzer
plans the query.

Every row came back once per replica. The parallel-replicas planner recognises
only a `TableNode` as the table a distributed read is anchored to, so a read
through a table function left the followers with no chosen table: each disabled
parallel replicas for its own read and scanned everything. A table function that
is nothing but a stable reference to an existing table can anchor the read just
like the table itself, because every replica resolves the call locally - so
`ITableFunction::getReferencedTableID` names that property, `TableFunctionTimeSeriesTarget`
implements it, and the planner is widened from `TableNode` to `ITableExpressionNode`.
Gating on the property rather than on "the resolved storage looks like a
`MergeTree`" keeps a table function that builds a storage of its own from ever
becoming an anchor.

The id it returns is the outer `db.ts`, not the hidden inner table the call
resolves to. That matters for `RemoteQueryExecutor::main_table`, which every
replica is asked about before it joins the read: an inner table is named after a
UUID the initiator assigned, so it is not a name another replica can be asked
about, while `db.ts` is.

`serialize_query_plan = 1` ships no plan for such a read.
`createRemotePlanForParallelReplicas` can only mark a `ReadFromTableStep` as the
coordinated read, and marking a `ReadFromTableFunctionStep` instead would need a
new query-plan serialization version, which excludes every older replica from
plan-based parallel replicas. It now returns null when it cannot mark the read,
and the caller sends the query text, which the replicas plan themselves. This is
also safer than before for the pre-existing view-over-`UNION` case, which used to
ship a plan with no read marked at all.

Thirteen tests gain an `is_initial_query` guard on their `system.query_log`
filter. They used `current_database = currentDatabase()` to mean "my query",
which only worked because a secondary query was attributed to `default`; now
that a replica runs it in the database the query was written against, the filter
has to say so.

Closes: #118130
Related: #118518

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2025bbe]

Summary: ✅


AI Review

Summary

The PR coordinates reads through TimeSeries target-table functions so parallel replicas return each row once, preserving the coordinator identity through SQL execution, serialized plan fragments, and projection replacement. No actionable findings remain at commit 2025bbe903598a17b6b82e9ac43dddba5996674b. The documented rolling-upgrade limitation and separately scoped argument-qualification fix are accepted.

Missing context / blind spots
  • ⚠️ The current workflow is still running. The targeted stateless and integration jobs have not produced results, and the added regressions are excluded from the completed fast-test job. Successful execution of those regressions would close this validation gap; tests were not run locally.
Final Verdict

Status: ⚠️ Request changes pending regression evidence. Obtain passing results for the added stateless regressions and the independent-UUID INSERT SELECT integration test before approval. No additional code changes are requested from this review.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 10, 2026
Comment thread src/Planner/PlannerJoinTree.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Sep 10, 2026
nickitat and others added 3 commits September 10, 2026 21:32
…table id

`getReferencedTableID` gives the parallel-replicas machinery a name that every
replica resolves to its own copy of the table, and the query-text path addresses
the read by it. The plan-based path does not: it ships a `ReadFromMergeTree`
fragment, and `ReadFromMergeTree::serialize` writes the id of the storage the
call resolved to on the initiator, which for a `TimeSeries` accessor is a hidden
inner table named after a UUID that replica assigned. This predates the
introduction of `getReferencedTableID` - `allowParallelReplicasForJoinTree`
returns true for a join tree that is a single table expression, so a table
function already reached that branch - and it fails with `UNKNOWN_TABLE` rather
than reading twice, so it is left as it is. Carrying the call in the fragment
instead of a resolved id needs a query-plan serialization version, so it does
not belong in a change that deliberately adds none.

Say so where a reader would otherwise assume the id covers both modes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
The test asserts that no arm of a coordinated read failed, by counting exception
rows in its own database. Now that a replica runs a secondary query in the
database the query was written against, that counter also sees the arms the
coordinator cancelled once it no longer needed them - which the slowdown
failpoint in this test makes routine. Whether a cancelled arm appears at all is
a race between the `Cancel` packet and the point where the replica applies the
query's settings, so the test would fail only sometimes: locally the same run
produces eight such rows and all of them still land in `default`, while CI saw
two of them land in the test's database.

A cancelled arm did not fail, so exclude the two cancellation codes rather than
restrict the counter to initial queries, which would stop it seeing the replica
arms it exists to check.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
Same shape as the ones already guarded: `current_database = currentDatabase()`
stood in for "the query this test ran", which held only while a replica was
running secondary queries in `default`. `04036` is the one where it is not a
matter of extra rows - it reads `ParallelReplicasNumRequests` off whatever rows
match and asserts a floor, so a replica's own row, which is below it by
construction, makes the `throwIf` fire.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
Comment thread src/Planner/PlannerJoinTree.cpp Resolved
nickitat and others added 2 commits September 11, 2026 01:22
Same shape as the others. This one is a reminder that the local sweep was not a
proof: the test was in the candidate set and did run there, and passed, because
the read never engaged parallel replicas on a single-host cluster.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
Excluding the two cancellation codes was not enough: a cancelled arm surfaces as
whatever it was doing when the cancel landed, and the flaky check caught it as
`NETWORK_ERROR` ("Connection reset by peer, while writing to socket") instead.
Enumerating benign codes is the wrong shape - the set is open.

Count `LOGICAL_ERROR` instead, which is what a missing callback raises and what
the test was written for. That keeps the counter looking at the replica arms,
which is the reason it does not restrict itself to initial queries.

Related: #111677

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
@clickhouse-gh

clickhouse-gh Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 2025bbe90 with master 3c82fa8cc (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

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

Job report

nickitat and others added 3 commits September 11, 2026 08:03
The old-analyzer job runs the whole file with `enable_analyzer = 0`, where two of
the assertions cannot hold: the `FINAL` refusal is raised by the planner, which
that path does not reach, so the query simply succeeds; and the coordinated read
the announce check looks for is the one the planner anchors. Leaving the body at
the job's default was meant to cover both analyzers, but the legacy path already
has its own subcase that sets `enable_analyzer = 0` per query, so it stays
covered whichever analyzer the job defaults to.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
Both are the same fix as the others, and both were missed by the sweep that was
supposed to bound this:

`02131` writes `current_database == currentDatabase()`, and the sweep matched
only the single-`=` spelling, so it was never a candidate.

`03552` is a `.j2` template whose `.gen.sql` is generated at test time and not
tracked, so globbing `*.sql` never saw it. Its scalar subquery is literally named
`initial_query_id_`, which is what it now selects.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
The AI review is right that the change stopped at the task-based path. The
custom-key fan-out reaches the same table functions and got neither half of the
fix: it addresses the read by `storage->getStorageID()`, the hidden inner table,
and it builds its remote context with `updateSettingsAndClientInfoForCluster`,
which strips `database` and never puts it back. Reproduced as

    SELECT count() FROM timeSeriesSamples(ts)
      SETTINGS parallel_replicas_mode = 'custom_key_sampling', ...
    -> Table default.ts does not exist

which only surfaced as an error because no `default.ts` happened to exist - with
one present the replica would have read the wrong table and returned rows.

`updateSettingsAndClientInfoForCluster` is shared with the `Distributed` fan-out,
where stripping `database` is what makes an unqualified remote table resolve
against the shard's own default, so the database is asked for explicitly by the
one caller that wants it rather than restored for everyone.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
`05024` lists its rows, so the follower `QueryFinish` rows would be appended to
the result; `03272` counts rows with `read_rows > 2` to assert the initiator does
not over-read, and a follower's own row would be counted instead.

Both were in the local sweep and both passed there, which is the third test to do
that: a single-host cluster does not produce the follower rows that make these
filters wrong, so the sweep can show that a test is affected but never that it
is not.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
Every test whose name says it reads with parallel replicas, and which identifies
its own query in `system.query_log` by `current_database` alone, now says
`is_initial_query` as well. The reviews had been naming these one or two at a
time; this is the rest of that set, found by taking every `parallel_replica` /
`autopr` test with that shape.

Three of them were left alone. Two aggregate to a threshold, so a follower row
carrying zeroes cannot change the answer. `03326_parallel_replicas_out_of_range`
is a crash repro that compares no results at all - it joins `system.query_log`
to `system.processes` in a deliberately odd way and formats to `Null` - so
narrowing its join would only risk it no longer reaching what it was written to
reach.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
nickitat and others added 3 commits September 11, 2026 14:23
I had set this one aside as unaffected because it aggregates, but it aggregates
with `groupArray` and then zips the array positionally against a fixed list of
expected sizes. A follower row joins the array and shifts everything after it, so
this is one of the ones that breaks hardest - the comparison silently pairs the
wrong entries rather than returning an obviously wrong count.

The other two set aside still hold: one groups by `log_comment` and takes
`max(...) > 0`, the other sums to a `> 0` threshold, and a follower row cannot
change either.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
Brings in the tests added since the branch point, which is where the reviewer
was pointing: the file it named is on master, not in this branch's tree.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
The reviewer named this file three times and I twice said it did not exist. It
does - it was added to master after this branch was cut, so it is absent from the
branch's tree but present in what CI actually tests, which is the branch merged
with master. Checking the working tree was the wrong place to look.

The check lists one row per initial query and keys them by `current_database`
and `log_comment`, so the follower rows join the result.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
Comment thread src/Planner/PlannerJoinTree.cpp Resolved
nickitat and others added 3 commits September 11, 2026 16:51
…-key path

Adding `getStorageIDForParallelReplicas` to the custom-key branch without the
guard the classic branch carries made it reachable with a table function that has
no referenced table. A parameterized view is exactly that - resolved as a
`TableFunctionNode` carrying no `ITableFunction` at all - so the helper hit its
own `chassert` and, with assertions compiled out, handed an empty `StorageID` to
the coordinator, which then asks no replica about any table.

Give such a table function its resolved storage id, which is what it was
addressed by before stable references existed and is a name the other replicas
can resolve, rather than keeping it out of the custom-key path and taking its
parallel replicas away.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
A custom key is refused outright when the plan is shipped instead of the query
text - `Parallel replicas with custom key are not supported with
serialize_query_plan enabled` - and the job that ships plans turns that on for
every query, so the cases added for the custom-key fix threw there.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
…ased mode

The plan-based mode was the last one where these table functions were still
addressed by the storage the call resolved to on the initiator - a hidden inner
table named after a UUID that server assigned - because a shipped
`ReadFromMergeTree` fragment cannot name anything else. Carrying the call in the
fragment needs a query-plan serialization version. Sending the query text needs
nothing: the replicas resolve the call themselves against the database that now
travels with the query, and coordinate exactly as they do with the mode off.

Falling back to a single-replica read would also have been correct, but there is
no reason to give up the parallelism: measured on a read of the samples table,
the routed query announces to the same three replicas as the query-text mode and
returns the same rows.

The mode now has a test, which it did not before.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
`03008` is the one CI found, and it exposes a hole in how the cost of this change
was measured. The A/B ran the candidates against a binary with the database
forwarding and one without, and called a test unaffected when it failed on both.
A test can fail on the baseline for a reason of its own - here, a storage policy
and an S3 endpoint the local harness does not have - and still be one of these,
so those were dropped from the count rather than examined.

Of the nine dropped that way, eight have the shape; five of those already name
one row by an exact `query_id`, which no follower shares, and are genuinely
unaffected. The other three are these.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HqTzo2T96TYt2BUpK1mHuK
…JOIN`

The plan-based mode coordinates the right side of a `RIGHT JOIN`, which the query-tree fallback never
sees: the left leaf is a table, and the right one is planned outside the join. Before the previous
commit such a read was shipped by the name of the inner table it resolved to on the initiator, which
fails with `UNKNOWN_TABLE` on a replica whose `TimeSeries` table was created independently. The
integration test checks that on separate servers; `05460` checks that nothing is shipped, since its
replicas are one server and find the table anyway.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nickitat and others added 5 commits October 30, 2026 09:50
Since daaa0be735e9 a row of the samples table holds the
samples of one series within one time bucket in the column `samples`, the table is an
`AggregatingMergeTree`, and `timeSeriesSelector` takes the argument `return_columns`. Counts of
individual samples are now `sum(length(samples))`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119350&sha=latest&name_0=PR
PR: #119350

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s` is set

A read through a table function is sent to the replicas as query text even with
`serialize_query_plan`, and a replica matches `additional_table_filters` against its own current
database. A filter keyed by the short name of the samples table matched on the initiator and not on
the replicas, which returned the excluded rows. The check uses the context of the subquery that
reads, so filters set only there are seen too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…_filters`

The query text sent to the replicas for a read through a table function is the whole query, so the
filters of every subquery in it are resolved by the replicas against their own current database, not
only the filters around the read. With the filters set only for the other side of a join, the
plan-based mode sent the join as text and the replicas returned the rows the filter excludes. The
check is now made once for the whole query tree and kept in `GlobalPlannerContext`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ot in the root query

A materialized CTE is planned by a standalone `Planner` with a fresh `GlobalPlannerContext`, which
carried the default `false` for the whole-query flag, so a table function read in a CTE whose body sets
`additional_table_filters` was still distributed: a self-joined filtered CTE returned 540/1080 instead
of 36/36. The fallback now walks the query tree it sends to the replicas, which is right for every
standalone planner, and the flag is gone from `GlobalPlannerContext`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nickitat and others added 3 commits October 30, 2026 16:17
A read through a `TimeSeries` table function was planned the query-tree way even with
`parallel_replicas_plan_based`, because a plan fragment names the inner table the call resolved to, and
that table is named after the `TimeSeries` table's UUID. That UUID is the same on every server when the
table is created with `ON CLUSTER` or in a `Replicated` database, so the fragment works there. Only a
table each server created on its own has another UUID, and then the replicas fail with `UNKNOWN_TABLE`
rather than read anything else, as for a materialized view (see
#128254 for what the query-tree way does with such a view).

A shipped read is deserialized on the replicas without its table function, so it is announced to the
coordinator under the inner table's name. The initiator's own copy of the read now uses that name too:
with the read identity of the call it asked for a stream the coordinator did not know, a logical error
that the new data in `05460` reproduces with `parallel_replicas_local_plan = 1`.

Related: #128254

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rojections

A read through a table function that the plan-based mode ships is coordinated under the name of the
storage it resolved to, because that is all a replica rebuilds it from. The flag saying so lived on
`ReadFromMergeTree` and was copied by hand in `clone` and in the local plan. The normal and aggregate
projection optimizers build a new read through `MergeTreeDataSelectExecutor::readFromParts`, which
dropped it: the initiator's projection read announced `timeSeriesSamples(db.ts)` while the replicas
announced the inner table, and the coordinator threw the logical error
`Got read request from replica 1 for unknown stream`.

The flag now lives in `SelectQueryInfo`, which every rebuilt read copies from the one it replaces.
The new test fails with that logical error on the previous commit, for both projection kinds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VMTxKf7NFjbjoSnJvhMFPd
…projection`

The flaky check of the debug build ran the test for up to 221 s, over the 180 s limit: with 8-row
granules and blocks, each of the six inserts took about 6.5 s and each parallel-replicas run of the
aggregate case about 25 s. Three parts of 2000 rows are enough: the previous commit's fix reverted,
both projection cases still fail with the logical error 5 runs out of 5.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119350&sha=606dd128f0cb126dd791c467ef67e34ea9c1eae6&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29
PR: #119350

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VMTxKf7NFjbjoSnJvhMFPd
@nickitat nickitat changed the title [TEST] Read time series table functions on parallel replicas Read time series table functions on parallel replicas Oct 31, 2026
@nickitat
nickitat marked this pull request as ready for review October 31, 2026 16:02
@nickitat
nickitat added this pull request to the merge queue Oct 31, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 31, 2026
@nickitat
nickitat added this pull request to the merge queue Oct 31, 2026
Merged via the queue into master with commit 1ee4912 Nov 1, 2026
179 checks passed
@nickitat
nickitat deleted the pr-table-function-anchor branch November 1, 2026 00:23
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 1, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 10, 2026
…allel replicas

Since the master merge, a table function that names the same table on every
replica (`timeSeriesSamples` and the other `timeSeries*` functions, ClickHouse#119350)
can drive a query-based parallel-replicas read. A `TimeSeries` table of
version 9 or earlier may use an external samples table, which can also be the
`TO` target of a non-APPEND refreshable materialized view. Such a read failed
the same way as a direct read: a refresh between resolution and planning
renamed the held storage to `.tmp.inner_id.<uuid>`, so the replicas'
table-status check dropped every remote replica (`ALL_CONNECTION_TRIES_FAILED`
with `parallel_replicas_local_plan = 0`).

isRefreshableMaterializedViewSwapTarget now takes any resolved table or table
function node; both record the storage id at resolution, before a later
refresh renames the storage. Table functions are checked at the candidate gate
(initiator only), the read-step gate and the RIGHT JOIN gate. The candidate
gate matters only within the refresh window: findQueryForParallelReplicas
re-checks its candidates on dummy tables named by the storage's current id,
which misses the renamed table and lets the whole subquery be sent.

05452 case F covers a direct read, a subquery and a RIGHT JOIN through
`timeSeriesSamples`, with APPEND controls.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 15, 2026
… replicas

Since ClickHouse#119350 a table function read is not distributed when any (sub)query sets
`additional_table_filters`, so a query that reads only through such a table
function runs locally and its filters apply as written. The check still saw a
session-relative key (the bare name of the table the function reads) and threw
`SUPPORT_IS_DISABLED` in strict mode with `serialize_query_plan = 1` and no local
plan, which made `05461_parallel_replicas_table_function_additional_table_filters`
fail on this branch after merging master.

On that branch the check now also requires `findTableForParallelReplicas` to find
a table that can be read with replicas, the same anchor the planner uses. The
other branches of the check are unchanged, and a query with a table that can be
read with replicas is still checked as before.

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-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). 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.

2 participants