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 68
Expand file tree
/
Copy pathtimedeltas.py
More file actions
266 lines (201 loc) · 9.56 KB
/
timedeltas.py
File metadata and controls
266 lines (201 loc) · 9.56 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# 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.
from __future__ import annotations
import dataclasses
import functools
import typing
from bigframes import dtypes
from bigframes import operations as ops
from bigframes.core import agg_expressions as ex_types
from bigframes.core import expression as ex
from bigframes.core import nodes, schema, utils
from bigframes.operations import aggregations as aggs
@dataclasses.dataclass
class _TypedExpr:
expr: ex.Expression
dtype: dtypes.Dtype
@classmethod
def create_op_expr(
cls, op: typing.Union[ops.ScalarOp, ops.RowOp], *inputs: _TypedExpr
) -> _TypedExpr:
expr = op.as_expr(*tuple(x.expr for x in inputs)) # type: ignore
dtype = op.output_type(*tuple(x.dtype for x in inputs))
return cls(expr, dtype)
def rewrite_timedelta_expressions(root: nodes.BigFrameNode) -> nodes.BigFrameNode:
"""
Rewrites expressions to properly handle timedelta values, because this type does not exist
in the SQL world.
"""
if isinstance(root, nodes.ProjectionNode):
updated_assignments = tuple(
(_rewrite_expressions(expr, root.schema).expr, column_id)
for expr, column_id in root.assignments
)
return nodes.ProjectionNode(root.child, updated_assignments)
if isinstance(root, nodes.FilterNode):
return nodes.FilterNode(
root.child, _rewrite_expressions(root.predicate, root.schema).expr
)
if isinstance(root, nodes.OrderByNode):
by = tuple(_rewrite_ordering_expr(x, root.schema) for x in root.by)
return nodes.OrderByNode(root.child, by)
if isinstance(root, nodes.WindowOpNode):
return nodes.WindowOpNode(
root.child,
tuple(
nodes.ColumnDef(
_rewrite_aggregation(cdef.expression, root.schema), cdef.id
)
for cdef in root.agg_exprs
),
root.window_spec,
)
if isinstance(root, nodes.AggregateNode):
updated_aggregations = tuple(
(_rewrite_aggregation(agg, root.child.schema), col_id)
for agg, col_id in root.aggregations
)
return nodes.AggregateNode(
root.child,
updated_aggregations,
root.by_column_ids,
root.order_by,
root.dropna,
)
return root
def _rewrite_ordering_expr(
expr: nodes.OrderingExpression, schema: schema.ArraySchema
) -> nodes.OrderingExpression:
by = _rewrite_expressions(expr.scalar_expression, schema).expr
return nodes.OrderingExpression(by, expr.direction, expr.na_last)
@functools.cache
def _rewrite_expressions(expr: ex.Expression, schema: schema.ArraySchema) -> _TypedExpr:
if isinstance(expr, ex.DerefOp):
return _TypedExpr(expr, schema.get_type(expr.id.sql))
if isinstance(expr, ex.ScalarConstantExpression):
return _rewrite_scalar_constant_expr(expr)
if isinstance(expr, ex.OpExpression):
updated_inputs = tuple(
map(lambda x: _rewrite_expressions(x, schema), expr.inputs)
)
return _rewrite_op_expr(expr, updated_inputs)
raise AssertionError(f"Unexpected expression type: {type(expr)}")
def _rewrite_scalar_constant_expr(expr: ex.ScalarConstantExpression) -> _TypedExpr:
if expr.value is None:
return _TypedExpr(ex.const(None, expr.dtype), expr.dtype)
if expr.dtype == dtypes.TIMEDELTA_DTYPE:
int_repr = utils.timedelta_to_micros(expr.value) # type: ignore
return _TypedExpr(ex.const(int_repr, expr.dtype), expr.dtype)
return _TypedExpr(expr, expr.dtype)
def _rewrite_op_expr(
expr: ex.OpExpression, inputs: typing.Tuple[_TypedExpr, ...]
) -> _TypedExpr:
if isinstance(expr.op, ops.SubOp):
return _rewrite_sub_op(inputs[0], inputs[1])
if isinstance(expr.op, ops.AddOp):
return _rewrite_add_op(inputs[0], inputs[1])
if isinstance(expr.op, ops.MulOp):
return _rewrite_mul_op(inputs[0], inputs[1])
if isinstance(expr.op, ops.DivOp):
return _rewrite_div_op(inputs[0], inputs[1])
if isinstance(expr.op, ops.FloorDivOp):
# We need to re-write floor div because for numerics: int // float => float
# but for timedeltas: int(timedelta) // float => int(timedelta)
return _rewrite_floordiv_op(inputs[0], inputs[1])
if isinstance(expr.op, ops.ToTimedeltaOp):
return _rewrite_to_timedelta_op(expr.op, inputs[0])
return _TypedExpr.create_op_expr(expr.op, *inputs)
def _rewrite_sub_op(left: _TypedExpr, right: _TypedExpr) -> _TypedExpr:
if dtypes.is_datetime_like(left.dtype) and dtypes.is_datetime_like(right.dtype):
return _TypedExpr.create_op_expr(ops.timestamp_diff_op, left, right)
if dtypes.is_datetime_like(left.dtype) and right.dtype == dtypes.TIMEDELTA_DTYPE:
return _TypedExpr.create_op_expr(ops.timestamp_sub_op, left, right)
if left.dtype == dtypes.DATE_DTYPE and right.dtype == dtypes.DATE_DTYPE:
return _TypedExpr.create_op_expr(ops.date_diff_op, left, right)
if left.dtype == dtypes.DATE_DTYPE and right.dtype == dtypes.TIMEDELTA_DTYPE:
return _TypedExpr.create_op_expr(ops.date_sub_op, left, right)
return _TypedExpr.create_op_expr(ops.sub_op, left, right)
def _rewrite_add_op(left: _TypedExpr, right: _TypedExpr) -> _TypedExpr:
if dtypes.is_datetime_like(left.dtype) and right.dtype == dtypes.TIMEDELTA_DTYPE:
return _TypedExpr.create_op_expr(ops.timestamp_add_op, left, right)
if left.dtype == dtypes.TIMEDELTA_DTYPE and dtypes.is_datetime_like(right.dtype):
# Re-arrange operands such that timestamp is always on the left and timedelta is
# always on the right.
return _TypedExpr.create_op_expr(ops.timestamp_add_op, right, left)
if left.dtype == dtypes.DATE_DTYPE and right.dtype == dtypes.TIMEDELTA_DTYPE:
return _TypedExpr.create_op_expr(ops.date_add_op, left, right)
if left.dtype == dtypes.TIMEDELTA_DTYPE and right.dtype == dtypes.DATE_DTYPE:
# Re-arrange operands such that date is always on the left and timedelta is
# always on the right.
return _TypedExpr.create_op_expr(ops.date_add_op, right, left)
return _TypedExpr.create_op_expr(ops.add_op, left, right)
def _rewrite_mul_op(left: _TypedExpr, right: _TypedExpr) -> _TypedExpr:
result = _TypedExpr.create_op_expr(ops.mul_op, left, right)
if left.dtype == dtypes.TIMEDELTA_DTYPE and dtypes.is_numeric(right.dtype):
return _TypedExpr.create_op_expr(ops.timedelta_floor_op, result)
if dtypes.is_numeric(left.dtype) and right.dtype == dtypes.TIMEDELTA_DTYPE:
return _TypedExpr.create_op_expr(ops.timedelta_floor_op, result)
return result
def _rewrite_div_op(left: _TypedExpr, right: _TypedExpr) -> _TypedExpr:
result = _TypedExpr.create_op_expr(ops.div_op, left, right)
if left.dtype == dtypes.TIMEDELTA_DTYPE and dtypes.is_numeric(right.dtype):
return _TypedExpr.create_op_expr(ops.timedelta_floor_op, result)
return result
def _rewrite_floordiv_op(left: _TypedExpr, right: _TypedExpr) -> _TypedExpr:
if left.dtype == dtypes.TIMEDELTA_DTYPE and dtypes.is_numeric(right.dtype):
return _TypedExpr.create_op_expr(
ops.timedelta_floor_op, _TypedExpr.create_op_expr(ops.div_op, left, right)
)
return _TypedExpr.create_op_expr(ops.floordiv_op, left, right)
def _rewrite_to_timedelta_op(op: ops.ToTimedeltaOp, arg: _TypedExpr):
if arg.dtype == dtypes.TIMEDELTA_DTYPE:
# Do nothing for values that are already timedeltas
return arg
return _TypedExpr.create_op_expr(op, arg)
@functools.cache
def _rewrite_aggregation(
aggregation: ex_types.Aggregation, schema: schema.ArraySchema
) -> ex_types.Aggregation:
if not isinstance(aggregation, ex_types.UnaryAggregation):
return aggregation
if isinstance(aggregation.arg, ex.DerefOp):
input_type = schema.get_type(aggregation.arg.id.sql)
else:
input_type = aggregation.arg.output_type
if isinstance(aggregation.op, aggs.DiffOp):
if dtypes.is_datetime_like(input_type):
return ex_types.UnaryAggregation(
aggs.TimeSeriesDiffOp(aggregation.op.periods), aggregation.arg
)
elif input_type == dtypes.DATE_DTYPE:
return ex_types.UnaryAggregation(
aggs.DateSeriesDiffOp(aggregation.op.periods), aggregation.arg
)
if isinstance(aggregation.op, aggs.StdOp) and input_type == dtypes.TIMEDELTA_DTYPE:
return ex_types.UnaryAggregation(
aggs.StdOp(should_floor_result=True), aggregation.arg
)
if isinstance(aggregation.op, aggs.MeanOp) and input_type == dtypes.TIMEDELTA_DTYPE:
return ex_types.UnaryAggregation(
aggs.MeanOp(should_floor_result=True), aggregation.arg
)
if (
isinstance(aggregation.op, aggs.QuantileOp)
and input_type == dtypes.TIMEDELTA_DTYPE
):
return ex_types.UnaryAggregation(
aggs.QuantileOp(q=aggregation.op.q, should_floor_result=True),
aggregation.arg,
)
return aggregation