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

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

OR you can use IPv4 domain as a key:

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

IPv4 domain supports custom input format as IPv4-strings:

  1. 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');
  2. SELECT * FROM hits;
  1. ┌─url────────────────────────────────┬───────────from─┐
  2. https://clickhouse.com/docs/en/ │ 116.106.34.242 │
  3. https://wikipedia.org │ 116.253.40.133 │
  4. https://clickhouse.com │ 183.247.232.58 │
  5. └────────────────────────────────────┴────────────────┘

Values are stored in compact binary form:

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

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:

  1. SELECT toTypeName(s), IPv4NumToString(from) as s FROM hits LIMIT 1;
  1. ┌─toTypeName(IPv4NumToString(from))─┬─s──────────────┐
  2. String 183.247.232.58
  3. └───────────────────────────────────┴────────────────┘

Or cast to a UInt32 value:

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