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: func to binary MOD generation #3440

Merged
merged 3 commits into from
May 9, 2024
Merged

fix: func to binary MOD generation #3440

merged 3 commits into from
May 9, 2024

Conversation

VaggelisD
Copy link
Collaborator

Fixes #3431

Dialects such as Trino support both MOD(a, b) and a % b representations of the modulo operation. Until now, the generation from the former to the latter would happen as is, which is incorrect for non-leaf operations; For example, the function call has the following semantics:

MOD(8 - 1 + 7, 7) == (8 - 1 + 7) % 7 == 0

But is generated to the following flat expression:

8 - 1 + 7 % 7 == 8 - 1 + (7 % 7) == 7

To solve this issue, when parsing a MOD(...) function call, the LHS is wrapped if it's a series of binary expressions i.e adds, subs etc, otherwise it remains as-is (case of single literals, nested function calls, columns etc).

sqlglot/dialects/bigquery.py Show resolved Hide resolved
sqlglot/parser.py Outdated Show resolved Hide resolved
sqlglot/parser.py Outdated Show resolved Hide resolved
@georgesittas georgesittas merged commit 1bc0ce5 into main May 9, 2024
5 checks passed
@georgesittas georgesittas deleted the vaggelisd/modulo_op branch May 9, 2024 10:35
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.

Incorrect transpiling of MOD function from trino to snowflake
2 participants