Skip to content

feat(core-spec): DATEDIFF counts the boundaries of part crossed in OSSIE_SQL_2026 - #816

Open
MrBlack1995 wants to merge 3 commits into
apache:mainfrom
MrBlack1995:feat/core-spec-datediff-boundaries
Open

MrBlack1995 wants to merge 3 commits into
apache:mainfrom
MrBlack1995:feat/core-spec-datediff-boundaries

Conversation

@MrBlack1995

@MrBlack1995 MrBlack1995 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

OSSIE_SQL_2026 lists DATEDIFF(part, start_date, end_date) as REQUIRED with the description "Difference between dates", and engines read that in two incompatible ways: some count the boundaries of part crossed (Snowflake), others count whole units elapsed (Databricks). So the same portable metric returns different numbers depending on where it runs (#743).

before after
DATEDIFF(month, DATE '2015-12-31', DATE '2016-01-01') 1 on Snowflake, 0 on Databricks 1
DATEDIFF(day, TIMESTAMP_NTZ '2026-01-01 23:59:59', TIMESTAMP_NTZ '2026-01-02 00:00:01') 1 on boundary engines, 0 on engines that count 86,400-second days 1
DATEDIFF(day, ...) over DATE values the same everywhere unchanged
  • A new Date Difference section in expression_language.md states the rule: DATEDIFF compares the two values truncated to part, so it is the number of whole part units between DATE_TRUNC(part, start_date) and DATE_TRUNC(part, end_date), negative when the end is before the start, with weeks starting on Monday as DATE_TRUNC('week', ...) already says.
  • An engine whose DATEDIFF counts whole units elapsed evaluates an equivalent expression, and the section gives two: the same function over the truncated values (both arguments sit on a boundary, so elapsed and crossed units agree), or the YEAR/MONTH arithmetic for month and year.
  • part is one of the parts of DATE_TRUNC (year, quarter, month, week, day, hour, minute, second); the spec gave no list for DATEDIFF before. An expression that needs elapsed time takes it from a smaller part, for example DATEDIFF(second, start_ts, end_ts) / 86400.0 (asked for on Spec gap: DATEDIFF does not say whether it counts boundaries crossed or whole units elapsed #743 by GASP, who counted 60 uses in their model: 48 day over DATE values, unchanged, and 12 second durations).
  • The DATEDIFF row in Date Arithmetic links to the section.
  • spec.md changelog (0.3.0.dev0) and docs/spec_changes.md: recorded as Breaking, like the ROUND entry: earlier versions did not specify it, so a document written for an engine with the other reading can change meaning.

Why boundaries crossed: it is Snowflake's reading, and on #743 djwaldo showed that ThoughtSpot already implements it on both Snowflake and Databricks, emitting FLOOR(MONTHS_BETWEEN(...)) on Databricks rather than passing the part through. A semantic layer that has to hold one meaning across warehouses already ends up here, and the truncation equivalent makes it cheap for elapsed-unit engines.

I checked every example and both equivalents against a reference implementation of the rule: for all eight parts, on 20,000 random pairs of timestamps with fractions of a second, the truncation form and the month and year arithmetic agree with it, and the two readings never differ by more than 1.

Impact on existing implementations

  • Engines that count boundaries (Snowflake): no change.
  • Engines that count whole units elapsed (Databricks' three-argument datediff), and converters targeting them: emit the equivalent the section gives.
  • DATEDIFF(day, ...) over DATE values, which the M6 portability fixture uses, is the same under both readings.
  • Week boundaries are those of DATE_TRUNC('week', ...), a Monday. A source whose weeks start on another day shifts accordingly.
  • Not covered: time-zone-aware timestamps. The boundary is that of DATE_TRUNC, which the spec does not define for them either.
  • No schema or validator change.

Validation

  • validation/tests: 1103 passed, 1 skipped. compliance: 58 passed. pre-commit passes on the three changed files.
  • Documentation only: no schema, validator, grader or fixture change.

Related Issues

dev@ discussion: [DISCUSS] thread, lazy consensus until Tuesday 1 December 2026 (UTC).

Closes #743. apache/ossie-converters#175 refuses DATEDIFF today for exactly this reason and can translate it once this lands.

Checklist

Specification

  • Spec changes are included in core-spec/ and follow the existing structure
  • Spec changes have a [DISCUSS] thread on dev@ with a stated end date, and reach lazy consensus (or pass a [VOTE]) before merge (thread open until 2026-12-01)
  • Breaking changes to the spec are clearly called out in the summary
  • For a material spec change, docs/spec_changes.md records rationale, compatibility and migration impact, or the PR explains why no entry is needed

Converters

Validation

  • Validation rules in validation/ are updated if the spec changed (no rule changes: the validator does not evaluate DATEDIFF)

Tests

  • All existing tests pass (pytest / CI green)

Compliance

  • ASF license headers are present on all new source files (no new files)

@MrBlack1995
MrBlack1995 force-pushed the feat/core-spec-datediff-boundaries branch from 94287be to e13b645 Compare November 24, 2026 05:54
@MrBlack1995
MrBlack1995 force-pushed the feat/core-spec-datediff-boundaries branch from e13b645 to d2588fb Compare November 24, 2026 06:01
@jbonofre
jbonofre self-requested a review November 24, 2026 12:29
…SIE_SQL_2026

expression_language.md said only "Difference between dates", and engines
read it two ways: some engines (Snowflake) count boundaries crossed, Databricks
counts whole units elapsed, so DATEDIFF(month, DATE '2015-12-31',
DATE '2016-01-01') was 1 or 0. Define the boundary reading, give examples,
and the equivalent an elapsed-unit engine evaluates (the same function over
DATE_TRUNC-ed values, or the YEAR/MONTH arithmetic). Closes apache#743.

Co-authored-by: Isaac <no-reply@databricks.com>
@MrBlack1995
MrBlack1995 force-pushed the feat/core-spec-datediff-boundaries branch from d2588fb to e3cc89b Compare November 24, 2026 12:49
MrBlack1995 and others added 2 commits November 24, 2026 14:08
Co-authored-by: Isaac <no-reply@databricks.com>
…ete part

The equivalent for engines that count elapsed units was written with a
generic `part`, which hides that DATE_TRUNC takes the part as a string
literal while DATEDIFF takes it unquoted. Write it out for month and say
how each function takes the part (review on dev@).

Co-authored-by: Isaac <no-reply@databricks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spec gap: DATEDIFF does not say whether it counts boundaries crossed or whole units elapsed

1 participant