简体   繁体   English

在BigQuery的查询中使用UDF作为列

[英]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. 我已经在BigQuery中创建了一个UDF,并设法像文档( https://cloud.google.com/bigquery/user-defined-functions#creating-the-query )中的示例一样运行它,其中UDF用于FROM子句。

However, what I need is to use the UDF in the select as a column. 但是,我需要在select中使用UDF作为一列。

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. 最好在较新的Standard SQL中定义UDF,而不是在有某些限制的Legacy SQL中定义。

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

In Standard SQL you can do this: 在标准SQL中,您可以执行以下操作:

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. 您可以通过仅从UDF内部公开location_title来在Legacy SQL中进行欺骗。

Also in Standard SQL you can 同样在标准SQL中,您可以

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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