# `Ch.Types`
[🔗](https://github.com/plausible/ch/blob/v0.8.3/lib/ch/types.ex#L1)

Helpers to turn ClickHouse types into Elixir terms for easier processing.

# `array`

Helper for `Array(T)` ClickHouse type:

    iex> array(u64())
    {:array, :u64}

    iex> to_string(encode(array(u64())))
    "Array(UInt64)"

    iex> decode("Array(UInt64)")
    array(u64())

# `boolean`

Helper for `Bool` ClickHouse type:

    iex> boolean()
    :boolean

    iex> encode(boolean())
    "Bool"

    iex> decode("Bool")
    boolean()

# `date32`

Helper for `Date32` ClickHouse type:

    iex> date32()
    :date32

    iex> encode(date32())
    "Date32"

    iex> decode("Date32")
    date32()

# `date`

Helper for `Date` ClickHouse type:

    iex> date()
    :date

    iex> encode(date())
    "Date"

    iex> decode("Date")
    date()

# `datetime64`

Helper for `DateTime64(precision)` ClickHouse type:

    iex> datetime64(3)
    {:datetime64, 3}

    iex> to_string(encode(datetime64(3)))
    "DateTime64(3)"

    iex> decode("DateTime64(3)")
    datetime64(3)

# `datetime64`

Helper for `DateTime64(precision, timezone)` ClickHouse type:

    iex> datetime64(3, "UTC")
    {:datetime64, 3, "UTC"}

    iex> to_string(encode(datetime64(3, "UTC")))
    "DateTime64(3, 'UTC')"

    iex> decode("DateTime64(3, 'UTC')")
    datetime64(3, "UTC")

# `datetime`

Helper for `DateTime` ClickHouse type:

    iex> datetime()
    :datetime

    iex> to_string(encode(datetime()))
    "DateTime"

    iex> decode("DateTime")
    datetime()

# `datetime`

Helper for `DateTime(timezone)` ClickHouse type:

    iex> datetime("Europe/Vienna")
    {:datetime, "Europe/Vienna"}

    iex> to_string(encode(datetime("UTC")))
    "DateTime('UTC')"

    iex> decode("DateTime('UTC')")
    datetime("UTC")

# `decimal32`

Helper for `Decimal32(S)` ClickHouse type:

    iex> decimal32(4)
    {:decimal32, 4}

    iex> to_string(encode(decimal32(4)))
    "Decimal(9, 4)"

    iex> decode("Decimal32(4)")
    {:decimal32, 4}

# `decimal64`

Helper for `Decimal64(S)` ClickHouse type:

    iex> decimal64(4)
    {:decimal64, 4}

    iex> to_string(encode(decimal64(4)))
    "Decimal(18, 4)"

    iex> decode("Decimal64(4)")
    {:decimal64, 4}

# `decimal128`

Helper for `Decimal128(S)` ClickHouse type:

    iex> decimal128(4)
    {:decimal128, 4}

    iex> to_string(encode(decimal128(4)))
    "Decimal(38, 4)"

    iex> decode("Decimal128(4)")
    {:decimal128, 4}

# `decimal256`

Helper for `Decimal256(S)` ClickHouse type:

    iex> decimal256(4)
    {:decimal256, 4}

    iex> to_string(encode(decimal256(4)))
    "Decimal(76, 4)"

    iex> decode("Decimal256(4)")
    {:decimal256, 4}

# `decimal`

Helper for `Decimal(P, S)` ClickHouse type:

    iex> decimal(18, 4)
    {:decimal, 18, 4}

    iex> to_string(encode(decimal(18, 4)))
    "Decimal(18, 4)"

    iex> decode("Decimal(18, 4)")
    decimal(18, 4)

# `decode`

Decodes a ClickHouse type into an intermediary Elixir term.

    iex> decode("String")
    :string

    iex> decode("Array(String)")
    {:array, :string}

    iex> decode("Enum8('hello' = 1, 'world' = 2)")
    {:enum8, [{"hello", 1}, {"world", 2}]}

    iex> decode("Nullable(Decimal(18, 4))")
    {:nullable, {:decimal, 18, 4}}

# `encode`

Encodes a type from Elixir atom / tuple to proper ClickHouse name.

    iex> encode(:string)
    "String"

    iex> IO.iodata_to_binary(encode({:nullable, :i8}))
    "Nullable(Int8)"

# `enum8`

Helper for `Enum8` ClickHouse type:

    iex> enum8([{"hello", 1}, {"world", 2}])
    {:enum8, [{"hello", 1}, {"world", 2}]}

    iex> to_string(encode(enum8([{"hello", 1}, {"world", 2}])))
    "Enum8('hello' = 1, 'world' = 2)"

    iex> decode("Enum8('hello' = 1, 'world' = 2)")
    enum8([{"hello", 1}, {"world", 2}])

# `enum16`

Helper for `Enum16` ClickHouse type:

    iex> enum16([{"hello", 1}, {"world", 2}])
    {:enum16, [{"hello", 1}, {"world", 2}]}

    iex> to_string(encode(enum16([{"hello", 1}, {"world", 2}])))
    "Enum16('hello' = 1, 'world' = 2)"

    iex> decode("Enum16('hello' = 1, 'world' = 2)")
    enum16([{"hello", 1}, {"world", 2}])

# `f32`

Helper for `Float32` ClickHouse type:

    iex> f32()
    :f32

    iex> encode(f32())
    "Float32"

    iex> decode("Float32")
    f32()

# `f64`

Helper for `Float64` ClickHouse type:

    iex> f64()
    :f64

    iex> encode(f64())
    "Float64"

    iex> decode("Float64")
    f64()

# `fixed_string`

Helper for `FixedString(n)` ClickHouse type:

    iex> fixed_string(3)
    {:fixed_string, 3}

    iex> to_string(encode(fixed_string(16)))
    "FixedString(16)"

    iex> decode("FixedString(16)")
    fixed_string(16)

# `i8`

Helper for `Int8` ClickHouse type:

    iex> i8()
    :i8

    iex> encode(i8())
    "Int8"

    iex> decode("Int8")
    i8()

# `i16`

Helper for `Int16` ClickHouse type:

    iex> i16()
    :i16

    iex> encode(i16())
    "Int16"

    iex> decode("Int16")
    i16()

# `i32`

Helper for `Int32` ClickHouse type:

    iex> i32()
    :i32

    iex> encode(i32())
    "Int32"

    iex> decode("Int32")
    i32()

# `i64`

Helper for `Int64` ClickHouse type:

    iex> i64()
    :i64

    iex> encode(i64())
    "Int64"

    iex> decode("Int64")
    i64()

# `i128`

Helper for `Int128` ClickHouse type:

    iex> i128()
    :i128

    iex> encode(i128())
    "Int128"

    iex> decode("Int128")
    i128()

# `i256`

Helper for `Int256` ClickHouse type:

    iex> i256()
    :i256

    iex> encode(i256())
    "Int256"

    iex> decode("Int256")
    i256()

# `ipv4`

Helper for `IPv4` ClickHouse type:

    iex> ipv4()
    :ipv4

    iex> encode(ipv4())
    "IPv4"

    iex> decode("IPv4")
    ipv4()

# `ipv6`

Helper for `IPv6` ClickHouse type:

    iex> ipv6()
    :ipv6

    iex> encode(ipv6())
    "IPv6"

    iex> decode("IPv6")
    ipv6()

# `json`

Helper for `JSON` ClickHouse type:

    iex> json()
    :json

    iex> encode(json())
    "JSON"

    iex> decode("JSON")
    json()

# `low_cardinality`

Helper for `LowCardinality(T)` ClickHouse type:

    iex> low_cardinality(string())
    {:low_cardinality, :string}

    iex> to_string(encode(low_cardinality(string())))
    "LowCardinality(String)"

    iex> decode("LowCardinality(String)")
    low_cardinality(string())

# `map`

Helper for `Map(K, V)` ClickHouse type:

    iex> map(string(), array(string()))
    {:map, :string, {:array, :string}}

    iex> to_string(encode(map(string(), array(string()))))
    "Map(String, Array(String))"

    iex> decode("Map(String, Array(String))")
    map(string(), array(string()))

# `multipolygon`

Helper for `MultiPolygon` ClickHouse type:

    iex> multipolygon()
    :multipolygon

    iex> encode(multipolygon())
    "MultiPolygon"

    iex> decode("MultiPolygon")
    multipolygon()

# `nothing`

Helper for `Nothing` ClickHouse type:

    iex> nothing()
    :nothing

    iex> encode(nothing())
    "Nothing"

    iex> decode("Nothing")
    nothing()

# `nullable`

Helper for `Nullable(T)` ClickHouse type:

    iex> nullable(array(boolean()))
    {:nullable, {:array, :boolean}}

    iex> to_string(encode(nullable(array(boolean()))))
    "Nullable(Array(Bool))"

    iex> decode("Nullable(Array(Bool))")
    nullable(array(boolean()))

# `point`

Helper for `Point` ClickHouse type:

    iex> point()
    :point

    iex> encode(point())
    "Point"

    iex> decode("Point")
    point()

# `polygon`

Helper for `Polygon` ClickHouse type:

    iex> polygon()
    :polygon

    iex> encode(polygon())
    "Polygon"

    iex> decode("Polygon")
    polygon()

# `ring`

Helper for `Ring` ClickHouse type:

    iex> ring()
    :ring

    iex> encode(ring())
    "Ring"

    iex> decode("Ring")
    ring()

# `simple_aggregate_function`

Helper for `SimpleAggregateFunction(name, type)` ClickHouse type:

    iex> simple_aggregate_function("any", u8())
    {:simple_aggregate_function, "any", :u8}

    iex> to_string(encode(simple_aggregate_function("any", u8())))
    "SimpleAggregateFunction(any, UInt8)"

    iex> decode("SimpleAggregateFunction(any, UInt8)")
    simple_aggregate_function("any", u8())

# `string`

Helper for `String` ClickHouse type:

    iex> string()
    :string

    iex> encode(string())
    "String"

    iex> decode("String")
    string()

# `time64`

Helper for `Time64(precision)` ClickHouse type:

    iex> time64(3)
    {:time64, 3}

    iex> to_string(encode(time64(3)))
    "Time64(3)"

    iex> decode("Time64(3)")
    time64(3)

# `time`

Helper for `Time` ClickHouse type:

    iex> time()
    :time

    iex> encode(time())
    "Time"

    iex> decode("Time")
    time()

# `tuple`

Helper for `Tuple(T1, T2, ...)` ClickHouse type:

    iex> tuple([u64(), array(string())])
    {:tuple, [:u64, {:array, :string}]}

    iex> to_string(encode(tuple([u64(), array(string())])))
    "Tuple(UInt64, Array(String))"

    iex> decode("Tuple(UInt64, Array(String))")
    tuple([u64(), array(string())])

# `u8`

Helper for `UInt8` ClickHouse type:

    iex> u8()
    :u8

    iex> encode(u8())
    "UInt8"

    iex> decode("UInt8")
    u8()

# `u16`

Helper for `UInt16` ClickHouse type:

    iex> u16()
    :u16

    iex> encode(u16())
    "UInt16"

    iex> decode("UInt16")
    u16()

# `u32`

Helper for `UInt32` ClickHouse type:

    iex> u32()
    :u32

    iex> encode(u32())
    "UInt32"

    iex> decode("UInt32")
    u32()

# `u64`

Helper for `UInt64` ClickHouse type:

    iex> u64()
    :u64

    iex> encode(u64())
    "UInt64"

    iex> decode("UInt64")
    u64()

# `u128`

Helper for `UInt128` ClickHouse type:

    iex> u128()
    :u128

    iex> encode(u128())
    "UInt128"

    iex> decode("UInt128")
    u128()

# `u256`

Helper for `UInt256` ClickHouse type:

    iex> u256()
    :u256

    iex> encode(u256())
    "UInt256"

    iex> decode("UInt256")
    u256()

# `uuid`

Helper for `UUID` ClickHouse type:

    iex> uuid()
    :uuid

    iex> encode(uuid())
    "UUID"

    iex> decode("UUID")
    uuid()

# `variant`

Helper for `Variant(T1, T2, ...)` ClickHouse type:

    iex> variant([u64(), string(), array(u64())])
    {:variant, [{:array, :u64}, :string, :u64]}

    iex> to_string(encode(variant([u64(), string(), array(u64())])))
    "Variant(Array(UInt64), String, UInt64)"

    iex> decode("Variant(UInt64, String, Array(UInt64))")
    variant([array(u64()), u64(), string()])

---

*Consult [api-reference.md](api-reference.md) for complete listing*
