Skip to content

Commit 06c7723

Browse files
committed
Merge remote-tracking branch 'origin/main' into b323176126-write_engine
2 parents 2f0d606 + dd3643d commit 06c7723

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

bigframes/functions/remote_function.py

+13-22
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import random
2222
import shutil
2323
import string
24-
import subprocess
2524
import sys
2625
import tempfile
2726
import textwrap
@@ -87,19 +86,6 @@ def _get_hash(def_, package_requirements=None):
8786
return hashlib.md5(def_repr).hexdigest()
8887

8988

90-
def _run_system_command(command):
91-
program = subprocess.Popen(
92-
[command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
93-
)
94-
stdout, stderr = program.communicate()
95-
exit_code = program.wait()
96-
if exit_code:
97-
raise RuntimeError(
98-
f"Command: {command}\nOutput: {stdout.decode()}\nError: {stderr.decode()}"
99-
f"{constants.FEEDBACK_LINK}"
100-
)
101-
102-
10389
def routine_ref_to_string_for_query(routine_ref: bigquery.RoutineReference) -> str:
10490
return f"`{routine_ref.project}.{routine_ref.dataset_id}`.{routine_ref.routine_id}"
10591

@@ -281,6 +267,8 @@ def generate_cloud_function_main_code(self, def_, dir):
281267
code_template = textwrap.dedent(
282268
"""\
283269
import cloudpickle
270+
import functions_framework
271+
from flask import jsonify
284272
import json
285273
286274
# original udf code is in {udf_code_file}
@@ -289,14 +277,17 @@ def generate_cloud_function_main_code(self, def_, dir):
289277
udf = cloudpickle.load(f)
290278
291279
def {handler_func_name}(request):
292-
request_json = request.get_json(silent=True)
293-
calls = request_json["calls"]
294-
replies = []
295-
for call in calls:
296-
reply = udf(*call)
297-
replies.append(reply)
298-
return_json = json.dumps({{"replies" : replies}})
299-
return return_json
280+
try:
281+
request_json = request.get_json(silent=True)
282+
calls = request_json["calls"]
283+
replies = []
284+
for call in calls:
285+
reply = udf(*call)
286+
replies.append(reply)
287+
return_json = json.dumps({{"replies" : replies}})
288+
return return_json
289+
except Exception as e:
290+
return jsonify( {{ "errorMessage": str(e) }} ), 400
300291
"""
301292
)
302293

tests/system/large/test_remote_function.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import tempfile
2222
import textwrap
2323

24-
from google.api_core.exceptions import NotFound, ResourceExhausted
24+
from google.api_core.exceptions import BadRequest, NotFound, ResourceExhausted
2525
from google.cloud import bigquery, functions_v2
2626
import pandas
2727
import pytest
@@ -1214,6 +1214,28 @@ def square(x):
12141214
)
12151215

12161216

1217+
@pytest.mark.flaky(retries=2, delay=120)
1218+
def test_remote_function_runtime_error(session, scalars_dfs, dataset_id):
1219+
try:
1220+
1221+
@session.remote_function([int], int, dataset=dataset_id)
1222+
def square(x):
1223+
return x * x
1224+
1225+
scalars_df, _ = scalars_dfs
1226+
1227+
with pytest.raises(
1228+
BadRequest, match="400.*errorMessage.*unsupported operand type"
1229+
):
1230+
# int64_col has nulls which should cause error in square
1231+
scalars_df["int64_col"].apply(square).to_pandas()
1232+
finally:
1233+
# clean up the gcp assets created for the remote function
1234+
cleanup_remote_function_assets(
1235+
session.bqclient, session.cloudfunctionsclient, square
1236+
)
1237+
1238+
12171239
@pytest.mark.flaky(retries=2, delay=120)
12181240
def test_remote_function_anonymous_dataset(session, scalars_dfs):
12191241
try:

0 commit comments

Comments
 (0)