1

我正在尝试在 BigQuery 中编写自定义聚合函数。在 PGSQL 中,我可以编写可与子句一起使用的用户定义聚合函数over,但我无法为 BigQuery 编写任何此类聚合函数 - 是否可以编写一个函数来接收一列的整个数组一个分区并根据一些自定义计算返回一个值?

我试过的例子:

CREATE OR REPLACE FUNCTION temp_db.temp_func(arr ARRAY<int64>)
RETURNS int64 LANGUAGE js AS """
  return arr.length*10 //example calculation
  //actual result involves looping over the array and doing few calculations 
""";

select s_id, temp_db.temp_func(s_price) over (partition by s_id order by s_date rows 40 preceding) as temp_col
from temp_db.s_table;

这给出了一个错误:Query error: Function temp_db.temp_func does not support an OVER clause at [6:19]

现有的聚合函数不足以满足我的目的,因此我需要能够对自定义窗口大小执行自定义计算。BigQuery 中是否有任何解决方法?

4

1 回答 1

3
CREATE OR REPLACE FUNCTION temp_db.temp_func(arr ARRAY<int64>)
RETURNS int64 LANGUAGE js AS """
  return arr.length*10 //example calculation
  //actual result involves looping over the array and doing few calculations 
""";

select s_id, temp_db.temp_func(ARRAY_AGG(s_price) over (partition by s_id order by s_date rows 40 preceding)) as temp_col
from temp_db.s_table;
于 2020-09-10T13:25:59.550 回答