【问题标题】:BigQuery Standard SQL using Python cannot use OFFSET keyword使用 Python 的 BigQuery 标准 SQL 不能使用 OFFSET 关键字
【发布时间】:2016-09-19 09:39:19
【问题描述】:

我正在尝试将 BigQuery Standard SQL 与 Python API 结合使用,但无法执行在 WEB UI 中成功运行的查询。

基本上,我正在拆分一个字符串,然后使用 OFFSET 关键字来获取特定索引处的值。如下:

CASE WHEN t.depth = 1 THEN '' WHEN t.depth = 2 THEN '' WHEN t.depth = 3 THEN '' WHEN t.depth = 4 THEN '' WHEN t.depth = 5 THEN '' WHEN t.depth = 6 THEN t.curr WHEN t.depth = 7 THEN SPLIT(t.ancestry,'/')[OFFSET(6)] ELSE '' END AS level7,


CASE WHEN t.depth = 1 THEN '' WHEN t.depth = 2 THEN '' WHEN t.depth = 3 THEN '' WHEN t.depth = 4 THEN '' WHEN t.depth = 5 THEN t.curr WHEN t.depth = 6 THEN SPLIT(t.ancestry,'/')[OFFSET(5)] WHEN t.depth = 7 THEN SPLIT(t.ancestry,'/')[OFFSET(5)] ELSE '' END AS level6,

以上代码在 WEB UI 中运行没有问题,而使用 Python API 并设置useLegacySQL = False,我收到以下错误

raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/bigquery/v2/projects/************* 
returned "Encountered " "]" "[OFFSET(6)] "" at line 7, column 217. Was expecting: "END" ...">

感谢任何帮助。

【问题讨论】:

  • 我现在试过了,它对我有用。你能在这里添加你的代码吗?

标签: python google-bigquery


【解决方案1】:

根据错误消息,查询似乎正在使用旧版 SQL 执行。当我尝试使用旧版 SQL 执行此操作时,我看到了相同的消息,例如:

SELECT
  CASE s
    WHEN 'first' THEN SPLIT(arr, ',')[OFFSET(0)]
    WHEN 'second' THEN SPLIT(arr, ',')[OFFSET(1)]
    ELSE NULL
  END AS val
FROM (SELECT '1,2' AS arr, 'second' AS s);

Error: Encountered " "]" "[OFFSET(0)] "" at line 1, column 48. Was expecting: "END" ...

编辑:Enabling Standard SQL 链接的示例不正确。选项不是useLegacySQL,而是useLegacySqlql 小写)。跟踪问题在https://code.google.com/p/google-bigquery/issues/detail?id=701

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2018-09-10
    相关资源
    最近更新 更多