clickhouse的where中使用不确定性函数
需求
要实现一个脚本,删除clickhouse七天前的数据
SQL
找到了date_trunc函数,参考了 https://clickhouse.com/docs/en/sql-reference/functions/date-time-functions/
官方用法,比如SELECT now(), date_trunc('hour', now());
1 | |
Alias: dateTrunc.
Arguments
unit— The type of interval to truncate the result. String Literal. Possible values:secondminutehourdayweekmonthquarteryear
value— Date and time. DateTime or DateTime64.timezone— Timezone name for the returned value (optional). If not specified, the function uses the timezone of thevalueparameter. String.
执行:
ALTER table t ON CLUSTER c DELETE WHERE create_time < date_add(DAY, -7, date_trunc('day', now())))
报错了:ALTER UPDATE/ALTER DELETE statements must use only deterministic functions.
报错原因:因为clickhouse在where中使用不确定性函数,now() 函数在 ClickHouse 中也是不确定性函数。”date_add” 和 “date_trunc” 不是确定性函数。where后面不可以放上不确定性函数。
通过shell脚本实现
1 | |
clickhouse的where中使用不确定性函数
https://nrbackback.github.io/2023/02/21/clickhouse的where中使用不确定性函数/