为什么在窗口函数内部进行转换会返回不正确的类型?

时间:2016-04-28 09:55:25

标签: google-bigquery

以下结合窗口函数和强制转换的示例查询不会产生预期结果:

SELECT
  visitorid,
  LEAD(TIMESTAMP(date)) OVER (PARTITION BY visitorid) AS ts
FROM (
  SELECT
    "000001" AS visitorid,
    "2014-04-28" AS date,
    INTEGER(21) AS metric),
  (
  SELECT
    "000001" AS visitorid,
    "2014-04-29" AS date,
    INTEGER(42) AS metric),
  (
  SELECT
    "000002" AS visitorid,
    "2014-04-28" AS date,
    INTEGER(84) AS metric)
ORDER BY
  visitorid ASC

鉴于lead函数的性质,只有第一行包含visitorid和ts的条目。

The result

如果我们仔细观察列ts,它的类型是整数而不是时间戳。也就是说,我希望在第一行ts中看到“2014-04-29 00:00:00 UTC”而不是“1398729600000000”。这是否意味着转换与LEAD等窗口函数不兼容?

2 个答案:

答案 0 :(得分:2)

正如oulenz的回答所指出的那样,标准的SQL方言解决了这个问题,这里是示例查询的改编版本:

SELECT
  visitorid,
  LEAD(CAST(date AS TIMESTAMP)) OVER (PARTITION BY visitorid ORDER BY visitorid ASC) AS ts
FROM (
  SELECT
    AS STRUCT "000001" AS visitorid,
    "2014-04-28" AS date,
    21 AS metric UNION ALL
  SELECT
    AS STRUCT "000001" AS visitorid,
    "2014-04-29" AS date,
    42 AS metric UNION ALL
  SELECT
    AS STRUCT "000002" AS visitorid,
    "2014-04-28" AS date,
    84 AS metric )

结果如预期:

New SQL Reference result

答案 1 :(得分:1)

known issue时间戳被转换为具有一些窗口函数的整数,但是当我提交该报告时,lead是对我有用的函数之一。

您可以在超选中将整数转换回时间戳。该问题已在昨天以alpha版本发布的standard sql dialect中得到解决。 (它似乎还不适合我。)