六 ClickHouse函数
6.1 普通函数
6.1.1 类型转换函数
-
toInt8(expr)
— Results in theInt8
data type. -
toInt16(expr)
— Results in theInt16
data type. -
toInt32(expr)
— Results in theInt32
data type. -
toInt64(expr)
— Results in theInt64
data type. -
toInt128(expr)
— Results in theInt128
data type. -
toInt256(expr)
— Results in theInt256
data type.
SELECT toInt64(nan), toInt32(32), toInt16('16'), toInt8(8.8); ┌─────────toInt64(nan)─┬─toInt32(32)─┬─toInt16('16')─┬─toInt8(8.8)─┐ │ -9223372036854775808 │ 32 │ 16 │ 8 │ └──────────────────────┴─────────────┴───────────────┴─────────────┘
-
toUInt(8|16|32|64|256)OrZero
-
toUInt(8|16|32|64|256)OrNull
-
toFloat(32|64)
-
toFloat(32|64)OrZero
-
toFloat(32|64)OrNull
-
toDate
-
toDateOrZero
-
toDateOrNull
-
toDateTime
-
toDateTimeOrZero
-
toDateTimeOrNull
-
toDecimal(32|64|128|256)
toString
now() AS now_local, toString(now(), 'Asia/Yekaterinburg') AS now_yekat; ┌───────────now_local─┬─now_yekat───────────┐ │ 2016-06-15 00:11:21 │ 2016-06-15 02:11:21 │ └─────────────────────┴─────────────────────┘
-
CAST(x, T)
Arguments - `x` — Any type. - `T` — Destination type. String **Returned value**
SELECT '2016-06-15 23:00:00' AS timestamp, CAST(timestamp AS DateTime) AS datetime, CAST(timestamp AS Date) AS date, CAST(timestamp, 'String') AS string, CAST(timestamp, 'FixedString(22)') AS fixed_string; ┌─timestamp───────────┬────────────datetime─┬───────date─┬─string──────────────┬─fixed_string────────── │ 2016-06-15 23:00:00 │ 2016-06-15 23:00:00 │ 2016-06-15 │ 2016-06-15 23:00:00 │ 2016-06-15 23:00:00\0\0\0 │ └─────────────────────┴─────────────────────┴────────────┴─────────────────────┴───────────────────────
6.1.2 日期函数
SELECT toDateTime('2016-06-15 23:00:00') AS time, toDate(time) AS date_local, toDate(time, 'Asia/Yekaterinburg') AS date_yekat, toString(time, 'US/Samoa') AS time_samoa ┌────────────────time─┬─date_local─┬─date_yekat─┬─time_samoa──────────┐ │ 2016-06-15 23:00:00 │ 2016-06-15 │ 2016-06-16 │ 2016-06-15 09:00:00 │ └─────────────────────┴────────────┴────────────┴─────────────────────┘
-
toDate
-
toYear
-
toMonth
-
toHour
-
toMinute
-
toSecond
-
toUnixTimestamp
-
date_trunc 将时间截断 date_trunc(unit, value[, timezone])
second minute hour day week month quarter year SELECT now(), date_trunc('hour', now()); ┌───────────────now()─┬─date_trunc('hour', now())─┐ │ 2021-05-21 13:52:42 │ 2021-05-21 13:00:00 │ └─────────────────────┴───────────────────────────┘
-
date_add
-
date_add(unit, value, date) second minute hour day week month quarter year SELECT date_add(YEAR, 3, toDate('2018-01-01')); date_diff('unit', startdate, enddate, [timezone])
date_diff
-
date_sub
-
timestamp_add
-
timestamp_sub
-
toYYYYMM
-
toYYYYMMDD
-
toYYYYMMDDhhmmss
-
formatDateTime
%C | year divided by 100 and truncated to integer (00-99) | 20 |
---|---|---|
%d | day of the month, zero-padded (01-31) | 02 |
%D | Short MM/DD/YY date, equivalent to %m/%d/%y | 01/02/18 |
%e | day of the month, space-padded ( 1-31) | 2 |
%F | short YYYY-MM-DD date, equivalent to %Y-%m-%d | 2018-01-02 |
%G | four-digit year format for ISO week number, calculated from the week-based year [defined by the ISO 860 standard, normally useful only with %V | 2018 |
%g | two-digit year format, aligned to ISO 8601, abbreviated from four-digit notation | 18 |
%H | hour in 24h format (00-23) | 22 |
%I | hour in 12h format (01-12) | 10 |
%j | day of the year (001-366) | 002 |
%m | month as a decimal number (01-12) | 01 |
%M | minute (00-59) | 33 |
%n | new-line character (‘’) | |
%p | AM or PM designation | PM |
%Q | Quarter (1-4) | 1 |
%R | 24-hour HH:MM time, equivalent to %H:%M | 22:33 |
%S | second (00-59) | 44 |
%t | horizontal-tab character (’) | |
%T | ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S | 22:33:44 |
%u | ISO 8601 weekday as number with Monday as 1 (1-7) | 2 |
%V | ISO 8601 week number (01-53) | 01 |
%w | weekday as a decimal number with Sunday as 0 (0-6) | 2 |
%y | Year, last two digits (00-99) | 18 |
%Y | Year | 2018 |
%% | a % sign | % |
SELECT formatDateTime(now(), '%D') ┌─formatDateTime(now(), '%D')─┐ │ 05/21/21 │ └─────────────────────────────┘
-
FROM_UNIXTIME
SELECT FROM_UNIXTIME(423543535) ┌─FROM_UNIXTIME(423543535)─┐ │ 1983-06-04 10:58:55 │ └──────────────────────────┘
6.1.3 条件函数
-
if(exp1 , exp2,exp3)
-
multiIf()
drop table if exists tb_if; create table if not exists tb_if( uid Int16, name String , gender String )engine = TinyLog ; insert into tb_if values(1,'zss1','M') ; insert into tb_if values(2,'zss2','M') ; insert into tb_if values(3,'zss3','F') ; insert into tb_if values(4,'zss4','O') ; insert into tb_if values(5,'zss5','F') ; --------单条件判断--------- SELECT *, if(gender = 'M', '男', '女') FROM tb_if ┌─uid─┬─name─┬─gender─┬─if(equals(gender, 'M'), '男', '女')─┐ │ 1 │ zss1 │ M │ 男 │ │ 2 │ zss2 │ M │ 男 │ │ 3 │ zss3 │ F │ 女 │ │ 4 │ zss4 │ O │ 女 │ │ 5 │ zss5 │ F │ 女 │ ------------------------------------------------------------- 多条件判断 SELECT *, multiIf(gender = 'M', '男', gender = 'F', '女', '保密') AS sex FROM tb_if ┌─uid─┬─name─┬─gender─┬─sex──┐ │ 1 │ zss1 │ M │ 男 │ │ 2 │ zss2 │ M │ 男 │ │ 3 │ zss3 │ F │ 女 │ │ 4 │ zss4 │ O │ 保密 │ │ 5 │ zss5 │ F │ 女 │ └─────┴──────┴────────┴──────┘
6.1.6 其他
visitParamExtractRaw('{"abc":"\\n\\u0000"}', 'abc') = '"\\n\\u0000"'; visitParamExtractRaw('{"abc":{"def":[1,2,3]}}', 'abc') = '{"def":[1,2,3]}'; select JSONExtract('{"a":"hello","b":"tom","c":12}', 'Tuple(String,String,UInt8)') as kn; -- 元组函数 select tupleElement((1,2,3,4,66),5); -- BitMap 函数 -- bitmapBuild SELECT bitmapBuild([1, 2, 3, 4, 5]) AS res, toTypeName(res) ┌─res─┬─toTypeName(bitmapBuild([1, 2, 3, 4, 5]))─┐ │ │ AggregateFunction(groupBitmap, UInt8) │ └─────┴──────────────────────────────────────────┘ -- bitmapToArray SELECT bitmapToArray(bitmapBuild([1, 2, 3, 4, 5])) AS res; -- bitmapSubsetInRange SELECT bitmapToArray(bitmapSubsetInRange(bitmapBuild([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 100, 200, 500]), toUInt32(30), toUInt32(200))) AS res ┌─res───────────────┐ │ [30,31,32,33,100] │ └───────────────────┘ -- bitmapContains SELECT bitmapContains(bitmapBuild([1, 5, 7, 9]), toUInt32(9)) AS res ┌─res─┐ │ 1 │ └─────┘ -- bitmapHasAny 有任意一个元素 SELECT bitmapHasAny(bitmapBuild([1, 2, 3]), bitmapBuild([3, 4, 5])) AS res ┌─res─┐ │ 1 │ └───── -- bitmapHasAll 有任意一个元素 -- bitmapMin -- bitmapMax -- bitmapAnd 交集 -- bitmapOr 并集 -- bitmapAndnot差集