Skip to content

Commit 05a5963

Browse files
committed
chore: Migrate timestamp_add_op operator to SQLGlot
Migrated the timestamp_add_op operator to SQLGlot.
1 parent ef5e83a commit 05a5963

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import bigframes.core.compile.sqlglot.scalar_compiler as scalar_compiler
2222

2323
register_unary_op = scalar_compiler.scalar_op_compiler.register_unary_op
24+
register_binary_op = scalar_compiler.scalar_op_compiler.register_binary_op
2425

2526

2627
@register_unary_op(ops.FloorDtOp, pass_op=True)
@@ -79,6 +80,20 @@ def _(expr: TypedExpr) -> sge.Expression:
7980
return sge.Extract(this=sge.Identifier(this="SECOND"), expression=expr.expr)
8081

8182

83+
@register_binary_op(ops.timestamp_diff_op)
84+
def _(left: TypedExpr, right: TypedExpr) -> sge.Expression:
85+
return sge.TimestampDiff(
86+
this=left.expr, expression=right.expr, unit=sge.Var(this="MICROSECOND")
87+
)
88+
89+
90+
@register_binary_op(ops.timestamp_add_op)
91+
def _(left: TypedExpr, right: TypedExpr) -> sge.Expression:
92+
return sge.TimestampAdd(
93+
this=left.expr, expression=right.expr, unit=sge.Var(this="MICROSECOND")
94+
)
95+
96+
8297
@register_unary_op(ops.StrftimeOp, pass_op=True)
8398
def _(expr: TypedExpr, op: ops.StrftimeOp) -> sge.Expression:
8499
return sge.func("FORMAT_TIMESTAMP", sge.convert(op.date_format), expr.expr)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`rowindex` AS `bfcol_0`,
4+
`timestamp_col` AS `bfcol_1`
5+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
6+
), `bfcte_1` AS (
7+
SELECT
8+
*,
9+
`bfcol_0` AS `bfcol_4`,
10+
TIMESTAMP_ADD(`bfcol_1`, INTERVAL 1 MICROSECOND) AS `bfcol_5`
11+
FROM `bfcte_0`
12+
)
13+
SELECT
14+
`bfcol_4` AS `rowindex`,
15+
`bfcol_5` AS `timestamp_col`
16+
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_DIFF(`bfcol_0`, `bfcol_0`, MICROSECOND) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `timestamp_col`
13+
FROM `bfcte_1`

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,20 @@ def test_second(scalar_types_df: bpd.DataFrame, snapshot):
142142
snapshot.assert_match(sql, "out.sql")
143143

144144

145-
def test_strftime(scalar_types_df: bpd.DataFrame, snapshot):
146-
col_name = "timestamp_col"
147-
bf_df = scalar_types_df[[col_name]]
148-
sql = utils._apply_ops_to_sql(
149-
bf_df, [ops.StrftimeOp("%Y-%m-%d").as_expr(col_name)], [col_name]
145+
def test_timestamp_diff(scalar_types_df: bpd.DataFrame, snapshot):
146+
bf_df = scalar_types_df[["timestamp_col", "date_col"]]
147+
sql = utils._apply_binary_op(
148+
bf_df, ops.timestamp_diff_op, "timestamp_col", "timestamp_col"
150149
)
151-
152150
snapshot.assert_match(sql, "out.sql")
153151

154152

153+
def test_timestamp_add(scalar_types_df: bpd.DataFrame, snapshot):
154+
bf_df = scalar_types_df[["timestamp_col"]]
155+
bf_df["timestamp_col"] = bf_df["timestamp_col"] + pd.Timedelta(1, unit="us")
156+
snapshot.assert_match(bf_df.sql, "out.sql")
157+
158+
155159
def test_time(scalar_types_df: bpd.DataFrame, snapshot):
156160
col_name = "timestamp_col"
157161
bf_df = scalar_types_df[[col_name]]

0 commit comments

Comments
 (0)