|
| 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 | + |
| 18 | +from bigframes.core import bigframe_node, expression |
| 19 | +from bigframes.core.rewrite import op_lowering |
| 20 | +import bigframes.functions.udf_def as udf_def |
| 21 | +import bigframes.operations as ops |
| 22 | + |
| 23 | + |
| 24 | +@dataclasses.dataclass |
| 25 | +class LowerRemoteFunctionRule(op_lowering.OpLoweringRule): |
| 26 | + @property |
| 27 | + def op(self) -> type[ops.ScalarOp]: |
| 28 | + return ops.RemoteFunctionOp |
| 29 | + |
| 30 | + def lower(self, expr: expression.OpExpression) -> expression.Expression: |
| 31 | + assert isinstance(expr.op, ops.RemoteFunctionOp) |
| 32 | + func_def = expr.op.function_def |
| 33 | + devirtualized_expr = ops.RemoteFunctionOp( |
| 34 | + func_def.with_devirtualize(), |
| 35 | + apply_on_null=expr.op.apply_on_null, |
| 36 | + ).as_expr(*expr.children) |
| 37 | + if isinstance(func_def.signature.output, udf_def.VirtualListTypeV1): |
| 38 | + return func_def.signature.output.out_expr(devirtualized_expr) |
| 39 | + else: |
| 40 | + return devirtualized_expr |
| 41 | + |
| 42 | + |
| 43 | +@dataclasses.dataclass |
| 44 | +class LowerBinaryRemoteFunctionRule(op_lowering.OpLoweringRule): |
| 45 | + @property |
| 46 | + def op(self) -> type[ops.ScalarOp]: |
| 47 | + return ops.BinaryRemoteFunctionOp |
| 48 | + |
| 49 | + def lower(self, expr: expression.OpExpression) -> expression.Expression: |
| 50 | + assert isinstance(expr.op, ops.BinaryRemoteFunctionOp) |
| 51 | + func_def = expr.op.function_def |
| 52 | + devirtualized_expr = ops.BinaryRemoteFunctionOp( |
| 53 | + func_def.with_devirtualize(), |
| 54 | + ).as_expr(*expr.children) |
| 55 | + if isinstance(func_def.signature.output, udf_def.VirtualListTypeV1): |
| 56 | + return func_def.signature.output.out_expr(devirtualized_expr) |
| 57 | + else: |
| 58 | + return devirtualized_expr |
| 59 | + |
| 60 | + |
| 61 | +@dataclasses.dataclass |
| 62 | +class LowerNaryRemoteFunctionRule(op_lowering.OpLoweringRule): |
| 63 | + @property |
| 64 | + def op(self) -> type[ops.ScalarOp]: |
| 65 | + return ops.NaryRemoteFunctionOp |
| 66 | + |
| 67 | + def lower(self, expr: expression.OpExpression) -> expression.Expression: |
| 68 | + assert isinstance(expr.op, ops.NaryRemoteFunctionOp) |
| 69 | + func_def = expr.op.function_def |
| 70 | + devirtualized_expr = ops.NaryRemoteFunctionOp( |
| 71 | + func_def.with_devirtualize(), |
| 72 | + ).as_expr(*expr.children) |
| 73 | + if isinstance(func_def.signature.output, udf_def.VirtualListTypeV1): |
| 74 | + return func_def.signature.output.out_expr(devirtualized_expr) |
| 75 | + else: |
| 76 | + return devirtualized_expr |
| 77 | + |
| 78 | + |
| 79 | +UDF_LOWERING_RULES = ( |
| 80 | + LowerRemoteFunctionRule(), |
| 81 | + LowerBinaryRemoteFunctionRule(), |
| 82 | + LowerNaryRemoteFunctionRule(), |
| 83 | +) |
| 84 | + |
| 85 | + |
| 86 | +def lower_udfs(root: bigframe_node.BigFrameNode) -> bigframe_node.BigFrameNode: |
| 87 | + return op_lowering.lower_ops(root, rules=UDF_LOWERING_RULES) |
0 commit comments