首页 文章

如何在标准SQL中使用BigQuery UDF - WebUI?

提问于
浏览
3

我想知道如何在新的标准SQL - WebUI中使用BigQuery UDF .

UDF函数似乎只在“使用传统SQL模式”中启用,但在新的标准SQL模式中不起作用 .

这是UDF编辑器中的UDF函数:

// UDF registration
bigquery.defineFunction(
  'urlDecode',  // Name used to call the function from SQL

  ['title', 'num_requests'],  // Input column names

  // JSON representation of the output schema
  [{name: 'title', type: 'string'},
   {name: 'requests', type: 'integer'}],

  // The UDF
function urlDecode(row, emit) {
  emit({title: decodeHelper(row.title),
        requests: row.num_requests});
}

// Helper function for error handling
function decodeHelper(s) {
  try {
    return decodeURI(s);
  } catch (ex) {
    return s;
  }
}
);

这是查询编辑器中的查询:

SELECT requests, title
FROM
  urlDecode(
    SELECT
      title, sum(requests) AS num_requests
    FROM
      [fh-bigquery:wikipedia.pagecounts_201504]
    WHERE language = 'fr'
    GROUP EACH BY title
  )
WHERE title LIKE '%ç%'
ORDER BY requests DESC
LIMIT 100

如果我从UDF编辑器中的“使用传统模式”中删除勾选,则会显示一条消息:“标准SQL中仅支持内联UDF” . 然后在Bigquery的验证器中出现一条红色消息:“错误:语法错误:预期”)“但是在[4:5]得到了关键字SELECT”...这最后引用了查询,它以红色突出显示选择句子 .

所以,我想到了下一个问题:

  • UDF功能有问题吗?也许它不是内联写的?

  • 上一条错误消息显示:"Only inline UDFs are supported in standard SQL" . 那么,这意味着我应该删除"Use Legacy Mode"的勾号?

谢谢你的帮助 .

1 回答

  • 2

    标量UDF(标准更多)是查询的“部分”,因此所有都需要放在查询编辑器中(这里不需要UDF编辑器)

    CREATE TEMPORARY FUNCTION timesTwo(x INT64)
    RETURNS INT64
      LANGUAGE js AS """
      return x*2;
    """;
    SELECT timesTwo(numbers) as doubles
    FROM UNNEST([1, 2, 3, 4, 5]) AS numbers;
    

    在标准SQL中查看User-Defined Functions的更多内容

    对于您的特定查询 - 请尝试以下

    CREATE TEMPORARY FUNCTION urlDecode(x STRING)
    RETURNS STRING
      LANGUAGE js AS """
      // Helper function for error handling
      function decodeHelper(s) {
        try {
          return decodeURI(s);
        } catch (ex) {
          return s;
        }
      }
      return decodeHelper(x);
    """;
    
    SELECT
      urlDecode(title) AS title, SUM(requests) AS requests
    FROM
      `fh-bigquery.wikipedia.pagecounts_201504`
    WHERE LANGUAGE = 'fr'
    GROUP BY title
    HAVING title LIKE '%ç%'
    ORDER BY requests DESC
    LIMIT 100
    

相关问题