Skip to content

fix(postgres): add missing TO_CHAR format tokens (Mon, Month, Day, Dy, AM/PM, HH)#7477

Open
mwade0118 wants to merge 1 commit intotobymao:mainfrom
mwade0118:fix/postgres-to_char-format-tokens
Open

fix(postgres): add missing TO_CHAR format tokens (Mon, Month, Day, Dy, AM/PM, HH)#7477
mwade0118 wants to merge 1 commit intotobymao:mainfrom
mwade0118:fix/postgres-to_char-format-tokens

Conversation

@mwade0118
Copy link
Copy Markdown

Summary

Adds bare (non-TM-prefixed) PostgreSQL TO_CHAR format tokens to Postgres.TIME_MAPPING:

  • Month names: Mon/mon/MON%b, Month/month/MONTH%B
  • Weekday names: Day/day/DAY%A, Dy/dy/DY%a
  • AM/PM: AM/PM/am/pm/A.M./P.M./a.m./p.m.%p
  • Bare HH: HH/hh%I (PostgreSQL defaults to 12-hour when no suffix)

Also adds INVERSE_TIME_MAPPING to prefer canonical bare forms (Mon, Day, Dy, Month, HH12, AM) when generating Postgres SQL.

Problem

Previously, these tokens were either left as unconverted literals or partially mangled:

PG Token Expected Actual (before) Issue
Mon %b Mon (literal) Not converted
Month %B Month (literal) Not converted
Day %A %uay D%u matched first, ay left as literal
Dy %a %uy D%u matched first, y left as literal
AM/PM %p literal Not converted

The TM-prefixed versions (TMDay, TMDy, TMMon, TMMonth) were already mapped. This PR adds the standard bare forms which are far more commonly used in practice.

The trie-based format_time() correctly prioritizes longer matches, so Day%A takes precedence over D%u without affecting the existing single-character D mapping.

Note: Redshift already extended Postgres with "MON": "%b" (line 27 of redshift.py), which is now redundant but harmless.

Fixes #7476

Test plan

  • Added 7 new validate_all tests in test_postgres.py covering all new format tokens
  • Updated 2 existing identity tests to account for HH/HH12 and PM/AM canonicalization
  • Updated 1 Exasol test expectation (TMDyDy for postgres write target)
  • All 862 existing tests pass (804 dialect + core tests, 58 bigquery tests)

…, AM/PM, HH)

Add bare (non-TM-prefixed) month names, weekday names, AM/PM meridiem
indicators, and bare HH to Postgres TIME_MAPPING. These tokens are
standard PostgreSQL TO_CHAR format specifiers but were not being
converted during transpilation, causing issues when targeting dialects
like ClickHouse.

Previously, `Day` was mangled to `%uay` because the single-char `D`
rule matched first. The trie correctly handles longer matches, so
`Day` → `%A`, `Dy` → `%a` now take precedence over `D` → `%u`.

Add INVERSE_TIME_MAPPING to prefer the canonical bare forms (Mon, Day,
Dy, Month, AM, HH12) when generating Postgres SQL from the internal
strftime representation.

Fixes tobymao#7476
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.

PostgreSQL TO_CHAR format tokens Mon, Month, Day, Dy, AM not converted for ClickHouse

1 participant