Skip to content

Disable parallel replicas for FINAL on a table function - #122492

Merged
fm4v merged 3 commits into
ClickHouse:masterfrom
groeneai:parallel-replicas-final-table-function
Oct 7, 2026
Merged

fm4v merged 3 commits into
ClickHouse:masterfrom
groeneai:parallel-replicas-final-table-function

Conversation

@groeneai

@groeneai groeneai commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #100377
Related: #118130

Changelog category (leave one):

  • Improvement

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

FINAL on a table function now disables parallel replicas the same way as FINAL on a table: the query runs without parallel replicas, or is rejected when enable_parallel_replicas = 2. Reading a TimeSeries table function such as timeSeriesTags with FINAL under parallel replicas now returns each row once.

Description

FINAL (and STREAM on the initiator) disables task-based parallel replicas in Planner::buildPlanForQueryNode, but that check only looked at TableNode. For f(...) FINAL the modifier is stored on the TableFunctionNode, so the setting stayed on. A table function that resolves directly to a MergeTree table (the TimeSeries target functions) then took the storage-level parallel read with FINAL still set:

The check now reads the modifiers from a TableFunctionNode too. This is the same hunk that the drafts #118518 and #119350 carry; those are about #118130 (duplicates without FINAL, UNKNOWN_TABLE), which this PR does not touch.

Behavior change: with enable_parallel_replicas = 2, FINAL on any table function (and STREAM on the initiator) now throws SUPPORT_IS_DISABLED, like the table form. For example, merge(...) FINAL ran silently before, while a Merge table with FINAL threw.

Validation on a debug build of master with a local 3-replica cluster: the unfixed binary aborts every time, including with the stress test settings. The new test 05272 fails on it and passes 50/50 randomized runs with the fix, plus 20/20 each with parallel_replicas_plan_based = 1 and with serialize_query_plan = 1. JOIN, CTE and UNION ALL queries that also aborted before now pass.


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

Version info

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

groeneai and others added 2 commits September 26, 2026 21:51
The planner disables task-based parallel replicas when a table expression
carries FINAL (or STREAM on the initiator), but the check only looked at
table nodes. FINAL written on a table function is stored on the table
function node, so it was skipped and the setting stayed on.

A table function that resolves directly to a MergeTree table, such as the
TimeSeries target functions (`timeSeriesTags` and the like), then took the
storage-level parallel replicas read with FINAL still set. With
`parallel_replicas_local_plan = 1` the coordinated local read hit the
`'!is_parallel_reading_from_replicas'` assertion in `spreadMarkRanges`
(STID 2508-3f82 / 2508-3ce3). With `parallel_replicas_local_plan = 0`
every replica read the whole table, so each row came back once per replica.

Read the modifiers from a table function node too. FINAL on a table
function now behaves like FINAL on a table: refused with
`enable_parallel_replicas = 2`, run without parallel replicas otherwise.

