Skip to content

Fix age(week, ...) being off by one week - #122633

Merged
NIKTONIKTO717 merged 1 commit into
masterfrom
122630-ageweek-returns-a-result-off-by-one-week
Sep 28, 2026
Merged

NIKTONIKTO717 merged 1 commit into
masterfrom
122630-ageweek-returns-a-result-off-by-one-week

Conversation

@NIKTONIKTO717

@NIKTONIKTO717 NIKTONIKTO717 commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Closes: #122630
Closes: #63812
Closes: #68345

age('week', start, end) counts the Mondays between start and end, then subtracts 1 if start is later in its week than end (weekday first, time only if the weekdays are equal). That second step had two bugs.

1. The time was compared even when the weekdays differ (misplaced brackets):

SELECT age('week', toDateTime('2018-01-01 10:10:00'), toDateTime('2018-01-11 10:00:00')); -- 0, should be 1
SELECT age('week', toDateTime('2018-01-01'), toDateTime('2018-01-11'));                   -- 1

Monday is before Thursday, but the hours are equal and 10 minutes > 0 minutes, so a week was subtracted.

2. The weekdays were not swapped for reversed arguments. When start is later than end, the code swaps the two values but not their weekdays. This affects dates too: age('week', toDate('2024-01-10'), toDate('2024-01-01')) returned 0 instead of -1, and 02477_age.reference had -103 instead of -104.

The fix compares the time only for equal weekdays, and swaps the weekdays with the values.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix age('week', ...) sometimes being off by one week when the dates fall on different weekdays.


Workflow [PR]
Sync PR [sync-upstream/pr/122633]

Version info

  • Merged into: 26.10.1.911-master (included in 26.10 and later)

@NIKTONIKTO717 NIKTONIKTO717 linked an issue Sep 28, 2026 that may be closed by this pull request
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e9a1794]

Summary: ✅


AI Review

Summary

This PR fixes the two broken correction paths in age('week', ...): the weekday comparison is now swapped together with reversed arguments, and the time-of-day tie-breaker now runs only when both timestamps fall on the same weekday. The new 02477_age coverage includes direct regressions plus randomized UTC checks across Date, Date32, DateTime, and DateTime64, and I did not find an unresolved correctness gap in the current patch.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit e9a1794.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 12/12 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 28, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-datetime Date/DateTime/TimeZone datatypes and date-time semantics. label Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e9a179491 with master ee5f404c9 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline.

Job report

@diegomestre2 diegomestre2 self-assigned this Sep 28, 2026

@diegomestre2 diegomestre2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic looks good but there is a comparison chain that is a bit complex to follow and bound to error, maybe the std::tie could help simplifying it.

Comment thread src/Functions/dateDiff.cpp Resolved
@den-crane

Copy link
Copy Markdown
Contributor

@diegomestre2 diegomestre2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to be merged, it fixes the issue and adds the necessary tests to check for correct output.

@NIKTONIKTO717
NIKTONIKTO717 added this pull request to the merge queue Sep 28, 2026
Merged via the queue into master with commit a25168f Sep 28, 2026
149 checks passed
@NIKTONIKTO717
NIKTONIKTO717 deleted the 122630-ageweek-returns-a-result-off-by-one-week branch September 28, 2026 13:47
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-datetime Date/DateTime/TimeZone datatypes and date-time semantics. pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

age('week', ...) returns a result off by one week age function returning wrong result age() function returns wrong results

4 participants