Skip to content

Commit 46994d7

Browse files
authored
chore: add duration_col to the sqlglot unit tests (#1998)
1 parent 44c1ec4 commit 46994d7

File tree

8 files changed

+124
-56
lines changed

8 files changed

+124
-56
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import sqlglot.expressions as sge
2323

2424
from bigframes import operations as ops
25+
from bigframes.core.compile.constants import UNIT_TO_US_CONVERSION_FACTORS
2526
from bigframes.core.compile.sqlglot.expressions.op_registration import OpRegistration
2627
from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr
2728

@@ -618,7 +619,11 @@ def _(op: ops.ToTimestampOp, expr: TypedExpr) -> sge.Expression:
618619

619620
@UNARY_OP_REGISTRATION.register(ops.ToTimedeltaOp)
620621
def _(op: ops.ToTimedeltaOp, expr: TypedExpr) -> sge.Expression:
621-
return sge.Interval(this=expr.expr, unit=sge.Identifier(this="SECOND"))
622+
value = expr.expr
623+
factor = UNIT_TO_US_CONVERSION_FACTORS[op.unit]
624+
if factor != 1:
625+
value = sge.Mul(this=value, expression=sge.convert(factor))
626+
return sge.Interval(this=value, unit=sge.Identifier(this="MICROSECOND"))
622627

623628

624629
@UNARY_OP_REGISTRATION.register(ops.UnixMicros)

tests/unit/core/compile/sqlglot/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def scalar_types_table_schema() -> typing.Sequence[bigquery.SchemaField]:
8989
bigquery.SchemaField("string_col", "STRING"),
9090
bigquery.SchemaField("time_col", "TIME"),
9191
bigquery.SchemaField("timestamp_col", "TIMESTAMP"),
92+
bigquery.SchemaField("duration_col", "INTEGER"),
9293
]
9394

9495

tests/unit/core/compile/sqlglot/expressions/snapshots/test_binary_compiler/test_mul_timedelta/out.sql

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,42 @@ WITH `bfcte_0` AS (
22
SELECT
33
`int64_col` AS `bfcol_0`,
44
`rowindex` AS `bfcol_1`,
5-
`timestamp_col` AS `bfcol_2`
5+
`timestamp_col` AS `bfcol_2`,
6+
`duration_col` AS `bfcol_3`
67
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
78
), `bfcte_1` AS (
89
SELECT
910
*,
10-
`bfcol_1` AS `bfcol_6`,
11-
`bfcol_2` AS `bfcol_7`,
12-
`bfcol_0` AS `bfcol_8`,
13-
CAST(FLOOR(86400000000 * `bfcol_0`) AS INT64) AS `bfcol_9`
11+
`bfcol_1` AS `bfcol_8`,
12+
`bfcol_2` AS `bfcol_9`,
13+
`bfcol_0` AS `bfcol_10`,
14+
INTERVAL `bfcol_3` MICROSECOND AS `bfcol_11`
1415
FROM `bfcte_0`
1516
), `bfcte_2` AS (
1617
SELECT
1718
*,
18-
`bfcol_6` AS `bfcol_14`,
19-
`bfcol_7` AS `bfcol_15`,
2019
`bfcol_8` AS `bfcol_16`,
2120
`bfcol_9` AS `bfcol_17`,
22-
CAST(FLOOR(`bfcol_8` * 86400000000) AS INT64) AS `bfcol_18`
21+
`bfcol_10` AS `bfcol_18`,
22+
`bfcol_11` AS `bfcol_19`,
23+
CAST(FLOOR(`bfcol_11` * `bfcol_10`) AS INT64) AS `bfcol_20`
2324
FROM `bfcte_1`
25+
), `bfcte_3` AS (
26+
SELECT
27+
*,
28+
`bfcol_16` AS `bfcol_26`,
29+
`bfcol_17` AS `bfcol_27`,
30+
`bfcol_18` AS `bfcol_28`,
31+
`bfcol_19` AS `bfcol_29`,
32+
`bfcol_20` AS `bfcol_30`,
33+
CAST(FLOOR(`bfcol_18` * `bfcol_19`) AS INT64) AS `bfcol_31`
34+
FROM `bfcte_2`
2435
)
2536
SELECT
26-
`bfcol_14` AS `rowindex`,
27-
`bfcol_15` AS `timestamp_col`,
28-
`bfcol_16` AS `int64_col`,
29-
`bfcol_17` AS `timedelta_mul_numeric`,
30-
`bfcol_18` AS `numeric_mul_timedelta`
31-
FROM `bfcte_2`
37+
`bfcol_26` AS `rowindex`,
38+
`bfcol_27` AS `timestamp_col`,
39+
`bfcol_28` AS `int64_col`,
40+
`bfcol_29` AS `duration_col`,
41+
`bfcol_30` AS `timedelta_mul_numeric`,
42+
`bfcol_31` AS `numeric_mul_timedelta`
43+
FROM `bfcte_3`

