Repository navigation
Enable lazy materialization for ORDER BY ... LIMIT in distributed plans (make_distributed_plan=1) - #113111
Conversation
`ORDER BY ... LIMIT` under `make_distributed_plan` did not use lazy materialization: the plan is distributed before the lazy-materialization pass runs, so the `LIMIT` ends up above a `GatherExchange` and the pattern no longer matches. The rewrite has to run per fragment on the worker anyway, because the main and lazy reads share in-memory row positions, and the worker already re-optimizes the fragment it deserializes. * `tryMakeDistributedSorting` adds a `LimitStep` for the bound the sorting step already applies: `SortingStep::serialize` drops the limit and the query's `LIMIT` stays on the coordinator, so the worker's sorting step arrived with `limit = 0`. `pushDownLimit` folds it back, which also stops a worker from sorting its whole bucket instead of its top N. * Lazy materialization and lazy FINAL no longer descend below a logical exchange on the initiator, where the two branches would be split across stages. Extra bytes read for a deferred column drop ~20x (200k rows, 200-byte payload, 4 read buckets). Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [75171e7] Summary: ✅
AI ReviewSummaryThis PR restores lazy materialization for Findings
Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 32/32 (100.00%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: The only flagged query is tpch_adapted_1_official Q4, a small improvement (-5.0%, -54ms). This PR is confined to the distributed query-planner: it inserts a local top-N bound inside distributed sorting fragments so ORDER BY ... LIMIT can materialize wide columns lazily, and keeps lazy branches from crossing a logical exchange. Q4 has no LIMIT and this scale-1 single-instance run does not build distributed plans, so the changed code never executes for it; I've downgraded the Q4 improvement to not_sure as run-to-run variance. The envelope/era gates flagged one unstable ClickBench cell (28) but produced no verdict-bearing rows there, which is consistent with the diff. clickbench🟢 No significant changes tpch_adapted_1_officialFlagged queries (1 of 22)
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. Debug info
|
|
The integration test failure shows the exact plan change this PR does - |
| ); | ||
|
|
||
| -- Optimization should not be shipped in distributed plan if exact_rows_before_limit is set | ||
| SELECT 'local top-N in fragment with exact_rows_before_limit=1', countIf(explain LIKE '%Limit (local top-N)%') > 0 |
There was a problem hiding this comment.
exact_rows_before_limit is a contract on rows_before_limit_at_least, but this new regression only asserts that the EXPLAIN output no longer contains Limit (local top-N). That is weaker than the behavior the fix is trying to preserve: any future fragment-local truncation that does not print this exact node would still pass while undercounting the result statistic again. Please add one result-level check that compares the reported rows_before_limit_at_least between make_distributed_plan = 0 and 1 with exact_rows_before_limit = 1.
There was a problem hiding this comment.
How is this weaker?
Your recommendation is to prevent any future breakages with distributed plans, could be unrelated to the lazy materialization optimization enabled in this PR.
There was a problem hiding this comment.
I tried to add a testcase, but what you are reporting has a pre-existing problem. rows_before_limit_at_least is not reported at all for distributed plans. I ran the below query and the field is missing.
SELECT a FROM t_dist_lazy ORDER BY b, a LIMIT 1 FORMAT JSONCompact
SETTINGS make_distributed_plan = 1, exact_rows_before_limit = 1,
output_format_write_statistics = 0, output_format_json_quote_64bit_integers = 0;
Distributed plans use ReadFromDistributedPlanSource and that is not mainitaining/publishing the counter.
For the only comment, I have responded here : https://github.com/ClickHouse/ClickHouse/pull/113111/changes/BASE..75171e7a83efaa2ae0eca1d7e7a980a47b5c044d#r3726612858 |
b311947
ORDER BY ... LIMIT kundermake_distributed_plannever used lazy materialization. The plan was distributed before the lazy-materialization pass runs, so theLIMITends up above aGatherExchangeand the pattern thatoptimizeLazyMaterialization()looked for did not match. Now, each replica fragment now carries top-kLimitStep, so the replica applies lazy materialization over its own parts/ranges and reads the wide columns only for the rows its local top-k kept.Impact : With this implementation, each replica in the query could end up reading
kwide granules and thus across the cluster a total of (num_replicas * k) wide granules could be readChangelog category
Changelog entry
ORDER BY ... LIMIT kqueries in distributed query plans (make_distributed_plan)