问题描述
我正在尝试在BigQuery中编写自定义聚合函数。在PGsql中,我可以编写user-defined aggregate functions并与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]
existing aggregate functions不足以满足我的目的,因此我需要能够对自定义窗口大小执行自定义计算。 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(ARRAY_AGG(s_price) over (partition by s_id order by s_date rows 40 preceding)) as temp_col
from temp_db.s_table;