Skip to content

fix: generate_series overflow panics at i64 boundary and out-of-range dates#23723

Open
u70b3 wants to merge 1 commit into
apache:mainfrom
u70b3:fix/generate-series-overflow
Open

fix: generate_series overflow panics at i64 boundary and out-of-range dates#23723
u70b3 wants to merge 1 commit into
apache:mainfrom
u70b3:fix/generate-series-overflow

Conversation

@u70b3

@u70b3 u70b3 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Supersedes #22250 (closed unmerged by its author on 2026-06-04 after the approach was approved). Note: both issues are currently assigned to @xiedeyantu (idle since 2026-05; #22193 was explicitly blocked on #22250). I've left a note on both issues; happy to coordinate if the assignee has work in progress.

Rationale for this change

Two overflow bugs in the generate_series / range table functions, both panicking in debug builds and silently wrapping in release:

  1. panic: generate_series table function overflows when integer step passes i64::MAX #22208 — integer series: SELECT * FROM generate_series(9223372036854775806, 9223372036854775807, 2) panics with attempt to add with overflow when advancing past i64::MAX. PostgreSQL and DuckDB both return one row (9223372036854775806), stopping after the last reachable value.
  2. panic: multiply overflow in generate_series(DATE, DATE, INTERVAL) for dates far from 1970 #22193 — date series: converting Date32 days to timestamp nanoseconds uses an unchecked multiplication, so generate_series(DATE '0001-01-01', ...) panics at planning time. Dates outside the nanosecond timestamp range (1677-09-21 – 2262-04-11) cannot be represented and must error cleanly instead.

What changes are included in this PR?

  • datafusion/functions-table/src/generate_series.rs
    • SeriesValue::advance now takes end: &mut Self; the i64 implementation uses checked_add and, on overflow, clamps end so the series terminates after the last reachable value (the approach approved in fix generate_series table function overflows #22250). The per-batch loop stops right after emitting the final value. TimestampValue::advance already errors on out-of-range interval addition and is unchanged apart from the signature.
    • The Date32→nanoseconds conversion uses checked_mul and returns a planning error naming the offending argument.
  • datafusion/sqllogictest/test_files/table_functions.slt
    • Regression cases: positive/negative step overflow, landing exactly on i64::MAX, range (end-exclusive) overflow, and first/second date argument out of range.

Are these changes tested?

Yes:

  • New sqllogictest cases (exact reproducers from both issues, plus boundary variants); verified the integer-case outputs match PostgreSQL/DuckDB.
  • Verified cargo fmt, workspace clippy (avro,integration-tests,extended_tests, -D warnings), cargo test -p datafusion-functions-table, and the extended workspace suite (ci profile with avro,json,backtrace,extended_tests,recursive_protection,parquet_encryption) — all green.

Are there any user-facing changes?

Only the bug fixes: integer series near i64::MAX/MIN now return the reachable values instead of panicking or wrapping (matching PostgreSQL/DuckDB), and out-of-range dates produce a planning error instead of a panic. No API changes.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Jul 20, 2026
… dates

Two overflow fixes in the generate_series/range table functions:

- Integer series: advancing past i64::MAX (or i64::MIN for negative
  steps) panicked in debug builds and silently wrapped in release.
  advance() now uses checked_add and clamps the series end so iteration
  stops after the last reachable value, matching PostgreSQL/DuckDB
  (e.g. generate_series(9223372036854775806, 9223372036854775807, 2)
  returns one row 9223372036854775806).

- Date series: converting Date32 days to timestamp nanoseconds used an
  unchecked multiplication, panicking at planning time for dates outside
  the nanosecond timestamp range. It now returns a planning error.

Supersedes apache#22250 (closed unmerged); the integer-side approach follows
the one approved there.

Closes apache#22208, closes apache#22193.
@github-actions

Copy link
Copy Markdown

Thank you for opening this pull request!

Reviewer note: cargo-semver-checks reported the current version number is not SemVer-compatible with the changes in this pull request (compared against the base branch).

Details
     Cloning apache/main
    Building datafusion-functions-table v54.0.0 (current)
       Built [  45.812s] (current)
     Parsing datafusion-functions-table v54.0.0 (current)
      Parsed [   0.012s] (current)
    Building datafusion-functions-table v54.0.0 (baseline)
       Built [  44.819s] (baseline)
     Parsing datafusion-functions-table v54.0.0 (baseline)
      Parsed [   0.012s] (baseline)
    Checking datafusion-functions-table v54.0.0 -> v54.0.0 (no change; assume patch)
     Checked [   0.090s] 223 checks: 222 pass, 1 fail, 0 warn, 30 skip

--- failure trait_method_parameter_count_changed: pub trait method parameter count changed ---

Description:
A trait method now takes a different number of parameters.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#trait-item-signature
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.48.0/src/lints/trait_method_parameter_count_changed.ron

Failed in:
  SeriesValue::advance now takes 2 instead of 1 parameters, in file /home/runner/work/datafusion/datafusion/datafusion/functions-table/src/generate_series.rs:88

     Summary semver requires new major version: 1 major and 0 minor checks failed
    Finished [  92.433s] datafusion-functions-table
    Building datafusion-sqllogictest v54.0.0 (current)
       Built [ 181.360s] (current)
     Parsing datafusion-sqllogictest v54.0.0 (current)
      Parsed [   0.023s] (current)
    Building datafusion-sqllogictest v54.0.0 (baseline)
       Built [ 181.256s] (baseline)
     Parsing datafusion-sqllogictest v54.0.0 (baseline)
      Parsed [   0.023s] (baseline)
    Checking datafusion-sqllogictest v54.0.0 -> v54.0.0 (no change; assume patch)
     Checked [   0.086s] 223 checks: 223 pass, 30 skip
     Summary no semver update required
    Finished [ 365.331s] datafusion-sqllogictest

@github-actions github-actions Bot added the auto detected api change Auto detected API change label Jul 21, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.71%. Comparing base (c111f0a) to head (bb38d8e).

Files with missing lines Patch % Lines
datafusion/functions-table/src/generate_series.rs 90.90% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23723      +/-   ##
==========================================
- Coverage   80.71%   80.71%   -0.01%     
==========================================
  Files        1089     1089              
  Lines      368275   368291      +16     
  Branches   368275   368291      +16     
==========================================
+ Hits       297250   297255       +5     
- Misses      53309    53318       +9     
- Partials    17716    17718       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto detected api change Auto detected API change functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

2 participants