Skip to content

Commit f8d851f

Browse files
authored
chore: migrate 12 scalar operators to SQLGlot (#1929)
Migrated operators: - hour_op - invert_op - isalnum_op - isalpha_op - isdecimal_op - isdigit_op - islower_op - isnumeric_op - isspace_op - isupper_op - iso_day_op - iso_week_op
1 parent 7e03252 commit f8d851f

File tree

14 files changed

+319
-0
lines changed

14 files changed

+319
-0
lines changed

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,84 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
234234
return sge.func("FARM_FINGERPRINT", expr.expr)
235235

236236

237+
@UNARY_OP_REGISTRATION.register(ops.hour_op)
238+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
239+
return sge.Extract(this=sge.Identifier(this="HOUR"), expression=expr.expr)
240+
241+
242+
@UNARY_OP_REGISTRATION.register(ops.invert_op)
243+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
244+
return sge.BitwiseNot(this=expr.expr)
245+
246+
247+
@UNARY_OP_REGISTRATION.register(ops.isalnum_op)
248+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
249+
return sge.RegexpLike(this=expr.expr, expression=sge.convert(r"^(\p{N}|\p{L})+$"))
250+
251+
252+
@UNARY_OP_REGISTRATION.register(ops.isalpha_op)
253+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
254+
return sge.RegexpLike(this=expr.expr, expression=sge.convert(r"^\p{L}+$"))
255+
256+
257+
@UNARY_OP_REGISTRATION.register(ops.isdecimal_op)
258+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
259+
return sge.RegexpLike(this=expr.expr, expression=sge.convert(r"^\d+$"))
260+
261+
262+
@UNARY_OP_REGISTRATION.register(ops.isdigit_op)
263+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
264+
return sge.RegexpLike(this=expr.expr, expression=sge.convert(r"^\p{Nd}+$"))
265+
266+
267+
@UNARY_OP_REGISTRATION.register(ops.islower_op)
268+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
269+
return sge.And(
270+
this=sge.EQ(
271+
this=sge.Lower(this=expr.expr),
272+
expression=expr.expr,
273+
),
274+
expression=sge.NEQ(
275+
this=sge.Upper(this=expr.expr),
276+
expression=expr.expr,
277+
),
278+
)
279+
280+
281+
@UNARY_OP_REGISTRATION.register(ops.isnumeric_op)
282+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
283+
return sge.RegexpLike(this=expr.expr, expression=sge.convert(r"^\pN+$"))
284+
285+
286+
@UNARY_OP_REGISTRATION.register(ops.isspace_op)
287+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
288+
return sge.RegexpLike(this=expr.expr, expression=sge.convert(r"^\s+$"))
289+
290+
291+
@UNARY_OP_REGISTRATION.register(ops.isupper_op)
292+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
293+
return sge.And(
294+
this=sge.EQ(
295+
this=sge.Upper(this=expr.expr),
296+
expression=expr.expr,
297+
),
298+
expression=sge.NEQ(
299+
this=sge.Lower(this=expr.expr),
300+
expression=expr.expr,
301+
),
302+
)
303+
304+
305+
@UNARY_OP_REGISTRATION.register(ops.iso_day_op)
306+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
307+
return sge.Extract(this=sge.Identifier(this="DAYOFWEEK"), expression=expr.expr)
308+
309+
310+
@UNARY_OP_REGISTRATION.register(ops.iso_week_op)
311+
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
312+
return sge.Extract(this=sge.Identifier(this="ISOWEEK"), expression=expr.expr)
313+
314+
237315
@UNARY_OP_REGISTRATION.register(ops.isnull_op)
238316
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
239317
return sge.Is(this=expr.expr, expression=sge.Null())
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(HOUR 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+
`int64_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
~`bfcol_0` AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `int64_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+
REGEXP_CONTAINS(`bfcol_0`, '^(\\p{N}|\\p{L})+$') AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `string_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+
REGEXP_CONTAINS(`bfcol_0`, '^\\p{L}+$') AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `string_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+
REGEXP_CONTAINS(`bfcol_0`, '^\\d+$') AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `string_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+
REGEXP_CONTAINS(`bfcol_0`, '^\\p{Nd}+$') AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `string_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+
LOWER(`bfcol_0`) = `bfcol_0` AND UPPER(`bfcol_0`) <> `bfcol_0` AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `string_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+
REGEXP_CONTAINS(`bfcol_0`, '^\\pN+$') AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `string_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(DAYOFWEEK 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)