Steps to Invoke Google Cloud Run Functions as SQL Functions
Shows the steps to invoke Google Cloud Run functions as SQL functions in your database.
To invoke Google Cloud Run functions as SQL functions, you create a library of SQL wrapper functions that reference and call their respective cloud functions. Before you create this catalog, it is assumed here that you have created the necessary cloud functions to be referenced by this catalog. See Create a Cloud Run function by using the Google Cloud console for more information.
- To access Google Cloud Run functions from an Autonomous AI Database instance use your Google service account. You must grant the cloudfunctions.functions.listpermission, andCloud Functions InvokerandCloud Run Invokerroles to the Google service account for the Google Cloud Platform (GCP) app.See the following for more information:
- Create a catalog. To create a catalog for Google Cloud Run functions with a user other than ADMINyou need to grant read privileges on theCLOUD_INTEGRATIONSview to that user.A catalog is a collection of wrapper functions that reference and call their respective cloud functions using their API endpoints. BEGINDBMS_CLOUD_FUNCTION.CREATE_CATALOG( credential_name => 'GCP$PA', catalog_name => 'GCP_DEMO_CATALOG', service_provider => 'GCP', cloud_params => '{"project_id":"example_XXXXXX"}' ); END; /The project_idvalue in thecloud_paramsis the project ID of the Google Cloud Platform (GCP).This creates the GCP_DEMO_CATALOGcatalog and catalogs all the functions of the providedproject_id.See CREATE_CATALOG Procedure for more information. You can query DBA_CLOUD_FUNCTION_CATALOG View and USER_CLOUD_FUNCTION_CATALOG View views to retrieve the list of all the catalogs in your database. 
- List the functions in a catalog.Following is the example to list Google Cloud Run functions: VAR function_list CLOB; BEGINDBMS_CLOUD_FUNCTION.LIST_FUNCTIONS( credential_name => 'GCP$PA', catalog_name => 'GCP_DEMO_CATALOG', function_list => :function_list ); END; / SELECT JSON_QUERY (:function_list, '$' RETURNING VARCHAR2(32676) pretty) AS search_results FROM dual;See LIST_FUNCTIONS Procedure for more information. 
- Run the DBMS_CLOUD_FUNCTION.SYNC_FUNCTIONSto create wrapper SQL functions. You can use one of the following methods to create the wrapper SQL functions in the catalog, that call their respective cloud functions:- 
SYNC_FUNCTIONS: SYNC_FUNCTIONSmethod to create wrapper SQL function is the quickest and simplest method, which automatically syncs (creates or deletes) wrapper functions in the catalog with the complete list of cloud functions defined in the region, compartment, and tenancy with which the catalog was created. For example:
 BEGINDBMS_CLOUD_FUNCTION.SYNC_FUNCTIONS( catalog_name => 'GCP_DEMO_CATALOG' ); END; /This creates a PL/SQL wrapper for adding new functions to the catalog and removing wrappers for functions that have been deleted from the catalog. Run the following query to verify the sync. SELECT object_name FROM sys.all_objects WHERE owner='TEST_USER' AND object_type='FUNCTION';Note
 Keep a note of the current user in order to run this command.See SYNC_FUNCTIONS Procedure for more information. - You can manually create a SQLFunction in your catalog that calls its respective cloud function usingDBMS_CLOUD.CREATE_FUNCTION.
 Example to create a function in the GCP_DEMO_CATALOGcatalog.EXEC :function_args := TO_CLOB('{"name": "VARCHAR2"}');BEGINDBMS_CLOUD_FUNCTION.CREATE_FUNCTION( credential_name => 'GCP$PA', catalog_name => 'GCP_DEMO_CATALOG', function_name => 'gcp_testfunc', function_id => 'function_id_path', input_args => :function_args ); END; /This creates the GCP_TESTFUNCfunction in theGCP_DEMO_CATALOGcatalog.The GCP_TESTFUNCfunction in the catalog is a reference to the respective Google Cloud Run function whose project id is referenced by theFUNCTION_IDparameter. Invoking the function in the catalog along with its arguments runs the corresponding Google Cloud Run function and provides the output returned by the function.You can query DBA_CLOUD_FUNCTION View and USER_CLOUD_FUNCTION View views to retrieve the list of all the functions in your database. See CREATE_FUNCTION Procedure for more information. 
- 
- After the function is created you can DESCRIBEand invoke it.DESC GCP_TESTFUNC COLUMN STATUS format a30 COLUMN OUTPUT format a30SELECT GCP_TESTFUNC(NULL) FROM dual;This invokes the GCP_TESTFUNCfunction by calling the function reference specified in thefunction_idparameter.
- You can drop an existing function using DROP_FUNCTIONprocedure. For example:EXEC DBMS_CLOUD_FUNCTION.DROP_FUNCTION (CATALOG_NAME => 'GCP_DEMO_CATALOG', FUNCTION_NAME => 'GCP_TESTFUNC');This drops the GCP_TESTFUNCfunction from theGCP_DEMO_CATALOGcatalog.See DROP_FUNCTION Procedure for more information. 
- You can drop an existing catalog using DROP_CATALOGprocedure. For example:BEGINDBMS_CLOUD_FUNCTION.DROP_CATALOG( catalog_name => 'GCP_DEMO_CATALOG' ); END; /This drops the GCP_DEMO_CATALOGfrom your database.See DROP_CATALOG Procedure for more information. 
Parent topic: Invoke User Defined Functions