Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions docs/clickhouse-baselines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# ClickHouse baseline variants

The `clickhouse` engine of `approxbench benchmark-external run` runs plans the planner made (`clickhouse.equivalent-only`, `clickhouse.with-substitutes`).
The baseline variants are SQL written the way ClickHouse users write it today, run by the same engine, so one report compares them:

| Variant | What | Answers |
|---|---|---|
| `clickhouse.native` | the raw table only; every query in plain SQL at query time | exact |
| `clickhouse.hand-mv` | one materialized view per query, `-State` in the view and `-Merge` at query time; no view shared between queries | exact |
| `clickhouse.sketch-functions` | `clickhouse.native`, with quantiles by `quantileTDigest(q)` | approximate |
| `clickhouse.sketch-functions-dd` | `clickhouse.native`, with quantiles by `quantileDD(0.01, q)` | approximate |
| `clickhouse.sketch-mv` | `clickhouse.hand-mv`, with quantiles kept as `quantileTDigestState(q)` | approximate |

`scripts/clickhouse_baselines.py <run-directory>` writes them from the run directory's workloads, next to the planner's variants:

```sh
ASAP_PLANNER=... scripts/plan-suite.sh suites/showcase-1m /data/run-1
python3 scripts/clickhouse_baselines.py /data/run-1
```

Each `plans/<variant>/<workload>.plan.json` is a hand-written deployment (`{"clickhouse_hand_written": {...}}`, read by [clickhouse_hand_written](../system-bench/src/engines_external/clickhouse_hand_written.rs)): source tables and views, then one query per workload query, with the rule that wrote it and, for a sketch function, the approximation.
The engine reports each approximation as a substitution, so the report's `substitutions` column names it, and the exact variants allow none.
A sketch variant writes `<workload>.no-plan.json` for a workload with no quantile query, since its SQL would equal the exact variant's.
A query no rule covers is a `no-plan.json` with the reason, in every variant.

## Rules

PromQL shapes with a rule: `F_over_time(m[w])` (F in sum, count, min, max, avg), `quantile_over_time(q, m[w])`, `rate(m[w])`, `increase(m[w])`, each optionally under `A by (labels) (...)` (A in sum, min, max, avg, count).
Windows are `(t − w, t]`, as in PromQL and the planner.
Per-series answers group by every label of the metric (PromQL drops `__name__` under a function).

| Inner function | `clickhouse.native` (query time) | `clickhouse.hand-mv` (one view per query) |
|---|---|---|
| sum / count / min / max / avg | `sum(value)`, `count()`, ... per series over `(t − w, t]`; `sum by` of sum or count, `min by` of min and `max by` of max are one `GROUP BY` of the kept labels | `sumState`, `countState`, ... per bucket, by the series' labels (by the kept labels for the one-`GROUP BY` pairs); `-Merge` over the buckets in `(t − w, t]` |
| quantile | `quantileExactInclusive(q)`: PromQL's interpolation at rank q·(n − 1) | `quantileExactInclusiveState(q)` per series and bucket (every value is kept) |
| rate / increase | Prometheus `extrapolatedRate` (v3.15.1) over each series' samples sorted by time, with reset correction | per insert, series and bucket, one row: count, first and last time and value, and the reset-corrected increase inside it (a MergeTree, not states); the query joins a series' rows in time order, then `extrapolatedRate` |
| `A by (labels)` | `A(value)` grouped by the labels, over the per-series result | the same, over the per-series result read from the view |

| Sketch variant | Quantile rule | Error parameters |
|---|---|---|
| `clickhouse.sketch-functions` | `quantileTDigest(q)(value)` | t-digest with ClickHouse's fixed parameters: epsilon 0.01, at most 2,048 centroids, Float32 centroids ([`QuantileTDigest.h`](https://github.com/ClickHouse/ClickHouse/blob/ed6dc0f3556499bed6bb0c41bf48eeb01f7293fe/src/AggregateFunctions/QuantileTDigest.h#L85-L87)); no stated error bound |
| `clickhouse.sketch-functions-dd` | `quantileDD(0.01, q)(value)` | DDSketch, relative accuracy 0.01 at rank q·(n − 1) |
| `clickhouse.sketch-mv` | `quantileTDigestState(q)` per series and bucket, `quantileTDigestMerge(q)` over the window | as `quantileTDigest`, after merging one digest per bucket |