tests/unit/core/compile/sqlglot/expressions/snapshots/test_binary_compiler/test_sub_timedelta/out.sql

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,81 @@ WITH `bfcte_0` AS (
22
SELECT
33
`date_col` AS `bfcol_0`,
44
`rowindex` AS `bfcol_1`,
5-
`timestamp_col` AS `bfcol_2`
5+
`timestamp_col` AS `bfcol_2`,
6+
`duration_col` AS `bfcol_3`
67
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
78
), `bfcte_1` AS (
89
SELECT
910
*,
10-
`bfcol_1` AS `bfcol_6`,
11-
`bfcol_2` AS `bfcol_7`,
12-
`bfcol_0` AS `bfcol_8`,
13-
TIMESTAMP_SUB(CAST(`bfcol_0` AS DATETIME), INTERVAL 86400000000 MICROSECOND) AS `bfcol_9`
11+
`bfcol_1` AS `bfcol_8`,
12+
`bfcol_2` AS `bfcol_9`,
13+
`bfcol_0` AS `bfcol_10`,
14+
INTERVAL `bfcol_3` MICROSECOND AS `bfcol_11`
1415
FROM `bfcte_0`
1516
), `bfcte_2` AS (
1617
SELECT
1718
*,
18-
`bfcol_6` AS `bfcol_14`,
19-
`bfcol_7` AS `bfcol_15`,
2019
`bfcol_8` AS `bfcol_16`,
2120
`bfcol_9` AS `bfcol_17`,
22-
TIMESTAMP_SUB(`bfcol_7`, INTERVAL 86400000000 MICROSECOND) AS `bfcol_18`
21+
`bfcol_11` AS `bfcol_18`,
22+
`bfcol_10` AS `bfcol_19`,
23+
TIMESTAMP_SUB(CAST(`bfcol_10` AS DATETIME), INTERVAL `bfcol_11` MICROSECOND) AS `bfcol_20`
2324
FROM `bfcte_1`
2425
), `bfcte_3` AS (
2526
SELECT
2627
*,
27-
`bfcol_14` AS `bfcol_24`,
28-
`bfcol_15` AS `bfcol_25`,
2928
`bfcol_16` AS `bfcol_26`,
3029
`bfcol_17` AS `bfcol_27`,
3130
`bfcol_18` AS `bfcol_28`,
32-
TIMESTAMP_DIFF(CAST(`bfcol_16` AS DATETIME), CAST(`bfcol_16` AS DATETIME), MICROSECOND) AS `bfcol_29`
31+
`bfcol_19` AS `bfcol_29`,
32+
`bfcol_20` AS `bfcol_30`,
33+
TIMESTAMP_SUB(`bfcol_17`, INTERVAL `bfcol_18` MICROSECOND) AS `bfcol_31`
3334
FROM `bfcte_2`
3435
), `bfcte_4` AS (
3536
SELECT
3637
*,
37-
`bfcol_24` AS `bfcol_36`,
38-
`bfcol_25` AS `bfcol_37`,
3938
`bfcol_26` AS `bfcol_38`,
4039
`bfcol_27` AS `bfcol_39`,
4140
`bfcol_28` AS `bfcol_40`,
4241
`bfcol_29` AS `bfcol_41`,
43-
TIMESTAMP_DIFF(`bfcol_25`, `bfcol_25`, MICROSECOND) AS `bfcol_42`
42+
`bfcol_30` AS `bfcol_42`,
43+
`bfcol_31` AS `bfcol_43`,
44+
TIMESTAMP_DIFF(CAST(`bfcol_29` AS DATETIME), CAST(`bfcol_29` AS DATETIME), MICROSECOND) AS `bfcol_44`
4445
FROM `bfcte_3`
4546
), `bfcte_5` AS (
4647
SELECT
4748
*,
48-
0 AS `bfcol_50`
49+
`bfcol_38` AS `bfcol_52`,
50+
`bfcol_39` AS `bfcol_53`,
51+
`bfcol_40` AS `bfcol_54`,
52+
`bfcol_41` AS `bfcol_55`,
53+
`bfcol_42` AS `bfcol_56`,
54+
`bfcol_43` AS `bfcol_57`,
55+
`bfcol_44` AS `bfcol_58`,
56+
TIMESTAMP_DIFF(`bfcol_39`, `bfcol_39`, MICROSECOND) AS `bfcol_59`
4957
FROM `bfcte_4`
58+
), `bfcte_6` AS (
59+
SELECT
60+
*,
61+
`bfcol_52` AS `bfcol_68`,
62+
`bfcol_53` AS `bfcol_69`,
63+
`bfcol_54` AS `bfcol_70`,
64+
`bfcol_55` AS `bfcol_71`,
65+
`bfcol_56` AS `bfcol_72`,
66+
`bfcol_57` AS `bfcol_73`,
67+
`bfcol_58` AS `bfcol_74`,
68+
`bfcol_59` AS `bfcol_75`,
69+
`bfcol_54` - `bfcol_54` AS `bfcol_76`
70+
FROM `bfcte_5`
5071
)
5172
SELECT
52-
`bfcol_36` AS `rowindex`,
53-
`bfcol_37` AS `timestamp_col`,
54-
`bfcol_38` AS `date_col`,
55-
`bfcol_39` AS `date_sub_timedelta`,
56-
`bfcol_40` AS `timestamp_sub_timedelta`,
57-
`bfcol_41` AS `timestamp_sub_date`,
58-
`bfcol_42` AS `date_sub_timestamp`,
59-
`bfcol_50` AS `timedelta_sub_timedelta`
60-
FROM `bfcte_5`
73+
`bfcol_68` AS `rowindex`,
74+
`bfcol_69` AS `timestamp_col`,
75+
`bfcol_70` AS `duration_col`,
76+
`bfcol_71` AS `date_col`,
77+
`bfcol_72` AS `date_sub_timedelta`,
78+
`bfcol_73` AS `timestamp_sub_timedelta`,
79+
`bfcol_74` AS `timestamp_sub_date`,
80+
`bfcol_75` AS `date_sub_timestamp`,
81+
`bfcol_76` AS `timedelta_sub_timedelta`
82+
FROM `bfcte_6`
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
WITH `bfcte_0` AS (
22
SELECT
3-
`int64_col` AS `bfcol_0`
3+
`int64_col` AS `bfcol_0`,
4+
`rowindex` AS `bfcol_1`
45
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
56
), `bfcte_1` AS (
67
SELECT
78
*,
8-
INTERVAL `bfcol_0` SECOND AS `bfcol_1`
9+
`bfcol_1` AS `bfcol_4`,
10+
`bfcol_0` AS `bfcol_5`,
11+
INTERVAL `bfcol_0` MICROSECOND AS `bfcol_6`
912
FROM `bfcte_0`
13+
), `bfcte_2` AS (
14+
SELECT
15+
*,
16+
`bfcol_4` AS `bfcol_10`,
17+
`bfcol_5` AS `bfcol_11`,
18+
`bfcol_6` AS `bfcol_12`,
19+
INTERVAL (`bfcol_5` * 1000000) MICROSECOND AS `bfcol_13`
20+
FROM `bfcte_1`
21+
), `bfcte_3` AS (
22+
SELECT
23+
*,
24+
`bfcol_10` AS `bfcol_18`,
25+
`bfcol_11` AS `bfcol_19`,
26+
`bfcol_12` AS `bfcol_20`,
27+
`bfcol_13` AS `bfcol_21`,
28+
INTERVAL (`bfcol_11` * 604800000000) MICROSECOND AS `bfcol_22`
29+
FROM `bfcte_2`
1030
)
1131
SELECT
12-
`bfcol_1` AS `int64_col`
13-
FROM `bfcte_1`
32+
`bfcol_18` AS `rowindex`,
33+
`bfcol_19` AS `int64_col`,
34+
`bfcol_20` AS `duration_us`,
35+
`bfcol_21` AS `duration_s`,
36+
`bfcol_22` AS `duration_w`
37+
FROM `bfcte_3`

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ def test_sub_numeric(scalar_types_df: bpd.DataFrame, snapshot):
124124

