将 ARRAY<STRUCT> 传递给 bigquery sql 中的 js UDF
pass an ARRAY<STRUCT> to a js UDF in bigquery sql
我正在尝试从一些表中获取数据,并将它们传递给 bigquery 中的 JavaScript UDF。
我写了下面的代码,但我似乎无法理解将 select 的结果存储在所需结构中的正确语法,然后如何将它传递给我的 udf 函数。
DECLARE arg1 ARRAY<STRING>;
DECLARE arg2 ARRAY<STRUCT <col1 STRING, col2 STRING> >;
DECLARE res1 ARRAY<STRING>;
SET arg1 = ARRAY<STRING>["Adams", "Joseph", "Davis", "Mary", "Jesus"] ;
CREATE TEMP FUNCTION myfunction(arg1 ARRAY<STRING> , arg2 ARRAY<STRUCT<col1 STRING, col2 STRING> > )
RETURNS ARRAY<STRING>
LANGUAGE js AS '''
return ["a", "b", "c"]
''';
SET ARG2 = (SELECT AS STRUCT(
WITH TBL1 AS
(SELECT 'Adams' as LastName, 50 as SchoolID UNION ALL
SELECT 'Buchanan', 52 UNION ALL
SELECT 'Coolidge', 52 UNION ALL
SELECT 'Davis', 51 UNION ALL
SELECT 'Eisenhower', 77)
SELECT LastName as col1, SchoolID as col2 FROM TBL1));
SET res1 = select res FROM UNNEST(myfunction( arg1, arg2 )) res;
您共享的代码存在一些语法错误,但根据程序逻辑,我对更具声明性的样式进行了一些调整并更正了这些错误。
- 我保留了
arg1
,但如果需要,我们也可以将其包含在 TBL1
CTE 中
arg2
现在由 select 将 TBL1
中的值编辑为 STRUCT
并使用 as col1
和 [=17] 重命名列=]
- 我将
SchoolId
转换为 string
因为你的 UDF 接受 col2
作为字符串
- 出于演示目的,我添加了另一个字段
id
,因为我将按此 id
列对数据进行分组或聚合,以获得您所需的结构 arg2
参数数组UDF
- 我终于 select
id
并使用正确格式的参数调用您的 UDF
。 array_agg
用于在此处创建结构数组。结果在名为 res2
的列中
DECLARE arg1 ARRAY<STRING>;
SET arg1 = ARRAY<STRING>["Adams", "Joseph", "Davis", "Mary", "Jesus"] ;
CREATE TEMP FUNCTION myfunction(arg1 ARRAY<STRING> , arg2 ARRAY<STRUCT<col1 STRING, col2 STRING> > )
RETURNS ARRAY<STRING>
LANGUAGE js AS '''
return ["a", "b", "c"]
''';
WITH TBL1 AS (
SELECT 'Adams' as LastName, 50 as SchoolID UNION ALL
SELECT 'Buchanan', 52 UNION ALL
SELECT 'Coolidge', 52 UNION ALL
SELECT 'Davis', 51 UNION ALL
SELECT 'Eisenhower', 77
),
tbl2 as (
SELECT
1 as id,
STRUCT(
LastName as col1,
CAST(SchoolID as STRING) as col2
) as arg2
FROM TBL1
)
select id, myfunction(arg1, array_agg( arg2)) as res1 from tbl2
group by id
让我知道这是否适合你。
我正在尝试从一些表中获取数据,并将它们传递给 bigquery 中的 JavaScript UDF。
我写了下面的代码,但我似乎无法理解将 select 的结果存储在所需结构中的正确语法,然后如何将它传递给我的 udf 函数。
DECLARE arg1 ARRAY<STRING>;
DECLARE arg2 ARRAY<STRUCT <col1 STRING, col2 STRING> >;
DECLARE res1 ARRAY<STRING>;
SET arg1 = ARRAY<STRING>["Adams", "Joseph", "Davis", "Mary", "Jesus"] ;
CREATE TEMP FUNCTION myfunction(arg1 ARRAY<STRING> , arg2 ARRAY<STRUCT<col1 STRING, col2 STRING> > )
RETURNS ARRAY<STRING>
LANGUAGE js AS '''
return ["a", "b", "c"]
''';
SET ARG2 = (SELECT AS STRUCT(
WITH TBL1 AS
(SELECT 'Adams' as LastName, 50 as SchoolID UNION ALL
SELECT 'Buchanan', 52 UNION ALL
SELECT 'Coolidge', 52 UNION ALL
SELECT 'Davis', 51 UNION ALL
SELECT 'Eisenhower', 77)
SELECT LastName as col1, SchoolID as col2 FROM TBL1));
SET res1 = select res FROM UNNEST(myfunction( arg1, arg2 )) res;
您共享的代码存在一些语法错误,但根据程序逻辑,我对更具声明性的样式进行了一些调整并更正了这些错误。
- 我保留了
arg1
,但如果需要,我们也可以将其包含在TBL1
CTE 中 arg2
现在由 select 将TBL1
中的值编辑为STRUCT
并使用as col1
和 [=17] 重命名列=]- 我将
SchoolId
转换为string
因为你的 UDF 接受col2
作为字符串 - 出于演示目的,我添加了另一个字段
id
,因为我将按此id
列对数据进行分组或聚合,以获得您所需的结构arg2
参数数组UDF - 我终于 select
id
并使用正确格式的参数调用您的UDF
。array_agg
用于在此处创建结构数组。结果在名为res2
的列中
DECLARE arg1 ARRAY<STRING>;
SET arg1 = ARRAY<STRING>["Adams", "Joseph", "Davis", "Mary", "Jesus"] ;
CREATE TEMP FUNCTION myfunction(arg1 ARRAY<STRING> , arg2 ARRAY<STRUCT<col1 STRING, col2 STRING> > )
RETURNS ARRAY<STRING>
LANGUAGE js AS '''
return ["a", "b", "c"]
''';
WITH TBL1 AS (
SELECT 'Adams' as LastName, 50 as SchoolID UNION ALL
SELECT 'Buchanan', 52 UNION ALL
SELECT 'Coolidge', 52 UNION ALL
SELECT 'Davis', 51 UNION ALL
SELECT 'Eisenhower', 77
),
tbl2 as (
SELECT
1 as id,
STRUCT(
LastName as col1,
CAST(SchoolID as STRING) as col2
) as arg2
FROM TBL1
)
select id, myfunction(arg1, array_agg( arg2)) as res1 from tbl2
group by id
让我知道这是否适合你。