|
| 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 math |
| 16 | +import operator |
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +import bigframes.operations |
| 20 | +from bigframes.operations import ( |
| 21 | + aggregations, |
| 22 | + array_ops, |
| 23 | + bool_ops, |
| 24 | + comparison_ops, |
| 25 | + numeric_ops, |
| 26 | + string_ops, |
| 27 | +) |
| 28 | + |
| 29 | +PYTHON_TO_BIGFRAMES = { |
| 30 | + ## operators |
| 31 | + operator.add: numeric_ops.add_op, |
| 32 | + operator.sub: numeric_ops.sub_op, |
| 33 | + operator.mul: numeric_ops.mul_op, |
| 34 | + operator.truediv: numeric_ops.div_op, |
| 35 | + operator.floordiv: numeric_ops.floordiv_op, |
| 36 | + operator.mod: numeric_ops.mod_op, |
| 37 | + operator.pow: numeric_ops.pow_op, |
| 38 | + operator.pos: numeric_ops.pos_op, |
| 39 | + operator.neg: numeric_ops.neg_op, |
| 40 | + operator.abs: numeric_ops.abs_op, |
| 41 | + operator.eq: comparison_ops.eq_op, |
| 42 | + operator.ne: comparison_ops.ne_op, |
| 43 | + operator.gt: comparison_ops.gt_op, |
| 44 | + operator.lt: comparison_ops.lt_op, |
| 45 | + operator.ge: comparison_ops.ge_op, |
| 46 | + operator.le: comparison_ops.le_op, |
| 47 | + operator.and_: bool_ops.and_op, |
| 48 | + operator.or_: bool_ops.or_op, |
| 49 | + operator.xor: bool_ops.xor_op, |
| 50 | + ## math |
| 51 | + math.log: numeric_ops.ln_op, |
| 52 | + math.log10: numeric_ops.log10_op, |
| 53 | + math.log1p: numeric_ops.log1p_op, |
| 54 | + math.expm1: numeric_ops.expm1_op, |
| 55 | + math.sin: numeric_ops.sin_op, |
| 56 | + math.cos: numeric_ops.cos_op, |
| 57 | + math.tan: numeric_ops.tan_op, |
| 58 | + math.sinh: numeric_ops.sinh_op, |
| 59 | + math.cosh: numeric_ops.cosh_op, |
| 60 | + math.tanh: numeric_ops.tanh_op, |
| 61 | + math.asin: numeric_ops.arcsin_op, |
| 62 | + math.acos: numeric_ops.arccos_op, |
| 63 | + math.atan: numeric_ops.arctan_op, |
| 64 | + math.floor: numeric_ops.floor_op, |
| 65 | + math.ceil: numeric_ops.ceil_op, |
| 66 | + ## str |
| 67 | + str.upper: string_ops.upper_op, |
| 68 | + str.lower: string_ops.lower_op, |
| 69 | + ## builtins |
| 70 | + len: string_ops.len_op, |
| 71 | + abs: numeric_ops.abs_op, |
| 72 | + pow: numeric_ops.pow_op, |
| 73 | + ### builtins -- iterable |
| 74 | + all: array_ops.ArrayReduceOp(aggregations.all_op), |
| 75 | + any: array_ops.ArrayReduceOp(aggregations.any_op), |
| 76 | + sum: array_ops.ArrayReduceOp(aggregations.sum_op), |
| 77 | + min: array_ops.ArrayReduceOp(aggregations.min_op), |
| 78 | + max: array_ops.ArrayReduceOp(aggregations.max_op), |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +def python_callable_to_op(obj) -> Optional[bigframes.operations.RowOp]: |
| 83 | + if obj in PYTHON_TO_BIGFRAMES: |
| 84 | + return PYTHON_TO_BIGFRAMES[obj] |
| 85 | + return None |
0 commit comments