GeezerVison is showing github.com — fix: Respect remote function config changes even if logic unchanged (… · google/bigframes@b952428
File treeExpand file treeCollapse file tree Open diff view settings core/compile/sqlglot/expressions
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bigframes/core/compile/ibis_compiler/ibis_compiler.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -88,6 +88,7 @@ def _replace_unsupported_ops(node: nodes.BigFrameNode): | 88 | 88 | node = nodes.bottom_up(node, rewrites.rewrite_slice) | 89 | 89 | node = nodes.bottom_up(node, rewrites.rewrite_timedelta_expressions) | 90 | 90 | node = nodes.bottom_up(node, rewrites.rewrite_range_rolling) | | 91 | + node = nodes.bottom_up(node, rewrites.lower_udfs) | 91 | 92 | return node | 92 | 93 |
| 93 | 94 |
| |
Collapse file Copy file name to clipboardExpand all lines: bigframes/core/compile/ibis_compiler/scalar_op_registry.py +14-4Lines changed: 14 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1037,7 +1037,8 @@ def timedelta_floor_op_impl(x: ibis_types.NumericValue): | 1037 | 1037 | @scalar_op_compiler.register_unary_op(ops.RemoteFunctionOp, pass_op=True) | 1038 | 1038 | def remote_function_op_impl(x: ibis_types.Value, op: ops.RemoteFunctionOp): | 1039 | 1039 | udf_sig = op.function_def.signature | 1040 | | - ibis_py_sig = (udf_sig.py_input_types, udf_sig.py_output_type) | | 1040 | + assert not udf_sig.is_virtual # should have been devirtualized in lowering pass | | 1041 | + ibis_py_sig = (tuple(arg.py_type for arg in udf_sig.inputs), udf_sig.output.py_type) | 1041 | 1042 |
| 1042 | 1043 | @ibis_udf.scalar.builtin( | 1043 | 1044 | name=str(op.function_def.routine_ref), signature=ibis_py_sig | @@ -1056,7 +1057,8 @@ def binary_remote_function_op_impl( | 1056 | 1057 | x: ibis_types.Value, y: ibis_types.Value, op: ops.BinaryRemoteFunctionOp | 1057 | 1058 | ): | 1058 | 1059 | udf_sig = op.function_def.signature | 1059 | | - ibis_py_sig = (udf_sig.py_input_types, udf_sig.py_output_type) | | 1060 | + assert not udf_sig.is_virtual # should have been devirtualized in lowering pass | | 1061 | + ibis_py_sig = (tuple(arg.py_type for arg in udf_sig.inputs), udf_sig.output.py_type) | 1060 | 1062 |
| 1061 | 1063 | @ibis_udf.scalar.builtin( | 1062 | 1064 | name=str(op.function_def.routine_ref), signature=ibis_py_sig | @@ -1073,8 +1075,9 @@ def nary_remote_function_op_impl( | 1073 | 1075 | *operands: ibis_types.Value, op: ops.NaryRemoteFunctionOp | 1074 | 1076 | ): | 1075 | 1077 | udf_sig = op.function_def.signature | 1076 | | - ibis_py_sig = (udf_sig.py_input_types, udf_sig.py_output_type) | 1077 | | - arg_names = tuple(arg.name for arg in udf_sig.input_types) | | 1078 | + assert not udf_sig.is_virtual # should have been devirtualized in lowering pass | | 1079 | + ibis_py_sig = (tuple(arg.py_type for arg in udf_sig.inputs), udf_sig.output.py_type) | | 1080 | + arg_names = tuple(arg.name for arg in udf_sig.inputs) | 1078 | 1081 |
| 1079 | 1082 | @ibis_udf.scalar.builtin( | 1080 | 1083 | name=str(op.function_def.routine_ref), | @@ -1153,6 +1156,13 @@ def array_reduce_op_impl(x: ibis_types.Value, op: ops.ArrayReduceOp): | 1153 | 1156 | ) | 1154 | 1157 |
| 1155 | 1158 |
| | 1159 | +@scalar_op_compiler.register_unary_op(ops.ArrayMapOp, pass_op=True) | | 1160 | +def array_map_op_impl(x: ibis_types.Value, op: ops.ArrayMapOp): | | 1161 | + return typing.cast(ibis_types.ArrayValue, x).map( | | 1162 | + lambda arr_vals: scalar_op_compiler.compile_row_op(op.map_op, (arr_vals,)) | | 1163 | + ) | | 1164 | + | | 1165 | + | 1156 | 1166 | # JSON Ops | 1157 | 1167 | @scalar_op_compiler.register_binary_op(ops.JSONSet, pass_op=True) | 1158 | 1168 | def json_set_op_impl(x: ibis_types.Value, y: ibis_types.Value, op: ops.JSONSet): | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/core/compile/sqlglot/compiler.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -369,4 +369,5 @@ def compile_aggregate( | 369 | 369 | def _replace_unsupported_ops(node: nodes.BigFrameNode): | 370 | 370 | node = nodes.bottom_up(node, rewrite.rewrite_slice) | 371 | 371 | node = nodes.bottom_up(node, rewrite.rewrite_range_rolling) | | 372 | + node = nodes.bottom_up(node, rewrite.lower_udfs) | 372 | 373 | return node |
Collapse file Copy file name to clipboardExpand all lines: bigframes/core/compile/sqlglot/expressions/array_ops.py +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,6 +73,28 @@ def _(expr: TypedExpr, op: ops.ArrayReduceOp) -> sge.Expression: | 73 | 73 | ) | 74 | 74 |
| 75 | 75 |
| | 76 | +@register_unary_op(ops.ArrayMapOp, pass_op=True) | | 77 | +def _(expr: TypedExpr, op: ops.ArrayMapOp) -> sge.Expression: | | 78 | + sub_expr = sg.to_identifier("bf_arr_map_uid") | | 79 | + sub_type = dtypes.get_array_inner_type(expr.dtype) | | 80 | + | | 81 | + # TODO: Expression should be provided instead of invoking compiler manually | | 82 | + map_expr = expression_compiler.expression_compiler.compile_row_op( | | 83 | + op.map_op, (TypedExpr(sub_expr, sub_type),) | | 84 | + ) | | 85 | + | | 86 | + return sge.array( | | 87 | + sge.select(map_expr) | | 88 | + .from_( | | 89 | + sge.Unnest( | | 90 | + expressions=[expr.expr], | | 91 | + alias=sge.TableAlias(columns=[sub_expr]), | | 92 | + ) | | 93 | + ) | | 94 | + .subquery() | | 95 | + ) | | 96 | + | | 97 | + | 76 | 98 | @register_unary_op(ops.ArraySliceOp, pass_op=True) | 77 | 99 | def _(expr: TypedExpr, op: ops.ArraySliceOp) -> sge.Expression: | 78 | 100 | if expr.dtype == dtypes.STRING_DTYPE: | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/core/compile/sqlglot/sql/__init__.py -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 |
| 16 | 16 | from bigframes.core.compile.sqlglot.sql.base import ( | 17 | 17 | cast, | 18 | | - escape_chars, | 19 | 18 | identifier, | 20 | 19 | is_null_literal, | 21 | 20 | literal, | | 28 | 27 | __all__ = [ | 29 | 28 | # From base.py | 30 | 29 | "cast", | 31 | | - "escape_chars", | 32 | 30 | "identifier", | 33 | 31 | "is_null_literal", | 34 | 32 | "literal", | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/core/compile/sqlglot/sql/base.py -23Lines changed: 0 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -136,29 +136,6 @@ def table(table: bigquery.TableReference) -> sge.Table: | 136 | 136 | ) | 137 | 137 |
| 138 | 138 |
| 139 | | -def escape_chars(value: str): | 140 | | - """Escapes all special characters""" | 141 | | - # TODO: Reuse literal's escaping logic instead of re-implementing it here. | 142 | | - # https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#string_and_bytes_literals | 143 | | - trans_table = str.maketrans( | 144 | | - { | 145 | | - "\a": r"\a", | 146 | | - "\b": r"\b", | 147 | | - "\f": r"\f", | 148 | | - "\n": r"\n", | 149 | | - "\r": r"\r", | 150 | | - "\t": r"\t", | 151 | | - "\v": r"\v", | 152 | | - "\\": r"\\", | 153 | | - "?": r"\?", | 154 | | - '"': r"\"", | 155 | | - "'": r"\'", | 156 | | - "`": r"\`", | 157 | | - } | 158 | | - ) | 159 | | - return value.translate(trans_table) | 160 | | - | 161 | | - | 162 | 139 | def is_null_literal(expr: sge.Expression) -> bool: | 163 | 140 | """Checks if the given expression is a NULL literal.""" | 164 | 141 | if isinstance(expr, sge.Null): | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/core/rewrite/__init__.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 27 | 27 | from bigframes.core.rewrite.select_pullup import defer_selection | 28 | 28 | from bigframes.core.rewrite.slices import pull_out_limit, pull_up_limits, rewrite_slice | 29 | 29 | from bigframes.core.rewrite.timedeltas import rewrite_timedelta_expressions | | 30 | +from bigframes.core.rewrite.udfs import lower_udfs | 30 | 31 | from bigframes.core.rewrite.windows import ( | 31 | 32 | pull_out_window_order, | 32 | 33 | rewrite_range_rolling, | | 53 | 54 | "pull_out_window_order", | 54 | 55 | "defer_selection", | 55 | 56 | "simplify_complex_windows", | | 57 | + "lower_udfs", | 56 | 58 | ] |
Collapse file Copy file name to clipboard +87Lines changed: 87 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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) |
Collapse file Copy file name to clipboardExpand all lines: bigframes/core/sql/__init__.py +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 48 | 48 | to_wkt = dumps | 49 | 49 |
| 50 | 50 |
| | 51 | +def identifier(name: str) -> str: | | 52 | + if len(name) > 256: | | 53 | + raise ValueError("Identifier must be less than 256 characters") | | 54 | + return f"`{escape_chars(name)}`" | | 55 | + | | 56 | + | | 57 | +def escape_chars(value: str): | | 58 | + """Escapes all special characters""" | | 59 | + # TODO: Reuse literal's escaping logic instead of re-implementing it here. | | 60 | + # https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#string_and_bytes_literals | | 61 | + trans_table = str.maketrans( | | 62 | + { | | 63 | + "\a": r"\a", | | 64 | + "\b": r"\b", | | 65 | + "\f": r"\f", | | 66 | + "\n": r"\n", | | 67 | + "\r": r"\r", | | 68 | + "\t": r"\t", | | 69 | + "\v": r"\v", | | 70 | + "\\": r"\\", | | 71 | + "?": r"\?", | | 72 | + '"': r"\"", | | 73 | + "'": r"\'", | | 74 | + "`": r"\`", | | 75 | + } | | 76 | + ) | | 77 | + return value.translate(trans_table) | | 78 | + | | 79 | + | 51 | 80 | def multi_literal(*values: Any): | 52 | 81 | literal_strings = [sql.to_sql(sql.literal(i)) for i in values] | 53 | 82 | return "(" + ", ".join(literal_strings) + ")" | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/dataframe.py +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4748,7 +4748,9 @@ def apply(self, func, *, axis=0, args: typing.Tuple = (), **kwargs): | 4748 | 4748 | # compatible with the data types of the input params. | 4749 | 4749 | # 3. The order of the columns in the dataframe must correspond | 4750 | 4750 | # to the order of the input params in the function. | 4751 | | - udf_input_dtypes = func.udf_def.signature.bf_input_types | | 4751 | + udf_input_dtypes = tuple( | | 4752 | + arg.bf_type for arg in func.udf_def.signature.inputs | | 4753 | + ) | 4752 | 4754 | if not args and len(udf_input_dtypes) != len(self.columns): | 4753 | 4755 | raise ValueError( | 4754 | 4756 | f"Parameter count mismatch: BigFrames BigQuery function" | @@ -4793,7 +4795,6 @@ def apply(self, func, *, axis=0, args: typing.Tuple = (), **kwargs): | 4793 | 4795 | ) | 4794 | 4796 | result_series.name = None | 4795 | 4797 |
| 4796 | | - result_series = func._post_process_series(result_series) | 4797 | 4798 | return result_series | 4798 | 4799 |
| 4799 | 4800 | # At this point column-wise or element-wise bigquery function operation will | |
|