{{ message }}
Repository navigation
[v1.5 backport] Fix: reset empty_range in the TIMESTAMP range() table function - #23879
Merged
Merged
Conversation
The TIMESTAMP/INTERVAL overload of the range() table function silently drops
rows when used in a correlated LATERAL and at least one input row produces an
empty range (start >= stop).
RangeDateTimeLocalState is reused across input rows, but
GenerateRangeDateTimeParameters only ever SETS empty_range to true and never
resets it to false. Once one row yields an empty range, the flag sticks and every
subsequent row is treated as empty and emits nothing.
The integer overload (GenerateRangeParameters) already resets the flag, which is
why only the timestamp overload was affected -- an identical LATERAL over
range(BIGINT, BIGINT, BIGINT) returns the correct rows.
Reproduction (10 rows, only i=5 yields an empty range, so 9 rows are expected):
CREATE TABLE t AS
SELECT i, TIMESTAMP '2024-01-01' + INTERVAL (i * 60) SECOND AS ts
FROM generate_series(0, 9) s(i);
SELECT COUNT(*) FROM t, LATERAL range(
ts + (CASE WHEN i = 5 THEN INTERVAL '7 minutes' ELSE INTERVAL '0 minutes' END),
ts + INTERVAL '5 minutes',
INTERVAL '10 minutes');
-- returns 1 (and the surviving row is i=3), expected 9
The corruption is not a truncation: which rows survive depends on where the empty
range falls, so the result is wrong in a way that is hard to notice. The scalar
range() (returning a LIST) is correct when unnested, so two spellings of the same
query disagree.
Adds test/sql/table_function/range_timestamp_lateral_empty.test, which fails
before this change and passes after.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CKmjtyYCWgo35X3ZCKaMYT
duckdb-draftbot
marked this pull request as draft
July 16, 2026 16:53
lnkuiper
marked this pull request as ready for review
July 17, 2026 07:40
kaka11chen
pushed a commit
to kaka11chen/astrovela_vane
that referenced
this pull request
Aug 14, 2026
… function (duckdb/duckdb#23879) Backport of #23811 to v1.5 cc @lnkuiper --- The TIMESTAMP/INTERVAL overload of the range() table function silently drops rows when used in a correlated LATERAL and at least one input row produces an empty range (start >= stop). RangeDateTimeLocalState is reused across input rows, but GenerateRangeDateTimeParameters only ever SETS empty_range to true and never resets it to false. Once one row yields an empty range, the flag sticks and every subsequent row is treated as empty and emits nothing. The integer overload (GenerateRangeParameters) already resets the flag, which is why only the timestamp overload was affected -- an identical LATERAL over range(BIGINT, BIGINT, BIGINT) returns the correct rows. Reproduction (10 rows, only i=5 yields an empty range, so 9 rows are expected): CREATE TABLE t AS SELECT i, TIMESTAMP '2024-01-01' + INTERVAL (i * 60) SECOND AS ts FROM generate_series(0, 9) s(i); SELECT COUNT(*) FROM t, LATERAL range( ts + (CASE WHEN i = 5 THEN INTERVAL '7 minutes' ELSE INTERVAL '0 minutes' END), ts + INTERVAL '5 minutes', INTERVAL '10 minutes'); -- returns 1 (and the surviving row is i=3), expected 9 The corruption is not a truncation: which rows survive depends on where the empty range falls, so the result is wrong in a way that is hard to notice. The scalar range() (returning a LIST) is correct when unnested, so two spellings of the same query disagree. Adds test/sql/table_function/range_timestamp_lateral_empty.test, which fails before this change and passes after. Claude-Session: https://claude.ai/code/session_01CKmjtyYCWgo35X3ZCKaMYT Upstream-Commit: duckdb/duckdb@4a11fb2
8 of 9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Backport of #23811 to v1.5
cc @lnkuiper
The TIMESTAMP/INTERVAL overload of the range() table function silently drops rows when used in a correlated LATERAL and at least one input row produces an empty range (start >= stop).
RangeDateTimeLocalState is reused across input rows, but GenerateRangeDateTimeParameters only ever SETS empty_range to true and never resets it to false. Once one row yields an empty range, the flag sticks and every subsequent row is treated as empty and emits nothing.
The integer overload (GenerateRangeParameters) already resets the flag, which is why only the timestamp overload was affected -- an identical LATERAL over range(BIGINT, BIGINT, BIGINT) returns the correct rows.
Reproduction (10 rows, only i=5 yields an empty range, so 9 rows are expected):
The corruption is not a truncation: which rows survive depends on where the empty range falls, so the result is wrong in a way that is hard to notice. The scalar range() (returning a LIST) is correct when unnested, so two spellings of the same query disagree.
Adds test/sql/table_function/range_timestamp_lateral_empty.test, which fails before this change and passes after.
Claude-Session: https://claude.ai/code/session_01CKmjtyYCWgo35X3ZCKaMYT