Skip to content

Commit f63ce9b

Browse files
authored
chore: Migrate up to 15 scalar operators to SQLGlot (#1938)
* chore: Migrate up to 15 scalar operators to SQLGlot Migrated the following unary scalar operators to SQLGlot: - iso_year_op - minute_op - month_op - quarter_op - second_op - time_op - year_op - normalize_op - geo_area_op - geo_st_astext_op - geo_x_op - geo_y_op - timedelta_floor_op - StrGetOp - StrSliceOp
1 parent ceca490 commit f63ce9b

File tree

17 files changed

+390
-0
lines changed

17 files changed

+390
-0
lines changed

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,26 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
239239
return sge.Floor(this=expr.expr)
240240

241241

242+
@UNARY_OP_REGISTRATION.register(ops.geo_area_op)
243+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
244+
return sge.func("ST_AREA", expr.expr)
245+
246+
247+
@UNARY_OP_REGISTRATION.register(ops.geo_st_astext_op)
248+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
249+
return sge.func("ST_ASTEXT", expr.expr)
250+
251+
252+
@UNARY_OP_REGISTRATION.register(ops.geo_x_op)
253+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
254+
return sge.func("SAFE.ST_X", expr.expr)
255+
256+
257+
@UNARY_OP_REGISTRATION.register(ops.geo_y_op)
258+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
259+
return sge.func("SAFE.ST_Y", expr.expr)
260+
261+
242262
@UNARY_OP_REGISTRATION.register(ops.hash_op)
243263
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
244264
return sge.func("FARM_FINGERPRINT", expr.expr)
@@ -361,6 +381,16 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
361381
return sge.Lower(this=expr.expr)
362382

363383

384+
@UNARY_OP_REGISTRATION.register(ops.minute_op)
385+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
386+
return sge.Extract(this=sge.Identifier(this="MINUTE"), expression=expr.expr)
387+
388+
389+
@UNARY_OP_REGISTRATION.register(ops.month_op)
390+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
391+
return sge.Extract(this=sge.Identifier(this="MONTH"), expression=expr.expr)
392+
393+
364394
@UNARY_OP_REGISTRATION.register(ops.StrLstripOp)
365395
def _(op: ops.StrLstripOp, expr: TypedExpr) -> sge.Expression:
366396
return sge.Trim(this=expr.expr, expression=sge.convert(op.to_strip), side="LEFT")
@@ -371,16 +401,31 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
371401
return sge.Neg(this=expr.expr)
372402

373403

404+
@UNARY_OP_REGISTRATION.register(ops.normalize_op)
405+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
406+
return sge.TimestampTrunc(this=expr.expr, unit=sge.Identifier(this="DAY"))
407+
408+
374409
@UNARY_OP_REGISTRATION.register(ops.pos_op)
375410
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
376411
return expr.expr
377412

378413

414+
@UNARY_OP_REGISTRATION.register(ops.quarter_op)
415+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
416+
return sge.Extract(this=sge.Identifier(this="QUARTER"), expression=expr.expr)
417+
418+
379419
@UNARY_OP_REGISTRATION.register(ops.reverse_op)
380420
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
381421
return sge.func("REVERSE", expr.expr)
382422

383423

424+
@UNARY_OP_REGISTRATION.register(ops.second_op)
425+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
426+
return sge.Extract(this=sge.Identifier(this="SECOND"), expression=expr.expr)
427+
428+
384429
@UNARY_OP_REGISTRATION.register(ops.StrRstripOp)
385430
def _(op: ops.StrRstripOp, expr: TypedExpr) -> sge.Expression:
386431
return sge.Trim(this=expr.expr, expression=sge.convert(op.to_strip), side="RIGHT")
@@ -414,6 +459,11 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
414459
return sge.Extract(this=sge.Identifier(this="ISOWEEK"), expression=expr.expr)
415460

416461

462+
@UNARY_OP_REGISTRATION.register(ops.iso_year_op)
463+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
464+
return sge.Extract(this=sge.Identifier(this="ISOYEAR"), expression=expr.expr)
465+
466+
417467
@UNARY_OP_REGISTRATION.register(ops.isnull_op)
418468
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
419469
return sge.Is(this=expr.expr, expression=sge.Null())
@@ -442,6 +492,31 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
442492
)
443493

444494

495+
@UNARY_OP_REGISTRATION.register(ops.StrGetOp)
496+
def _(op: ops.StrGetOp, expr: TypedExpr) -> sge.Expression:
497+
return sge.Substring(
498+
this=expr.expr,
499+
start=sge.convert(op.i + 1),
500+
length=sge.convert(1),
501+
)
502+
503+
504+
@UNARY_OP_REGISTRATION.register(ops.StrSliceOp)
505+
def _(op: ops.StrSliceOp, expr: TypedExpr) -> sge.Expression:
506+
start = op.start + 1 if op.start is not None else None
507+
if op.end is None:
508+
length = None
509+
elif op.start is None:
510+
length = op.end
511+
else:
512+
length = op.end - op.start
513+
return sge.Substring(
514+
this=expr.expr,
515+
start=sge.convert(start) if start is not None else None,
516+
length=sge.convert(length) if length is not None else None,
517+
)
518+
519+
445520
@UNARY_OP_REGISTRATION.register(ops.tan_op)
446521
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
447522
return sge.func("TAN", expr.expr)
@@ -452,6 +527,16 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
452527
return sge.func("TANH", expr.expr)
453528

454529

530+
@UNARY_OP_REGISTRATION.register(ops.time_op)
531+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
532+
return sge.func("TIME", expr.expr)
533+
534+
535+
@UNARY_OP_REGISTRATION.register(ops.timedelta_floor_op)
536+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
537+
return sge.Floor(this=expr.expr)
538+
539+
455540
# JSON Ops
456541
@UNARY_OP_REGISTRATION.register(ops.JSONExtract)
457542
def _(op: ops.JSONExtract, expr: TypedExpr) -> sge.Expression:
@@ -501,3 +586,8 @@ def _(op: ops.ToJSONString, expr: TypedExpr) -> sge.Expression:
501586
@UNARY_OP_REGISTRATION.register(ops.upper_op)
502587
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
503588
return sge.Upper(this=expr.expr)
589+
590+
591+
@UNARY_OP_REGISTRATION.register(ops.year_op)
592+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
593+
return sge.Extract(this=sge.Identifier(this="YEAR"), 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_AREA(`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+
`geography_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
ST_ASTEXT(`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+
`geography_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
SAFE.ST_X(`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+
`geography_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
SAFE.ST_Y(`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+
`timestamp_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
EXTRACT(ISOYEAR FROM `bfcol_0`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `timestamp_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+
`timestamp_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
EXTRACT(MINUTE FROM `bfcol_0`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `timestamp_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+
`timestamp_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
EXTRACT(MONTH FROM `bfcol_0`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `timestamp_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+
`timestamp_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
TIMESTAMP_TRUNC(`bfcol_0`, DAY) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `timestamp_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+
`timestamp_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
EXTRACT(QUARTER FROM `bfcol_0`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `timestamp_col`
13+
FROM `bfcte_1`

0 commit comments

Comments
 (0)