|
| 1 | +# Copyright 2026 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 | +from __future__ import annotations |
| 15 | + |
| 16 | +import dataclasses |
| 17 | +from typing import Any, Hashable |
| 18 | + |
| 19 | +import bigframes.core.expression as bf_expression |
| 20 | +import bigframes.operations as bf_ops |
| 21 | + |
| 22 | + |
| 23 | +# Not to be confused with internal Expressions class |
| 24 | +# Name collision unintended |
| 25 | +@dataclasses.dataclass(frozen=True) |
| 26 | +class Expression: |
| 27 | + _value: bf_expression.Expression |
| 28 | + |
| 29 | + def _apply_unary(self, op: bf_ops.UnaryOp) -> Expression: |
| 30 | + return Expression(op.as_expr(self._value)) |
| 31 | + |
| 32 | + def _apply_binary(self, other: Any, op: bf_ops.BinaryOp, reverse: bool = False): |
| 33 | + if isinstance(other, Expression): |
| 34 | + other_value = other._value |
| 35 | + else: |
| 36 | + other_value = bf_expression.const(other) |
| 37 | + if reverse: |
| 38 | + return Expression(op.as_expr(other_value, self._value)) |
| 39 | + else: |
| 40 | + return Expression(op.as_expr(self._value, other_value)) |
| 41 | + |
| 42 | + def __add__(self, other: Any) -> Expression: |
| 43 | + return self._apply_binary(other, bf_ops.add_op) |
| 44 | + |
| 45 | + def __radd__(self, other: Any) -> Expression: |
| 46 | + return self._apply_binary(other, bf_ops.add_op, reverse=True) |
| 47 | + |
| 48 | + def __sub__(self, other: Any) -> Expression: |
| 49 | + return self._apply_binary(other, bf_ops.sub_op) |
| 50 | + |
| 51 | + def __rsub__(self, other: Any) -> Expression: |
| 52 | + return self._apply_binary(other, bf_ops.sub_op, reverse=True) |
| 53 | + |
| 54 | + def __mul__(self, other: Any) -> Expression: |
| 55 | + return self._apply_binary(other, bf_ops.mul_op) |
| 56 | + |
| 57 | + def __rmul__(self, other: Any) -> Expression: |
| 58 | + return self._apply_binary(other, bf_ops.mul_op, reverse=True) |
| 59 | + |
| 60 | + def __truediv__(self, other: Any) -> Expression: |
| 61 | + return self._apply_binary(other, bf_ops.div_op) |
| 62 | + |
| 63 | + def __rtruediv__(self, other: Any) -> Expression: |
| 64 | + return self._apply_binary(other, bf_ops.div_op, reverse=True) |
| 65 | + |
| 66 | + def __floordiv__(self, other: Any) -> Expression: |
| 67 | + return self._apply_binary(other, bf_ops.floordiv_op) |
| 68 | + |
| 69 | + def __rfloordiv__(self, other: Any) -> Expression: |
| 70 | + return self._apply_binary(other, bf_ops.floordiv_op, reverse=True) |
| 71 | + |
| 72 | + def __ge__(self, other: Any) -> Expression: |
| 73 | + return self._apply_binary(other, bf_ops.ge_op) |
| 74 | + |
| 75 | + def __gt__(self, other: Any) -> Expression: |
| 76 | + return self._apply_binary(other, bf_ops.gt_op) |
| 77 | + |
| 78 | + def __le__(self, other: Any) -> Expression: |
| 79 | + return self._apply_binary(other, bf_ops.le_op) |
| 80 | + |
| 81 | + def __lt__(self, other: Any) -> Expression: |
| 82 | + return self._apply_binary(other, bf_ops.lt_op) |
| 83 | + |
| 84 | + def __eq__(self, other: object) -> Expression: # type: ignore |
| 85 | + return self._apply_binary(other, bf_ops.eq_op) |
| 86 | + |
| 87 | + def __ne__(self, other: object) -> Expression: # type: ignore |
| 88 | + return self._apply_binary(other, bf_ops.ne_op) |
| 89 | + |
| 90 | + def __mod__(self, other: Any) -> Expression: |
| 91 | + return self._apply_binary(other, bf_ops.mod_op) |
| 92 | + |
| 93 | + def __rmod__(self, other: Any) -> Expression: |
| 94 | + return self._apply_binary(other, bf_ops.mod_op, reverse=True) |
| 95 | + |
| 96 | + def __and__(self, other: Any) -> Expression: |
| 97 | + return self._apply_binary(other, bf_ops.and_op) |
| 98 | + |
| 99 | + def __rand__(self, other: Any) -> Expression: |
| 100 | + return self._apply_binary(other, bf_ops.and_op, reverse=True) |
| 101 | + |
| 102 | + def __or__(self, other: Any) -> Expression: |
| 103 | + return self._apply_binary(other, bf_ops.or_op) |
| 104 | + |
| 105 | + def __ror__(self, other: Any) -> Expression: |
| 106 | + return self._apply_binary(other, bf_ops.or_op, reverse=True) |
| 107 | + |
| 108 | + def __xor__(self, other: Any) -> Expression: |
| 109 | + return self._apply_binary(other, bf_ops.xor_op) |
| 110 | + |
| 111 | + def __rxor__(self, other: Any) -> Expression: |
| 112 | + return self._apply_binary(other, bf_ops.xor_op, reverse=True) |
| 113 | + |
| 114 | + def __invert__(self) -> Expression: |
| 115 | + return self._apply_unary(bf_ops.invert_op) |
| 116 | + |
| 117 | + |
| 118 | +def col(col_name: Hashable) -> Expression: |
| 119 | + return Expression(bf_expression.free_var(col_name)) |
0 commit comments