Skip to content

Commit 30d1df7

Browse files
committed
chore: Migrate timestamp_diff_op operator to SQLGlot
Migrated the timestamp_diff_op operator to SQLGlot.
1 parent ef5e83a commit 30d1df7

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

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

Lines changed: 8 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,13 @@ 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+
8290
@register_unary_op(ops.StrftimeOp, pass_op=True)
8391
def _(expr: TypedExpr, op: ops.StrftimeOp) -> sge.Expression:
8492
return sge.func("FORMAT_TIMESTAMP", sge.convert(op.date_format), 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+
`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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,11 @@ def test_sub_timedelta(scalar_types_df: bpd.DataFrame, snapshot):
270270
bf_df["timedelta_sub_timedelta"] = bf_df["duration_col"] - bf_df["duration_col"]
271271

272272
snapshot.assert_match(bf_df.sql, "out.sql")
273+
274+
275+
def test_timestamp_diff(scalar_types_df: bpd.DataFrame, snapshot):
276+
bf_df = scalar_types_df[["timestamp_col", "date_col"]]
277+
sql = utils._apply_binary_op(
278+
bf_df, ops.timestamp_diff_op, "timestamp_col", "timestamp_col"
279+
)
280+
snapshot.assert_match(sql, "out.sql")

0 commit comments

Comments
 (0)