diff --git a/bigframes/core/compile/sqlglot/expressions/geo_ops.py b/bigframes/core/compile/sqlglot/expressions/geo_ops.py index 10ccdfbeb3..5716dba0e4 100644 --- a/bigframes/core/compile/sqlglot/expressions/geo_ops.py +++ b/bigframes/core/compile/sqlglot/expressions/geo_ops.py @@ -116,6 +116,11 @@ def _(expr: TypedExpr) -> sge.Expression: return sge.func("SAFE.ST_Y", expr.expr) +@register_binary_op(ops.GeoStDistanceOp, pass_op=True) +def _(left: TypedExpr, right: TypedExpr, op: ops.GeoStDistanceOp) -> sge.Expression: + return sge.func("ST_DISTANCE", left.expr, right.expr, sge.convert(op.use_spheroid)) + + @register_binary_op(ops.geo_st_difference_op) def _(left: TypedExpr, right: TypedExpr) -> sge.Expression: return sge.func("ST_DIFFERENCE", left.expr, right.expr) diff --git a/tests/unit/core/compile/sqlglot/expressions/snapshots/test_geo_ops/test_geo_st_distance/out.sql b/tests/unit/core/compile/sqlglot/expressions/snapshots/test_geo_ops/test_geo_st_distance/out.sql new file mode 100644 index 0000000000..e98a581de7 --- /dev/null +++ b/tests/unit/core/compile/sqlglot/expressions/snapshots/test_geo_ops/test_geo_st_distance/out.sql @@ -0,0 +1,15 @@ +WITH `bfcte_0` AS ( + SELECT + `geography_col` + FROM `bigframes-dev`.`sqlglot_test`.`scalar_types` +), `bfcte_1` AS ( + SELECT + *, + ST_DISTANCE(`geography_col`, `geography_col`, TRUE) AS `bfcol_1`, + ST_DISTANCE(`geography_col`, `geography_col`, FALSE) AS `bfcol_2` + FROM `bfcte_0` +) +SELECT + `bfcol_1` AS `spheroid`, + `bfcol_2` AS `no_spheroid` +FROM `bfcte_1` \ No newline at end of file diff --git a/tests/unit/core/compile/sqlglot/expressions/test_geo_ops.py b/tests/unit/core/compile/sqlglot/expressions/test_geo_ops.py index 5684ff6f15..9047ce4d04 100644 --- a/tests/unit/core/compile/sqlglot/expressions/test_geo_ops.py +++ b/tests/unit/core/compile/sqlglot/expressions/test_geo_ops.py @@ -81,6 +81,21 @@ def test_geo_st_convexhull(scalar_types_df: bpd.DataFrame, snapshot): snapshot.assert_match(sql, "out.sql") +def test_geo_st_distance(scalar_types_df: bpd.DataFrame, snapshot): + col_name = "geography_col" + bf_df = scalar_types_df[[col_name]] + + sql = utils._apply_ops_to_sql( + bf_df, + [ + ops.GeoStDistanceOp(use_spheroid=True).as_expr(col_name, col_name), + ops.GeoStDistanceOp(use_spheroid=False).as_expr(col_name, col_name), + ], + ["spheroid", "no_spheroid"], + ) + snapshot.assert_match(sql, "out.sql") + + def test_geo_st_difference(scalar_types_df: bpd.DataFrame, snapshot): col_name = "geography_col" bf_df = scalar_types_df[[col_name]]