|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import dataclasses |
| 16 | +import functools |
| 17 | +import typing |
| 18 | + |
| 19 | +from bigframes import dtypes |
| 20 | +from bigframes import operations as ops |
| 21 | +from bigframes.core import expression as ex |
| 22 | +from bigframes.core import nodes, schema |
| 23 | + |
| 24 | + |
| 25 | +@dataclasses.dataclass |
| 26 | +class _TypedExpr: |
| 27 | + expr: ex.Expression |
| 28 | + dtype: dtypes.Dtype |
| 29 | + |
| 30 | + |
| 31 | +def rewrite_timedelta_ops(root: nodes.BigFrameNode) -> nodes.BigFrameNode: |
| 32 | + """ |
| 33 | + Rewrites expressions to properly handle timedelta values, because this type does not exist |
| 34 | + in the SQL world. |
| 35 | + """ |
| 36 | + if isinstance(root, nodes.ProjectionNode): |
| 37 | + updated_assignments = tuple( |
| 38 | + (_rewrite_expressions(expr, root.schema).expr, column_id) |
| 39 | + for expr, column_id in root.assignments |
| 40 | + ) |
| 41 | + root = nodes.ProjectionNode(root.child, updated_assignments) |
| 42 | + |
| 43 | + # TODO(b/394354614): FilterByNode and OrderNode also contain expressions. Need to update them too. |
| 44 | + return root |
| 45 | + |
| 46 | + |
| 47 | +@functools.cache |
| 48 | +def _rewrite_expressions(expr: ex.Expression, schema: schema.ArraySchema) -> _TypedExpr: |
| 49 | + if isinstance(expr, ex.DerefOp): |
| 50 | + return _TypedExpr(expr, schema.get_type(expr.id.sql)) |
| 51 | + |
| 52 | + if isinstance(expr, ex.ScalarConstantExpression): |
| 53 | + return _TypedExpr(expr, expr.dtype) |
| 54 | + |
| 55 | + if isinstance(expr, ex.OpExpression): |
| 56 | + updated_inputs = tuple( |
| 57 | + map(lambda x: _rewrite_expressions(x, schema), expr.inputs) |
| 58 | + ) |
| 59 | + return _rewrite_op_expr(expr, updated_inputs) |
| 60 | + |
| 61 | + raise AssertionError(f"Unexpected expression type: {type(expr)}") |
| 62 | + |
| 63 | + |
| 64 | +def _rewrite_op_expr( |
| 65 | + expr: ex.OpExpression, inputs: typing.Tuple[_TypedExpr, ...] |
| 66 | +) -> _TypedExpr: |
| 67 | + if isinstance(expr.op, ops.SubOp): |
| 68 | + return _rewrite_sub_op(inputs[0], inputs[1]) |
| 69 | + |
| 70 | + input_types = tuple(map(lambda x: x.dtype, inputs)) |
| 71 | + return _TypedExpr(expr, expr.op.output_type(*input_types)) |
| 72 | + |
| 73 | + |
| 74 | +def _rewrite_sub_op(left: _TypedExpr, right: _TypedExpr) -> _TypedExpr: |
| 75 | + result_op: ops.BinaryOp = ops.sub_op |
| 76 | + if dtypes.is_datetime_like(left.dtype) and dtypes.is_datetime_like(right.dtype): |
| 77 | + result_op = ops.timestamp_diff_op |
| 78 | + |
| 79 | + return _TypedExpr( |
| 80 | + result_op.as_expr(left.expr, right.expr), |
| 81 | + result_op.output_type(left.dtype, right.dtype), |
| 82 | + ) |
0 commit comments