SQLite Forum

Integer overflow when calling generate_series

Integer overflow when calling generate_series

(1) By Suyang Zhong (suyang) on 2023-04-26 14:50:42 [source]

Consider the following query:

SELECT count(*) FROM generate_series(9223372036854775807,9223372036854775807);

I suppose the expected result should be 1. However, it takes a long time to run. I have read the documentation and noticed it's not defined in very detail. I also noticed one post before, saying that the default end value was 9223372036854775807.

For your reference, here are some other queries and their results.

> SELECT count(*) FROM generate_series(9223372036854775806,9223372036854775806);
> SELECT count(*) FROM generate_series(1<<64,1<<64);
> SELECT * FROM generate_series(9223372036854775807,9223372036854775807) LIMIT 10;

I also checked with UBSan, the messages are follows, which indicates that the signed long long value overflows

SQLite version 3.42.0 2023-04-26 13:25:30
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> SELECT count(*) FROM generate_series(9223372036854775807,9223372036854775807);
shell.c:4454:18: runtime error: signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long long'

I run on the latest version 3051d6a7.

(2.2) By Larry Brasfield (larrybr) on 2023-04-29 15:48:48 edited from 2.1 in reply to 1 [link] [source]

(Edited to reflect developments.)

A cure for this is checked-in.