Repository navigation
In case of trivial views, push whole outer query to shards. - #101791
Conversation
|
Hi team, If there are some better ideas on how to approach the problem I am happy to rework the PR. |
|
Workflow [PR], commit [9a26fca] Summary: ✅
AI ReviewSummaryThis PR adds Final Verdict✅ No new actionable findings on the current PR head. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 299/317 (94.32%) · Uncovered code |
f83b984 to
78882fe
Compare
b7949ee to
f04444a
Compare
7c40bc1 to
1ba5413
Compare
3a85fdd to
f0ad9ed
Compare
f0ad9ed to
e33f22e
Compare
|
@alexey-milovidov : all comments from |
e33f22e to
5bb0aa4
Compare
…ables Master (c260811, hardened by 43718e2) now rejects a row policy defined directly on a Distributed table with ILLEGAL_PREWHERE instead of silently returning unfiltered rows: a Distributed table only ships the query text to the remote servers, so the filter can never actually be applied there. This is intentional, documented behavior, unrelated to the trivial- view pushdown, and reproduces identically with no view involved at all (confirmed with a plain `SELECT ... FROM dist` against a Distributed table carrying such a policy). 04098's dist-level-policy scenario predates this change and asserted the old behavior (pushdown suppressed, query still returns correct filtered results via the standard path). Update it to assert the new expected error instead. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…group_by StorageView::readImpl fetches the view's raw rows and performs the outer GROUP BY entirely on the initiator, so max_rows_to_group_by is enforced once against the global key set. The pushdown ships the whole GROUP BY to each shard instead, so each shard's Aggregator enforces the limit independently against only its own local rows; for group_by_overflow_mode = 'any'/'break' the merge phase does not re-apply the cap globally (Aggregator::ensureLimitsFixedMapMerge), so shards could each keep a different, locally-permitted set of keys and the initiator would return more groups in total than the limit allows. Confirmed empirically via system.query_log: with the pushdown off, the query shipped to each shard has no GROUP BY at all (raw SELECT); with it on, the full GROUP BY/aggregation is shipped and each shard aggregates independently. Suppress the pushdown whenever the outer query has a GROUP BY and max_rows_to_group_by is non-zero, matching the precedent set by AggregatingStep::canUseShardedAggregation and useDataParallelAggregation, which disable independent aggregation for the same reason. Co-Authored-By: Claude Sonnet 5 <[email protected]>
|
@alexey-milovidov all builds green, no new concerns from AI reviewer (at least at the moment) |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes6 object files changed (+79.95 KiB total), 0 added.
737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units27 translation units recompiled, 272 s compile time in total, 27 of them have a recent master baseline. |
tryGetTrivialViewUnderlyingStorage rejects a view whose own body's WHERE contains a subquery, so such a view is never eligible for the trivial pushdown regardless of the setting. Every existing *trivial_view_pushdown* test places its subquery in the outer query, a view-keyed additional_table_filter, or a row policy -- none defines the view body itself with a subquery, so a regression in that guard would go unnoticed. Confirmed empirically that the guard already suppresses the pushdown correctly; no source change needed. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…projection tryGetTrivialViewUnderlyingStorage rejects a view whose own projection contains an aggregate, a window function, or a scalar subquery, so such a view is never eligible for the trivial pushdown. 04695 and 04372 cover an aggregating outer query, but no existing test defines a non-trivial view body like this, so a regression in that guard would go unnoticed and let each shard emit partial aggregate/window/subquery results instead of a single value computed once on the initiator. Confirmed empirically that the guard already suppresses the pushdown correctly; no source change needed. Co-Authored-By: Claude Sonnet 5 <[email protected]>
|
@alexey-milovidov: green again :-) and no concerns from the AI reviewer :-) |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Thanks! The implementation is complex as it checks a lot of corner cases. But it is well contained in just two files, so good to have.
|
@simonmichal @alexey-milovidov — this PR looks backward incompatible but is categorized as Why: The setting defaults to Suggested category: |
|
@simonmichal @alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |




Changelog category:
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
For views whose body is a plain
SELECTover a singleDistributedtable, the whole outer query is now pushed to the shards (new settingoptimize_trivial_view_pushdown_to_distributed, enabled by default). This changes observable behaviour for such views:FINALandSAMPLEwritten on the view reference are now propagated to the shard-local table instead of being ignored, andextremesis not reported on single-shard clusters. Setoptimize_trivial_view_pushdown_to_distributed = 0to restore the previous behaviour.Documentation entry for user-facing changes
Description / Proposed Solution
When a VIEW is defined over a Distributed table, ClickHouse traditionally executes it on the shards without enclosing outer query. This means filters and expressions declared in the outer query are evaluated on the coordinator after pulling raw data from shards.
For views whose body is a plain SELECT (column references, *, or arbitrary expressions — but no aggregation, grouping, ordering, joins, window functions, or scalar subqueries) over a single Distributed table, we can do better: inline the view body as a subquery and hand the whole thing to StorageDistributed. Each shard then receives the full outer query with the view body inlined, evaluates it against its local table, and only ships the result back.
A view qualifies as "trivial" if its inner query:
The optimization can be disbaled by setting (enabled by default):
Example:
Env setup:
Performance:
Plan:
Note
Medium Risk
Changes query planning/execution for a subset of views over
Distributedtables and touches access checks/row policy enforcement and SQL SECURITY semantics, which can affect correctness and security-sensitive behavior.Overview
Adds a new default-on setting
optimize_trivial_view_pushdown_to_distributedto inline trivial views overDistributedtables and push the full outer query down to shards, reducing coordinator-side filtering/processing and network transfer.Implements planner rewrites to swap the view table expression with an analyzed subquery, merge
FINAL/SAMPLEmodifiers, and preserve semantics by suppressing pushdown when the outer query contains non-deterministic functions, while also explicitly handling SQL SECURITY modes, row-policy injection/logging, and column-pruned privilege checks.Extends integration/stateless tests to cover modifier propagation, non-determinism suppression, row-policy enforcement, SQL SECURITY behavior, and interactions with
max_rows_to_read_leaf.Reviewed by Cursor Bugbot for commit 583e1e6. Bugbot is set up for automated code reviews on this repo. Configure here.
Version info
26.8.1.1380(included in26.8and later)