【发布时间】:2020-11-26 16:53:42
【问题描述】:
BigQuery 支持:
- SQL 和 JavaScript 中的User Defined Functions (UDF)。
- Analytic functions 计算一组行的值并为每一行返回一个结果。这些函数可以与OVER 子句一起使用。有一组预定义的分析函数。
问题 #1:“BigQuery 是否支持分析用户定义的函数?”
这背后的动机是我想实现 Python pandas 代码中常见的split-apply-combine 模式。这对于组内归一化和使用组统计的其他转换可能很有用。
我在 Standart SQL 中做了一个小测试:
create or replace function `mydataset.mylen`(arr array<string>) returns int64 as (
array_length(arr)
);
WITH Produce AS
(SELECT 'kale' as item, 23 as purchases, 'vegetable' as category
UNION ALL SELECT 'orange', 2, 'fruit'
UNION ALL SELECT 'cabbage', 9, 'vegetable'
UNION ALL SELECT 'apple', 8, 'fruit'
UNION ALL SELECT 'leek', 2, 'vegetable'
UNION ALL SELECT 'lettuce', 10, 'vegetable')
SELECT
item,
purchases,
category,
`mydataset.mylen`(item) over (mywindow) as windowlen
FROM Produce
window mywindow as (
partition by category
)
当我运行上面的代码时,我得到:
查询错误:函数 mydataset.mylen 在 [16:3] 不支持 OVER 子句
因此,如果 BigQuery 确实支持分析 UDF,问题 #2:“如何实现 UDF 以使其支持 OVER 子句?”
【问题讨论】:
标签: google-bigquery analytic-functions bigquery-udf