Reputation: 1029
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'
Upvotes: 0
Views: 1496
Reputation: 207828
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'
Upvotes: 3