Skip to content

Fix DATETIME_DIFF divergence between PostgreSQL and BigQuery#1980

Open
Chessing234 wants to merge 1 commit intoMIT-LCP:mainfrom
Chessing234:fix/datetime-diff-postgres-bigquery-parity
Open

Fix DATETIME_DIFF divergence between PostgreSQL and BigQuery#1980
Chessing234 wants to merge 1 commit intoMIT-LCP:mainfrom
Chessing234:fix/datetime-diff-postgres-bigquery-parity

Conversation

@Chessing234
Copy link
Copy Markdown

Summary

Fixes #1549.

As noted by @alistairewj, DATETIME_DIFF in PostgreSQL returned a fractional value while BigQuery returns a truncated integer. This caused subtle divergence in derived concept queries between the two backends, particularly in urine_output_rate.sql where DATETIME_DIFF(..., HOUR) was used in comparisons like <= 5 and <= 11.

Changes:

  • postgres-functions.sql: Wrapped the DATETIME_DIFF result with TRUNC() to match BigQuery's truncation-toward-zero behavior for all date parts (SECOND, MINUTE, HOUR, DAY, YEAR).
  • urine_output_rate.sql (BigQuery): Replaced DATETIME_DIFF(..., HOUR) comparisons with DATETIME_DIFF(..., SECOND) / 3600.0 to get fractional hours, following the same approach already used in kdigo_uo.sql. Also updated the uo_tm CTE to use COALESCE with SECOND-based diffs instead of MINUTE-based diffs for consistency.
  • urine_output_rate.sql (PostgreSQL): Applied matching changes to keep the postgres-converted version in sync.

Test plan

  • Verify postgres-functions.sql DATETIME_DIFF now returns truncated integers matching BigQuery behavior
  • Compare urine_output_rate results between BigQuery and PostgreSQL to confirm parity
  • Verify no regression in other queries using DATETIME_DIFF (ventilation.sql, oasis.sql, icustay_detail.sql, etc.)

🤖 Generated with Claude Code

…#1549)

BigQuery's DATETIME_DIFF returns a truncated integer, while the
PostgreSQL implementation returned a fractional value. This caused
subtle differences in query results, particularly in
urine_output_rate.sql where HOUR-based comparisons (<= 5, <= 11)
included different time ranges depending on the backend.

Changes:
- Update postgres-functions.sql: wrap DATETIME_DIFF result with TRUNC
  to match BigQuery's truncation behavior
- Update urine_output_rate.sql (BigQuery): use SECOND-based diffs
  divided by 3600.0 for fractional hour comparisons, matching the
  approach used in kdigo_uo.sql
- Update urine_output_rate.sql (PostgreSQL): apply the same
  SECOND-based approach for consistency

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

DATETIME_DIFF implementation varies between postgres and bigquery

1 participant