ipv4
IPv4β
IPv4
is a domain based on UInt32
type and serves as a typed replacement for storing IPv4 values. It provides compact storage with the human-friendly input-output format and column type information on inspection.
Basic Usageβ
CREATE TABLE hits (url String, from IPv4) ENGINE = MergeTree() ORDER BY url;
DESCRIBE TABLE hits;
ββnameββ¬βtypeββββ¬βdefault_typeββ¬βdefault_expressionββ¬βcommentββ¬βcodec_expressionββ
β url β String β β β β β
β from β IPv4 β β β β β
ββββββββ΄βββββββββ΄βββββββββββββββ΄βββββββββββββββββββββ΄ββββββββββ΄βββββββββββββββββββ
OR you can use IPv4 domain as a key:
CREATE TABLE hits (url String, from IPv4) ENGINE = MergeTree() ORDER BY from;
IPv4
domain supports custom input format as IPv4-strings:
INSERT INTO hits (url, from) VALUES ('https://wikipedia.org', '116.253.40.133')('https://clickhouse.com', '183.247.232.58')('https://clickhouse.com/docs/en/', '116.106.34.242');
SELECT * FROM hits;
ββurlβββββββββββββββββββββββββββββββββ¬βββββββββββfromββ
β https://clickhouse.com/docs/en/ β 116.106.34.242 β
β https://wikipedia.org β 116.253.40.133 β
β https://clickhouse.com β 183.247.232.58 β
ββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββ
Values are stored in compact binary form:
SELECT toTypeName(from), hex(from) FROM hits LIMIT 1;
ββtoTypeName(from)ββ¬βhex(from)ββ
β IPv4 β B7F7E83A β
ββββββββββββββββββββ΄ββββββββββββ
Domain values are not implicitly convertible to types other than UInt32
.
If you want to convert IPv4
value to a string, you have to do that explicitly with IPv4NumToString()
function:
SELECT toTypeName(s), IPv4NumToString(from) as s FROM hits LIMIT 1;
ββtoTypeName(IPv4NumToString(from))ββ¬βsβββββββββββββββ
β String β 183.247.232.58 β
βββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββ
Or cast to a UInt32
value:
SELECT toTypeName(i), CAST(from as UInt32) as i FROM hits LIMIT 1;
ββtoTypeName(CAST(from, 'UInt32'))ββ¬ββββββββββiββ
β UInt32 β 3086477370 β
ββββββββββββββββββββββββββββββββββββ΄βββββββββββββ