Skip to main content

ipv6

IPv6​

IPv6 is a domain based on FixedString(16) type and serves as a typed replacement for storing IPv6 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 IPv6) ENGINE = MergeTree() ORDER BY url;

DESCRIBE TABLE hits;
β”Œβ”€name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┐
β”‚ url β”‚ String β”‚ β”‚ β”‚ β”‚ β”‚
β”‚ from β”‚ IPv6 β”‚ β”‚ β”‚ β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

OR you can use IPv6 domain as a key:

CREATE TABLE hits (url String, from IPv6) ENGINE = MergeTree() ORDER BY from;

IPv6 domain supports custom input as IPv6-strings:

INSERT INTO hits (url, from) VALUES ('https://wikipedia.org', '2a02:aa08:e000:3100::2')('https://clickhouse.com', '2001:44c8:129:2632:33:0:252:2')('https://clickhouse.com/docs/en/', '2a02:e980:1e::1');

SELECT * FROM hits;
β”Œβ”€url────────────────────────────────┬─from──────────────────────────┐
β”‚ https://clickhouse.com β”‚ 2001:44c8:129:2632:33:0:252:2 β”‚
β”‚ https://clickhouse.com/docs/en/ β”‚ 2a02:e980:1e::1 β”‚
β”‚ https://wikipedia.org β”‚ 2a02:aa08:e000:3100::2 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Values are stored in compact binary form:

SELECT toTypeName(from), hex(from) FROM hits LIMIT 1;
β”Œβ”€toTypeName(from)─┬─hex(from)────────────────────────┐
β”‚ IPv6 β”‚ 200144C8012926320033000002520002 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Domain values are not implicitly convertible to types other than FixedString(16). If you want to convert IPv6 value to a string, you have to do that explicitly with IPv6NumToString() function:

SELECT toTypeName(s), IPv6NumToString(from) as s FROM hits LIMIT 1;
β”Œβ”€toTypeName(IPv6NumToString(from))─┬─s─────────────────────────────┐
β”‚ String β”‚ 2001:44c8:129:2632:33:0:252:2 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Or cast to a FixedString(16) value:

SELECT toTypeName(i), CAST(from as FixedString(16)) as i FROM hits LIMIT 1;
β”Œβ”€toTypeName(CAST(from, 'FixedString(16)'))─┬─i───────┐
β”‚ FixedString(16) β”‚ οΏ½οΏ½οΏ½ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Original article