Skip to content

Conversation

@sim1984
Copy link
Contributor

@sim1984 sim1984 commented Nov 25, 2025

The GENERATE_SERIES function behaves incorrectly at boundary values ​​for the BIGINT and INT128 types. A loop occurs with the BIGINT type, and an exception is thrown with the INT128 type. Examples of incorrect behavior:

select * from generate_series(-9223372036854775807, -9223372036854775809, -1) as x rows 10;
INPUT message field count: 0
 
OUTPUT message field count: 1
01: sqltype: 32752 INT128 scale: 0 subtype: 0 len: 16
  :  name: GENERATE_SERIES  alias: GENERATE_SERIES
  : table:   schema:   owner:
 
                              GENERATE_SERIES
=============================================
                         -9223372036854775807
                         -9223372036854775808
                         -9223372036854775809

(OK, expected)

select * from generate_series(-9223372036854775807, -9223372036854775808, -1) as x rows 10;
INPUT message field count: 0
 
OUTPUT message field count: 1
01: sqltype: 580 INT64 scale: 0 subtype: 0 len: 8
  :  name: GENERATE_SERIES  alias: GENERATE_SERIES
  : table:   schema:   owner:
 
      GENERATE_SERIES
=====================
 -9223372036854775807
 -9223372036854775808
  9223372036854775807 // <-- Error. 
  9223372036854775806
  9223372036854775805
  9223372036854775804
  9223372036854775803
  9223372036854775802
  9223372036854775801
  9223372036854775800
===============

For INT128 example:

select * from generate_series(bin_shl(cast(2 as int128),126)+1, bin_shl(cast(2 as int128),126), -1) as x rows 10;
INPUT message field count: 0

OUTPUT message field count: 1
01: sqltype: 32752 INT128 scale: 0 subtype: 0 len: 16
  :  name: GENERATE_SERIES  alias: GENERATE_SERIES
  : table:   schema:   owner:

                              GENERATE_SERIES
=============================================
     -170141183460469231731687303715884105727
Statement failed, SQLSTATE = 22003
arithmetic exception, numeric overflow, or string truncation
-Integer overflow.  The result of an integer operation caused the most significant bit of the result to carry.

@sim1984 sim1984 requested a review from asfernandes November 30, 2025 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants