Definition
Create dataset definitions over ClickHouse tables and views in Python.
A dataset is a semantic model over a source table or view.
from hypequery.datasets import Dataset, dimension, measure
orders = Dataset(
name="orders",
source="orders",
tenant_key="tenant_id",
time_key="created_at",
dimensions={
"id": dimension("number"),
"created_at": dimension("timestamp"),
},
measures={
"revenue": measure.sum("amount"),
},
)name is the logical dataset name and source is the physical table or view. tenant_key and time_key are the
columns used for runtime tenant isolation and time grains.
Datasets are immutable Pydantic models, validated when you create them. Define them at module level and import them
where you query. The dataset(name, *, source=..., dimensions=..., ...) helper builds the same model with the
keyword spelling the TypeScript dataset() entrypoint uses.
Fields
A dataset can include:
nameandsourcetenant_keytime_keydimensionsmeasuresfiltersrelationshipslimitsrow_resolutionsegments
See Dimensions, Measures, and Relationships for the main building blocks.
Row resolution
ReplacingMergeTree and CollapsingMergeTree tables hold superseded and cancelled rows until a background merge
removes them, so a plain SELECT over-counts. row_resolution reads only the current rows:
from hypequery.datasets import dataset, resolved_rows
accounts = dataset(
"accounts",
source="accounts", # ReplacingMergeTree(version, is_deleted)
row_resolution=resolved_rows.replacing(deleted_column="is_deleted"),
dimensions={...},
measures={...},
)resolved_rows.final():FROM accounts FINAL.resolved_rows.replacing(deleted_column=...):FINAL, and also drops rows where the deleted column is1.resolved_rows.collapsing(sign_column=...)andresolved_rows.versioned_collapsing(sign_column=...):FINAL, keeping rows whose sign is1.
Relationship joins to a resolved dataset read its resolved rows too. FINAL costs more than a plain read, so use it
only on sources that need it. For pre-aggregated state tables, see
aggregate states.
Segments
A segment is a named, author-defined row filter that callers select by name instead of rebuilding it:
from hypequery.datasets import DatasetQuery, dataset, filter, segment
orders = dataset(
"orders",
source="orders",
dimensions={...},
measures={...},
segments={
"paid": segment(filter.eq("status", "paid"), label="Paid orders"),
"unrefunded": segment(filter.is_null("refunded_at")),
},
)
DatasetQuery(measures=("revenue",), segments=("paid", "unrefunded"))Selected segments are AND-ed with the query's filters. A segment may use any of the dataset's own dimensions,
including ones callers cannot filter on, but not a relationship field or the tenant column. Catalogs list each
segment's label and description, never its filters. Served requests select segments with "segments": ["paid"].