Skip to content

Commit 9ed0078

Browse files
authored
chore: implement GeoStBufferOp, geo_st_centroid_op, geo_st_convexhull_op and MapOp for sqlglot compilers (#2021)
1 parent 9d4504b commit 9ed0078

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,27 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
344344
return sge.func("ST_BOUNDARY", expr.expr)
345345

346346

347+
@UNARY_OP_REGISTRATION.register(ops.GeoStBufferOp)
348+
def _(op: ops.GeoStBufferOp, expr: TypedExpr) -> sge.Expression:
349+
return sge.func(
350+
"ST_BUFFER",
351+
expr.expr,
352+
sge.convert(op.buffer_radius),
353+
sge.convert(op.num_seg_quarter_circle),
354+
sge.convert(op.use_spheroid),
355+
)
356+
357+
358+
@UNARY_OP_REGISTRATION.register(ops.geo_st_centroid_op)
359+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
360+
return sge.func("ST_CENTROID", expr.expr)
361+
362+
363+
@UNARY_OP_REGISTRATION.register(ops.geo_st_convexhull_op)
364+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
365+
return sge.func("ST_CONVEXHULL", expr.expr)
366+
367+
347368
@UNARY_OP_REGISTRATION.register(ops.geo_st_geogfromtext_op)
348369
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
349370
return sge.func("SAFE.ST_GEOGFROMTEXT", expr.expr)
@@ -516,6 +537,17 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
516537
return sge.Lower(this=expr.expr)
517538

518539

540+
@UNARY_OP_REGISTRATION.register(ops.MapOp)
541+
def _(op: ops.MapOp, expr: TypedExpr) -> sge.Expression:
542+
return sge.Case(
543+
this=expr.expr,
544+
ifs=[
545+
sge.If(this=sge.convert(key), true=sge.convert(value))
546+
for key, value in op.mappings
547+
],
548+
)
549+
550+
519551
@UNARY_OP_REGISTRATION.register(ops.minute_op)
520552
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
521553
return sge.Extract(this=sge.Identifier(this="MINUTE"), expression=expr.expr)
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_BUFFER(`bfcol_0`, 1.0, 8.0, FALSE) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `geography_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+
`geography_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
ST_CENTROID(`bfcol_0`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `geography_col`
13+
FROM `bfcte_1`
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_CONVEXHULL(`bfcol_0`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `geography_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+
CASE `bfcol_0` WHEN 'value1' THEN 'mapped1' END AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `string_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
@@ -174,6 +174,27 @@ def test_geo_st_boundary(scalar_types_df: bpd.DataFrame, snapshot):
174174
snapshot.assert_match(sql, "out.sql")
175175

176176

177+
def test_geo_st_buffer(scalar_types_df: bpd.DataFrame, snapshot):
178+
bf_df = scalar_types_df[["geography_col"]]
179+
sql = _apply_unary_op(bf_df, ops.GeoStBufferOp(1.0, 8.0, False), "geography_col")
180+
181+
snapshot.assert_match(sql, "out.sql")
182+
183+
184+
def test_geo_st_centroid(scalar_types_df: bpd.DataFrame, snapshot):
185+
bf_df = scalar_types_df[["geography_col"]]
186+
sql = _apply_unary_op(bf_df, ops.geo_st_centroid_op, "geography_col")
187+
188+
snapshot.assert_match(sql, "out.sql")
189+
190+
191+
def test_geo_st_convexhull(scalar_types_df: bpd.DataFrame, snapshot):
192+
bf_df = scalar_types_df[["geography_col"]]
193+
sql = _apply_unary_op(bf_df, ops.geo_st_convexhull_op, "geography_col")
194+
195+
snapshot.assert_match(sql, "out.sql")
196+
197+
177198
def test_geo_st_geogfromtext(scalar_types_df: bpd.DataFrame, snapshot):
178199
bf_df = scalar_types_df[["string_col"]]
179200
sql = _apply_unary_op(bf_df, ops.geo_st_geogfromtext_op, "string_col")
@@ -370,6 +391,15 @@ def test_lower(scalar_types_df: bpd.DataFrame, snapshot):
370391
snapshot.assert_match(sql, "out.sql")
371392

372393

394+
def test_map(scalar_types_df: bpd.DataFrame, snapshot):
395+
bf_df = scalar_types_df[["string_col"]]
396+
sql = _apply_unary_op(
397+
bf_df, ops.MapOp(mappings=(("value1", "mapped1"),)), "string_col"
398+
)
399+
400+
snapshot.assert_match(sql, "out.sql")
401+
402+
373403
def test_lstrip(scalar_types_df: bpd.DataFrame, snapshot):
374404
bf_df = scalar_types_df[["string_col"]]
375405
sql = _apply_unary_op(bf_df, ops.StrLstripOp(" "), "string_col")

0 commit comments

Comments
 (0)