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

Fix(snowflake): Handle form of CONVERT_TIMEZONE with a source TZ #1598

Merged
merged 2 commits into from
May 11, 2023

Conversation

pmsanford
Copy link
Contributor

@pmsanford pmsanford commented May 11, 2023

Snowflake's CONVERT_TIMEZONE has two forms:

CONVERT_TIMEZONE( <target_tz> , <source_timestamp> )
-- and
CONVERT_TIMEZONE( <source_tz> , <target_tz> , <source_timestamp_ntz> )

The second is required if the timestamp doesn't have timezone information. Currently, parsing

CONVERT_TIMEZONE('UTC', 'America/Los_Angeles', tzcolumn)

produces

CONVERT_TIMEZONE('UTC', 'America/Los_Angeles')

when re-rendered, which is invalid. This PR still treats the target-only case as exp.AtTimeZone, but parses the source-and-target case as exp.Anonymous so that rendering back to Snowflake will be correct.

Snowflake's [CONVERT_TIMEZONE](https://docs.snowflake.com/en/sql-reference/functions/convert_timezone) allows optionally specifying a source timezone for columns without timezone information. This currently causes incorrect SQL to be generated when parsed and re-rendered.
def _parse_convert_timezone(args: t.Sequence) -> exp.Expression:
if len(args) == 3:
converted: t.List[exp.Expression] = [maybe_parse(arg, dialect=Snowflake) for arg in args]
return exp.Anonymous(this="CONVERT_TIMEZONE", expressions=converted)
Copy link
Owner

Choose a reason for hiding this comment

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

exp.func("CONVERT_TIMEZONE", expression=...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I initially tried exp.func, but since CONVERT_TIMEZONE is in the parser.FUNCTIONS dict, it ends up calling this function again recursively on line 5414 of expressions.py and causing a recursion loop. (that's also why I was calling maybe_parse - I was looking at the body of exp.func, but it makes sense that it's not necessary here now)

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.

Thanks for the PR!

@tobymao tobymao merged commit 41b90be into tobymao:main May 11, 2023
adrianisk pushed a commit to adrianisk/sqlglot that referenced this pull request Jun 21, 2023
…ymao#1598)

* Fix(snowflake): Handle form of CONVERT_TIMEZONE with a source TZ

Snowflake's [CONVERT_TIMEZONE](https://docs.snowflake.com/en/sql-reference/functions/convert_timezone) allows optionally specifying a source timezone for columns without timezone information. This currently causes incorrect SQL to be generated when parsed and re-rendered.

* Address code review comments
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.

3 participants