@@ -145,7 +145,13 @@ def __init__(
145145 self ._cloud_function_docker_repository = cloud_function_docker_repository
146146
147147 def create_bq_remote_function (
148- self , input_args , input_types , output_type , endpoint , bq_function_name
148+ self ,
149+ input_args ,
150+ input_types ,
151+ output_type ,
152+ endpoint ,
153+ bq_function_name ,
154+ max_batching_rows ,
149155 ):
150156 """Create a BigQuery remote function given the artifacts of a user defined
151157 function and the http endpoint of a corresponding cloud function."""
@@ -169,14 +175,25 @@ def create_bq_remote_function(
169175 bq_function_args .append (
170176 f"{ name } { third_party_ibis_bqtypes .BigQueryType .from_ibis (input_types [idx ])} "
171177 )
178+
179+ remote_function_options = {
180+ "endpoint" : endpoint ,
181+ "max_batching_rows" : max_batching_rows ,
182+ }
183+
184+ remote_function_options_str = ", " .join (
185+ [
186+ f'{ key } ="{ val } "' if isinstance (val , str ) else f"{ key } ={ val } "
187+ for key , val in remote_function_options .items ()
188+ if val is not None
189+ ]
190+ )
191+
172192 create_function_ddl = f"""
173193 CREATE OR REPLACE FUNCTION `{ self ._gcp_project_id } .{ self ._bq_dataset } `.{ bq_function_name } ({ ',' .join (bq_function_args )} )
174194 RETURNS { bq_function_return_type }
175195 REMOTE WITH CONNECTION `{ self ._gcp_project_id } .{ self ._bq_location } .{ self ._bq_connection_id } `
176- OPTIONS (
177- endpoint = "{ endpoint } ",
178- max_batching_rows = 1000
179- )"""
196+ OPTIONS ({ remote_function_options_str } )"""
180197
181198 logger .info (f"Creating BQ remote function: { create_function_ddl } " )
182199
@@ -438,6 +455,7 @@ def provision_bq_remote_function(
438455 reuse ,
439456 name ,
440457 package_requirements ,
458+ max_batching_rows ,
441459 ):
442460 """Provision a BigQuery remote function."""
443461 # If reuse of any existing function with the same name (indicated by the
@@ -485,7 +503,12 @@ def provision_bq_remote_function(
485503 "Exactly one type should be provided for every input arg."
486504 )
487505 self .create_bq_remote_function (
488- input_args , input_types , output_type , cf_endpoint , remote_function_name
506+ input_args ,
507+ input_types ,
508+ output_type ,
509+ cf_endpoint ,
510+ remote_function_name ,
511+ max_batching_rows ,
489512 )
490513 else :
491514 logger .info (f"Remote function { remote_function_name } already exists." )
@@ -607,6 +630,7 @@ def remote_function(
607630 cloud_function_service_account : Optional [str ] = None ,
608631 cloud_function_kms_key_name : Optional [str ] = None ,
609632 cloud_function_docker_repository : Optional [str ] = None ,
633+ max_batching_rows : Optional [int ] = 1000 ,
610634):
611635 """Decorator to turn a user defined function into a BigQuery remote function.
612636
@@ -723,6 +747,15 @@ def remote_function(
723747 projects/PROJECT_ID/locations/LOCATION/repositories/REPOSITORY_NAME.
724748 For more details see
725749 https://cloud.google.com/functions/docs/securing/cmek#before_you_begin.
750+ max_batching_rows (int, Optional):
751+ The maximum number of rows to be batched for processing in the
752+ BQ remote function. Default value is 1000. A lower number can be
753+ passed to avoid timeouts in case the user code is too complex to
754+ process large number of rows fast enough. A higher number can be
755+ used to increase throughput in case the user code is fast enough.
756+ `None` can be passed to let BQ remote functions service apply
757+ default batching. See for more details
758+ https://cloud.google.com/bigquery/docs/remote-functions#limiting_number_of_rows_in_a_batch_request.
726759 """
727760 import bigframes .pandas as bpd
728761
@@ -846,6 +879,7 @@ def wrapper(f):
846879 reuse ,
847880 name ,
848881 packages ,
882+ max_batching_rows ,
849883 )
850884
851885 # TODO: Move ibis logic to compiler step
0 commit comments