timestamp `range` / `generate_series` ignore LIMIT and break `sum()` because the row estimate is used as a maximum
Author: kita-renjiCreated Sep 29, 2026Updated Sep 29, 2026
Labelsneeds triage
What happens?
- On v2.0, a constant LIMIT over a TIMESTAMP
rangeor a TIMESTAMPTZrange/generate_seriescan return more rows than the LIMIT. EXPLAIN shows no LIMIT operator. - On v2.0, a filter on
sum(...)over such a series can drop the row: sum's result statistics are computed from the same number. - On 1.5.5 as well,
sum(bigint)over such a series can silently overflow and return a negative number. 1.5.5 picks the int64 no-overflow kernel based on the same number. - Cause: these functions estimate the row count by converting the step to microseconds and return it as both estimate and maximum (
NodeStatistics(c, c)insrc/function/table/range.cppRangeDateTimeCardinalityand inextension/icu/icu-table-range.cppCardinality). DST days, February, other calendars and spans that aren't a multiple of the step make the real count larger. - Same kind of bug as #25691 (descending integer generate_series), which was fixed on v2.0.
To Reproduce
-- 1. expected 1 row, v2.0 returns 2
SELECT * FROM range(TIMESTAMP '2023-02-01', TIMESTAMP '2023-03-02', INTERVAL 1 MONTH) LIMIT 1;
-- 2. a daily calendar in a DST time zone
SET TimeZone = 'America/New_York';
-- expected 30, v2.0 returns 31
SELECT count(*) FROM (SELECT * FROM generate_series(TIMESTAMPTZ '2024-03-01', TIMESTAMPTZ '2024-03-31', INTERVAL 1 DAY) LIMIT 30);
-- expected 31, v2.0 returns no row
SELECT s FROM (SELECT sum(1::BIGINT) AS s FROM generate_series(TIMESTAMPTZ '2024-03-01', TIMESTAMPTZ '2024-03-31', INTERVAL 1 DAY)) WHERE s = 31;
-- 3. no DST needed: the inclusive end is not counted (UTC). Expected 8, v2.0 returns no row
SET TimeZone = 'UTC';
SELECT s FROM (SELECT sum(1::BIGINT) AS s FROM generate_series(now() - INTERVAL 7 DAY, now(), INTERVAL 1 DAY)) WHERE s >= 8;
-- 4. 1.5.5 too: expected 10200000000000000000, 1.5.5 returns -8246744073709551616
CREATE TABLE big AS SELECT 1700000000000000000::BIGINT AS v;
SELECT sum(v), count(*) FROM big, generate_series(TIMESTAMPTZ '2024-01-01', TIMESTAMPTZ '2024-01-06', INTERVAL 1 DAY);Observed:
| v2.0 (v2.0-cyanoptera d591bb1da2) | 1.5.5 | |
|---|---|---|
| 1 | 2 rows | 1 row |
| 2 LIMIT | 31 | 30 |
| 2 sum | no row | 31 |
| 3 | no row | 8 |
| 4 | UBSan "signed integer overflow" in sum_helpers.hpp:110 on a relassert build |
-8246744073709551616 |
OS:
macOS 26.6.2, arm64 (Apple Silicon, 12 cores)
DuckDB Version:
v2.0-cyanoptera d591bb1da2 (v2.0.0-dev85971), built from source (relassert); 1.5.5 release (homebrew)
DuckDB Client:
CLI
Hardware:
No response
Full Name:
Panchawat Buranathamrong
Affiliation:
Independent
Did you include all relevant configuration (e.g., CPU architecture, Linux distribution) to reproduce the issue?
- Yes, I have
Did you include all code required to reproduce the issue?
- Yes, I have
Did you include all relevant data sets for reproducing the issue?
Not applicable - the reproduction does not require a data set
Source: duckdb/duckdb