在 BigQuery 中模拟 window 聚合的 UDF 的解决方法?
Workarounds for emulating UDFs for window aggregates in BigQuery?
我正在尝试在 BigQuery 中编写自定义聚合函数。在 PGSQL 中,我可以编写 user-defined aggregate functions ,它可以与 over
子句一起使用,但我无法为 BigQuery 编写任何此类聚合函数——是否可以编写一个接受一个函数的函数分区列的整个数组和 return 基于某些自定义计算的值?
我尝试过的示例:
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 不足以满足我的目的,因此我需要能够对自定义 window 大小执行自定义计算。在 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;
我正在尝试在 BigQuery 中编写自定义聚合函数。在 PGSQL 中,我可以编写 user-defined aggregate functions ,它可以与 over
子句一起使用,但我无法为 BigQuery 编写任何此类聚合函数——是否可以编写一个接受一个函数的函数分区列的整个数组和 return 基于某些自定义计算的值?
我尝试过的示例:
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 不足以满足我的目的,因此我需要能够对自定义 window 大小执行自定义计算。在 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;