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 67
chore: support timestamp subtractions #1346
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0a58e98
chore: support timestamp subtractions
sycai 2a32701
Fix format
sycai c6d5635
Merge branch 'main' into sycai_timestamp_diff
sycai 6150324
Merge branch 'main' into sycai_timestamp_diff
sycai 1e87e0c
use tree rewrites to dispatch timestamp_diff operator
sycai c621cac
Merge branch 'main' into sycai_timestamp_diff
sycai 9e3038c
add TODO for more node updates
sycai 6ca29ee
polish the code and fix typos
sycai cf0a5b1
Merge branch 'main' into sycai_timestamp_diff
sycai 066cef3
fix comment
sycai 1e5d9d2
Merge branch 'main' into sycai_timestamp_diff
sycai ab1e1b0
add rewrites to compile_raw and compile_peek_sql
sycai 2a128e9
Merge branch 'main' into sycai_timestamp_diff
sycai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Contributor
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.
Contributor
Author
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), | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!