@@ -695,9 +695,12 @@ def remote_function(
695695 persistent name.
696696
697697 """
698+ import bigframes .pandas as bpd
699+
700+ session = session or bpd .get_global_session ()
698701
699702 # A BigQuery client is required to perform BQ operations
700- if not bigquery_client and session :
703+ if not bigquery_client :
701704 bigquery_client = session .bqclient
702705 if not bigquery_client :
703706 raise ValueError (
@@ -706,7 +709,7 @@ def remote_function(
706709 )
707710
708711 # A BigQuery connection client is required to perform BQ connection operations
709- if not bigquery_connection_client and session :
712+ if not bigquery_connection_client :
710713 bigquery_connection_client = session .bqconnectionclient
711714 if not bigquery_connection_client :
712715 raise ValueError (
@@ -716,8 +719,7 @@ def remote_function(
716719
717720 # A cloud functions client is required to perform cloud functions operations
718721 if not cloud_functions_client :
719- if session :
720- cloud_functions_client = session .cloudfunctionsclient
722+ cloud_functions_client = session .cloudfunctionsclient
721723 if not cloud_functions_client :
722724 raise ValueError (
723725 "A cloud functions client must be provided, either directly or via session. "
@@ -726,8 +728,7 @@ def remote_function(
726728
727729 # A resource manager client is required to get/set IAM operations
728730 if not resource_manager_client :
729- if session :
730- resource_manager_client = session .resourcemanagerclient
731+ resource_manager_client = session .resourcemanagerclient
731732 if not resource_manager_client :
732733 raise ValueError (
733734 "A resource manager client must be provided, either directly or via session. "
@@ -740,56 +741,41 @@ def remote_function(
740741 dataset_ref = bigquery .DatasetReference .from_string (
741742 dataset , default_project = bigquery_client .project
742743 )
743- elif session :
744+ else :
744745 dataset_ref = bigquery .DatasetReference .from_string (
745746 session ._session_dataset_id , default_project = bigquery_client .project
746747 )
747- else :
748- raise ValueError (
749- "Project and dataset must be provided, either directly or via session. "
750- f"{ constants .FEEDBACK_LINK } "
751- )
752748
753749 bq_location , cloud_function_region = get_remote_function_locations (
754750 bigquery_client .location
755751 )
756752
757753 # A connection is required for BQ remote function
758754 # https://cloud.google.com/bigquery/docs/reference/standard-sql/remote-functions#create_a_remote_function
759- if not bigquery_connection and session :
760- bigquery_connection = session ._bq_connection # type: ignore
761755 if not bigquery_connection :
756+ bigquery_connection = session ._bq_connection # type: ignore
757+
758+ bigquery_connection = clients .get_connection_name_full (
759+ bigquery_connection ,
760+ default_project = dataset_ref .project ,
761+ default_location = bq_location ,
762+ )
763+ # Guaranteed to be the form of <project>.<location>.<connection_id>
764+ (
765+ gcp_project_id ,
766+ bq_connection_location ,
767+ bq_connection_id ,
768+ ) = bigquery_connection .split ("." )
769+ if gcp_project_id .casefold () != dataset_ref .project .casefold ():
762770 raise ValueError (
763- "BigQuery connection must be provided, either directly or via session. "
764- f"{ constants .FEEDBACK_LINK } "
771+ "The project_id does not match BigQuery connection gcp_project_id: "
772+ f"{ dataset_ref .project } ."
773+ )
774+ if bq_connection_location .casefold () != bq_location .casefold ():
775+ raise ValueError (
776+ "The location does not match BigQuery connection location: "
777+ f"{ bq_location } ."
765778 )
766-
767- # Check connection_id with `LOCATION.CONNECTION_ID` or `PROJECT_ID.LOCATION.CONNECTION_ID` format.
768- if bigquery_connection .count ("." ) == 1 :
769- bq_connection_location , bq_connection_id = bigquery_connection .split ("." )
770- if bq_connection_location .casefold () != bq_location .casefold ():
771- raise ValueError (
772- "The location does not match BigQuery connection location: "
773- f"{ bq_location } ."
774- )
775- bigquery_connection = bq_connection_id
776- elif bigquery_connection .count ("." ) == 2 :
777- (
778- gcp_project_id ,
779- bq_connection_location ,
780- bq_connection_id ,
781- ) = bigquery_connection .split ("." )
782- if gcp_project_id .casefold () != dataset_ref .project .casefold ():
783- raise ValueError (
784- "The project_id does not match BigQuery connection gcp_project_id: "
785- f"{ dataset_ref .project } ."
786- )
787- if bq_connection_location .casefold () != bq_location .casefold ():
788- raise ValueError (
789- "The location does not match BigQuery connection location: "
790- f"{ bq_location } ."
791- )
792- bigquery_connection = bq_connection_id
793779
794780 def wrapper (f ):
795781 if not callable (f ):
@@ -808,7 +794,7 @@ def wrapper(f):
808794 dataset_ref .dataset_id ,
809795 bigquery_client ,
810796 bigquery_connection_client ,
811- bigquery_connection ,
797+ bq_connection_id ,
812798 resource_manager_client ,
813799 )
814800
0 commit comments