Skip to content

Commit e19598e

Browse files
authored
Merge branch 'main' into b455813915-pyformat-str
2 parents f774fd8 + 24ba4a1 commit e19598e

File tree

7 files changed

+108
-0
lines changed

7 files changed

+108
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import bigframes.core.compile.sqlglot.scalar_compiler as scalar_compiler
2222

2323
register_unary_op = scalar_compiler.scalar_op_compiler.register_unary_op
24+
register_binary_op = scalar_compiler.scalar_op_compiler.register_binary_op
2425

2526

2627
@register_unary_op(ops.geo_area_op)
@@ -108,3 +109,8 @@ def _(expr: TypedExpr) -> sge.Expression:
108109
@register_unary_op(ops.geo_y_op)
109110
def _(expr: TypedExpr) -> sge.Expression:
110111
return sge.func("SAFE.ST_Y", expr.expr)
112+
113+
114+
@register_binary_op(ops.geo_st_difference_op)
115+
def _(left: TypedExpr, right: TypedExpr) -> sge.Expression:
116+
return sge.func("ST_DIFFERENCE", left.expr, right.expr)

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ def _(expr: TypedExpr) -> sge.Expression:
7777
return sge.func("ASINH", expr.expr)
7878

7979

80+
@register_binary_op(ops.arctan2_op)
81+
def _(left: TypedExpr, right: TypedExpr) -> sge.Expression:
82+
left_expr = _coerce_bool_to_int(left)
83+
right_expr = _coerce_bool_to_int(right)
84+
return sge.func("ATAN2", left_expr, right_expr)
85+
86+
8087
@register_unary_op(ops.arctan_op)
8188
def _(expr: TypedExpr) -> sge.Expression:
8289
return sge.func("ATAN", expr.expr)
@@ -118,6 +125,18 @@ def _(expr: TypedExpr) -> sge.Expression:
118125
)
119126

120127

128+
@register_binary_op(ops.cosine_distance_op)
129+
def _(left: TypedExpr, right: TypedExpr) -> sge.Expression:
130+
return sge.Anonymous(
131+
this="ML.DISTANCE",
132+
expressions=[
133+
left.expr,
134+
right.expr,
135+
sge.Literal.string("COSINE"),
136+
],
137+
)
138+
139+
121140
@register_unary_op(ops.exp_op)
122141
def _(expr: TypedExpr) -> sge.Expression:
123142
return sge.Case(
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+
`geography_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
ST_DIFFERENCE(`bfcol_0`, `bfcol_0`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `geography_col`
13+
FROM `bfcte_1`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`bool_col` AS `bfcol_0`,
4+
`int64_col` AS `bfcol_1`,
5+
`float64_col` AS `bfcol_2`
6+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
7+
), `bfcte_1` AS (
8+
SELECT
9+
*,
10+
ATAN2(`bfcol_1`, `bfcol_2`) AS `bfcol_6`,
11+
ATAN2(CAST(`bfcol_0` AS INT64), `bfcol_2`) AS `bfcol_7`
12+
FROM `bfcte_0`
13+
)
14+
SELECT
15+
`bfcol_6` AS `int64_col`,
16+
`bfcol_7` AS `bool_col`
17+
FROM `bfcte_1`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`int_list_col` AS `bfcol_0`,
4+
`float_list_col` AS `bfcol_1`
5+
FROM `bigframes-dev`.`sqlglot_test`.`repeated_types`
6+
), `bfcte_1` AS (
7+
SELECT
8+
*,
9+
ML.DISTANCE(`bfcol_0`, `bfcol_0`, 'COSINE') AS `bfcol_2`,
10+
ML.DISTANCE(`bfcol_1`, `bfcol_1`, 'COSINE') AS `bfcol_3`
11+
FROM `bfcte_0`
12+
)
13+
SELECT
14+
`bfcol_2` AS `int_list_col`,
15+
`bfcol_3` AS `float_list_col`
16+
FROM `bfcte_1`

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ def test_geo_st_convexhull(scalar_types_df: bpd.DataFrame, snapshot):
8181
snapshot.assert_match(sql, "out.sql")
8282

8383

84+
def test_geo_st_difference(scalar_types_df: bpd.DataFrame, snapshot):
85+
col_name = "geography_col"
86+
bf_df = scalar_types_df[[col_name]]
87+
sql = utils._apply_binary_op(bf_df, ops.geo_st_difference_op, col_name, col_name)
88+
89+
snapshot.assert_match(sql, "out.sql")
90+
91+
8492
def test_geo_st_geogfromtext(scalar_types_df: bpd.DataFrame, snapshot):
8593
col_name = "string_col"
8694
bf_df = scalar_types_df[[col_name]]

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ def test_arcsinh(scalar_types_df: bpd.DataFrame, snapshot):
5555
snapshot.assert_match(sql, "out.sql")
5656

5757

58+
def test_arctan2(scalar_types_df: bpd.DataFrame, snapshot):
59+
bf_df = scalar_types_df[["int64_col", "float64_col", "bool_col"]]
60+
61+
sql = utils._apply_ops_to_sql(
62+
bf_df,
63+
[
64+
ops.arctan2_op.as_expr("int64_col", "float64_col"),
65+
ops.arctan2_op.as_expr("bool_col", "float64_col"),
66+
],
67+
["int64_col", "bool_col"],
68+
)
69+
snapshot.assert_match(sql, "out.sql")
70+
71+
5872
def test_arctan(scalar_types_df: bpd.DataFrame, snapshot):
5973
col_name = "float64_col"
6074
bf_df = scalar_types_df[[col_name]]
@@ -103,6 +117,21 @@ def test_cosh(scalar_types_df: bpd.DataFrame, snapshot):
103117
snapshot.assert_match(sql, "out.sql")
104118

105119

120+
def test_cosine_distance(repeated_types_df: bpd.DataFrame, snapshot):
121+
col_names = ["int_list_col", "float_list_col"]
122+
bf_df = repeated_types_df[col_names]
123+
124+
sql = utils._apply_ops_to_sql(
125+
bf_df,
126+
[
127+
ops.cosine_distance_op.as_expr("int_list_col", "int_list_col"),
128+
ops.cosine_distance_op.as_expr("float_list_col", "float_list_col"),
129+
],
130+
["int_list_col", "float_list_col"],
131+
)
132+
snapshot.assert_match(sql, "out.sql")
133+
134+
106135
def test_exp(scalar_types_df: bpd.DataFrame, snapshot):
107136
col_name = "float64_col"
108137
bf_df = scalar_types_df[[col_name]]

0 commit comments

Comments
 (0)