No showcase query asks for a distinct count or top-k, so there is no rule for them yet; the functions for them would be `uniqCombined` (HyperLogLog, precision 17 by default) and `topK(k)` (Space-Saving).
The accuracy of every sketch variant is measured by the report (`--truth clickhouse/clickhouse.native`), not assumed.

Physical choices:
- The raw table of `clickhouse.native` and the sketch-function variants has the planner lane's layout: a MergeTree sorted by the labels and `ts`, with a `minmax` index on `ts`.
- The raw table of `clickhouse.hand-mv` and `clickhouse.sketch-mv` is `ENGINE = Null`, ClickHouse's pre-aggregation pattern: its views see every insert, and no raw row is kept.
- A bucket is gcd(window, evaluation interval) wide (10 s for a 1 m window every 10 s, 1 min for a 1 h window every minute), keyed `(intDiv(ts_ms − 1, b) + 1) · b`, so a bucket is `(end − b, end]`. A view table is sorted by bucket end, then the labels. A query that reads buckets fails (`throwIf`) at an evaluation time that is not a bucket end.

Where ClickHouse cannot match PromQL the way users write it:
- Counter resets: no built-in aggregate state merges PromQL's reset rule. `deltaSumTimestamp`, the usual choice, adds 0 for the step across a reset, where PromQL adds the value after it. `clickhouse.hand-mv` therefore keeps one row per insert, series and bucket and joins them in time order at query time; this needs each series inserted in time order, as the harness does, and two overlapping rows are an error.
- Floating-point sums run in another order than PromQL's, so they agree to rounding, not bit for bit.

## Conformance

`system-bench/tests/clickhouse_baselines_conformance.rs` runs every variant on a real server (`#[ignore]`; the same environment as `clickhouse_conformance`) and compares each evaluation with an oracle written in the test from PromQL's semantics: the showcase-1m workloads at full size, the showcase-1h workloads with 100 series, and a counter with resets inside and across buckets.
The exact variants must answer the oracle's series to 1e-9 relative; the sketch variants must answer the oracle's series, and the test prints their errors.
5 changes: 4 additions & 1 deletion docs/system-bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ approxbench benchmark-external write-throughput --help
```

What to run comes from a run directory: the workloads, and the plans if a system runs planned queries.
`scripts/plan-suite.sh` prepares a run directory from a suite.
`scripts/plan-suite.sh` prepares a run directory from a suite, and `scripts/clickhouse_baselines.py` adds the ClickHouse baseline variants to it.
`scripts/one-shot-workload.sh` and `scripts/one-shot-clickbench.sh` prepare one and run every system for one-shot queries.
`scripts/write-throughput-asapquery-precompute-engine.sh` measures the write throughput of an ASAPQuery precompute engine.

Expand Down Expand Up @@ -80,6 +80,9 @@ DuckDB and PilotDB are installed by `scripts/install-*.sh`.
A plan step the translator has no rule for is refused, so the translator is also the check of what the system can run.
What each system can run is written down for the planner in [capabilities](../system-bench/capabilities/README.md).

The ClickHouse engine also runs hand-written deployments: `scripts/clickhouse_baselines.py` writes the ClickHouse baseline variants (`clickhouse.native`, `clickhouse.hand-mv` and three sketch variants), SQL written as ClickHouse users write it today, next to the planner's variants.
Their rules are in [ClickHouse baselines](./clickhouse-baselines.md).

### planeval

`planeval` plans each query of a workload with ASAPPlanner and runs the plan with ASAPPlanner's executor, in the same process.
Expand Down
Loading