bigquery标准sql udf映射到struct返回内部错误

时间:2016-12-12 17:28:26

标签: google-bigquery udf

我在下面有一个代码块,用于使用udf解析查询参数。当传递给函数的值被硬编码时,它可以正常工作。当我尝试解析从表中获取的相同值时,我想到了 发生内部错误,无法完成请求。 (错误代码:internalError)

CREATE TEMPORARY FUNCTION parse(queryString STRING) RETURNS ARRAY<STRUCT<key STRING, value STRING>> LANGUAGE js AS
"""
    var params = {}
    var array = []
    // split into key/value pairs
    var queries = queryString.split('&');
    var ind = 0
    // convert the array of strings into an object
    for (var i = 0; i < queries.length; i++ ) {
        var temp = queries[i].split('=');
        if(temp.length < 2) continue;
        array[ind++] = { key: temp[0], value: decodeURI(temp[1]) }
    }
    return array;
""";
select parse('ca_chid=2002810&ca_source=gaw&ca_ace=&ca_nw=g&ca_dev=c&ca_pl=&ca_pos=1t3&ca_agid=32438864366&ca_caid=260997846&ca_adid=151983037851&ca_kwt=florists%20in%20walsall&ca_mt=e&ca_fid=&ca_tid=aud-117534990726:kwd-420175760&ca_lp=9045676&ca_li=&ca_devm=&ca_plt=&ca_sadt=&ca_smid=&ca_spc=&ca_spid=&ca_sco=&ca_sla=&ca_sptid=&ca_ssc=&gclid=CLaDoa6ZrdACFcyRGwodG8IFvQ') as params

--not working
--select parse(page_urlquery) from (
--SELECT page_urlquery  FROM `query_param_snapshot` where page_urlquery != '' LIMIT 1

1 个答案:

答案 0 :(得分:1)

还报道了issue tracker(我们正在修复)。一种解决方法是使用SQL函数而不是JavaScript函数,例如:

CREATE TEMPORARY FUNCTION parse(queryString STRING)
    RETURNS ARRAY<STRUCT<key STRING, value STRING>> AS (
  (SELECT
     ARRAY_AGG(STRUCT(
       entry[OFFSET(0)] AS key,
       entry[OFFSET(1)] AS value))
   FROM (
     SELECT SPLIT(pairString, '=') AS entry
     FROM UNNEST(SPLIT(queryString, '&')) AS pairString)
   )
);
SELECT parse('ca_chid=2002810&ca_source=gaw&ca_ace=&ca_nw=g&ca_dev=c&ca_pl=&ca_pos=1t3&ca_agid=32438864366&ca_caid=260997846&ca_adid=151983037851&ca_kwt=florists%20in%20walsall&ca_mt=e&ca_fid=&ca_tid=aud-117534990726:kwd-420175760&ca_lp=9045676&ca_li=&ca_devm=&ca_plt=&ca_sadt=&ca_smid=&ca_spc=&ca_spid=&ca_sco=&ca_sla=&ca_sptid=&ca_ssc=&gclid=CLaDoa6ZrdACFcyRGwodG8IFvQ') AS params;