简体   繁体   中英

Using UDF as a column in a query in BigQuery

I have created a UDF in BigQuery and managed to run it like the example in the documentation ( https://cloud.google.com/bigquery/user-defined-functions#creating-the-query ) where the UDF is used in the FROM clause.

However, what I need is to use the UDF in the select as a column.

For an example - this is my function that returns for each coordinate in which quarter of the globe is it:

function getQuarter(row, emit) {
  emit({quarter: getQuarterHelper(row.lon,row.lat)});
}

function getQuarterHelper(lon,lat) {
  try {
    var NS = lat > 0 ? 'N' : 'S';
    var EW = lon > 0 ? 'E' : 'W';
    return(NS + EW);
  } catch (ex) {
    return 'N/A';
  }
}

bigquery.defineFunction(
  'getQuarter',
  ['lon', 'lat'], //input columns
  [{name: 'quarter', type: 'string'}], //output
  getQuarter
);

This works:

SELECT quarter
FROM 
  getQuarter(
     SELECT lon,lat
     FROM [table_name]
  )

But this, for an example, isn't :

SELECT location_title, getQuarter(lon, lat)
FROM [table_name]

And neither this:

SELECT *
FROM [table_name]
WHERE getQuarter(lon,lat) = 'NE'

You are better to define your UDF in the newer Standard SQL, and not Legacy SQL where you have some limitations.

https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions

In Standard SQL you can do this:

SELECT location_title, getQuarter(lon, lat)
FROM `table_name`

which in Legacy SQL you can trick, by exposing the location_title from the UDF inside only.

Also in Standard SQL you can

SELECT getQuarter(lon,lat) as q
FROM `table_name`
WHERE q = 'NE'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM