This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathtest_windows.py
More file actions
141 lines (125 loc) · 5.03 KB
/
test_windows.py
File metadata and controls
141 lines (125 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import pandas as pd
import pytest
import sqlglot.expressions as sge
from bigframes.core import window_spec
from bigframes.core.compile.sqlglot.aggregations.windows import (
apply_window_if_present,
get_window_order_by,
)
import bigframes.core.expression as ex
import bigframes.core.ordering as ordering
class WindowsTest(unittest.TestCase):
def test_get_window_order_by_empty(self):
self.assertIsNone(get_window_order_by(tuple()))
def test_get_window_order_by(self):
result = get_window_order_by((ordering.OrderingExpression(ex.deref("col1")),))
self.assertEqual(
sge.Order(expressions=result).sql(dialect="bigquery"),
"ORDER BY `col1` ASC NULLS LAST",
)
def test_get_window_order_by_override_nulls(self):
result = get_window_order_by(
(ordering.OrderingExpression(ex.deref("col1")),),
override_null_order=True,
)
self.assertEqual(
sge.Order(expressions=result).sql(dialect="bigquery"),
"ORDER BY `col1` IS NULL ASC NULLS LAST, `col1` ASC NULLS LAST",
)
def test_get_window_order_by_override_nulls_desc(self):
result = get_window_order_by(
(
ordering.OrderingExpression(
ex.deref("col1"),
direction=ordering.OrderingDirection.DESC,
na_last=False,
),
),
override_null_order=True,
)
self.assertEqual(
sge.Order(expressions=result).sql(dialect="bigquery"),
"ORDER BY `col1` IS NULL DESC NULLS FIRST, `col1` DESC NULLS FIRST",
)
def test_apply_window_if_present_no_window(self):
value = sge.func(
"SUM", sge.Column(this=sge.to_identifier("col_0", quoted=True))
)
result = apply_window_if_present(value)
self.assertEqual(result, value)
def test_apply_window_if_present_row_bounded_no_ordering_raises(self):
with pytest.raises(
ValueError, match="No ordering provided for ordered analytic function"
):
apply_window_if_present(
sge.Var(this="value"),
window_spec.WindowSpec(
bounds=window_spec.RowsWindowBounds(start=-1, end=1)
),
)
def test_apply_window_if_present_unbounded_grouping_no_ordering(self):
result = apply_window_if_present(
sge.Var(this="value"),
window_spec.WindowSpec(
grouping_keys=(ex.deref("col1"),),
),
)
self.assertEqual(
result.sql(dialect="bigquery"),
"value OVER (PARTITION BY `col1`)",
)
def test_apply_window_if_present_range_bounded(self):
result = apply_window_if_present(
sge.Var(this="value"),
window_spec.WindowSpec(
ordering=(ordering.OrderingExpression(ex.deref("col1")),),
bounds=window_spec.RangeWindowBounds(start=None, end=pd.Timedelta(0)),
),
)
self.assertEqual(
result.sql(dialect="bigquery"),
"value OVER (ORDER BY `col1` ASC NULLS LAST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)",
)
def test_apply_window_if_present_range_bounded_timedelta(self):
result = apply_window_if_present(
sge.Var(this="value"),
window_spec.WindowSpec(
ordering=(ordering.OrderingExpression(ex.deref("col1")),),
bounds=window_spec.RangeWindowBounds(
start=pd.Timedelta(days=-1), end=pd.Timedelta(hours=12)
),
),
)
self.assertEqual(
result.sql(dialect="bigquery"),
"value OVER (ORDER BY `col1` ASC NULLS LAST RANGE BETWEEN 86400000000 PRECEDING AND 43200000000 FOLLOWING)",
)
def test_apply_window_if_present_all_params(self):
result = apply_window_if_present(
sge.Var(this="value"),
window_spec.WindowSpec(
grouping_keys=(ex.deref("col1"),),
ordering=(ordering.OrderingExpression(ex.deref("col2")),),
bounds=window_spec.RowsWindowBounds(start=-1, end=0),
),
)
self.assertEqual(
result.sql(dialect="bigquery"),
"value OVER (PARTITION BY `col1` ORDER BY `col2` ASC NULLS LAST ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)",
)
if __name__ == "__main__":
unittest.main()