Skip to content

Commit 8cc56d5

Browse files
authored
fix: improve robustness of managed udf code extraction (#1634)
* fix: improve robustness of managed udf code extraction * resolve comments * quick fix
1 parent 58e7cb0 commit 8cc56d5

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

bigframes/functions/_function_client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import logging
2020
import os
2121
import random
22+
import re
2223
import shutil
2324
import string
2425
import tempfile
@@ -272,7 +273,10 @@ def provision_bq_managed_function(
272273
# i.e. there are no references to variables or imports outside the
273274
# body.
274275
udf_code = textwrap.dedent(inspect.getsource(func))
275-
udf_code = udf_code[udf_code.index("def") :]
276+
match = re.search(r"^def ", udf_code, flags=re.MULTILINE)
277+
if match is None:
278+
raise ValueError("The UDF is not defined correctly.")
279+
udf_code = udf_code[match.start() :]
276280

277281
with_connection_clause = (
278282
(

tests/system/large/functions/test_managed_function.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,12 @@ def featurize(x: int) -> list[float]:
171171
def test_managed_function_series_apply(session, dataset_id, scalars_dfs):
172172
try:
173173

174-
@session.udf(dataset=dataset_id, name=prefixer.create_prefix())
174+
# An explicit name with "def" in it is used to test the robustness of
175+
# the user code extraction logic, which depends on that term.
176+
bq_name = f"{prefixer.create_prefix()}_def_to_test_code_extraction"
177+
assert "def" in bq_name, "The substring 'def' was not found in 'bq_name'"
178+
179+
@session.udf(dataset=dataset_id, name=bq_name)
175180
def foo(x: int) -> bytes:
176181
return bytes(abs(x))
177182

0 commit comments

Comments
 (0)