|
| 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__ importannotations |
| 15 | + |
| 16 | +importdataclasses |
| 17 | +fromtypingimportAny, Hashable |
| 18 | + |
| 19 | +importbigframes_vendored.pandas.core.colaspd_col |
| 20 | + |
| 21 | +importbigframes.core.expressionasbf_expression |
| 22 | +importbigframes.operationsasbf_ops |
| 23 | + |
| 24 | + |
| 25 | +# Not to be confused with the Expression class in `bigframes.core.expressions` |
| 26 | +# Name collision unintended |
| 27 | +@dataclasses.dataclass(frozen=True) |
| 28 | +classExpression: |
| 29 | +__doc__=pd_col.Expression.__doc__ |
| 30 | + |
| 31 | +_value: bf_expression.Expression |
| 32 | + |
| 33 | +def_apply_unary(self, op: bf_ops.UnaryOp) ->Expression: |
| 34 | +returnExpression(op.as_expr(self._value)) |
| 35 | + |
| 36 | +def_apply_binary(self, other: Any, op: bf_ops.BinaryOp, reverse: bool=False): |
| 37 | +ifisinstance(other, Expression): |
| 38 | +other_value=other._value |
| 39 | +else: |
| 40 | +other_value=bf_expression.const(other) |
| 41 | +ifreverse: |
| 42 | +returnExpression(op.as_expr(other_value, self._value)) |
| 43 | +else: |
| 44 | +returnExpression(op.as_expr(self._value, other_value)) |
| 45 | + |
| 46 | +def__add__(self, other: Any) ->Expression: |
| 47 | +returnself._apply_binary(other, bf_ops.add_op) |
| 48 | + |
| 49 | +def__radd__(self, other: Any) ->Expression: |
| 50 | +returnself._apply_binary(other, bf_ops.add_op, reverse=True) |
| 51 | + |
| 52 | +def__sub__(self, other: Any) ->Expression: |
| 53 | +returnself._apply_binary(other, bf_ops.sub_op) |
| 54 | + |
| 55 | +def__rsub__(self, other: Any) ->Expression: |
| 56 | +returnself._apply_binary(other, bf_ops.sub_op, reverse=True) |
| 57 | + |
| 58 | +def__mul__(self, other: Any) ->Expression: |
| 59 | +returnself._apply_binary(other, bf_ops.mul_op) |
| 60 | + |
| 61 | +def__rmul__(self, other: Any) ->Expression: |
| 62 | +returnself._apply_binary(other, bf_ops.mul_op, reverse=True) |
| 63 | + |
| 64 | +def__truediv__(self, other: Any) ->Expression: |
| 65 | +returnself._apply_binary(other, bf_ops.div_op) |
| 66 | + |
| 67 | +def__rtruediv__(self, other: Any) ->Expression: |
| 68 | +returnself._apply_binary(other, bf_ops.div_op, reverse=True) |
| 69 | + |
| 70 | +def__floordiv__(self, other: Any) ->Expression: |
| 71 | +returnself._apply_binary(other, bf_ops.floordiv_op) |
| 72 | + |
| 73 | +def__rfloordiv__(self, other: Any) ->Expression: |
| 74 | +returnself._apply_binary(other, bf_ops.floordiv_op, reverse=True) |
| 75 | + |
| 76 | +def__ge__(self, other: Any) ->Expression: |
| 77 | +returnself._apply_binary(other, bf_ops.ge_op) |
| 78 | + |
| 79 | +def__gt__(self, other: Any) ->Expression: |
| 80 | +returnself._apply_binary(other, bf_ops.gt_op) |
| 81 | + |
| 82 | +def__le__(self, other: Any) ->Expression: |
| 83 | +returnself._apply_binary(other, bf_ops.le_op) |
| 84 | + |
| 85 | +def__lt__(self, other: Any) ->Expression: |
| 86 | +returnself._apply_binary(other, bf_ops.lt_op) |
| 87 | + |
| 88 | +def__eq__(self, other: object) ->Expression: # type: ignore |
| 89 | +returnself._apply_binary(other, bf_ops.eq_op) |
| 90 | + |
| 91 | +def__ne__(self, other: object) ->Expression: # type: ignore |
| 92 | +returnself._apply_binary(other, bf_ops.ne_op) |
| 93 | + |
| 94 | +def__mod__(self, other: Any) ->Expression: |
| 95 | +returnself._apply_binary(other, bf_ops.mod_op) |
| 96 | + |
| 97 | +def__rmod__(self, other: Any) ->Expression: |
| 98 | +returnself._apply_binary(other, bf_ops.mod_op, reverse=True) |
| 99 | + |
| 100 | +def__and__(self, other: Any) ->Expression: |
| 101 | +returnself._apply_binary(other, bf_ops.and_op) |
| 102 | + |
| 103 | +def__rand__(self, other: Any) ->Expression: |
| 104 | +returnself._apply_binary(other, bf_ops.and_op, reverse=True) |
| 105 | + |
| 106 | +def__or__(self, other: Any) ->Expression: |
| 107 | +returnself._apply_binary(other, bf_ops.or_op) |
| 108 | + |
| 109 | +def__ror__(self, other: Any) ->Expression: |
| 110 | +returnself._apply_binary(other, bf_ops.or_op, reverse=True) |
| 111 | + |
| 112 | +def__xor__(self, other: Any) ->Expression: |
| 113 | +returnself._apply_binary(other, bf_ops.xor_op) |
| 114 | + |
| 115 | +def__rxor__(self, other: Any) ->Expression: |
| 116 | +returnself._apply_binary(other, bf_ops.xor_op, reverse=True) |
| 117 | + |
| 118 | +def__invert__(self) ->Expression: |
| 119 | +returnself._apply_unary(bf_ops.invert_op) |
| 120 | + |
| 121 | + |
| 122 | +defcol(col_name: Hashable) ->Expression: |
| 123 | +returnExpression(bf_expression.free_var(col_name)) |
| 124 | + |
| 125 | + |
| 126 | +col.__doc__=pd_col.col.__doc__ |
0 commit comments