Related: ClickHouse#100377

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Rename the test to `05272_streaming_queries_...`, because the style check requires that name for any test that enables streaming queries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Sep 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding in the first round, none on the fixed head) plus my own cold reviews (2 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test covers FINAL on a table function but not the STREAM refusal that the same change also applies (05272) AGREE, fixed @ f6b330a Added a timeSeriesTags(...) STREAM statement at enable_parallel_replicas = 2 expecting SUPPORT_IS_DISABLED: rows on the unfixed binary, refused with the fix. The test is renamed 05272_streaming_queries_... because the style check requires that name for any test that enables streaming queries
2 💡 The no-FINAL control discards its rows without saying so AGREE, fixed @ f6b330a The comment now states that only the absence of the error is checked
3 💡 The test's header comment names only FINAL; the STREAM refusal is annotated at its own statement noted, not blocking No change

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-48:20260927-005800

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. timeSeriesTags(currentDatabase(), 'ts') FINAL with enable_parallel_replicas = 1, parallel_replicas_local_plan = 1 on a local 3-replica cluster aborts the unfixed debug server 3/3 (also with the stress test settings); with parallel_replicas_local_plan = 0 it returns every row 3 times.
b Root cause explained? The FINAL/STREAM check that disables task-based parallel replicas only looked at TableNode; FINAL on a table function is stored on the TableFunctionNode, so the setting stayed on and the storage-level parallel read ran with FINAL.
c Fix matches root cause? Yes. The check reads the modifiers from both node types; the assertion and the read path are unchanged.
d Test intent preserved / new tests added? New 05272_streaming_queries_parallel_replicas_final_table_function: both parallel_replicas_local_plan values, the enable_parallel_replicas = 2 refusal of FINAL for timeSeriesTags and merge() and of STREAM for timeSeriesTags, and a control without either modifier. Each non-control line fails on the unfixed binary.
e Both directions demonstrated? Yes: the test fails on the unfixed binary (server abort) and passes with the fix, Build IDs checked on the running servers.
f Fix is general across code paths? One check, one site. JOIN (either side), CTE, UNION ALL, IN subquery, timeSeriesMetrics, timeSeriesData and STREAM were run on both binaries; JOIN-left, CTE and UNION ALL also aborted before and pass now.
g Fix generalizes across inputs (params/datatypes/wrappers)? Not a type fix. Covered: modes 1 and 2, parallel_replicas_local_plan 0/1, parallel_replicas_plan_based 0/1, serialize_query_plan 0/1, 50 randomized runs.
h Backward compatible? (maintainer-approved exception only) No setting or format change. With enable_parallel_replicas = 2, FINAL/STREAM on a table function now throws SUPPORT_IS_DISABLED as the table form does; with 1 the query runs without parallel replicas.
i Invariants and contracts preserved? "A table expression with FINAL (or STREAM on the initiator) is read without task-based parallel replicas" now holds for both table-expression node types; the mode-2 throw, the early break and the initiator-only STREAM condition are unchanged.

Session id: cron:clickhouse-impl-slot-5:20260926-204100

@clickhouse-gh

clickhouse-gh Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8e59092]

Summary: ✅


AI Review

Summary

This PR extends the analyzer-side parallel-replicas guard to read TableExpressionModifiers from TableFunctionNode as well as TableNode, so ... FINAL and initiator-side ... STREAM on table functions disable or reject parallel replicas the same way table reads already do. The added stateless coverage exercises the duplicate-row path, the local-plan assertion path, and the forced-refusal cases on timeSeriesTags and merge(). I did not find a correctness, compatibility, or test-coverage issue in the current head.

Final Verdict

✅ Approve. No new findings.

LLVM Coverage Report

Measured on commit 8e59092.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.50% 79.60% +0.10%

