Skip to content

Commit c03b84e

Browse files
committed
chore: Migrate up to 5 scalar operators to SQLGlot
Migrated cos, hash, isnull, notnull, and sin operators.
1 parent e5d996c commit c03b84e

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,31 @@ def _(op: ops.ArraySliceOp, expr: TypedExpr) -> sge.Expression:
7272
return sge.array(selected_elements)
7373

7474

75+
@UNARY_OP_REGISTRATION.register(ops.cos_op)
76+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
77+
return sge.func("cos", expr.expr)
78+
79+
80+
@UNARY_OP_REGISTRATION.register(ops.hash_op)
81+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
82+
return sge.func("FARM_FINGERPRINT", expr.expr)
83+
84+
85+
@UNARY_OP_REGISTRATION.register(ops.isnull_op)
86+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
87+
return sge.Is(this=expr.expr, expression=sge.Null())
88+
89+
90+
@UNARY_OP_REGISTRATION.register(ops.notnull_op)
91+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
92+
return sge.Not(this=sge.Is(this=expr.expr, expression=sge.Null()))
93+
94+
95+
@UNARY_OP_REGISTRATION.register(ops.sin_op)
96+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
97+
return sge.func("sin", expr.expr)
98+
99+
75100
# JSON Ops
76101
@UNARY_OP_REGISTRATION.register(ops.JSONExtract)
77102
def _(op: ops.JSONExtract, expr: TypedExpr) -> sge.Expression:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`float64_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
cos(`bfcol_0`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `float64_col`
13+
FROM `bfcte_1`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`string_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
FARM_FINGERPRINT(`bfcol_0`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `string_col`
13+
FROM `bfcte_1`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`float64_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
`bfcol_0` IS NULL AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `float64_col`
13+
FROM `bfcte_1`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`float64_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
NOT `bfcol_0` IS NULL AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `float64_col`
13+
FROM `bfcte_1`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`float64_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
sin(`bfcol_0`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `float64_col`
13+
FROM `bfcte_1`

tests/unit/core/compile/sqlglot/expressions/test_unary_compiler.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,36 @@ def test_array_slice_with_start_and_stop(repeated_types_df: bpd.DataFrame, snaps
5858
snapshot.assert_match(sql, "out.sql")
5959

6060

61+
def test_cos(scalar_types_df: bpd.DataFrame, snapshot):
62+
bf_df = scalar_types_df[["float64_col"]]
63+
sql = _apply_unary_op(bf_df, ops.cos_op, "float64_col")
64+
snapshot.assert_match(sql, "out.sql")
65+
66+
67+
def test_hash(scalar_types_df: bpd.DataFrame, snapshot):
68+
bf_df = scalar_types_df[["string_col"]]
69+
sql = _apply_unary_op(bf_df, ops.hash_op, "string_col")
70+
snapshot.assert_match(sql, "out.sql")
71+
72+
73+
def test_isnull(scalar_types_df: bpd.DataFrame, snapshot):
74+
bf_df = scalar_types_df[["float64_col"]]
75+
sql = _apply_unary_op(bf_df, ops.isnull_op, "float64_col")
76+
snapshot.assert_match(sql, "out.sql")
77+
78+
79+
def test_notnull(scalar_types_df: bpd.DataFrame, snapshot):
80+
bf_df = scalar_types_df[["float64_col"]]
81+
sql = _apply_unary_op(bf_df, ops.notnull_op, "float64_col")
82+
snapshot.assert_match(sql, "out.sql")
83+
84+
85+
def test_sin(scalar_types_df: bpd.DataFrame, snapshot):
86+
bf_df = scalar_types_df[["float64_col"]]
87+
sql = _apply_unary_op(bf_df, ops.sin_op, "float64_col")
88+
snapshot.assert_match(sql, "out.sql")
89+
90+
6191
def test_json_extract(json_types_df: bpd.DataFrame, snapshot):
6292
bf_df = json_types_df[["json_col"]]
6393
sql = _apply_unary_op(bf_df, ops.JSONExtract(json_path="$"), "json_col")

0 commit comments

Comments
 (0)