125125

126126
def test_sub_timedelta(scalar_types_df: bpd.DataFrame, snapshot):
127-
bf_df = scalar_types_df[["timestamp_col", "date_col"]]
128-
timedelta = pd.Timedelta(1, unit="d")
127+
bf_df = scalar_types_df[["timestamp_col", "duration_col", "date_col"]]
128+
bf_df["duration_col"] = bpd.to_timedelta(bf_df["duration_col"], unit="us")
129129

130-
bf_df["date_sub_timedelta"] = bf_df["date_col"] - timedelta
131-
bf_df["timestamp_sub_timedelta"] = bf_df["timestamp_col"] - timedelta
130+
bf_df["date_sub_timedelta"] = bf_df["date_col"] - bf_df["duration_col"]
131+
bf_df["timestamp_sub_timedelta"] = bf_df["timestamp_col"] - bf_df["duration_col"]
132132
bf_df["timestamp_sub_date"] = bf_df["date_col"] - bf_df["date_col"]
133133
bf_df["date_sub_timestamp"] = bf_df["timestamp_col"] - bf_df["timestamp_col"]
134-
bf_df["timedelta_sub_timedelta"] = timedelta - timedelta
134+
bf_df["timedelta_sub_timedelta"] = bf_df["duration_col"] - bf_df["duration_col"]
135135

