{{ message }}
Repository navigation
Fix incorrect $dateTrunc behavior for variety of cases - #712
Open
conradmerling wants to merge 3 commits into
Open
conradmerling wants to merge 3 commits into
conradmerling wants to merge 3 commits into
Conversation
|
🤖 Auto-triaged by documentdb-triage-tool. Applied: Reasoningcomponent from path globs (query); effort from diff stats (1168+258 LOC, 6 files); LLM: Fixes incorrect $dateTrunc aggregation expression results across multiple date units (day, week, month, quarter, year) covering pre-2000 dates, leap years, timezone boundaries, and week origins — a functional correctness bug in the query/aggregation component. If a label is wrong, remove it manually and ping |
Author
Signed-off-by: conradmerling <conrad.merling@gmail.com>
Signed-off-by: conradmerling <conrad.merling@gmail.com>
Signed-off-by: conradmerling <conrad.merling@gmail.com>
conradmerling
force-pushed
the
dateTrunc-fixes-cmerling
branch
from
August 12, 2026 13:25
41032cb to
3dedf22
Compare
This branch has not been deployed
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.

Description
The
$dateTruncexpression returns incorrect results for a variety ofday,week,month,quarter, andyearcases. Affected scenarios include dates before the2000-01-01reference date, leap years, timezone boundaries, alternate week origins, ISO year0000and BCE dates, and day/weekbinSizevalues larger than PostgreSQL interval calendar fields can represent.The following examples reproduce one failure for each affected unit on
ghcr.io/documentdb/documentdb/documentdb-local:pg17-0.114.0:The expected results are:
$dateTruncargumentsday_2026_122026-12-15T12:34:56.789Zunit: "day", binSize: 1, timezone: "UTC"2026-12-15T00:00:00.000Zweek_start_saturday2024-02-07T12:34:56.000Zunit: "week", binSize: 1, timezone: "UTC", startOfWeek: "saturday"2024-02-03T00:00:00.000Zmonth_ref_before1999-12-31T23:59:59.999Zunit: "month", binSize: 2, timezone: "UTC"1999-11-01T00:00:00.000Zquarter_ref_before1999-12-31T23:59:59.999Zunit: "quarter", binSize: 2, timezone: "UTC"1999-07-01T00:00:00.000Zyear_ref_before1999-12-31T23:59:59.999Zunit: "year", binSize: 2, timezone: "UTC"1998-01-01T00:00:00.000ZThe existing implementation uses PostgreSQL's internal
timestamp_agefunction to calculate a symbolic interval between the input and reference timestamps. Some paths then convert or divide those symbolic calendar components using nominal durations.Calendar units are not fixed durations: months have different lengths, years may contain leap days, and a local calendar day does not always contain 24 hours. Mixing symbolic intervals with nominal-duration arithmetic therefore causes bin boundaries to drift.
Dates before the reference date also require floor division toward negative infinity. Truncating negative indexes toward zero incorrectly places them in a later bin. The week implementation additionally aligns some
startOfWeekvalues to the wrong reference week.Finally, the previous implementation performs some timezone adjustment after selecting the bin. This can select the wrong calendar day, month, quarter, or year when an input is near a local boundary relative to UTC.
Solution
The affected units are now calculated in local wall-clock time:
2000-01-01origin.floor(unitIndex / binSize) * binSize.startOfWeekon or after2000-01-01.0000represents 1 BCE and year-000001represents 2 BCE.Sub-day units continue to use PostgreSQL
date_bin. Month, quarter, and year require calendar indexes becausedate_bindoes not allow strides containing months or larger units.Validation
Added a regression suite covering:
binSizevalues2000-01-01reference date0000and BCE month, quarter, and year truncationstartOfWeekvalueThe new cases failed against the previous implementation and pass with these changes.
Type of Change
AI Disclosure
pg_documentdb/src/operators/bson_expression_date_operators.cto rewrite buggy functions.Checklist
git commit -s)