Skip to content

Commit 706fac3

Browse files
authored
feat: add bigquery mod op (#3157)
* feat: add bigquery mod op * feedback * add parser func * fix tests
1 parent be8c5ea commit 706fac3

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

sqlglot/dialects/bigquery.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ class Generator(generator.Generator):
616616
exp.IntDiv: rename_func("DIV"),
617617
exp.JSONFormat: rename_func("TO_JSON_STRING"),
618618
exp.Max: max_or_greatest,
619+
exp.Mod: rename_func("MOD"),
619620
exp.MD5: lambda self, e: self.func("TO_HEX", self.func("MD5", e.this)),
620621
exp.MD5Digest: rename_func("MD5"),
621622
exp.Min: min_or_least,

sqlglot/parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class Parser(metaclass=_Parser):
121121
"LOG": build_logarithm,
122122
"LOG2": lambda args: exp.Log(this=exp.Literal.number(2), expression=seq_get(args, 0)),
123123
"LOG10": lambda args: exp.Log(this=exp.Literal.number(10), expression=seq_get(args, 0)),
124+
"MOD": lambda args: exp.Mod(this=seq_get(args, 0), expression=seq_get(args, 1)),
124125
"TIME_TO_TIME_STR": lambda args: exp.Cast(
125126
this=seq_get(args, 0),
126127
to=exp.DataType(this=exp.DataType.Type.TEXT),

tests/dialects/test_bigquery.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,28 +1068,36 @@ def test_bigquery(self):
10681068
)
10691069
self.validate_all(
10701070
"""SELECT
1071-
`u`.`harness_user_email` AS `harness_user_email`,
1072-
`d`.`harness_user_id` AS `harness_user_id`,
1073-
`harness_account_id` AS `harness_account_id`
1074-
FROM `analytics_staging`.`stg_mongodb__users` AS `u`, UNNEST(`u`.`harness_cluster_details`) AS `d`, UNNEST(`d`.`harness_account_ids`) AS `harness_account_id`
1071+
`u`.`user_email` AS `user_email`,
1072+
`d`.`user_id` AS `user_id`,
1073+
`account_id` AS `account_id`
1074+
FROM `analytics_staging`.`stg_mongodb__users` AS `u`, UNNEST(`u`.`cluster_details`) AS `d`, UNNEST(`d`.`account_ids`) AS `account_id`
10751075
WHERE
1076-
NOT `harness_account_id` IS NULL""",
1076+
NOT `account_id` IS NULL""",
10771077
read={
10781078
"": """
10791079
SELECT
1080-
"u"."harness_user_email" AS "harness_user_email",
1081-
"_q_0"."d"."harness_user_id" AS "harness_user_id",
1082-
"_q_1"."harness_account_id" AS "harness_account_id"
1080+
"u"."user_email" AS "user_email",
1081+
"_q_0"."d"."user_id" AS "user_id",
1082+
"_q_1"."account_id" AS "account_id"
10831083
FROM
10841084
"analytics_staging"."stg_mongodb__users" AS "u",
1085-
UNNEST("u"."harness_cluster_details") AS "_q_0"("d"),
1086-
UNNEST("_q_0"."d"."harness_account_ids") AS "_q_1"("harness_account_id")
1085+
UNNEST("u"."cluster_details") AS "_q_0"("d"),
1086+
UNNEST("_q_0"."d"."account_ids") AS "_q_1"("account_id")
10871087
WHERE
1088-
NOT "_q_1"."harness_account_id" IS NULL
1088+
NOT "_q_1"."account_id" IS NULL
10891089
"""
10901090
},
10911091
pretty=True,
10921092
)
1093+
self.validate_all(
1094+
"SELECT MOD(x, 10)",
1095+
read={"postgres": "SELECT x % 10"},
1096+
write={
1097+
"bigquery": "SELECT MOD(x, 10)",
1098+
"postgres": "SELECT x % 10",
1099+
},
1100+
)
10931101

10941102
def test_errors(self):
10951103
with self.assertRaises(TokenError):

tests/dialects/test_duckdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ def test_time(self):
754754
write={"duckdb": "SELECT (90 * INTERVAL '1' DAY)"},
755755
)
756756
self.validate_all(
757-
"SELECT ((DATE_TRUNC('DAY', CAST(CAST(DATE_TRUNC('DAY', CURRENT_TIMESTAMP) AS DATE) AS TIMESTAMP) + INTERVAL (0 - MOD((DAYOFWEEK(CAST(CAST(DATE_TRUNC('DAY', CURRENT_TIMESTAMP) AS DATE) AS TIMESTAMP)) % 7) - 1 + 7, 7)) DAY) + (7 * INTERVAL (-5) DAY))) AS t1",
757+
"SELECT ((DATE_TRUNC('DAY', CAST(CAST(DATE_TRUNC('DAY', CURRENT_TIMESTAMP) AS DATE) AS TIMESTAMP) + INTERVAL (0 - (DAYOFWEEK(CAST(CAST(DATE_TRUNC('DAY', CURRENT_TIMESTAMP) AS DATE) AS TIMESTAMP)) % 7) - 1 + 7 % 7) DAY) + (7 * INTERVAL (-5) DAY))) AS t1",
758758
read={
759759
"presto": "SELECT ((DATE_ADD('week', -5, DATE_TRUNC('DAY', DATE_ADD('day', (0 - MOD((DAY_OF_WEEK(CAST(CAST(DATE_TRUNC('DAY', NOW()) AS DATE) AS TIMESTAMP)) % 7) - 1 + 7, 7)), CAST(CAST(DATE_TRUNC('DAY', NOW()) AS DATE) AS TIMESTAMP)))))) AS t1",
760760
},

0 commit comments

Comments
 (0)