|
| 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 | +from __future__ import annotations |
| 16 | + |
| 17 | +import dataclasses |
| 18 | +import typing |
| 19 | + |
| 20 | +from bigframes import dtypes |
| 21 | +import bigframes.operations.type as op_typing |
| 22 | + |
| 23 | +if typing.TYPE_CHECKING: |
| 24 | + # Avoids circular dependency |
| 25 | + import bigframes.core.expression |
| 26 | + |
| 27 | + |
| 28 | +class RowOp(typing.Protocol): |
| 29 | + @property |
| 30 | + def name(self) -> str: |
| 31 | + ... |
| 32 | + |
| 33 | + def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType: |
| 34 | + ... |
| 35 | + |
| 36 | + @property |
| 37 | + def is_monotonic(self) -> bool: |
| 38 | + """Whether the row operation preserves total ordering. Can be pruned from ordering expressions.""" |
| 39 | + ... |
| 40 | + |
| 41 | + @property |
| 42 | + def is_bijective(self) -> bool: |
| 43 | + """Whether the operation has a 1:1 mapping between inputs and outputs""" |
| 44 | + ... |
| 45 | + |
| 46 | + @property |
| 47 | + def deterministic(self) -> bool: |
| 48 | + """Whether the operation is deterministic" (given deterministic inputs)""" |
| 49 | + ... |
| 50 | + |
| 51 | + |
| 52 | +@dataclasses.dataclass(frozen=True) |
| 53 | +class ScalarOp: |
| 54 | + @property |
| 55 | + def name(self) -> str: |
| 56 | + raise NotImplementedError("RowOp abstract base class has no implementation") |
| 57 | + |
| 58 | + def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType: |
| 59 | + raise NotImplementedError("Abstract operation has no output type") |
| 60 | + |
| 61 | + @property |
| 62 | + def is_monotonic(self) -> bool: |
| 63 | + """Whether the row operation preserves total ordering. Can be pruned from ordering expressions.""" |
| 64 | + return False |
| 65 | + |
| 66 | + @property |
| 67 | + def is_bijective(self) -> bool: |
| 68 | + """Whether the operation has a 1:1 mapping between inputs and outputs""" |
| 69 | + return False |
| 70 | + |
| 71 | + @property |
| 72 | + def deterministic(self) -> bool: |
| 73 | + """Whether the operation is deterministic" (given deterministic inputs)""" |
| 74 | + return True |
| 75 | + |
| 76 | + |
| 77 | +@dataclasses.dataclass(frozen=True) |
| 78 | +class NaryOp(ScalarOp): |
| 79 | + def as_expr( |
| 80 | + self, |
| 81 | + *exprs: typing.Union[str, bigframes.core.expression.Expression], |
| 82 | + ) -> bigframes.core.expression.Expression: |
| 83 | + import bigframes.core.expression |
| 84 | + |
| 85 | + # Keep this in sync with output_type and compilers |
| 86 | + inputs: list[bigframes.core.expression.Expression] = [] |
| 87 | + |
| 88 | + for expr in exprs: |
| 89 | + inputs.append(_convert_expr_input(expr)) |
| 90 | + |
| 91 | + return bigframes.core.expression.OpExpression( |
| 92 | + self, |
| 93 | + tuple(inputs), |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +# These classes can be used to create simple ops that don't take local parameters |
| 98 | +# All is needed is a unique name, and to register an implementation in ibis_mappings.py |
| 99 | +@dataclasses.dataclass(frozen=True) |
| 100 | +class UnaryOp(ScalarOp): |
| 101 | + @property |
| 102 | + def arguments(self) -> int: |
| 103 | + return 1 |
| 104 | + |
| 105 | + def as_expr( |
| 106 | + self, input_id: typing.Union[str, bigframes.core.expression.Expression] = "arg" |
| 107 | + ) -> bigframes.core.expression.Expression: |
| 108 | + import bigframes.core.expression |
| 109 | + |
| 110 | + return bigframes.core.expression.OpExpression( |
| 111 | + self, (_convert_expr_input(input_id),) |
| 112 | + ) |
| 113 | + |
| 114 | + |
| 115 | +@dataclasses.dataclass(frozen=True) |
| 116 | +class BinaryOp(ScalarOp): |
| 117 | + @property |
| 118 | + def arguments(self) -> int: |
| 119 | + return 2 |
| 120 | + |
| 121 | + def as_expr( |
| 122 | + self, |
| 123 | + left_input: typing.Union[str, bigframes.core.expression.Expression] = "arg1", |
| 124 | + right_input: typing.Union[str, bigframes.core.expression.Expression] = "arg2", |
| 125 | + ) -> bigframes.core.expression.Expression: |
| 126 | + import bigframes.core.expression |
| 127 | + |
| 128 | + return bigframes.core.expression.OpExpression( |
| 129 | + self, |
| 130 | + ( |
| 131 | + _convert_expr_input(left_input), |
| 132 | + _convert_expr_input(right_input), |
| 133 | + ), |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +@dataclasses.dataclass(frozen=True) |
| 138 | +class TernaryOp(ScalarOp): |
| 139 | + @property |
| 140 | + def arguments(self) -> int: |
| 141 | + return 3 |
| 142 | + |
| 143 | + def as_expr( |
| 144 | + self, |
| 145 | + input1: typing.Union[str, bigframes.core.expression.Expression] = "arg1", |
| 146 | + input2: typing.Union[str, bigframes.core.expression.Expression] = "arg2", |
| 147 | + input3: typing.Union[str, bigframes.core.expression.Expression] = "arg3", |
| 148 | + ) -> bigframes.core.expression.Expression: |
| 149 | + import bigframes.core.expression |
| 150 | + |
| 151 | + return bigframes.core.expression.OpExpression( |
| 152 | + self, |
| 153 | + ( |
| 154 | + _convert_expr_input(input1), |
| 155 | + _convert_expr_input(input2), |
| 156 | + _convert_expr_input(input3), |
| 157 | + ), |
| 158 | + ) |
| 159 | + |
| 160 | + |
| 161 | +def _convert_expr_input( |
| 162 | + input: typing.Union[str, bigframes.core.expression.Expression] |
| 163 | +) -> bigframes.core.expression.Expression: |
| 164 | + """Allows creating column references with just a string""" |
| 165 | + import bigframes.core.expression |
| 166 | + |
| 167 | + if isinstance(input, str): |
| 168 | + return bigframes.core.expression.deref(input) |
| 169 | + else: |
| 170 | + return input |
| 171 | + |
| 172 | + |
| 173 | +# Operation Factories |
| 174 | +def create_unary_op(name: str, type_signature: op_typing.UnaryTypeSignature) -> UnaryOp: |
| 175 | + return dataclasses.make_dataclass( |
| 176 | + name, |
| 177 | + [ |
| 178 | + ("name", typing.ClassVar[str], name), |
| 179 | + ("output_type", typing.ClassVar[typing.Callable], type_signature.as_method), |
| 180 | + ], |
| 181 | + bases=(UnaryOp,), |
| 182 | + frozen=True, |
| 183 | + )() |
| 184 | + |
| 185 | + |
| 186 | +def create_binary_op( |
| 187 | + name: str, type_signature: op_typing.BinaryTypeSignature |
| 188 | +) -> BinaryOp: |
| 189 | + return dataclasses.make_dataclass( |
| 190 | + name, |
| 191 | + [ |
| 192 | + ("name", typing.ClassVar[str], name), |
| 193 | + ("output_type", typing.ClassVar[typing.Callable], type_signature.as_method), |
| 194 | + ], |
| 195 | + bases=(BinaryOp,), |
| 196 | + frozen=True, |
| 197 | + )() |
0 commit comments