ClickHouse 中的时间比较

Time comparison in ClickHouse

也许我遗漏了一些简单的东西,但我无法让时间过滤发挥作用。

这是我的示例查询:

select toTimeZone(ts, 'Etc/GMT+2') as z
from (select toDateTime('2019-08-31 20:35:00') AS ts)
where z > '2019-08-31 20:34:00'

我希望有 0 个结果,但得到:

2019-08-31T18:35:00+00:00

这是一个错误,还是我误用了 toTimeZone() 函数?

谢谢!

ClickHouse 将 DateTime 存储为 Unix 时间戳 - 换句话说没有时区。 但是在执行 sql-query 时会考虑时区:

SELECT
    toDateTime('2019-08-31 20:35:00', 'UTC') AS origin_date,

    toTimeZone(origin_date, 'Etc/GMT+2') AS d1,
    toTypeName(d1) AS type1,
    toUnixTimestamp(d1) AS t1,

    toTimeZone(origin_date, 'UTC') AS d2,
    toTypeName(d2) AS type2,
    toUnixTimestamp(d2) AS t2
FORMAT Vertical

Row 1:
──────
origin_date: 2019-08-31 20:35:00

d1:          2019-08-31 18:35:00
type1:       DateTime('Etc/GMT+2')
t1:          1567283700 # <-- t1 == t2

d2:          2019-08-31 20:35:00
type2:       DateTime('UTC')
t2:          1567283700 # <-- t1 == t2

您的查询工作正常。

至'reset the timezone' of z-date可以这样使用:

SELECT toDateTime(toString(toTimeZone(ts, 'Etc/GMT+2'))) AS z
FROM
(
    SELECT toDateTime('2019-08-31 20:35:00') AS ts
)
WHERE z > '2019-08-31 20:34:00'

TZ 是 属性 类型而不是值

DESCRIBE TABLE
(
    SELECT
        toTimeZone(toDateTime('2019-08-31 20:35:00'), 'Etc/GMT+2') AS x,
        toDateTime('2019-08-31 20:35:00') AS y
)

┌─name─┬─type──────────────────┬─
│ x    │ DateTime('Etc/GMT+2') │
│ y    │ DateTime              │
└──────┴───────────────────────┴─


SELECT toTimeZone(ts, 'Etc/GMT+2') AS z
FROM
(
    SELECT toDateTime('2019-08-31 20:35:00') AS ts
)
WHERE z > toDateTime('2019-08-31 20:34:00', 'Etc/GMT+2')

Ok.

0 rows in set. Elapsed: 0.002 sec.