BigQuery无法识别分区表谓词

时间:2019-09-09 12:53:26

标签: google-bigquery

我在timestamp列的BigQuery中有一个分区表,我希望提取过去96小时内发生的所有事件。

WITH events AS (
SELECT
    concat(module, '_', replace(lower(action), ' ', '_')) type,
    detail,
    cast(IF(id=0, null, id) as string) id,
    timestamp,
    userId,
    pageName,
FROM fe.logs l
WHERE l.timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 96 HOUR)
   AND devicetype in ('desktop', 'mobile', 'tablet')
   AND osname in ('Windows', 'Android', 'Mac OS', 'iOS'))
SELECT TO_JSON_STRING(e) payload
from events e

但我不断得到

Cannot query over table 'fe.logs' without a filter over column(s) 'timestamp' that can be used for partition elimination

我以为 WHERE l.timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 96 HOUR)将成为分区列上的有效过滤器。

为了完整起见,我从查询中删除了一些列名和WHERE条件,但都没有碰到timestamp列,因此我认为它们在这里并不重要。

E:实际上,我省略了(现在添加了)原始查询的另一部分,该部分将所有行都转换为JSON,以使其尽可能完整。

是否有一些特定的运算符或语法?

1 个答案:

答案 0 :(得分:0)

结果是,在缩短此问题的代码时,我省略了包含该错误的部分。做

WHERE l.timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 96 HOUR)
      AND predicate2
      OR predicate3

无效。

完整代码是

WITH events AS (
  SELECT
        concat(module, '_', replace(lower(action), ' ', '_')) type,
        detail,
        cast(IF(id=0, null, id) as string) id,
        timestamp,
        userId,
        pageName,
  FROM  fe.logs l
  WHERE l.timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 96 HOUR)
        AND devicetype in ('desktop', 'mobile', 'tablet')
        AND osname in ('Windows', 'Android', 'Mac OS', 'iOS')
        AND (module='bar' AND action='qux')
        OR (module='foo' AND action='baz') -- bug is here
)
SELECT TO_JSON_STRING(e) payload
from events e

在原始代码中,我把括号弄乱了,应该是

  WHERE l.timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 96 HOUR)
        AND devicetype in ('desktop', 'mobile', 'tablet')
        AND osname in ('Windows', 'Android', 'Mac OS', 'iOS')
        AND ((module='bar' AND action='qux')
          OR (module='foo' AND action='baz'))