|
| 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 | + |
| 18 | +frombigframes.coreimportbigframe_node, expression |
| 19 | +frombigframes.core.rewriteimportop_lowering |
| 20 | +importbigframes.functions.udf_defasudf_def |
| 21 | +importbigframes.operationsasops |
| 22 | + |
| 23 | + |
| 24 | +@dataclasses.dataclass |
| 25 | +classLowerRemoteFunctionRule(op_lowering.OpLoweringRule): |
| 26 | +@property |
| 27 | +defop(self) ->type[ops.ScalarOp]: |
| 28 | +returnops.RemoteFunctionOp |
| 29 | + |
| 30 | +deflower(self, expr: expression.OpExpression) ->expression.Expression: |
| 31 | +assertisinstance(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 | +ifisinstance(func_def.signature.output, udf_def.VirtualListTypeV1): |
| 38 | +returnfunc_def.signature.output.out_expr(devirtualized_expr) |
| 39 | +else: |
| 40 | +returndevirtualized_expr |
| 41 | + |
| 42 | + |
| 43 | +@dataclasses.dataclass |
| 44 | +classLowerBinaryRemoteFunctionRule(op_lowering.OpLoweringRule): |
| 45 | +@property |
| 46 | +defop(self) ->type[ops.ScalarOp]: |
| 47 | +returnops.BinaryRemoteFunctionOp |
| 48 | + |
| 49 | +deflower(self, expr: expression.OpExpression) ->expression.Expression: |
| 50 | +assertisinstance(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 | +ifisinstance(func_def.signature.output, udf_def.VirtualListTypeV1): |
| 56 | +returnfunc_def.signature.output.out_expr(devirtualized_expr) |
| 57 | +else: |
| 58 | +returndevirtualized_expr |
| 59 | + |
| 60 | + |
| 61 | +@dataclasses.dataclass |
| 62 | +classLowerNaryRemoteFunctionRule(op_lowering.OpLoweringRule): |
| 63 | +@property |
| 64 | +defop(self) ->type[ops.ScalarOp]: |
| 65 | +returnops.NaryRemoteFunctionOp |
| 66 | + |
| 67 | +deflower(self, expr: expression.OpExpression) ->expression.Expression: |
| 68 | +assertisinstance(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 | +ifisinstance(func_def.signature.output, udf_def.VirtualListTypeV1): |
| 74 | +returnfunc_def.signature.output.out_expr(devirtualized_expr) |
| 75 | +else: |
| 76 | +returndevirtualized_expr |
| 77 | + |
| 78 | + |
| 79 | +UDF_LOWERING_RULES= ( |
| 80 | +LowerRemoteFunctionRule(), |
| 81 | +LowerBinaryRemoteFunctionRule(), |
| 82 | +LowerNaryRemoteFunctionRule(), |
| 83 | +) |
| 84 | + |
| 85 | + |
| 86 | +deflower_udfs(root: bigframe_node.BigFrameNode) ->bigframe_node.BigFrameNode: |
| 87 | +returnop_lowering.lower_ops(root, rules=UDF_LOWERING_RULES) |
0 commit comments