Skip to content

Commit 148bb20

Browse files
committed
add duration column
1 parent fd4c482 commit 148bb20

File tree

6 files changed

+91
-63
lines changed

6 files changed

+91
-63
lines changed

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`

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: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import typing
16-
1715
import pytest
1816

1917
from bigframes import operations as ops
@@ -23,21 +21,14 @@
2321
pytest.importorskip("pytest_snapshot")
2422

2523

26-
def _apply_unary_op(
27-
obj: bpd.DataFrame, ops: ops.UnaryOp | typing.Sequence[ops.UnaryOp], arg: str
28-
) -> str:
24+
def _apply_unary_op(obj: bpd.DataFrame, op: ops.UnaryOp, arg: str) -> str:
2925
array_value = obj._block.expr
30-
if not isinstance(ops, typing.Sequence):
31-
ops = [ops]
32-
op_expr = [op.as_expr(arg) for op in ops]
33-
result, col_ids = array_value.compute_values(op_expr)
26+
op_expr = op.as_expr(arg)
27+
result, col_ids = array_value.compute_values([op_expr])
3428

3529
# Rename columns for deterministic golden SQL results.
36-
rename_cols = {
37-
col_id: arg if index == 0 else f"{arg}_{index}"
38-
for index, col_id in enumerate(col_ids)
39-
}
40-
result = result.rename_columns(rename_cols).select_columns([arg])
30+
assert len(col_ids) == 1
31+
result = result.rename_columns({col_ids[0]: arg}).select_columns([arg])
4132

4233
sql = result.session._executor.to_sql(result, enable_cache=False)
4334
return sql

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)