Repository navigation
fix: emit milliseconds as microseconds on sql.mysql and offset timestamps/times as TIMESTAMPTZ/TIMETZ on sql.postgres - #6593
Conversation
…amps as TIMESTAMPTZ on sql.postgres Refs #6592
# Conflicts: # CHANGELOG.md
prql-bot
left a comment
There was a problem hiding this comment.
On sql.postgres, a time literal with an offset has the same problem this PR fixes for timestamps. @10:00+05:00 still compiles to TIME '10:00+0500', because the Literal::Time arm of translate_literal always uses TimezoneInfo::None. With timezone='UTC', Postgres 16 evaluates TIME '10:00+0500' to 10:00:00 and drops the offset, while TIMETZ '10:00+0500' keeps 10:00:00+05. I'll push a fix that applies the same has_utc_offset check to the time arm.
sql.redshift probably has the same gap for timestamps. RedshiftDialect isn't PostgresDialect, so @2020-01-01T10:00:00+05:00 there still compiles to TIMESTAMP '…+0500'. I couldn't test Redshift's handling of an offset in a TIMESTAMP literal here, so I'm leaving that dialect out of this PR.
for more information, see https://pre-commit.ci
Fixes the two rows of #6592 that
translate_literalcan settle by itself:sql.mysql:5millisecondscompiled toINTERVAL 5 MILLISECOND. MySQL has noMILLISECONDunit (its units stop atMICROSECOND), so MySQL 8.0 rejected it with a syntax error. It now compiles toINTERVAL 5000 MICROSECOND. A value that overflows when multiplied by 1000 is a compile error instead of a panic.sql.postgres:@2020-01-01T10:00:00+05:00compiled toTIMESTAMP '2020-01-01T10:00:00+0500'. Postgres reads that astimestamp without time zoneand silently drops the offset. A timestamp ending inZor a±HH[:]MMoffset now compiles toTIMESTAMPTZ '…'. A time literal such as@10:00+05:00had the same problem withTIME '…'and now compiles toTIMETZ '…'. Literals without an offset still compile toTIMESTAMP/TIME, and other dialects are unchanged.The inline tests
test_mysql_milliseconds_intervalandtest_postgres_datetime_with_offsetcover both rows. I ran the emitted SQL on local servers. On MySQL 8.0.46,TIMESTAMP '2020-01-01 10:00:00' + INTERVAL 5000 MICROSECONDgives10:00:00.005000, while the oldINTERVAL 5 MILLISECONDfails with ERROR 1064. On Postgres 16 withtimezone='UTC',TIMESTAMPTZ '2020-01-01T10:00:00+0500'gives05:00:00+00, while the old literal gives10:00:00with no zone.TIMETZ '10:00+0500'gives10:00:00+05, whileTIME '10:00+0500'gives10:00:00. Theprqlclib and integration tests pass, and no existing snapshot changed.Not covered here:
sql.mssqlrows (typed-string date literals andINTERVAL) and thesql.sqliteINTERVALrow. Each needs a choice between translating toDATEADD/DATETIME(d, '+3 days')where+/-combine a date and an interval, or reporting a compile error. That choice stays open on the issue.sql.redshift, which is a separate dialect fromsql.postgresand still emitsTIMESTAMP '…+0500'. I couldn't test how Redshift handles an offset in that literal.Refs #6592 — automated triage