在BigQuery中为窗口聚合模拟UDF的解决方法?

问题描述

我正在尝试在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;

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...