Fix DATETIME_DIFF divergence between PostgreSQL and BigQuery#1980
Open
Chessing234 wants to merge 1 commit intoMIT-LCP:mainfrom
Open
Fix DATETIME_DIFF divergence between PostgreSQL and BigQuery#1980Chessing234 wants to merge 1 commit intoMIT-LCP:mainfrom
Chessing234 wants to merge 1 commit intoMIT-LCP:mainfrom
Conversation
…#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>
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
Fixes #1549.
As noted by @alistairewj,
DATETIME_DIFFin PostgreSQL returned a fractional value while BigQuery returns a truncated integer. This caused subtle divergence in derived concept queries between the two backends, particularly inurine_output_rate.sqlwhereDATETIME_DIFF(..., HOUR)was used in comparisons like<= 5and<= 11.Changes:
postgres-functions.sql: Wrapped theDATETIME_DIFFresult withTRUNC()to match BigQuery's truncation-toward-zero behavior for all date parts (SECOND, MINUTE, HOUR, DAY, YEAR).urine_output_rate.sql(BigQuery): ReplacedDATETIME_DIFF(..., HOUR)comparisons withDATETIME_DIFF(..., SECOND) / 3600.0to get fractional hours, following the same approach already used inkdigo_uo.sql. Also updated theuo_tmCTE to useCOALESCEwithSECOND-based diffs instead ofMINUTE-based diffs for consistency.urine_output_rate.sql(PostgreSQL): Applied matching changes to keep the postgres-converted version in sync.Test plan
postgres-functions.sqlDATETIME_DIFFnow returns truncated integers matching BigQuery behaviorurine_output_rateresults between BigQuery and PostgreSQL to confirm parityDATETIME_DIFF(ventilation.sql, oasis.sql, icustay_detail.sql, etc.)🤖 Generated with Claude Code