Skip to content

Commit db21a85

Browse files
authored
refactor: add paren to the invert and neg op (#2189)
1 parent b9a5804 commit db21a85

File tree

6 files changed

+76
-28
lines changed

6 files changed

+76
-28
lines changed

bigframes/core/compile/sqlglot/expressions/generic_ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ def _(expr: TypedExpr) -> sge.Expression:
7676
@register_unary_op(ops.invert_op)
7777
def _(expr: TypedExpr) -> sge.Expression:
7878
if expr.dtype == dtypes.BOOL_DTYPE:
79-
return sge.Not(this=expr.expr)
80-
return sge.BitwiseNot(this=expr.expr)
79+
return sge.Not(this=sge.paren(expr.expr))
80+
return sge.BitwiseNot(this=sge.paren(expr.expr))
8181

8282

8383
@register_unary_op(ops.isnull_op)

bigframes/core/compile/sqlglot/expressions/numeric_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def _(expr: TypedExpr) -> sge.Expression:
190190

191191
@register_unary_op(ops.neg_op)
192192
def _(expr: TypedExpr) -> sge.Expression:
193-
return sge.Neg(this=expr.expr)
193+
return sge.Neg(this=sge.paren(expr.expr))
194194

195195

196196
@register_unary_op(ops.pos_op)

tests/system/small/engines/test_generic_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def test_engines_isin_op(scalars_array_value: array_value.ArrayValue, engine):
454454
assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine)
455455

456456

457-
@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)
457+
@pytest.mark.parametrize("engine", ["polars", "bq", "bq-sqlglot"], indirect=True)
458458
def test_engines_isin_op_nested_filter(
459459
scalars_array_value: array_value.ArrayValue, engine
460460
):

tests/unit/core/compile/sqlglot/expressions/snapshots/test_generic_ops/test_invert/out.sql

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ WITH `bfcte_0` AS (
77
), `bfcte_1` AS (
88
SELECT
99
*,
10-
~`bfcol_2` AS `bfcol_6`,
11-
~`bfcol_1` AS `bfcol_7`,
12-
NOT `bfcol_0` AS `bfcol_8`
10+
~(
11+
`bfcol_2`
12+
) AS `bfcol_6`,
13+
~(
14+
`bfcol_1`
15+
) AS `bfcol_7`,
16+
NOT (
17+
`bfcol_0`
18+
) AS `bfcol_8`
1319
FROM `bfcte_0`
1420
)
1521
SELECT

tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_mod_numeric/out.sql

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,43 @@ WITH `bfcte_0` AS (
3838
`bfcol_8` AS `bfcol_16`,
3939
`bfcol_9` AS `bfcol_17`,
4040
CASE
41-
WHEN -`bfcol_7` = CAST(0 AS INT64)
41+
WHEN -(
42+
`bfcol_7`
43+
) = CAST(0 AS INT64)
4244
THEN CAST(0 AS INT64) * `bfcol_7`
43-
WHEN -`bfcol_7` < CAST(0 AS INT64)
45+
WHEN -(
46+
`bfcol_7`
47+
) < CAST(0 AS INT64)
4448
AND (
45-
MOD(`bfcol_7`, -`bfcol_7`)
49+
MOD(`bfcol_7`, -(
50+
`bfcol_7`
51+
))
4652
) > CAST(0 AS INT64)
47-
THEN -`bfcol_7` + (
48-
MOD(`bfcol_7`, -`bfcol_7`)
53+
THEN -(
54+
`bfcol_7`
55+
) + (
56+
MOD(`bfcol_7`, -(
57+
`bfcol_7`
58+
))
4959
)
50-
WHEN -`bfcol_7` > CAST(0 AS INT64)
60+
WHEN -(
61+
`bfcol_7`
62+
) > CAST(0 AS INT64)
5163
AND (
52-
MOD(`bfcol_7`, -`bfcol_7`)
64+
MOD(`bfcol_7`, -(
65+
`bfcol_7`
66+
))
5367
) < CAST(0 AS INT64)
54-
THEN -`bfcol_7` + (
55-
MOD(`bfcol_7`, -`bfcol_7`)
68+
THEN -(
69+
`bfcol_7`
70+
) + (
71+
MOD(`bfcol_7`, -(
72+
`bfcol_7`
73+
))
5674
)
57-
ELSE MOD(`bfcol_7`, -`bfcol_7`)
75+
ELSE MOD(`bfcol_7`, -(
76+
`bfcol_7`
77+
))
5878
END AS `bfcol_18`
5979
FROM `bfcte_1`
6080
), `bfcte_3` AS (
@@ -152,23 +172,43 @@ WITH `bfcte_0` AS (
152172
`bfcol_56` AS `bfcol_72`,
153173
`bfcol_57` AS `bfcol_73`,
154174
CASE
155-
WHEN CAST(-`bfcol_52` AS BIGNUMERIC) = CAST(0 AS INT64)
175+
WHEN CAST(-(
176+
`bfcol_52`
177+
) AS BIGNUMERIC) = CAST(0 AS INT64)
156178
THEN CAST('NaN' AS FLOAT64) * CAST(`bfcol_52` AS BIGNUMERIC)
157-
WHEN CAST(-`bfcol_52` AS BIGNUMERIC) < CAST(0 AS INT64)
179+
WHEN CAST(-(
180+
`bfcol_52`
181+
) AS BIGNUMERIC) < CAST(0 AS INT64)
158182
AND (
159-
MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-`bfcol_52` AS BIGNUMERIC))
183+
MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-(
184+
`bfcol_52`
185+
) AS BIGNUMERIC))
160186
) > CAST(0 AS INT64)
161-
THEN CAST(-`bfcol_52` AS BIGNUMERIC) + (
162-
MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-`bfcol_52` AS BIGNUMERIC))
187+
THEN CAST(-(
188+
`bfcol_52`
189+
) AS BIGNUMERIC) + (
190+
MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-(
191+
`bfcol_52`
192+
) AS BIGNUMERIC))
163193
)
164-
WHEN CAST(-`bfcol_52` AS BIGNUMERIC) > CAST(0 AS INT64)
194+
WHEN CAST(-(
195+
`bfcol_52`
196+
) AS BIGNUMERIC) > CAST(0 AS INT64)
165197
AND (
166-
MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-`bfcol_52` AS BIGNUMERIC))
198+
MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-(
199+
`bfcol_52`
200+
) AS BIGNUMERIC))
167201
) < CAST(0 AS INT64)
168-
THEN CAST(-`bfcol_52` AS BIGNUMERIC) + (
169-
MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-`bfcol_52` AS BIGNUMERIC))
202+
THEN CAST(-(
203+
`bfcol_52`
204+
) AS BIGNUMERIC) + (
205+
MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-(
206+
`bfcol_52`
207+
) AS BIGNUMERIC))
170208
)
171-
ELSE MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-`bfcol_52` AS BIGNUMERIC))
209+
ELSE MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-(
210+
`bfcol_52`
211+
) AS BIGNUMERIC))
172212
END AS `bfcol_74`
173213
FROM `bfcte_5`
174214
), `bfcte_7` AS (

tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_neg/out.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ WITH `bfcte_0` AS (
55
), `bfcte_1` AS (
66
SELECT
77
*,
8-
-`bfcol_0` AS `bfcol_1`
8+
-(
9+
`bfcol_0`
10+
) AS `bfcol_1`
911
FROM `bfcte_0`
1012
)
1113
SELECT

0 commit comments

Comments
 (0)