Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(bigquery): transpile format_datetime, datetime_trunc to duckdb #3894

Conversation

skadel
Copy link
Contributor

@skadel skadel commented Aug 11, 2024

No description provided.

@georgesittas
Copy link
Collaborator

georgesittas commented Aug 11, 2024

Hi @skadel, thanks for the PR! Please run make style to format & lint the code, I'll review shortly.

@skadel skadel force-pushed the askhiri/bq_timestamp_trunc_and_format_datetime branch from 412a6d2 to c6e9b08 Compare August 11, 2024 13:06
sqlglot/dialects/duckdb.py Outdated Show resolved Hide resolved
sqlglot/dialects/duckdb.py Outdated Show resolved Hide resolved
@skadel skadel force-pushed the askhiri/bq_timestamp_trunc_and_format_datetime branch from c6e9b08 to 724e3fa Compare August 11, 2024 14:28
Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

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

Seems good based on https://github.com/tobymao/sqlglot/pull/3894/files#r1712999820, but I may try a few cases myself in BQ to double check this later or tomorrow.

@georgesittas georgesittas changed the title Askhiri/bq timestamp trunc and format datetime Feat(bigquery): transpile format_datetime, datetime_trunc Aug 11, 2024
@georgesittas georgesittas changed the title Feat(bigquery): transpile format_datetime, datetime_trunc Feat(bigquery): transpile format_datetime, datetime_trunc to duckdb Aug 11, 2024
@georgesittas
Copy link
Collaborator

@skadel I tried a few examples and found at least one category that fails with the FORMAT_DATETIME -> FORMAT_DATE change. Check this out:

-- Works
SELECT FORMAT_DATETIME('%x', '2008-12-25 12:05:01') AS US_format

-- Doesn't work:  Could not cast literal "2008-12-25 12:05:01" to type DATE at [1:26]
-- This is the query we get when we read/write with BQ using this PR
SELECT FORMAT_DATE('%x', '2008-12-25 12:05:01') AS US_format

Let's not convert any of these two functions into the other one, these transformations are tricky to get right and I don't want to risk breaking users' queries with this.

Perhaps you could leverage the fact that at parse time you produce exp.TsOrDsToTimestamp(this=seq_get(args, 1)) for the FORMAT_DATETIME one, and so in the timetostr_sql method you could check this and generate the same function name instead of FORMAT_DATE.

@skadel skadel force-pushed the askhiri/bq_timestamp_trunc_and_format_datetime branch from 724e3fa to b059b5a Compare August 12, 2024 20:10
@skadel
Copy link
Contributor Author

skadel commented Aug 12, 2024

@skadel I tried a few examples and found at least one category that fails with the FORMAT_DATETIME -> FORMAT_DATE change. Check this out:

-- Works
SELECT FORMAT_DATETIME('%x', '2008-12-25 12:05:01') AS US_format

-- Doesn't work:  Could not cast literal "2008-12-25 12:05:01" to type DATE at [1:26]
-- This is the query we get when we read/write with BQ using this PR
SELECT FORMAT_DATE('%x', '2008-12-25 12:05:01') AS US_format

Let's not convert any of these two functions into the other one, these transformations are tricky to get right and I don't want to risk breaking users' queries with this.

Perhaps you could leverage the fact that at parse time you produce exp.TsOrDsToTimestamp(this=seq_get(args, 1)) for the FORMAT_DATETIME one, and so in the timetostr_sql method you could check this and generate the same function name instead of FORMAT_DATE.

You're right, i thought that TsOrDsToTimestamp cast it to timestamp so i tested SELECT FORMAT_DATE('%x', CAST('2008-12-25 12:05:01' AS DATETIME)). But you're right, it's better to keep the same function.

Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution 🙌

@georgesittas georgesittas merged commit 6f1527f into tobymao:main Aug 12, 2024
6 checks passed
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