> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentation for Capnproto

# CapnProto

export const CloudNotSupportedBadge = () => {
  return <div className="cloudNotSupportedBadge">
            <div className="cloudNotSupportedIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.5" d="M6.33366 12.6666L12.3739 12.6667C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00003 12.3739 8.00003C12.3739 8.00003 12.3337 7.66659 12.0003 7.33325M10.667 5.33322C8.00033 2.33325 4.45395 4.78537 4.14195 6.68203C2.55728 6.7627 1.29395 8.06203 1.29395 9.6667C1.29395 11.3234 2.66699 12.6666 4.00033 12.6666" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.5" d="M2.66699 14L12.0003 4.66663" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>

        </div>
            Not supported in ClickHouse Cloud
        </div>;
};

| Input | Output | Alias |
| ----- | ------ | ----- |
| ✔     | ✔      |       |

<h2 id="description">
  Description
</h2>

The `CapnProto` format is a binary message format similar to the [`Protocol Buffers`](https://developers.google.com/protocol-buffers/) format and [Thrift](https://en.wikipedia.org/wiki/Apache_Thrift), but not like [JSON](/reference/formats/JSON/JSON) or [MessagePack](https://msgpack.org/).
CapnProto messages are strictly typed and not self-describing, meaning they need an external schema description. The schema is applied on the fly and cached for each query.

See also [Format Schema](/reference/formats#formatschema).

<h2 id="data_types-matching-capnproto">
  Data types matching
</h2>

The table below shows supported data types and how they match ClickHouse [data types](/reference/data-types) in `INSERT` and `SELECT` queries.

| CapnProto data type (`INSERT`)                       | ClickHouse data type                                                                                                              | CapnProto data type (`SELECT`)                       |
| ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
| `UINT8`, `BOOL`                                      | [UInt8](/reference/data-types/int-uint)                                                                                           | `UINT8`                                              |
| `INT8`                                               | [Int8](/reference/data-types/int-uint)                                                                                            | `INT8`                                               |
| `UINT16`                                             | [UInt16](/reference/data-types/int-uint), [Date](/reference/data-types/date)                                                      | `UINT16`                                             |
| `INT16`                                              | [Int16](/reference/data-types/int-uint)                                                                                           | `INT16`                                              |
| `UINT32`                                             | [UInt32](/reference/data-types/int-uint), [DateTime](/reference/data-types/datetime)                                              | `UINT32`                                             |
| `INT32`                                              | [Int32](/reference/data-types/int-uint), [Decimal32](/reference/data-types/decimal)                                               | `INT32`                                              |
| `UINT64`                                             | [UInt64](/reference/data-types/int-uint)                                                                                          | `UINT64`                                             |
| `INT64`                                              | [Int64](/reference/data-types/int-uint), [DateTime64](/reference/data-types/datetime), [Decimal64](/reference/data-types/decimal) | `INT64`                                              |
| `FLOAT32`                                            | [Float32](/reference/data-types/float)                                                                                            | `FLOAT32`                                            |
| `FLOAT64`                                            | [Float64](/reference/data-types/float)                                                                                            | `FLOAT64`                                            |
| `TEXT, DATA`                                         | [String](/reference/data-types/string), [FixedString](/reference/data-types/fixedstring)                                          | `TEXT, DATA`                                         |
| `union(T, Void), union(Void, T)`                     | [Nullable(T)](/reference/data-types/date)                                                                                         | `union(T, Void), union(Void, T)`                     |
| `ENUM`                                               | [Enum(8/16)](/reference/data-types/enum)                                                                                          | `ENUM`                                               |
| `LIST`                                               | [Array](/reference/data-types/array)                                                                                              | `LIST`                                               |
| `STRUCT`                                             | [Tuple](/reference/data-types/tuple)                                                                                              | `STRUCT`                                             |
| `UINT32`                                             | [IPv4](/reference/data-types/ipv4)                                                                                                | `UINT32`                                             |
| `DATA`                                               | [IPv6](/reference/data-types/ipv6)                                                                                                | `DATA`                                               |
| `DATA`                                               | [Int128/UInt128/Int256/UInt256](/reference/data-types/int-uint)                                                                   | `DATA`                                               |
| `DATA`                                               | [Decimal128/Decimal256](/reference/data-types/decimal)                                                                            | `DATA`                                               |
| `STRUCT(entries LIST(STRUCT(key Key, value Value)))` | [Map](/reference/data-types/map)                                                                                                  | `STRUCT(entries LIST(STRUCT(key Key, value Value)))` |

* Integer types can be converted into each other during input/output.
* For working with `Enum` in CapnProto format use the [format\_capn\_proto\_enum\_comparising\_mode](/reference/settings/formats#format_capn_proto_enum_comparising_mode) setting.
* Arrays can be nested and can have a value of the `Nullable` type as an argument. `Tuple` and `Map` types also can be nested.

<h2 id="example-usage">
  Example usage
</h2>

<h3 id="inserting-and-selecting-data-capnproto">
  Inserting and selecting data
</h3>

You can insert CapnProto data from a file into ClickHouse table by the following command:

```bash theme={null}
$ cat capnproto_messages.bin | clickhouse-client --query "INSERT INTO test.hits SETTINGS format_schema = 'schema:Message' FORMAT CapnProto"
```

Where the `schema.capnp` looks like this:

```capnp theme={null}
struct Message {
  SearchPhrase @0 :Text;
  c @1 :Uint64;
}
```

You can select data from a ClickHouse table and save them into some file in the `CapnProto` format using the following command:

```bash theme={null}
$ clickhouse-client --query = "SELECT * FROM test.hits FORMAT CapnProto SETTINGS format_schema = 'schema:Message'"
```

<h3 id="using-autogenerated-capn-proto-schema">
  Using autogenerated schema
</h3>

If you don't have an external `CapnProto` schema for your data, you can still output/input data in `CapnProto` format using autogenerated schema.

For example:

```sql theme={null}
SELECT * FROM test.hits 
FORMAT CapnProto 
SETTINGS format_capn_proto_use_autogenerated_schema=1
```

In this case, ClickHouse will autogenerate CapnProto schema according to the table structure using function [structureToCapnProtoSchema](/reference/functions/regular-functions/other-functions#structureToCapnProtoSchema) and will use this schema to serialize data in CapnProto format.

You can also read CapnProto file with autogenerated schema (in this case the file must be created using the same schema):

```bash theme={null}
$ cat hits.bin | clickhouse-client --query "INSERT INTO test.hits SETTINGS format_capn_proto_use_autogenerated_schema=1 FORMAT CapnProto"
```

<h2 id="format-settings">
  Format settings
</h2>

The setting [`format_capn_proto_use_autogenerated_schema`](/reference/settings/formats#format_capn_proto_use_autogenerated_schema) is enabled by default and is applicable if [`format_schema`](/reference/formats#formatschema) is not set.

You can also save the autogenerated schema to a file during input/output using setting [`output_format_schema`](/reference/settings/formats#output_format_schema).

For example:

```sql theme={null}
SELECT * FROM test.hits 
FORMAT CapnProto 
SETTINGS 
    format_capn_proto_use_autogenerated_schema=1,
    output_format_schema='path/to/schema/schema.capnp'
```

In this case, the autogenerated `CapnProto` schema will be saved in file `path/to/schema/schema.capnp`.
