Skip to content

Support negative step in generate_series table function - #101056

Merged
alexey-milovidov merged 38 commits into
masterfrom
generate-series-negative-step
Apr 27, 2026
Merged

alexey-milovidov merged 38 commits into
masterfrom
generate-series-negative-step

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 29, 2026 •

Copy link
Copy Markdown
Member

generate_series table function now supports negative step values for generating descending sequences, matching PostgreSQL behavior.

Previously, SELECT * FROM generate_series(99, 0, -1) would fail with The value -1 is not representable as UInt64. Now it correctly produces 99, 98, 97, ..., 0.

Closes #90075

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The generate_series table 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

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.125

Closes #90075

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [addbdd2]

Summary: ✅


AI Review

Summary

This PR adds negative STEP support for generate_series, wires descending generation through StorageSystemNumbers/ReadFromSystemNumbersStep, and extends docs/tests for the new behavior and edge cases. I did not find new blocker- or major-level issues in the current PR head.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Mar 29, 2026
@nihalzp nihalzp self-assigned this Mar 29, 2026
alexey-milovidov and others added 3 commits March 29, 2026 23:03
…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]>
alexey-milovidov and others added 4 commits March 30, 2026 05:31
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]>
alexey-milovidov and others added 2 commits March 30, 2026 09:29
`(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]>
@nihalzp

nihalzp commented Mar 30, 2026

Copy link
Copy Markdown
Member

I have refactored the code to improve readability.

Additionally, added LIMIT pushdown to the negative step path.

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 0 9223372036854775808.

@nihalzp
nihalzp force-pushed the generate-series-negative-step branch from abfa926 to aefb5b8 Compare April 3, 2026 12:11
@alexey-milovidov

Copy link
Copy Markdown
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.

@clickhouse-gh

clickhouse-gh Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.40% -0.10%

Changed lines: 91.40% (85/93) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 27, 2026
Merged via the queue into master with commit 6387321 Apr 27, 2026
325 of 326 checks passed
@alexey-milovidov
alexey-milovidov deleted the generate-series-negative-step branch April 27, 2026 05:28
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

generate_series is not fully-compatible with PostgreSQL

3 participants