Javascript BigQuery中的用户定义函数使用带点的列名
我需要在BigQuery中创建自定义JS函数。首先,我想基于Google示例创建简单的starter,但在我的情况下,我需要使用名称中带有点的列中的数据,例如hits.item.transactionId,这将返回错误 我在UDF编辑器中的自定义函数代码: 我的SQL Select使用了我的passthroughExample函数:Javascript BigQuery中的用户定义函数使用带点的列名,javascript,sql,google-bigquery,Javascript,Sql,Google Bigquery,我需要在BigQuery中创建自定义JS函数。首先,我想基于Google示例创建简单的starter,但在我的情况下,我需要使用名称中带有点的列中的数据,例如hits.item.transactionId,这将返回错误 我在UDF编辑器中的自定义函数代码: 我的SQL Select使用了我的passthroughExample函数: SELECT outputA, outputB FROM (passthrough(SELECT fullVisitorId, hits.item.tran
SELECT
outputA, outputB
FROM
(passthrough(SELECT fullVisitorId, hits.item.transactionId AS transactionId FROM TABLE_DATE_RANGE( [00000000.ga_sessions_], TIMESTAMP('2016-01-01'), TIMESTAMP('2016-01-05') )))
我得到消息:
错误:不受支持的别名:无法访问字段hits.item.transactionId作为transactionId
bigquery.defineFunction(
'passthrough',
['fullVisitorId', 'hits.item.transactionId'],
[{'name': 'outputA', 'type': 'string'},
{'name': 'outputB', 'type': 'string'}],
passthroughExample
);
并且查询中没有别名:
SELECT
outputA, outputB
FROM
(passthrough(SELECT fullVisitorId, hits.item.transactionId FROM TABLE_DATE_RANGE( [00000000.ga_sessions_], TIMESTAMP('2016-01-01'), TIMESTAMP('2016-01-05') )))
为此,最好使用标准SQL而不是传统SQL。您可以在中阅读有关用户定义函数的信息。在您的情况下,您可能需要以下内容:
#standardSQL
CREATE TEMP FUNCTION passthrough(fullVisitorId INT64, transactionId STRING)
RETURNS STRUCT<outputA INT64, outputB STRING>
LANGUAGE js AS """
var res = new Object();
res.outputA = fullVisitorId;
res.outputB = transactionId;
return res;
""";
SELECT passthrough(hit.item.fullVisitorId, transactionId).*
FROM `your_dataset.ga_sessions_*`
CROSS JOIN UNNEST(hits) AS hit
WHERE _TABLE_SUFFIX BETWEEN '2016-01-01' AND '2016-01-05';
中解释了传统SQL中用户定义函数与标准SQL中用户定义函数之间的一些差异。I checked。别名的名称并不重要。错误消息总是相同的。
#standardSQL
CREATE TEMP FUNCTION passthrough(fullVisitorId INT64, transactionId STRING)
RETURNS STRUCT<outputA INT64, outputB STRING>
LANGUAGE js AS """
var res = new Object();
res.outputA = fullVisitorId;
res.outputB = transactionId;
return res;
""";
SELECT passthrough(hit.item.fullVisitorId, transactionId).*
FROM `your_dataset.ga_sessions_*`
CROSS JOIN UNNEST(hits) AS hit
WHERE _TABLE_SUFFIX BETWEEN '2016-01-01' AND '2016-01-05';