Skip to content

[Bug] Fix date_bin month bins counting intra-second milliseconds twice - #19267

Open
DaZuiZui wants to merge 1 commit into
apache:masterfrom
DaZuiZui:fix/date-bin-month-subsecond-origin
Open

DaZuiZui wants to merge 1 commit into
apache:masterfrom
DaZuiZui:fix/date-bin-month-subsecond-origin

Conversation

@DaZuiZui

@DaZuiZui DaZuiZui commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #19264.

DateBinFunctionColumnTransformer.convertToTimestamp computed the result as
toEpochMilli() + getNano(). toEpochMilli() already contains the whole
milliseconds of the second, and getNano() is the full nanosecond-of-second
(0–999_999_999), not the remainder below one millisecond. The intra-second
milliseconds were therefore counted twice, so month/year bins started that many
milliseconds late. For example date_bin(1mo, time, 500) returned 1000 for a
source of 500 and 2678401000 for 2678400500.

The fix converts from Instant.getEpochSecond() and Instant.getNano(), so each
part of the instant is counted exactly once, for ms, us and ns precision.

Hour and day bins do not use this helper and are unaffected. Month bins with a
whole-second origin are also unaffected because getNano() is 0 for them.

How to reproduce

CREATE DATABASE testdb;
USE testdb;
CREATE TABLE t (device_id STRING TAG, s INT32 FIELD);
INSERT INTO t (time, device_id, s) VALUES
  (500, 'd1', 1),
  (2678400500, 'd1', 2);
SELECT time, date_bin(1mo, time, 500) FROM t ORDER BY time;

Before: 1000 and 2678401000. After: 500 and 2678400500.

Tests

Added cases in DateBinFunctionTest for the issue's scenario, a 2024 origin,
dateBinStartEnd, nextDateBin, and us/ns precision with a sub-second
origin.

Tests run: 12, Failures: 0, Errors: 0, Skipped: 0

🤖 Generated with Claude Code

DateBinFunctionColumnTransformer.convertToTimestamp added
Instant.toEpochMilli() and LocalDateTime.getNano() together. The former
already contains the whole milliseconds of the second and the latter is
the full nanosecond-of-second, so the milliseconds were counted twice and
month/year bins started that many milliseconds late. Convert from
getEpochSecond() and getNano() instead so each part is counted once.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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.

[Bug] date_bin month bins add the origin's intra-second milliseconds twice

1 participant