Skip to content

fix: add ParseTime transpilation for BigQuery to DuckDB#7673

Open
william-goode wants to merge 1 commit into
tobymao:mainfrom
william-goode:fix-parsetime-duckdb
Open

fix: add ParseTime transpilation for BigQuery to DuckDB#7673
william-goode wants to merge 1 commit into
tobymao:mainfrom
william-goode:fix-parsetime-duckdb

Conversation

@william-goode
Copy link
Copy Markdown

Summary

BigQuery's PARSE_TIME is parsed into exp.ParseTime but DuckDB's generator has no handler for it. The expression falls through to function_fallback_sql, which emits PARSE_TIME(...) verbatim — DuckDB rejects this since it has no such function.

Fix: Add a parsetime_sql method to DuckDBGenerator that mirrors the existing strtodate_sql pattern: STRPTIME + CAST to TIME, with TRY_STRPTIME for SAFE expressions.

Reproduction

import sqlglot

# Works
sqlglot.transpile("SELECT PARSE_DATE('%Y-%m-%d', '2023-01-15')", read="bigquery", write="duckdb")
# => ["SELECT CAST(STRPTIME('2023-01-15', '%Y-%m-%d') AS DATE)"]

# Broken (before this fix)
sqlglot.transpile("SELECT PARSE_TIME('%H:%M', '14:30')", read="bigquery", write="duckdb")
# => ["SELECT PARSE_TIME('14:30', '%H:%M')"]  -- DuckDB rejects this

# After this fix
# => ["SELECT CAST(STRPTIME('14:30', '%H:%M') AS TIME)"]

Test plan

  • Added validate_all test for BigQuery PARSE_TIME → DuckDB CAST(STRPTIME(...) AS TIME)
  • Full test suite: 1111 passed, 18119 subtests passed
  • BigQuery and DuckDB dialect tests both green

BigQuery's PARSE_TIME is parsed into exp.ParseTime but DuckDB's
generator has no handler, so it falls through to function_fallback_sql
and emits PARSE_TIME(...) verbatim, which DuckDB rejects.

Add parsetime_sql method mirroring strtodate_sql: STRPTIME + CAST to
TIME, with TRY_STRPTIME for SAFE expressions.
@geooo109 geooo109 self-assigned this May 25, 2026

def parsetime_sql(self, expression: exp.ParseTime) -> str:
formatted_time = self.format_time(expression)
function_name = "STRPTIME" if not expression.args.get("safe") else "TRY_STRPTIME"
Copy link
Copy Markdown
Collaborator

@geooo109 geooo109 May 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exp.ParseTime doesn't have safe arg (because with wrong format it returns an error), so just using the STRPTIME should be fine.

self.validate_all(
"SELECT CAST(STRPTIME('14:30', '%H:%M') AS TIME)",
read={"bigquery": "SELECT PARSE_TIME('%H:%M', '14:30')"},
)
Copy link
Copy Markdown
Collaborator

@geooo109 geooo109 May 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also include a test with different time formats:

bigquery input:
SELECT PARSE_TIME('%H:%M:%E6S', '15:30:00.123456')

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.

2 participants