Skip to content

Anchor month-based date_bin bins at the origin - #19375

Open
Karman580 wants to merge 1 commit into
apache:masterfrom
Karman580:fix-date-bin-month-end-origin
Open

Karman580 wants to merge 1 commit into
apache:masterfrom
Karman580:fix-date-bin-month-end-origin

Conversation

@Karman580

@Karman580 Karman580 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #19374.

With a month or year interval and an origin on day 29-31 (or Feb 29 with a year interval), table-model date_bin, GROUP BY date_bin(...) and date_bin_gapfill used three different, wrong bin schedules. DateBinFunctionColumnTransformer caused all three:

  • dateBin / dateBinStartEnd took the bin index from ChronoUnit.MONTHS.between(origin, source) / monthDuration. MONTHS.between compares the day of month, so it is one short when the origin day is clamped in the source month: from 2024-01-31 to 2024-02-29T12:00 it returns 0. The code only stepped back when the bin start was after the source and never stepped forward, so the row went to the previous bin.
  • dateBinStartEnd built the window end from the clamped start (binStart.plusMonths(m)), so the window could end before the source. TableDateBinTimeRangeIterator uses this window for the pushed-down aggregation. The block start fell outside the window, nothing was consumed, and the same empty window was emitted again on every call. The query never finished.
  • nextDateBin(int, ZoneId, long) (month gap fill) added months to the previous step, so after Jan 31 -> Feb 29 every later step stayed on the 29th.

Change

  • Bin k always starts at origin.plusMonths(k * monthDuration). monthBinIndex starts from the MONTHS.between estimate and corrects it in both directions, so it returns the last bin that starts at or before the source. dateBin returns bin k, and dateBinStartEnd returns [bin k, bin k + 1). The estimate is off by at most one, so each loop runs at most once.
  • nextDateBin(int monthDuration, long origin, ZoneId, long currentTime) returns bin index(currentTime) + 1 instead of adding months to the current step.
  • GapFillNode carries the date_bin_gapfill origin (it is already parsed in QueryPlanner.gapFill). TableOperatorGenerator passes it to GapFillWoGroupWMoOperator / GapFillWGroupWMoOperator. The node serializes the new long after nonMonthDuration.

Origins on day 1-28 (including the default origin) produce the same bins as before, because MONTHS.between is exact for them and clamping never happens. Non-month intervals are unchanged.

Before / after

Data (UTC, ms precision): rows at 2024-01-31, 2024-02-29T12:00, 2024-03-30, 2024-04-30, 2024-05-31 with v = 1..5. Both runs used a 1C1D standalone build: master 45cb9959f1 before, this branch after.

SELECT time, date_bin(1mo, time, 2024-01-31T00:00:00+00:00) AS b FROM db1.t ORDER BY time
time before after
2024-01-31 2024-01-31 2024-01-31
2024-02-29T12:00 2024-01-31 2024-02-29
2024-03-30 2024-02-29 2024-02-29
2024-04-30 2024-03-31 2024-04-30
2024-05-31 2024-05-31 2024-05-31
SELECT date_bin(1mo, time, 2024-01-31T00:00:00+00:00) AS b, count(*) AS c FROM db1.t GROUP BY 1 ORDER BY 1
  • before: with GROUP BY 1 LIMIT 6 it returns 01-31:1 followed by five 01-31:0 rows, because the pushed-down scan keeps emitting the same empty window (the pushed-down date_bin_gapfill case below shows the same loop running until the query timeout)
  • after: 01-31:1, 02-29:2, 04-30:1, 05-31:1 (exit code 0)
SELECT date_bin_gapfill(1mo, time, 2024-01-31T00:00:00+00:00) AS b, avg(v) AS a FROM db1.t
WHERE time >= 2024-01-31T00:00:00+00:00 AND time < 2024-07-31T00:00:00+00:00 GROUP BY 1
  • before: 01-31:1.0 followed by 10,628,999 01-31:null rows, then 720: Query execution is time out; the non-pushdown variant returns 01-31:1.5, 02-29:3.0, 03-29:null, 03-31:4.0, 05-29:null, 05-31:5.0
  • after (both variants): 01-31:1.0, 02-29:2.5, 03-31:null, 04-30:4.0, 05-31:5.0

date_bin(1y, time, 2024-02-29T00:00:00+00:00) with GROUP BY now returns 2023-02-28:1, 2024-02-29:4 on the same data.

The missing trailing empty bin (06-30) for time < 2024-07-31 happens with any origin on master, including 2024-01-01. That is #18988 and is not changed here.

Tests

  • DateBinFunctionTest: four new cases. They cover a Jan 31 origin with 1- and 3-month intervals, including sources before the origin; a Feb 29 origin with a 1-year interval; dateBinStartEnd windows that contain the source and end at the next bin; and nextDateBin steps that match date_bin. On master all four fail (for example expected:<1709164800000> but was:<1706659200000>); the other 13 cases pass with and without the change.
  • GapFillWoGroupWMoOperatorTest / GapFillWGroupWMoOperatorTest: pass the origin. Their start time is a bin start, so the expected output is unchanged.
  • IoTDBGapFillTableIT.monthIntervalWithMonthEndOriginTest: covers scalar date_bin, the pushed-down GROUP BY ... ORDER BY 1 (which did not finish before), and date_bin_gapfill with and without group keys.

Commands run locally (JDK 25):

mvn test -pl iotdb-core/datanode -Dtest='AggregationTest,DateBinFunctionTest,GapFill*OperatorTest,JoinTest,OrderingPropertyPropagationTest,SelectAliasReuseTest,TSBSTest,*TimeRangeIterator*'
  -> Tests run: 117, Failures: 0, Errors: 0, Skipped: 2, BUILD SUCCESS
mvn verify -DskipUTs -Dit.test=IoTDBGapFillTableIT -pl integration-test -am -PTableSimpleIT -P with-integration-tests
  -> IoTDBGapFillTableIT Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, BUILD SUCCESS
python3 .github/scripts/check-it-selection.py -> OK

The two skipped tests are existing ignored cases. Only ms precision was tested end to end, and only the TableSimpleIT (1C1D) profile was run locally.


This PR has:

  • been self-reviewed.
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage.
  • added integration tests.

date_bin, the aggregation scan windows and date_bin_gapfill computed month
bins from ChronoUnit.MONTHS.between, which is one short when the origin day
does not exist in the source month (origin Jan 31, source Feb 29). They also
built the window end and the next gap-fill step from the previous, already
clamped bin. With an origin on day 29-31 rows went to the previous bin, the
pushed-down GROUP BY kept emitting the same empty window and never finished,
and gap fill stepped on the 29th of every month.

Bin k now always starts at origin.plusMonths(k * monthDuration), the index is
corrected in both directions, the window end is bin k + 1, and the month
gap-fill operators get the origin through GapFillNode to step by bin index.

Fixes apache#19374
@Karman580

Copy link
Copy Markdown
Contributor Author

The CI workflows for this PR are waiting for maintainer approval to run. Could someone approve them when convenient? The targeted unit tests and IoTDBGapFillTableIT pass locally; the commands and results are in the description.

@Karman580

Copy link
Copy Markdown
Contributor Author

Hi @JackieTien97  could you please review this PR when you get a chance

@Karman580

Copy link
Copy Markdown
Contributor Author

@JackieTien97 please review

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] [Table model] date_bin with a month/year interval and an origin on day 29-31 uses wrong bins, and the pushed-down GROUP BY never finishes

1 participant