Panda
Panda

Reputation: 643

Using BQ Command line create View with UDF in BigQuery

I want to create View in BigQuery using UDF and BQ command line. BQ command :

bq query --use_legacy_sql=false --project_id="myProject" \
'CREATE OR REPLACE FUNCTION udfFunc(str STRING) RETURNS STRING LANGUAGE js AS 
    """
      data = fromLib(str);
      return JSON.stringify(data);
    """ OPTIONS(library = "gs://<bucket>/lib_file.js");
SELECT'
      col1,
      col2,
      udfFunc(col2) as new_col
    FROM
      `myProject:mySataset.table`'

I am getting an error Invalid value: Routine name "udfFunc" missing dataset while no default dataset is set in the request.

Upvotes: 0

Views: 465

Answers (1)

Yun Zhang
Yun Zhang

Reputation: 5518

From your query (and the comment on the question), it seems that you only need a temp function during the query time, this is the query that you:

  1. define a temp function which is only visible in this query
  2. use the temp function immediately in the query
CREATE TEMP FUNCTION udfFunc(str STRING) RETURNS STRING LANGUAGE js AS 
    """
      data = fromLib(str);
      return JSON.stringify(data);
    """ OPTIONS(library = "gs://<bucket>/lib_file.js");
SELECT
      col1,
      col2,
      udfFunc(col2) as new_col
    FROM
      `myProject:mySataset.table`

Upvotes: 2

Related Questions