136136
snapshot.assert_match(bf_df.sql, "out.sql")
137137

@@ -157,11 +157,11 @@ def test_mul_numeric(scalar_types_df: bpd.DataFrame, snapshot):
157157

158158

159159
def test_mul_timedelta(scalar_types_df: bpd.DataFrame, snapshot):
160-
bf_df = scalar_types_df[["timestamp_col", "int64_col"]]
161-
timedelta = pd.Timedelta(1, unit="d")
160+
bf_df = scalar_types_df[["timestamp_col", "int64_col", "duration_col"]]
161+
bf_df["duration_col"] = bpd.to_timedelta(bf_df["duration_col"], unit="us")
162162

163-
bf_df["timedelta_mul_numeric"] = timedelta * bf_df["int64_col"]
164-
bf_df["numeric_mul_timedelta"] = bf_df["int64_col"] * timedelta
163+
bf_df["timedelta_mul_numeric"] = bf_df["duration_col"] * bf_df["int64_col"]
164+
bf_df["numeric_mul_timedelta"] = bf_df["int64_col"] * bf_df["duration_col"]
165165

166166
snapshot.assert_match(bf_df.sql, "out.sql")
167167

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,11 @@ def test_to_timestamp(scalar_types_df: bpd.DataFrame, snapshot):
599599

600600
def test_to_timedelta(scalar_types_df: bpd.DataFrame, snapshot):
601601
bf_df = scalar_types_df[["int64_col"]]
602-
sql = _apply_unary_op(bf_df, ops.ToTimedeltaOp("s"), "int64_col")
602+
bf_df["duration_us"] = bpd.to_timedelta(bf_df["int64_col"], "us")
603+
bf_df["duration_s"] = bpd.to_timedelta(bf_df["int64_col"], "s")
604+
bf_df["duration_w"] = bpd.to_timedelta(bf_df["int64_col"], "W")
603605

604-
snapshot.assert_match(sql, "out.sql")
606+
snapshot.assert_match(bf_df.sql, "out.sql")
605607

606608

607609
def test_unix_micros(scalar_types_df: bpd.DataFrame, snapshot):

tests/unit/core/compile/sqlglot/snapshots/test_compile_readtable/test_compile_readtable/out.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ WITH `bfcte_0` AS (
1313
`rowindex_2` AS `bfcol_10`,
1414
`string_col` AS `bfcol_11`,
1515
`time_col` AS `bfcol_12`,
16-
`timestamp_col` AS `bfcol_13`
16+
`timestamp_col` AS `bfcol_13`,
17+
`duration_col` AS `bfcol_14`
1718
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
1819
)
1920
SELECT
@@ -31,5 +32,6 @@ SELECT
3132
`bfcol_10` AS `rowindex_2`,
3233
`bfcol_11` AS `string_col`,
3334
`bfcol_12` AS `time_col`,
34-
`bfcol_13` AS `timestamp_col`
35+
`bfcol_13` AS `timestamp_col`,
36+
`bfcol_14` AS `duration_col`
3537
FROM `bfcte_0`

0 commit comments

Comments
 (0)