fix(datediff): use boundary-count semantics to match Snowflake#132
Merged
rampage644 merged 1 commit intomainfrom Apr 23, 2026
Merged
fix(datediff): use boundary-count semantics to match Snowflake#132rampage644 merged 1 commit intomainfrom
rampage644 merged 1 commit intomainfrom
Conversation
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
34a2b79 to
9da2929
Compare
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.
Summary
DATEDIFF(part, a, b)previously returnedCEIL((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).floor(b / unit) - floor(a / unit), implemented viadiv_euclidon each endpoint's nanosecond value.01:30 → 02:30returned 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.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