Skip to content

fix(datediff): use boundary-count semantics to match Snowflake#132

Merged
rampage644 merged 1 commit intomainfrom
claude/fix-issue-130-fidRl
Apr 23, 2026
Merged

fix(datediff): use boundary-count semantics to match Snowflake#132
rampage644 merged 1 commit intomainfrom
claude/fix-issue-130-fidRl

Conversation

@rampage644
Copy link
Copy Markdown
Contributor

Summary

  • DATEDIFF(part, a, b) previously returned CEIL((b - a) / unit), which rounded any positive sub-unit difference up to 1 (e.g. DATEDIFF('second', '…:00.100', '…:00.900') returned 1 instead of 0).
  • Switched to Snowflake's documented boundary-count semantics: floor(b / unit) - floor(a / unit), implemented via div_euclid on each endpoint's nanosecond value.
  • Applied uniformly to Second, Minute, Hour, Day, Millisecond, Microsecond, Nanosecond for both timestamp and TIME inputs. The old Hour branch also had a heuristic that over-counted whenever the hour component changed within a sub-hour span (e.g. 01:30 → 02:30 returned 2); now returns 1. Year/Quarter/Month/Week paths were already correct and are unchanged.

Test plan

  • cargo test -p functions datediff — 7 tests pass, including 3 new snapshot tests (boundary_count_same_bucket, boundary_count_straddle, boundary_count_not_ceiling) covering the matrix from the issue.
  • Updated query_different_types.snap: date_ts (DATEDIFF('day', DATE '2024-08-14', TS '2024-08-20 15:30:00')) now reads 6 instead of the old buggy 7.
  • cargo test -p functions datetime — all 25 datetime tests pass.
  • cargo clippy -p functions --lib — clean.

Fixes #130


Generated by Claude Code

DATEDIFF previously returned CEIL((b-a) / unit) for second, minute, day,
and sub-second parts, which rounded any positive sub-unit difference up
to 1. Snowflake's documented semantics is the count of `part`-boundaries
crossed between the two endpoints: floor(b / unit) - floor(a / unit).

Truncate each endpoint to `part` precision via div_euclid and subtract
the quotients. The hour branch also gained a correct implementation
(the old heuristic over-counted whenever the hour component changed
within a single-hour span, e.g. 01:30 -> 02:30 returned 2).

Fixes #130
@rampage644 rampage644 force-pushed the claude/fix-issue-130-fidRl branch from 34a2b79 to 9da2929 Compare April 23, 2026 15:59
@rampage644 rampage644 merged commit 7482768 into main Apr 23, 2026
3 checks passed
@rampage644 rampage644 deleted the claude/fix-issue-130-fidRl branch April 23, 2026 16:06
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.

DATEDIFF('second', a, b) returns CEIL of duration instead of boundary-count (deviates from Snowflake semantics)

2 participants