-
Notifications
You must be signed in to change notification settings - Fork 52
chore: support timestamp subtractions #1346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0a58e98
2a32701
c6d5635
6150324
1e87e0c
c621cac
9e3038c
6ca29ee
cf0a5b1
066cef3
1e5d9d2
ab1e1b0
2a128e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# 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 dataclasses | ||
import functools | ||
import typing | ||
|
||
from bigframes import dtypes | ||
from bigframes import operations as ops | ||
from bigframes.core import expression as ex | ||
from bigframes.core import nodes, schema | ||
|
||
|
||
@dataclasses.dataclass | ||
class _TypedExpr: | ||
expr: ex.Expression | ||
dtype: dtypes.Dtype | ||
|
||
|
||
def rewrite_timedelta_ops(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 | ||
) | ||
root = nodes.ProjectionNode(root.child, updated_assignments) | ||
|
||
# TODO(b/394354614): FilterByNode and OrderNode also contain expressions. Need to update them too. | ||
return root | ||
|
||
|
||
@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 _TypedExpr(expr, expr.dtype) | ||
|
||
if isinstance(expr, ex.OpExpression): | ||
updated_inputs = tuple( | ||
map(lambda x: _rewrite_expressions(x, schema), expr.inputs) | ||
) | ||
return _rewrite_op_expr(expr, updated_inputs) | ||
Comment on lines
+55
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this will also need to be top-down rather than bottom-up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's possible to do this top-down, because we cannot get the input types by first processing the parent node. The parent node output type can only be decided once we have rewrite all the subtrees. |
||
|
||
raise AssertionError(f"Unexpected expression type: {type(expr)}") | ||
|
||
|
||
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]) | ||
|
||
input_types = tuple(map(lambda x: x.dtype, inputs)) | ||
return _TypedExpr(expr, expr.op.output_type(*input_types)) | ||
|
||
|
||
def _rewrite_sub_op(left: _TypedExpr, right: _TypedExpr) -> _TypedExpr: | ||
result_op: ops.BinaryOp = ops.sub_op | ||
if dtypes.is_datetime_like(left.dtype) and dtypes.is_datetime_like(right.dtype): | ||
result_op = ops.timestamp_diff_op | ||
|
||
return _TypedExpr( | ||
result_op.as_expr(left.expr, right.expr), | ||
result_op.output_type(left.dtype, right.dtype), | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as long as we get support those nodes before anybody starts using this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR soon to follow!