Repository navigation
LIMIT ... AFTER/UNTIL to return rows between boundary conditions - #99508
Conversation
|
implements this #15341 (comment) |
|
Does this return N rows after every occurrence of the condition, or only the first? It would be cool to have a variant |
Only after the first. |
|
Workflow [PR], commit [8397fda] Summary: ✅
AI ReviewSummaryThis PR adds Findings❌ Blockers
Tests
Final Verdict❌ Not ready to merge: the new boundary-expression validation still lets LLVM Coverage ReportMeasured on commit 8397fda.
Changed lines: Changed C/C++ lines covered: 1251/1323 (94.56%) · Uncovered code |
…`limitAlwaysReadsTillEnd`
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes123 object files changed (+353.89 KiB total), 3 added.
716 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 units1051 translation units recompiled, 6645 s compile time in total, 1048 of them have a recent master baseline. |
# Conflicts: # src/Core/ProtocolDefines.h
…pped `LIMIT AFTER` range
…IN` from a child query tree
Reading a Merge table that matches a Distributed table with a JOIN and a GROUP BY ... WITH TOTALS/ROLLUP/CUBE/GROUPING SETS failed outright: Code: 48. DB::Exception: WITH TOTALS, ROLLUP, CUBE or GROUPING SETS are not supported without aggregation: Child table: default.d. (NOT_IMPLEMENTED) replaceTableExpressionAndRemoveJoin builds a separate query for each matched child table. It clears the GROUP BY, ORDER BY and LIMIT BY node lists, because they may reference the JOIN it removes, and it replaces the projection with plain column identifiers, which drops the aggregate functions. It did not reset the modifier flags belonging to those clauses, so the child query was left with no GROUP BY keys and no aggregates while still carrying WITH TOTALS. When the child is a Distributed table that tree is re-analyzed and validateAggregates rejects it. The child query the rewrite produced is visible in the server log as: SELECT `__table1`.`a` AS `a` FROM `t_99508` AS `__table1` WITH TOTALS The old-analyzer twin of this rewrite, removeJoin in getHeaderForProcessingStage.cpp, already resets all seven of these flags, which is why enable_analyzer = 0 was unaffected. This mirrors it, in the same spirit as the is_limit_with_ties reset the function already performs for the same reason: a clause modifier flag must not outlive the clause it modifies. WITH TOTALS, WITH ROLLUP, WITH CUBE, and GROUPING SETS with more than one grouping set are the independently observable cases, reachable through JOIN, ARRAY JOIN, GLOBAL JOIN and the merge() table function. A single-set GROUPING SETS list is collapsed during resolution by GroupingFunctionsResolvePass, which clears the flag with it, so only a multi-set list survives to reach this rewrite. is_group_by_all, is_order_by_all and is_limit_by_all are cleared during resolution, so they cannot be witnessed on their own today; they are reset as well so that the two rewrites agree, and because a surviving order_by_all with no ORDER BY list later dereferences a null orderBy() in ASTSelectQuery. The AST side of that pair was fixed in ClickHouse#109139 and ClickHouse#113759. Reported by Nihal Z. Miaji on ClickHouse#99508 as a pre-existing master bug. Co-Authored-By: Claude Opus 5 <[email protected]>
Closes #15341
Adds a range form of
LIMITthat selects rows by boundary conditions instead of by position:AFTER start_exprstarts the output at the first row wherestart_expris true (inclusive);UNTIL end_exprstops before the first row whereend_expris true (exclusive); the optionalncaps the length of the opened range.AFTER ... ALLopens a new range at every matching row and returns their union without duplicating overlapping rows, sonis the length of each range. "First" is defined by stream order, soORDER BYcontrols it; withoutALL, anUNTILmatch before the firstAFTERmatch yields an empty result. Boundary expressions may reference columns that are not selected.This answers questions like "give me the rows from the first error until the next checkpoint" or "N rows after every marker" in one pass over an ordered stream, where today users either post-filter with window functions or fetch everything and cut client-side.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
New
LIMITforms that select rows by boundary conditions instead of by position, following the stream order (useORDER BYto define it):LIMIT n AFTER condreturns up tonrows starting at the first row wherecondis true (SELECT number FROM numbers(10) ORDER BY number LIMIT 3 AFTER number >= 5returns5, 6, 7);LIMIT UNTIL condreturns the rows before the first row wherecondis true (LIMIT UNTIL number >= 3returns0, 1, 2);LIMIT AFTER cond1 UNTIL cond2returns the rows between the two (LIMIT AFTER number >= 2 UNTIL number >= 6returns2, 3, 4, 5);LIMIT n AFTER cond ALLopens a range ofnrows at every matching row and returns their union without duplicates (LIMIT 2 AFTER number IN (2, 6) ALLreturns2, 3, 6, 7).Documentation entry for user-facing changes
Workflow [PR]
Sync PR [sync-upstream/pr/99508]