clickhouse 日期函式無效報錯問題處理

2021-10-11 18:53:30 字數 3258 閱讀 1393

create

table test.user_action_log

(`event_time`

datetime,`

action

` string,

`user_id` string,

`school_id` string,

`uuid` string,

`student_id` string,

`date_key` string

)engine

= mergetree(

)partition

by date_key

primary

key user_id

order

bysettings index_granularity =

8192

;

現象:

當我們使用指定日期查詢時發現可以查詢出結果:

localhost :)

select

count(1

)from test.user_action_log where date_key =

'2020-12-04'

;select

count(1

)from test.user_action_log

where date_key =

'2020-12-04'

query id: cab1314f-

25e2-

4328

-b865-

17ede5f0c2fc

┌──count(1

)─┐│ 105338324 │

└───────────┘

1rows

inset

. elapsed: 0.002 sec.

但是我們發現在使用日期加減函式時卻發生報錯:

localhost :)

select

count(1

)from test.user_action_log where date_key = addsdays(today(),

6);select

count(1

)from test.user_action_log

where date_key = addsdays(today(),

-6)query id: 8d757fd0-

6cbc-

4b95-

8b32-f87f78850db6

received exception from server (version 20.11

.3):

code: 46. db::exception: received from

10.12

.80.130:9000. db::exception: unknown function addsdays. maybe you meant: [

'adddays'

,'addyears'

]: while processing select

count(1

)from test.user_action_log where date_key = addsdays(today(),

-6).

0rows

inset

. elapsed: 0.006 sec.

經過排查我們發現建表時字段使用了string型別,而addsdays(today(), 6)生成的資料型別是date型別。這樣我們就有了兩種解決的思路。

一種是將where條件中的日期欄位強轉為date:

localhost :)

select

count(1

)from test.user_action_log where cast(date_key as

date

)= adddays(today(),

-6);

select

count(1

)from test.user_action_log

where cast(date_key,

'date'

)= adddays(today(),

-6)query id: d0fb99a6-

023f-

403a-

832b-

629ce6011b53

┌──count(1

)─┐│ 105338324 │

└───────────┘

1rows

inset

. elapsed: 0.003 sec.

另一種是將where條件中的日期欄位強轉為date:

localhost :)

select

count(1

)from test.user_action_log where date_key = cast(adddays(today(),

-6)as string)

;select

count(1

)from test.user_action_log

where date_key = cast(adddays(today(),

-6),

'string'

)query id: 20cf4d53-

07b9-

47cd-

9b66-

51356f6d625a

┌──count(1

)─┐│ 105338324 │

└───────────┘

1rows

inset

. elapsed: 0.003 sec.

兩種方式相比差別就是在where條件中在操作符前邊新增運算會導致索引失效,也就是where cast(date_key, 'date') = adddays(today(), -6)會導致date_key索引失效,也就是該任務在執行時不在走索引。

我們發現引起這種問題的根本原因是建表時日期字段使用錯誤,使用為string型別而不是date型別導致,這就要求我們在建表一定要慎重使用字段型別,尤其是日期型別。

click house函式的使用

格式 hdfs url,format,struct select toint32ornull id name jobfrom hdfs hdfs linux01 8020 doit18 user2.csv csv id string name string,job string 格式 file pa...

clickhouse簡單使用 函式整理

一 ddl 如果想按集群操作,需要借助zookeeper,在config.xml中新增配置 clickhouse task queue ddl 乙個節點建立表,會同步到各個節點 create table db.table on cluster cluster 新增 刪除 修改列 alter tabl...

十一 clickhouse取整函式

1.向下取整 floor x n select floor tofloat32 12.08098 2 12.08 floor tofloat32 12.2323 2 12.23 floor tofloat32 12.89788 1 10 floor tofloat32 12.09590 3 12.0...