大查询 - 用户定义函数 - 标量子查询错误
Posted
技术标签:
【中文标题】大查询 - 用户定义函数 - 标量子查询错误【英文标题】:Big Query - User Defined Function - Scalar Subquery Error 【发布时间】:2020-11-25 03:11:11 【问题描述】:我正在尝试根据下面的查询重新创建用户定义的函数,但是,当我运行它时,它反而返回了这个错误。感谢这里是否有人知道解决方法。
标量子查询产生了多个元素:
create or replace function `dataset.list_of_days`
(user_id string, start_date date, end_date date) AS
((
with temp as (
select user_id, day from
unnest(generate_date_array(start_date, end_date)) day)
select as struct row_number() over (partition by user_id order by day asc) as row_num,
user_id, day
from temp
));
with temp as (
select '100110' as user_id, date('2020-01-31') as start_date,
date('2020-02-28') as end_date )
select dataset.list_of_days(user_id, start_date, end_date)
from temp;
【问题讨论】:
如果我错了请纠正我,但我可能误解了用户定义函数的目的是什么 【参考方案1】:BigQuery 的 UDF 只能返回一个标量值,看来你的 UDF 内部没有太大的输出,你可以考虑重写为
create or replace function `dataset.list_of_days`
(user_id string, start_date date, end_date date) AS
(ARRAY( -- NOTE: ARRAY() was added to you original query
with temp as (
select user_id, day from
unnest(generate_date_array(start_date, end_date)) day)
select as struct row_number() over (partition by user_id order by day asc) as row_num,
user_id, day
from temp
));
with temp as (
select '100110' as user_id, date('2020-01-31') as start_date,
date('2020-02-28') as end_date )
select dataset.list_of_days(user_id, start_date, end_date)
from temp;
注意现在UDF的返回类型是ARRAY>,如果你需要它作为一个多行的表,你可以取消嵌套。
【讨论】:
以上是关于大查询 - 用户定义函数 - 标量子查询错误的主要内容,如果未能解决你的问题,请参考以下文章