Skip to content

fix generate_series table function overflows#22250

Open
xiedeyantu wants to merge 2 commits into
apache:mainfrom
xiedeyantu:generate_series
Open

fix generate_series table function overflows#22250
xiedeyantu wants to merge 2 commits into
apache:mainfrom
xiedeyantu:generate_series

Conversation

@xiedeyantu
Copy link
Copy Markdown
Member

Which issue does this PR close?

Closes #22208.

Rationale for this change

The table-valued generate_series(start, stop, step) function uses an integer iterator that called current += step without overflow checking. When the series approaches i64::MAX and the step would advance past it, Rust's debug build panics and release build silently wraps, producing incorrect results or a crash visible to end users.

What changes are included in this PR?

  • In generate_series.rs, replaced the unchecked *self += step in the SeriesValue for i64 advance method with checked_add, returning a DataFusionError::Execution on overflow instead of panicking.
  • Added a regression test in table_functions.slt that verifies SELECT * FROM generate_series(9223372036854775806, 9223372036854775807, 2) returns an execution error rather than panicking.

Are these changes tested?

Yes. A statement error case is added to table_functions.slt covering the exact query from the bug report. The timestamp-based advance implementation already used Option-returning arithmetic and was not affected.

Are there any user-facing changes?

Previously this query would cause a panic (or silent wrap in release mode); it now returns a clean DataFusion error: Execution error: generate_series: integer overflow while advancing series.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels May 16, 2026
@Dandandan Dandandan enabled auto-merge May 16, 2026 15:36
@Dandandan Dandandan disabled auto-merge May 16, 2026 15:41

fn advance(&mut self, step: &Self::StepType) -> Result<()> {
*self += step;
*self = self.checked_add(*step).ok_or_else(|| {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, this is what PostgreSQL returns:

Output:

119 ms
   generate_series   
---------------------
 9223372036854775806
(1 row)

And DuckDB does the same.

┌─────────────────────┐
│   generate_series   │
│        int64        │
├─────────────────────┤
│ 9223372036854775806 │
│ (9.22 quintillion)  │
└─────────────────────┘

I think it makes sense to implement the same behavior (i.e. find max value during instantiation).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! I'll align DataFusion's behavior with these database behaviors.

Copy link
Copy Markdown
Contributor

@Dandandan Dandandan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make it compatible with PostgreSQL / DuckDB?

@xiedeyantu
Copy link
Copy Markdown
Member Author

Can we make it compatible with PostgreSQL / DuckDB?

Yes, please wait a moment.

@xiedeyantu
Copy link
Copy Markdown
Member Author

@Dandandan Please review again. Thanks!

include_end: bool,
name: &'static str,
/// Set to true when advancing would overflow, so the next call returns None.
finished: bool,
Copy link
Copy Markdown
Contributor

@Dandandan Dandandan May 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead of adding finished we can just update end and also remove the checked_add (use += again since we don't overflow)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified the implementation.

Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: generate_series table function overflows when integer step passes i64::MAX

2 participants