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(mysql): DATE_ADD for datetimes #2360

Merged
merged 3 commits into from
Oct 3, 2023
Merged

fix(mysql): DATE_ADD for datetimes #2360

merged 3 commits into from
Oct 3, 2023

Conversation

barakalon
Copy link
Collaborator

The recent logic I added for transpiling DATE_ADD in MySQL was wrong.

MySQL's DATE_ADD semantics are different than Hive's: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add

I'm not sure of a good way to handle this at the parser/generator level without a bunch of new TsOrDs* functions. So I added it to the optimizer for now.

@@ -9775,7 +9775,7 @@ JOIN "date_dim" AS "d1"
ON "catalog_sales"."cs_sold_date_sk" = "d1"."d_date_sk"
AND "d1"."d_week_seq" = "d2"."d_week_seq"
AND "d1"."d_year" = 2002
AND "d3"."d_date" > CONCAT("d1"."d_date", INTERVAL '5' day)
AND "d3"."d_date" > "d1"."d_date" + INTERVAL '5' day
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Seems like this change caught a bug.

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.

Nice work @barakalon, these date/time optimizations look really interesting. I wonder if they are valid in all supported dialects though? Date/time stuff can vary wildly...

Left a few comments to consider.

sqlglot/expressions.py Outdated Show resolved Hide resolved
sqlglot/expressions.py Show resolved Hide resolved
sqlglot/optimizer/annotate_types.py Outdated Show resolved Hide resolved
tests/test_optimizer.py Outdated Show resolved Hide resolved
@barakalon
Copy link
Collaborator Author

Yeah, I also am unsure of all the different nuances of different dialects.
I guess it should be easy to make configurable though, given the implementation with BINARY_COERCIONS?

@barakalon barakalon merged commit 2bc30a5 into main Oct 3, 2023
@barakalon barakalon deleted the barak/tsordsadd-secs branch October 3, 2023 00:16
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