Skip to content

Commit bb66915

Browse files
authored
chore: Migrate GeoStDistanceOp operator to SQLGlot (#2282)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-bigquery-dataframes/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes b/447388852 🦕
1 parent c35e21f commit bb66915

File tree

3 files changed

+35
-0
lines changed
  • bigframes/core/compile/sqlglot/expressions
  • tests/unit/core/compile/sqlglot/expressions

3 files changed

+35
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ def _(expr: TypedExpr) -> sge.Expression:
116116
return sge.func("SAFE.ST_Y", expr.expr)
117117

118118

119+
@register_binary_op(ops.GeoStDistanceOp, pass_op=True)
120+
def _(left: TypedExpr, right: TypedExpr, op: ops.GeoStDistanceOp) -> sge.Expression:
121+
return sge.func("ST_DISTANCE", left.expr, right.expr, sge.convert(op.use_spheroid))
122+
123+
119124
@register_binary_op(ops.geo_st_difference_op)
120125
def _(left: TypedExpr, right: TypedExpr) -> sge.Expression:
121126
return sge.func("ST_DIFFERENCE", left.expr, right.expr)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`geography_col`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
ST_DISTANCE(`geography_col`, `geography_col`, TRUE) AS `bfcol_1`,
9+
ST_DISTANCE(`geography_col`, `geography_col`, FALSE) AS `bfcol_2`
10+
FROM `bfcte_0`
11+
)
12+
SELECT
13+
`bfcol_1` AS `spheroid`,
14+
`bfcol_2` AS `no_spheroid`
15+
FROM `bfcte_1`

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ 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_distance(scalar_types_df: bpd.DataFrame, snapshot):
85+
col_name = "geography_col"
86+
bf_df = scalar_types_df[[col_name]]
87+
88+
sql = utils._apply_ops_to_sql(
89+
bf_df,
90+
[
91+
ops.GeoStDistanceOp(use_spheroid=True).as_expr(col_name, col_name),
92+
ops.GeoStDistanceOp(use_spheroid=False).as_expr(col_name, col_name),
93+
],
94+
["spheroid", "no_spheroid"],
95+
)
96+
snapshot.assert_match(sql, "out.sql")
97+
98+
8499
def test_geo_st_difference(scalar_types_df: bpd.DataFrame, snapshot):
85100
col_name = "geography_col"
86101
bf_df = scalar_types_df[[col_name]]

0 commit comments

Comments
 (0)