Changed lines: Changed C/C++ lines covered: 12/12 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Sep 27, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Sep 27, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8e59092c1 with master 69ea8da8c (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

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

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - f6b330a

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
AST fuzzer (amd_debug, targeted, old_compatibility) / Logical error: Column A already added for reading (STID: 4792-5e4c) trunk bug, not caused by this diff: with query_plan_optimize_lazy_final = 1 and a Nullable sorting key, lazy FINAL registers the text-index read column a second time when PREWHERE and WHERE carry the same predicate. It reproduces on master on a plain single-node table, without this PR. In the last 30 days it failed on 6 PRs (0 master rows), always through optimizeLazyFinal. This diff changes only the parallel replicas FINAL guard in Planner.cpp a fix task is created (investigating at full effort, fixing-PR link to follow here)
PR rollup, red only because of the row above a fix task is created (the same one as for the row above)

All 169 check runs completed (127 pass, 41 not selected, 1 red), Finish Workflow and
Mergeable Check are green, and CH Inc sync passed. CIDB has one FAIL test row on this
head, the row above.

Session id: cron:our-pr-ci-monitor:20260927-123259

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the AST fuzzer (amd_debug, targeted, old_compatibility) / Logical error: Column A already added for reading (STID: 4792-5e4c) row of the ledger above, and the PR rollup row it causes: #122534.

@devcrafter devcrafter self-assigned this Sep 27, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 37 queries analysed

No query shows a performance change that can be tied to this PR. The only flagged row, ClickBench Q8 (+10.3%, median 376 ms vs 341 ms), was held at not_sure because its value falls inside master's recent range of 307-383 ms, and the diff supports that call. The diff only extends the planner's parallel-replicas FINAL/STREAM check so it also covers table functions. That check runs once while a query is being planned, and it does nothing for queries without FINAL or STREAM. The PR measurements on this query were also noisier than usual, and the ClickBench and TPC-H runs for this PR shared a machine, which fits run-to-run variance rather than a code effect.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 8 not_sure 341 376 +10.3% <0.0001 The +10.3% shift stays inside master's recent range of 307-383 ms. The PR only changes a once-per-query planner check for FINAL/STREAM on table functions, which Q8 never uses, so the change cannot be attributed to this PR.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: c36b6d00-604b-400a-a23e-c884693ff51f
  • MIRAI run: f27cca7c-1290-4e17-8ead-589d8f329c8f
  • PR check IDs:
    • clickbench_2671706_1790833987
    • clickbench_2671712_1790833987
    • clickbench_2671723_1790833986
    • tpch_adapted_1_official_2671749_1790833986
    • tpch_adapted_1_official_2671763_1790833987
    • tpch_adapted_1_official_2671777_1790833987

pull Bot pushed a commit to peijunlin2008/ClickHouse that referenced this pull request Sep 29, 2026
…oint sorting key

`trySplitNonIntersectingParts` returns "not replaced" for a primary key the parts splitter
cannot compare (`isSafePrimaryKey`: `Nullable`, `Float*`, `BFloat16`, `Variant`, `Dynamic`,
`JSON`, also nested), for mixed `ASC`/`DESC` key columns, and when a full non-intersecting
replacement selects nothing. All three return before the checks that keep a direct read from
a text index (ClickHouse#106894) and a query that stops reading early (ClickHouse#110576) away from the
set-building plan, so `optimizeLazyFinal` built that plan anyway. Its read inherits the
`__text_index_*` column name from `getAllColumnNames` but not the index read tasks that
produce it: the filter drops rows, and when the `WHERE` repeats the `PREWHERE` predicate,
`set_plan.optimize` rewrites the copy to the same name and `createReadTasksForTextIndex`
throws `Column __text_index_... already added for reading`.

Apply the same two conditions right after the split. On every path that reaches the new
check with either condition true the reading step is still untouched, so the query keeps the
regular `FINAL` read, as it already does for a comparable key. The in-split checks stay, as
they must run before the partial split narrows the reading step. With both conditions false
the control flow is unchanged.

On 26.8.10.6, `SELECT count() FROM tab FINAL WHERE str = 'ccc'` with only
`query_plan_optimize_lazy_final = 1`, over a `ReplacingMergeTree` with a `Nullable(UInt64)`
sorting key and a text index on `str`, returns 0 instead of 1.

Found by `AST fuzzer (amd_debug, targeted, old_compatibility)` (STID 4792-5e4c) on
ClickHouse#122492:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122492&sha=f6b330abde632fb2c271eb66e098d4230b4a7004&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29

Tests: `02346_text_index_bug106809` gains a `Nullable`-key section (its `PREWHERE` + `WHERE`
row is 1 because `PREWHERE` is evaluated before `FINAL`), and
`04539_lazy_final_read_in_order_limit_disabled` gains `Float64`-key rows for both gates plus
a positive control.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fm4v
fm4v added this pull request to the merge queue Oct 7, 2026
Merged via the queue into ClickHouse:master with commit b0918a4 Oct 7, 2026
149 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 7, 2026
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-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-improvement Pull request with some product improvements 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