-
Notifications
You must be signed in to change notification settings - Fork 756
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
Conversation
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.
sqlglot/dialects/snowflake.py
Outdated
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) |
There was a problem hiding this comment.
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=...)
There was a problem hiding this comment.
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)
There was a problem hiding this 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!
…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
Snowflake's CONVERT_TIMEZONE has two forms:
The second is required if the timestamp doesn't have timezone information. Currently, parsing
produces
when re-rendered, which is invalid. This PR still treats the target-only case as
exp.AtTimeZone
, but parses the source-and-target case asexp.Anonymous
so that rendering back to Snowflake will be correct.