我想知道如何在新的标准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”...这最后是指查询,它以红色突出显示选择句子。 / p>
所以,我想到了下一个问题:
感谢您的帮助。
答案 0 :(得分: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