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

  1. CREATE TABLE hits (url String, from IPv6) ENGINE = MergeTree() ORDER BY url;
  2. DESCRIBE TABLE hits;
  1. ┌─name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┐
  2. url String
  3. from IPv6
  4. └──────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┘

OR you can use IPv6 domain as a key:

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

IPv6 domain supports custom input as IPv6-strings:

  1. INSERT INTO hits (url, from) VALUES ('https://wikipedia.org', '2a02:aa08:e000:3100::2')('https://clickhouse.tech', '2001:44c8:129:2632:33:0:252:2')('https://clickhouse.tech/docs/en/', '2a02:e980:1e::1');
  2. SELECT * FROM hits;
  1. ┌─url────────────────────────────────┬─from──────────────────────────┐
  2. https://clickhouse.tech │ 2001:44c8:129:2632:33:0:252:2 │
  3. https://clickhouse.tech/docs/en/ │ 2a02:e980:1e::1 │
  4. https://wikipedia.org │ 2a02:aa08:e000:3100::2 │
  5. └────────────────────────────────────┴───────────────────────────────┘

Values are stored in compact binary form:

  1. SELECT toTypeName(from), hex(from) FROM hits LIMIT 1;
  1. ┌─toTypeName(from)─┬─hex(from)────────────────────────┐
  2. IPv6 200144C8012926320033000002520002
  3. └──────────────────┴──────────────────────────────────┘

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:

  1. SELECT toTypeName(s), IPv6NumToString(from) as s FROM hits LIMIT 1;
  1. ┌─toTypeName(IPv6NumToString(from))─┬─s─────────────────────────────┐
  2. String 2001:44c8:129:2632:33:0:252:2
  3. └───────────────────────────────────┴───────────────────────────────┘

Or cast to a FixedString(16) value:

  1. SELECT toTypeName(i), CAST(from as FixedString(16)) as i FROM hits LIMIT 1;
  1. ┌─toTypeName(CAST(from, 'FixedString(16)'))─┬─i───────┐
  2. FixedString(16) ���
  3. └───────────────────────────────────────────┴─────────┘

Original article