-1

我正在尝试从某些表中获取数据,并将它们传递给 bigquery 中的 JavaScript UDF。

我编写了以下代码,但我似乎无法理解将我的选择结果存储在所需结构中的正确语法,然后如何将其传递给我的 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;
4

1 回答 1

1

您共享的代码存在一些语法错误,但基于程序逻辑,我对更具声明性的样式进行了一些调整并更正了这些错误。

  1. arg1即使TBL1如果需要我们也可以将其包含在 CTE中,我也保留了
  2. arg2现在是通过从TBL1as a中选择的值创建的,并且使用和STRUCT重命名列as col1as col2
  3. 由于您的 UDF 接受为字符串,因此我已将其转换SchoolIdstringcol2
  4. id为演示目的添加了另一个字段,因为我将按此id列对数据进行分组或聚合,以获取arg2UDF 所需的结构参数数组
  5. 我最终选择id并使用正确格式的参数调用您UDFarray_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


让我知道这是否适合您。

于 2021-08-29T03:34:12.437 回答