Repository navigation
Support negative step in generate_series table function - #101056
Merged
Merged
Conversation
Closes #90075 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Contributor
|
Workflow [PR], commit [addbdd2] Summary: ✅ AI ReviewSummaryThis PR adds negative ClickHouse Rules
Final Verdict
|
…arge UInt64 steps - Fix undefined behavior when step is `INT64_MIN`: use unsigned arithmetic (`UInt64(0) - static_cast<UInt64>(step_val)`) instead of signed negation. - Preserve backward compatibility for large positive UInt64 step values (> `INT64_MAX`): try Int64 conversion first to detect negative values, fall back to UInt64 for values that don't fit in Int64. - Remove the now-unnecessary `evaluateSignedArgument` method; step parsing is done inline with the dual-conversion approach. - Fix the element count formula for positive steps != 1 to use `(stop - start) / abs_step + 1` instead of `(stop - start) + 1`. - Add test for large UInt64 step backward compatibility. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The `limit` parameter of `StorageSystemNumbers` defines the integer domain size [offset, offset + limit), not the number of stepped output values. The stepping is applied later during range intersection in `ReadFromSystemNumbersStep`. Pre-dividing by step shrinks the domain and drops values that should be generated. Restore the original formula `(stop - start) + 1` for the positive step branch. The descending branch is unaffected because it uses `DescendingNumbersSource` which generates exactly `limit` values. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The expression `(start - stop) / abs_step + 1` wraps to 0 when the quotient equals `UInt64::max`, causing an empty result instead of the expected series. Now throws an exception when the cardinality would overflow UInt64. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…ped count The `limit` parameter of `StorageSystemNumbers` is the raw domain window size (number of consecutive integers), not the number of stepped values. `ReadFromSystemNumbersStep` handles stepping internally via `steppedRangeFromRange`. The previous code divided by step before passing the limit, causing the domain to be too small and producing fewer values than expected (e.g. `generate_series(10, 20, 3)` returned 2 values instead of 4). The descending path using `DescendingNumbersSource` correctly needs the actual stepped count, so `computeCardinality` is kept for that case only. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
`(stop - start) + 1` wraps to 0 when `stop - start == UInt64_MAX`, causing an empty result instead of an error. Add the same overflow guard as the descending path. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Member
|
I have refactored the code to improve readability. Additionally, added Fixed one of the existing bugs: SELECT * FROM generate_series(0, CAST('18446744073709551615', 'UInt64'), toUInt64(9223372036854775808));This query returned empty result (wrong). Now, it correctly returns |
nihalzp
approved these changes
Mar 30, 2026
nihalzp
force-pushed
the
generate-series-negative-step
branch
from
April 3, 2026 12:11
abfa926 to
aefb5b8
Compare
Member
Author
|
The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix. |
…ative-step # Conflicts: # src/Storages/System/StorageSystemNumbers.cpp
Merged
1 task done
Contributor
LLVM Coverage Report
Changed lines: 91.40% (85/93) · Uncovered code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
generate_seriestable function now supports negative step values for generating descending sequences, matching PostgreSQL behavior.Previously,
SELECT * FROM generate_series(99, 0, -1)would fail withThe value -1 is not representable as UInt64. Now it correctly produces 99, 98, 97, ..., 0.Closes #90075
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The
generate_seriestable function now supports negative step values for generating descending sequences, e.g.SELECT * FROM generate_series(99, 0, -1).Documentation entry for user-facing changes
Version info
26.5.1.125