Repository navigation
Fix wrong results of the trivial GROUP BY LIMIT optimization with DISTINCT, QUALIFY, window functions or arrayJoin - #114695
Conversation
The trivial `GROUP BY ... LIMIT` optimization (`optimize_trivial_group_by_limit_query`, default `true`) caps the aggregation at `LIMIT + OFFSET` distinct keys, which is only sound when the LIMIT directly cuts the produced groups. The shape check missed four constructs that consume or filter the groups between the aggregation and the LIMIT, and each of them returned wrong results with default settings: - `DISTINCT` collapses the projected groups: `SELECT DISTINCT intDiv(k, 25000) FROM t GROUP BY k LIMIT 3` returned 1 row instead of 3. - A window function in the projection is evaluated over all groups (`hasWindow` only catches the named `WINDOW` clause, not inline `OVER ()`): `SELECT count() OVER () FROM t GROUP BY k LIMIT 5` returned ~100 instead of 100000. - `arrayJoin` in the projection can drop rows, so the kept groups may produce fewer rows than the LIMIT while more groups exist: 1 row instead of 10. - `QUALIFY` filters the groups after the aggregation: 0 rows instead of 10 (a `QUALIFY` on plain group keys alone is pushed down below the aggregation and was not affected; a window function keeps it in place). The pass now skips these shapes, keeping the optimization to queries where the first `LIMIT + OFFSET` groups are a valid unspecified subset of the result. Closes: #114693 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
Workflow [PR], commit [29a48e9] Summary: ✅
AI ReviewSummaryThis PR tightens Final Verdict✅ No remaining review findings on the current PR head. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 13/13 (100.00%) · Uncovered code |
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. Compile time of recompiled translation units9 translation units recompiled, 22 s compile time in total, 9 of them have a recent master baseline. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Yes, this is good.
Backport #114695 to 26.7: Fix wrong results of the trivial GROUP BY LIMIT optimization with DISTINCT, QUALIFY, window functions or arrayJoin
Backport #114695 to 26.6: Fix wrong results of the trivial GROUP BY LIMIT optimization with DISTINCT, QUALIFY, window functions or arrayJoin
Backport #114695 to 26.5: Fix wrong results of the trivial GROUP BY LIMIT optimization with DISTINCT, QUALIFY, window functions or arrayJoin
…GROUP BY LIMIT optimization with DISTINCT, QUALIFY, window functions or arrayJoin
…uard The guard checked the stored `FunctionNode` name literally, so with `normalize_function_names = 0` the case-insensitive alias `unnest` reached the query tree unchanged and bypassed it, reproducing the wrong results the guard exists to prevent. Compare canonical names via `getFunctionCanonicalNameIfAny` instead, as requested by the automated review. Also remove the duplicated window/`arrayJoin` check from `OptimizeTrivialGroupByLimitPass` (the shared `getTrivialGroupByLimit` performs it), and trim test 04837 to the planner-side subquery forms plus the new `unnest` case: the top-level `INSERT ... SELECT` forms are covered by 04840, which came in with the backportable guards of #114695 that have merged into master since.
…GROUP BY LIMIT optimization with DISTINCT, QUALIFY, window functions or arrayJoin
Closes: #114693
Related: #114690
The trivial
GROUP BY ... LIMIToptimization (optimize_trivial_group_by_limit_query, defaulttruesince 26.5) caps the aggregation atLIMIT + OFFSETdistinct keys, which is only sound when the LIMIT directly cuts the produced groups. The shape check missed four constructs that consume or filter the groups between the aggregation and the LIMIT, and each of them returned wrong results with default settings (verified on master; reproducers in the issue and in the added test):DISTINCTcollapses the projected groups:SELECT DISTINCT intDiv(k, 25000) FROM t GROUP BY k LIMIT 3returned 1 row instead of 3.hasWindowonly catches the namedWINDOWclause, not an inlineOVER ()):SELECT count() OVER () FROM t GROUP BY k LIMIT 5returned ~100 instead of 100000.arrayJoinin the projection can drop rows (empty arrays), so the kept groups may produce fewer rows than the LIMIT while more groups exist: 1 row instead of 10.QUALIFYfilters the groups after the aggregation: 0 rows instead of 10 (aQUALIFYon plain group keys alone is pushed down below the aggregation and was not affected; a window function in the projection keeps it in place).The pass now skips these shapes. #114690 contains the same guards as part of a shared shape-check helper; whichever merges second needs a trivial conflict resolution, and this separate pull request keeps the wrong-results fix minimal and backportable on its own.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results of the trivial
GROUP BY ... LIMIToptimization (settingoptimize_trivial_group_by_limit_query) for queries withDISTINCT,QUALIFY, window functions, orarrayJoinin the projection: these consume or filter the groups after the aggregation, so capping the aggregation atLIMIT + OFFSETkeys could return too few rows or wrong values. The optimization no longer applies to such queries.Documentation entry for user-facing changes
Version info
26.8.1.1479(included in26.8and later)26.7.4.55,26.6.3.60,26.5.7.60