DBIO-ClickHouse

 view release on metacpan or  search on metacpan

docs/adr/0001-mysql-wire-protocol-transport.md  view on Meta::CPAN

# ADR 0001 — Transport is the MySQL wire protocol, over DBD::MariaDB / DBD::mysql

- Status: accepted
- Date: 2026-07-20

## Context

ClickHouse is a column-oriented OLAP database. It exposes several client
protocols: its native TCP protocol (port 9000), an HTTP interface (port 8123),
and — the one that matters here — the **MySQL wire protocol** (port 9004).

There is **no native `DBD::ClickHouse` on CPAN** (verified). Writing and
maintaining a native XS/FFI DBD against ClickHouse's native protocol is a large,
open-ended effort and is out of scope for a DBIO driver. DBIO drivers are thin
layers on top of an existing DBI driver; they do not implement the DBI transport
themselves.

ClickHouse's MySQL-wire endpoint lets any MySQL-protocol client connect. The
DBIO family already depends on two such DBI drivers for `dbio-mysql`:
`DBD::MariaDB` (bundles the MariaDB Connector/C) and `DBD::mysql`. Either can
talk to ClickHouse's port 9004.

## Decision

`DBIO::ClickHouse` rides the **MySQL wire protocol** via an existing MySQL-wire
DBI driver rather than a native `DBD::ClickHouse`:

1. `DBD::mysql` is the **recommended** transport (`recommends` in the
   cpanfile); `DBD::MariaDB` is **supported** (`recommends`) for servers whose
   config you control. This reverses the original choice (which mirrored
   `dbio-mysql`'s).
   *Revised by the Addendum (2026-08-05): the ranking is dropped, both DBDs
   are `recommends`, and which one is usable depends on whether you control
   the server config. Revised again by the Addendum (2026-08-17): the live
   measurements landed, `DBD::mysql` is the verified standard transport.*
2. The connection DSN is an ordinary MySQL-wire DSN pointed at port 9004, e.g.
   `dbi:MariaDB:database=default;host=127.0.0.1;port=9004`.
3. Identifier quoting is the backtick `` ` `` (MySQL-wire compatible), set on
   the storage via `sql_quote_char('`')`.

## Consequences

- **DSN-based driver detection cannot work.** Core's driver registry
  autodetects a storage class from the DSN's `dbi:<Driver>:` prefix. A
  ClickHouse DSN is `dbi:MariaDB:...` (or `dbi:mysql:...`), which is
  **indistinguishable from a real MySQL DSN** and resolves to the MySQL storage.
  There is therefore no autodetect route for ClickHouse. The storage **must** be
  pinned explicitly, either by `__PACKAGE__->load_components('ClickHouse')`
  (whose `connection()` sets `storage_type('+DBIO::ClickHouse::Storage')`) or by
  the `-ch` shortcut. This is documented prominently in the README, CLAUDE.md
  and the module POD.
- The `ClickHouse` driver name is still registered
  (`register_driver('ClickHouse' => ...)`) so the component / shortcut resolver
  and `apply_driver` work; it is simply never reached by *DSN* autodetection.
- Any MySQL feature the protocol advertises but ClickHouse does not actually
  implement (transactions, savepoints, `FOR UPDATE`, `RETURNING`,
  auto-increment) must be neutralised in the storage/SQLMaker rather than
  inherited blindly. See ADR 0002.
- The driver depends on ClickHouse's MySQL-wire endpoint being enabled
  (server config `mysql_port = 9004`).
- **`DBD::MariaDB` additionally requires a server-side setting to connect at
  all.** Found in the first live run (ClickHouse 25.3.14.14, 2026-07-26), not
  by reading docs. On connect, `DBD::MariaDB` unconditionally issues
  `SET character_set_server = 'utf8mb4'` — the statement is a hardcoded string
  in the compiled driver and there is no connect attribute that suppresses it.
  ClickHouse rejects a `SET` of any name it does not know unless that name
  begins with a registered custom-settings prefix, and ships with only `SQL_`:

      Code: 115. DB::Exception: Setting character_set_server is neither a
      builtin setting nor started with the prefix 'SQL_' registered for
      user-defined settings. (UNKNOWN_SETTING)

  The connection therefore fails outright against a stock server. The server
  must register the prefix, which turns those handshake settings into harmless
  user-defined ones:

      <clickhouse>
          <custom_settings_prefixes>SQL_,character_set_,collation_,sql_</custom_settings_prefixes>
      </clickhouse>

  It is read only at server **start** — `SYSTEM RELOAD CONFIG` does not pick it
  up. `maint/k8s/clickhouse.yaml` ships it as a ConfigMap. Note what this costs:
  the recommended DBD does not work against an unmodified ClickHouse, so this is
  a deployment prerequisite the driver cannot satisfy on its own, and it is not
  available on ClickHouse Cloud, which forbids custom prefixes. Whether
  `DBD::mysql` avoids the problem (it may send `SET NAMES` instead) is untested
  and tracked separately — if it does, the recommendation in this ADR should be
  reconsidered. *That question is answered as far as it can be answered without
  a server in the Addendum below.*
- No native-protocol-only features (native columnar block transport, etc.) are
  reachable over the MySQL wire. If those are ever wanted they would need a
  separate escape-hatch path; they are out of scope for this driver.

## Addendum (2026-08-05) — the ranking is dropped, not reversed

- Status: accepted
- Revises: Decision item 1
- Ticket: karr k15

### Why this was reopened

Decision item 1 ranked `DBD::MariaDB` first for one reason: `dbio-mysql` does.
That reason survived exactly until the first live run, which found the
first-ranked DBD cannot connect to a ClickHouse server whose config we do not
control — and on ClickHouse Cloud, cannot connect at all. Ranking a transport
above one that might work everywhere, on the strength of a sibling
distribution's habit, is not a decision worth keeping.

### What was measured, and how

The connect paths of both DBDs were read off the drivers themselves. **No
ClickHouse server was involved in this addendum** — every statement below is
about what the DBD sends, not about what ClickHouse answers. The one thing that
*is* a live measurement is the failure recorded in the Consequences above.

**`DBD::MariaDB` always sends the statement, on every connect.** In
`DBD-MariaDB-1.24/dbdimp.c` (the version installed here; the same calls are
visible in the compiled `MariaDB.so`, inside `mariadb_db_my_login`, after
`mysql_real_connect`):



( run in 1.563 second using v1.01-cache-2.11-cpan-eb134014566 )