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

Incorrect transpiling of MOD function from trino to snowflake #3431

Closed
daniel769 opened this issue May 8, 2024 · 0 comments · Fixed by #3440
Closed

Incorrect transpiling of MOD function from trino to snowflake #3431

daniel769 opened this issue May 8, 2024 · 0 comments · Fixed by #3440
Assignees

Comments

@daniel769
Copy link

Appears in 3.14
The following expression in Trino
(MOD((DAY_OF_YEAR(world."orders.date") % 7) - 1 + 7, 7))
is transpiled in Snowflake as
((DAYOFYEAR(orders.date) % 7) - 1 + 7 % 7)

The transpiled expression returns incorrect result, as values could be negative (-1). Missing a parenthesis before the modulo operator, to make it correct.

Expected result:
(MOD((DAYOFYEAR(orders.date) % 7) - 1 + 7, 7))
or
(((DAYOFYEAR(orders.date) % 7) - 1 + 7 ) % 7)

Fully reproducible code snippet

from sqlglot import Dialects, parse_one
trino_sql = 'SELECT (MOD((DAY_OF_YEAR(orders.date) % 7) - 1 + 7, 7)) FROM orders'
parsed = parse_one(sql, dialect=Dialects.TRINO)
snowflake_sql = parsed.sql(dialect=Dialects.SNOWFLAKE)

Official Documentation
https://trino.io/docs/current/functions/datetime.html#day_of_year
https://trino.io/docs/current/functions/math.html#mod
https://docs.snowflake.com/en/sql-reference/functions/year
https://docs.snowflake.com/en/sql-reference/functions/mod

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 a pull request may close this issue.

2 participants