ClickHouse¶
ClickHouse is a (1) column-oriented OLAP database designed for real-time analytics. To use Metaxy with ClickHouse, configure ClickHouseMetadataStore. Versioning computations run natively in ClickHouse, making it well-suited for high-throughput production workloads.
- extremely fast
Installation¶
Metaxy's Versioning Struct Columns¶
Metaxy uses struct columns (metaxy_provenance_by_field, metaxy_data_version_by_field) to track field-level versioning. In Python world this corresponds to dict[str, str]. In ClickHouse, there are several options to represent these columns.
How ClickHouse Handles Structs¶
ClickHouse offers multiple approaches to represent Metaxy's structured versioning columns:
| Type | Description | Use Case |
|---|---|---|
Map(String, String) |
Native key-value map | Recommended for Metaxy because of dynamic keys |
JSON |
Native JSON with typed subcolumns | Less performant than Map(String, String) but more flexible than Nested |
Nested(field_1 String, ...) |
Static struct with named fields | More performant than Map(String, String) but keys are static |
Recommended: Map(String, String)
For Metaxy's metaxy_provenance_by_field and metaxy_data_version_by_field columns, use Map(String, String):
-
No migrations required when feature fields change
-
Good performance for key-value lookups
Special Map columns handling
Metaxy transforms its system columns (metaxy_provenance_by_field, metaxy_data_version_by_field):
-
Reading: System Map columns are converted into Ibis Structs (e.g.,
Struct[{"field_a": str, "field_b": str}]) -
Writing: If the input comes from Polars, then Polars Structs are converted into expected ClickHouse Map format
User-defined Map columns are not transformed. They remain as List[Struct[{"key": str, "value": str}]] (Arrow's Map representation). Make sure to use the right format when providing a Polars DataFrame for writing.
SQLAlchemy and Alembic Migrations¶
For SQLAlchemy and Alembic migrations support, use the clickhouse-sqlalchemy driver with the native protocol:
Use Native Clickhouse Protocol
The HTTP protocol has limited reflection support. Always use the native protocol (clickhouse+native://) for full SQLAlchemy/Alembic compatibility:
The ClickHouseMetadataStore.sqlalchemy_url property is tweaked to return the native connection string variant.
Alternative: ClickHouse Connect
Alternatively, use the official clickhouse-connect driver.
Alembic Integration
See Alembic setup guide for additional instructions on how to use Alembic with Metaxy.
Performance Optimization¶
Table Design
For optimal query performance, create your ClickHouse tables with:
- Partitioning: Partition your tables!
- Ordering: It's probably a good idea to use
(metaxy_feature_version, <id_columns>, metaxy_updated_at)
metaxy.ext.metadata_stores.clickhouse
¶
This module implements IbisMetadataStore for ClickHouse.
It takes care of some ClickHouse-specific logic such as nw.Struct type conversion against ClickHouse types such as Map(K,V).
metaxy.ext.metadata_stores.clickhouse.ClickHouseMetadataStore
¶
ClickHouseMetadataStore(
connection_string: str | None = None,
*,
connection_params: dict[str, Any] | None = None,
fallback_stores: list[MetadataStore] | None = None,
auto_cast_struct_for_map: bool = True,
**kwargs: Any,
)
Bases: IbisMetadataStore
ClickHouse metadata store using Ibis backend.
Connection Parameters
Parameters:
-
connection_string(str | None, default:None) β -
connection_params(dict[str, Any] | None, default:None) βAlternative to connection_string, specify params as dict:
-
host: Server host
-
port: Server port (default:
8443) -
database: Database name
-
user: Username
-
password: Password
-
secure: Use secure connection (default:
False)
-
-
fallback_stores(list[MetadataStore] | None, default:None) βOrdered list of read-only fallback stores.
-
auto_cast_struct_for_map(bool, default:True) βwhether to auto-convert DataFrame user-defined Struct columns to Map format on write when the ClickHouse column is Map type. Metaxy system columns are always converted.
-
**kwargs(Any, default:{}) βPassed to
IbisMetadataStore`
Raises:
-
ImportErrorβIf ibis-clickhouse not installed
-
ValueErrorβIf neither connection_string nor connection_params provided
Source code in src/metaxy/ext/metadata_stores/clickhouse.py
def __init__(
self,
connection_string: str | None = None,
*,
connection_params: dict[str, Any] | None = None,
fallback_stores: list["MetadataStore"] | None = None,
auto_cast_struct_for_map: bool = True,
**kwargs: Any,
):
"""
Initialize [ClickHouse](https://clickhouse.com/) metadata store.
Args:
connection_string: ClickHouse connection string.
Format: `clickhouse://[user[:password]@]host[:port]/database[?param=value]`
Example:
```
"clickhouse://localhost:8443/default"
```
connection_params: Alternative to connection_string, specify params as dict:
- host: Server host
- port: Server port (default: `8443`)
- database: Database name
- user: Username
- password: Password
- secure: Use secure connection (default: `False`)
fallback_stores: Ordered list of read-only fallback stores.
auto_cast_struct_for_map: whether to auto-convert DataFrame user-defined Struct columns to Map format on write when the ClickHouse column is Map type. Metaxy system columns are always converted.
**kwargs: Passed to [`IbisMetadataStore`][metaxy.metadata_store.ibis.IbisMetadataStore]`
Raises:
ImportError: If ibis-clickhouse not installed
ValueError: If neither connection_string nor connection_params provided
"""
if connection_string is None and connection_params is None:
raise ValueError(
"Must provide either connection_string or connection_params. "
"Example: connection_string='clickhouse://localhost:8443/default'"
)
# Cache for ClickHouse table schemas (cleared on close)
self._ch_schema_cache: dict[str, IbisSchema] = {}
# Store auto_cast_struct_for_map setting
self.auto_cast_struct_for_map = auto_cast_struct_for_map
# Initialize Ibis store with ClickHouse backend
super().__init__(
connection_string=connection_string,
backend="clickhouse" if connection_string is None else None,
connection_params=connection_params,
fallback_stores=fallback_stores,
**kwargs,
)
Configuration¶
fallback_stores¶
List of fallback store names to search when features are not found in the current store.
Type: list[str]
hash_algorithm¶
Hash algorithm for versioning. If None, uses store's default.
Type: metaxy.versioning.types.HashAlgorithm | None
versioning_engine¶
Which versioning engine to use: 'auto' (prefer native), 'native', or 'polars'.
Type: Literal['auto', 'native', 'polars'] | Default: "auto"
connection_string¶
Ibis connection string (e.g., 'clickhouse://host:9000/db').
Type: str | None
connection_params¶
Backend-specific connection parameters.
Type: dict[str, Any | None
table_prefix¶
Optional prefix for all table names.
Type: str | None
auto_create_tables¶
If True, create tables on open. For development/testing only.
Type: bool | None
auto_cast_struct_for_map¶
Auto-convert DataFrame Struct columns to Map format on write when the ClickHouse column is Map type. Metaxy system columns are always converted.
Type: bool | Default: True