-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(snowflake)!: update transpilation of DATE_TRUNC to duckdb #6644
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
Conversation
SQLGlot Integration Test ResultsComparing:
By Dialect
Overallmain: 5697 total, 5428 passed (pass rate: 95.3%), sqlglot version: sqlglot:tori/date-trunc-casting: 5697 total, 5429 passed (pass rate: 95.3%), sqlglot version: Difference: No change |
8be4fc7 to
3083567
Compare
sqlglot/expressions.py
Outdated
| class DateTrunc(Func): | ||
| arg_types = {"unit": True, "this": True, "zone": False} | ||
| arg_types = {"unit": True, "this": True, "zone": False, "cast_to_granularity_type": False} |
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.
Don't we want to set a flag in Snowflake that means "the output type matches the value's type" instead of specifying an "action" like "cast", etc? This feels backwards to me.
Perhaps input_type_preserved more accurately represents this? Ditto for TimestampTrunc.
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.
renamed to input_type_preserved. i agree, this naming is a better representation since we set the flag in Snowflake (snowflake.py#L267)
sqlglot/dialects/duckdb.py
Outdated
| if not date.type: | ||
| from sqlglot.optimizer.annotate_types import annotate_types | ||
|
|
||
| date = annotate_types(date, dialect=self.dialect) |
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.
Let's only annotate if the boolean flag is set, otherwise the type attribute is not needed.
d22dc92 to
9105f96
Compare
georgesittas
left a comment
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.
Couple of final comments, feel free to merge when addressed.
Improve Snowflake --> DuckDB transpilation of DATE_TRUNC.
Return types / casting
Fix:
In the Snowflake parser, modify the AST when the input of
DATE_TRUNC(granularity_part, value)is either not both dates or both not times. Then, handle casting to the granularity_part type on DuckDB side.Support Snowflake's DATE_TRUNC(time_part, time)
Snowflake can truncate time input (HH:MM:SS), but DuckDB cannot.
Fix: Add a dummy date to the time expression to create a full date and time. After truncating, cast it to TIME to isolate the time.