diff --git a/bigframes/core/compile/sqlglot/expressions/generic_ops.py b/bigframes/core/compile/sqlglot/expressions/generic_ops.py index 60366b02c9..9782ef11d4 100644 --- a/bigframes/core/compile/sqlglot/expressions/generic_ops.py +++ b/bigframes/core/compile/sqlglot/expressions/generic_ops.py @@ -76,8 +76,8 @@ def _(expr: TypedExpr) -> sge.Expression: @register_unary_op(ops.invert_op) def _(expr: TypedExpr) -> sge.Expression: if expr.dtype == dtypes.BOOL_DTYPE: - return sge.Not(this=expr.expr) - return sge.BitwiseNot(this=expr.expr) + return sge.Not(this=sge.paren(expr.expr)) + return sge.BitwiseNot(this=sge.paren(expr.expr)) @register_unary_op(ops.isnull_op) diff --git a/bigframes/core/compile/sqlglot/expressions/numeric_ops.py b/bigframes/core/compile/sqlglot/expressions/numeric_ops.py index 3bbe2623ea..e732d064a1 100644 --- a/bigframes/core/compile/sqlglot/expressions/numeric_ops.py +++ b/bigframes/core/compile/sqlglot/expressions/numeric_ops.py @@ -189,7 +189,7 @@ def _(expr: TypedExpr) -> sge.Expression: @register_unary_op(ops.neg_op) def _(expr: TypedExpr) -> sge.Expression: - return sge.Neg(this=expr.expr) + return sge.Neg(this=sge.paren(expr.expr)) @register_unary_op(ops.pos_op) diff --git a/tests/system/small/engines/test_generic_ops.py b/tests/system/small/engines/test_generic_ops.py index f209b95496..5641f91a9a 100644 --- a/tests/system/small/engines/test_generic_ops.py +++ b/tests/system/small/engines/test_generic_ops.py @@ -454,7 +454,7 @@ def test_engines_isin_op(scalars_array_value: array_value.ArrayValue, engine): assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine) -@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) +@pytest.mark.parametrize("engine", ["polars", "bq", "bq-sqlglot"], indirect=True) def test_engines_isin_op_nested_filter( scalars_array_value: array_value.ArrayValue, engine ): diff --git a/tests/unit/core/compile/sqlglot/expressions/snapshots/test_generic_ops/test_invert/out.sql b/tests/unit/core/compile/sqlglot/expressions/snapshots/test_generic_ops/test_invert/out.sql index b5a5b92b52..bf005efb05 100644 --- a/tests/unit/core/compile/sqlglot/expressions/snapshots/test_generic_ops/test_invert/out.sql +++ b/tests/unit/core/compile/sqlglot/expressions/snapshots/test_generic_ops/test_invert/out.sql @@ -7,9 +7,15 @@ WITH `bfcte_0` AS ( ), `bfcte_1` AS ( SELECT *, - ~`bfcol_2` AS `bfcol_6`, - ~`bfcol_1` AS `bfcol_7`, - NOT `bfcol_0` AS `bfcol_8` + ~( + `bfcol_2` + ) AS `bfcol_6`, + ~( + `bfcol_1` + ) AS `bfcol_7`, + NOT ( + `bfcol_0` + ) AS `bfcol_8` FROM `bfcte_0` ) SELECT diff --git a/tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_mod_numeric/out.sql b/tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_mod_numeric/out.sql index 7913b43aa6..64f456a72d 100644 --- a/tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_mod_numeric/out.sql +++ b/tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_mod_numeric/out.sql @@ -38,23 +38,43 @@ WITH `bfcte_0` AS ( `bfcol_8` AS `bfcol_16`, `bfcol_9` AS `bfcol_17`, CASE - WHEN -`bfcol_7` = CAST(0 AS INT64) + WHEN -( + `bfcol_7` + ) = CAST(0 AS INT64) THEN CAST(0 AS INT64) * `bfcol_7` - WHEN -`bfcol_7` < CAST(0 AS INT64) + WHEN -( + `bfcol_7` + ) < CAST(0 AS INT64) AND ( - MOD(`bfcol_7`, -`bfcol_7`) + MOD(`bfcol_7`, -( + `bfcol_7` + )) ) > CAST(0 AS INT64) - THEN -`bfcol_7` + ( - MOD(`bfcol_7`, -`bfcol_7`) + THEN -( + `bfcol_7` + ) + ( + MOD(`bfcol_7`, -( + `bfcol_7` + )) ) - WHEN -`bfcol_7` > CAST(0 AS INT64) + WHEN -( + `bfcol_7` + ) > CAST(0 AS INT64) AND ( - MOD(`bfcol_7`, -`bfcol_7`) + MOD(`bfcol_7`, -( + `bfcol_7` + )) ) < CAST(0 AS INT64) - THEN -`bfcol_7` + ( - MOD(`bfcol_7`, -`bfcol_7`) + THEN -( + `bfcol_7` + ) + ( + MOD(`bfcol_7`, -( + `bfcol_7` + )) ) - ELSE MOD(`bfcol_7`, -`bfcol_7`) + ELSE MOD(`bfcol_7`, -( + `bfcol_7` + )) END AS `bfcol_18` FROM `bfcte_1` ), `bfcte_3` AS ( @@ -152,23 +172,43 @@ WITH `bfcte_0` AS ( `bfcol_56` AS `bfcol_72`, `bfcol_57` AS `bfcol_73`, CASE - WHEN CAST(-`bfcol_52` AS BIGNUMERIC) = CAST(0 AS INT64) + WHEN CAST(-( + `bfcol_52` + ) AS BIGNUMERIC) = CAST(0 AS INT64) THEN CAST('NaN' AS FLOAT64) * CAST(`bfcol_52` AS BIGNUMERIC) - WHEN CAST(-`bfcol_52` AS BIGNUMERIC) < CAST(0 AS INT64) + WHEN CAST(-( + `bfcol_52` + ) AS BIGNUMERIC) < CAST(0 AS INT64) AND ( - MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-`bfcol_52` AS BIGNUMERIC)) + MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-( + `bfcol_52` + ) AS BIGNUMERIC)) ) > CAST(0 AS INT64) - THEN CAST(-`bfcol_52` AS BIGNUMERIC) + ( - MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-`bfcol_52` AS BIGNUMERIC)) + THEN CAST(-( + `bfcol_52` + ) AS BIGNUMERIC) + ( + MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-( + `bfcol_52` + ) AS BIGNUMERIC)) ) - WHEN CAST(-`bfcol_52` AS BIGNUMERIC) > CAST(0 AS INT64) + WHEN CAST(-( + `bfcol_52` + ) AS BIGNUMERIC) > CAST(0 AS INT64) AND ( - MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-`bfcol_52` AS BIGNUMERIC)) + MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-( + `bfcol_52` + ) AS BIGNUMERIC)) ) < CAST(0 AS INT64) - THEN CAST(-`bfcol_52` AS BIGNUMERIC) + ( - MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-`bfcol_52` AS BIGNUMERIC)) + THEN CAST(-( + `bfcol_52` + ) AS BIGNUMERIC) + ( + MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-( + `bfcol_52` + ) AS BIGNUMERIC)) ) - ELSE MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-`bfcol_52` AS BIGNUMERIC)) + ELSE MOD(CAST(`bfcol_52` AS BIGNUMERIC), CAST(-( + `bfcol_52` + ) AS BIGNUMERIC)) END AS `bfcol_74` FROM `bfcte_5` ), `bfcte_7` AS ( diff --git a/tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_neg/out.sql b/tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_neg/out.sql index 46c58f766d..39bdd6da7f 100644 --- a/tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_neg/out.sql +++ b/tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_neg/out.sql @@ -5,7 +5,9 @@ WITH `bfcte_0` AS ( ), `bfcte_1` AS ( SELECT *, - -`bfcol_0` AS `bfcol_1` + -( + `bfcol_0` + ) AS `bfcol_1` FROM `bfcte_0` ) SELECT