Skip to content
Merged
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
152 changes: 108 additions & 44 deletions crates/cost/src/node/operations_per_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,51 +218,108 @@ const KMV_OPERATIONS_PER_COMPARED_HASH: f64 = 0.5;
/// its lookups and inserts below, the measure's own
/// `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, its grouping keys
/// (`HASH_AGGREGATE_OPERATIONS_PER_ROW`,
/// `HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY`) and its group visits
/// (`OPERATIONS_PER_GROUP_PER_RAW_BATCH`): reading the row's value.
/// Measured on the executor's exact `COUNT(DISTINCT user_id)` over 300,000
/// and 3,000,000 rows of 100 to 100,000,000 distinct values, in 1 to 1,000
/// groups (45 aggregates). Of 26 forms, the one with the lowest error
/// leaving each distinct count and row count out in turn is a constant,
/// a term per square root of the distinct values and one per inserted
/// entry (0.215 by squared log error, fitted 61 + 0.4845 × √(distinct
/// values) + 103.7 per entry with the key and visits inside the
/// constant). Refit with the key, the measure, the visits and each
/// group's output row (`OUTPUT_ROW_OPERATIONS_PER_GROUP`) fixed at their
/// constants, so a grouped count pays them once: 41.2 per row, 0.529 per
/// square root and 71.7 per entry (leave-one-out error 0.233; measured ÷
/// charged 0.74–2.40, 0.74–1.29 for grouped counts over at most 1,000
/// distinct values, which were 0.59–1.05).
const EXACT_DISTINCT_OPERATIONS_PER_ROW: f64 = 41.0;

/// Per input row of an exact distinct count, per square root of the
/// distinct values its value table holds (at most
/// `EXACT_DISTINCT_LOOKUP_MOST_VALUES`): looking the value up, a cache
/// miss more often as the table grows (fitted 0.529). Example: 300,000
/// rows of 10,000 distinct values in one group measure 115 ns per row,
/// charged 4 + 41 + 53 + 5 = 103; of 1,000,000 (charged as 300,000, the
/// rows read), 589, charged 4 + 41 + 290 + 118 = 453.
const EXACT_DISTINCT_LOOKUP_OPERATIONS_PER_SQRT_VALUE: f64 = 0.53;

/// The most distinct values measured: past 3,000,000 the lookup is
/// charged as at 3,000,000 (943 ns per row at 2,955,000 values against
/// 918 at 2,592,000, as each lookup misses the cache either way).
const EXACT_DISTINCT_LOOKUP_MOST_VALUES: f64 = 3_000_000.0;

/// Per entry an exact distinct count inserts, shared over its input rows:
/// each distinct value into its value table and each (group, value) pair
/// into its set of pairs (fitted 71.7).
const EXACT_DISTINCT_OPERATIONS_PER_INSERTED_ENTRY: f64 = 72.0;
/// `HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY`), its group visits
/// (`OPERATIONS_PER_GROUP_PER_RAW_BATCH`) and each group's output row
/// (`OUTPUT_ROW_OPERATIONS_PER_GROUP`), which are fixed at their
/// constants: reading the row's value and looking it and its pair up while
/// both tables fit in the cache, at `EXACT_DISTINCT_FEWEST_VALUES`.
///
/// Measured on the executor's exact `COUNT(DISTINCT user_id)` (median of 3
/// runs on one pinned core) over 10,000 series, 10 rows per series per
/// batch: 1,200,000, 12,000,000 and 36,000,000 rows of 1,000 to 10,000,000
/// distinct values, ungrouped and in 10, 100, 1,000 and 10,000 groups (75
/// aggregates); with an earlier grid over 1,000 series: 300,000 and
/// 3,000,000 rows of 100 to 100,000,000 values in 1 to 1,000 groups (44).
/// The terms are of the values the planner passes (at most the rows read).
/// Of the forms tried, the one with the lowest error leaving out each
/// (distinct values, rows, series) setting in turn (0.122 RMS log error;
/// 0.116 leaving out each group count, 0.148 each distinct count, 0.128
/// each row count) is this constant, a term per doubling of the values,
/// a cache-miss term for each table, and one per inserted value and per
/// inserted pair. Fitted 59.2, 12.9, 207, 79.2, 356 and 13.3, minimizing
/// the squared log error: measured ÷ charged 0.76–1.32 (RMS log 0.109),
/// where a constant, a term per square root of the values and one per
/// inserted entry gave 0.61–1.58 (0.224). Fitted on one grid alone, the
/// form predicts the other within 0.65–1.40.
const EXACT_DISTINCT_OPERATIONS_PER_ROW: f64 = 59.0;

/// Per doubling of the distinct values an exact distinct count's value
/// table holds past `EXACT_DISTINCT_FEWEST_VALUES` (at most
/// `EXACT_DISTINCT_MOST_VALUES`): a larger table spreads its lookups over
/// more cache lines (fitted 12.9). Example: 100,000 values are 6.6
/// doublings past 1,000, 86 per row.
const EXACT_DISTINCT_OPERATIONS_PER_VALUE_DOUBLING: f64 = 13.0;

/// The fewest distinct values on the 10,000-series grid: fewer are charged as
/// 1,000 (the earlier grid measured 60–92 ns per row over 100 values and
/// 58–123 over 1,000).
const EXACT_DISTINCT_FEWEST_VALUES: f64 = 1_000.0;

/// The most distinct values on the 10,000-series grid: more are charged as
/// 10,000,000 (the earlier grid measured 943–1,155 ns per row over
/// 100,000,000 values and 918–1,114 over 10,000,000).
const EXACT_DISTINCT_MOST_VALUES: f64 = 10_000_000.0;

/// Per input row when every value lookup misses the cache, so charged
/// times the fraction that miss ([`cache_miss_fraction`] of the values over
/// `EXACT_DISTINCT_VALUES_IN_CACHE`): a lookup in a value table that has
/// outgrown the cache reads memory (fitted 207). Example: 1,000,000 values
/// miss 0.8 of their lookups, 166 per row.
const EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_VALUE_MISS: f64 = 207.0;

/// The distinct values whose table still fits in the cache: the best by
/// leave-one-setting-out error of 100,000, 200,000, 300,000, 500,000 and
/// 1,000,000 (0.122 at 200,000, 0.123–0.128 from 100,000 to 500,000).
/// Machine-specific: it follows the cache size of the machine the grid
/// ran on, so it is a default until calibration measures it per machine.
const EXACT_DISTINCT_VALUES_IN_CACHE: f64 = 200_000.0;

/// Per input row when every (group, value) pair lookup misses the cache,
/// so charged times the fraction that miss ([`cache_miss_fraction`] of the
/// pairs over
/// `EXACT_DISTINCT_PAIRS_IN_CACHE`, fitted 79.2).
const EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_PAIR_MISS: f64 = 79.0;

/// The (group, value) pairs whose set still fits in the cache: the best by
/// leave-one-setting-out error of 500,000, 1,000,000, 3,000,000 and
/// 10,000,000 (0.122 at 500,000, 0.127 at 1,000,000, 0.134 at 3,000,000).
/// A pair is two 4-byte numbers, so fewer bytes than the value table
/// holds per value. Machine-specific: it follows the cache size of the
/// machine the grid ran on, so it is a default until calibration measures
/// it per machine.
const EXACT_DISTINCT_PAIRS_IN_CACHE: f64 = 500_000.0;

/// Per distinct value an exact distinct count interns into its value
/// table, shared over its input rows: storing the value and growing the
/// table (fitted 356). Example: 300,000 rows of 259,000 distinct values
/// pay 307 per row for them.
const EXACT_DISTINCT_OPERATIONS_PER_INSERTED_VALUE: f64 = 356.0;

/// Per (group, value) pair an exact distinct count inserts into its set of
/// pairs, shared over its input rows (fitted 13.3).
const EXACT_DISTINCT_OPERATIONS_PER_INSERTED_PAIR: f64 = 13.0;

/// The fraction of random lookups into a table of `entries` that miss a
/// cache holding `entries_in_cache` of them: none while the table fits,
/// then the share of the table outside the cache. Example: 2,000,000
/// entries in a cache of 500,000 miss 0.75 of their lookups.
fn cache_miss_fraction(entries: f64, entries_in_cache: f64) -> f64 {
(1.0 - entries_in_cache / entries.max(1.0)).max(0.0)
}

/// The operations one input row costs an exact distinct count (or
/// frequency statistic) over `rows` rows in `group_count` groups, of a
/// column taking `distinct_values` values, beyond the measure's own, its
/// grouping keys and its group visits: the executor interns each value in
/// one table of every distinct value read and keeps each group's (group,
/// value) pairs in a set, so its time per row grows with the distinct
/// values. The pairs are each group's expected distinct values among its
/// rows, drawn from `distinct_values`. Example: 300,000 rows of 10,000
/// values in one group cost 41 + 0.53 × 100 + 72 × 20,000 ÷ 300,000 = 99.
/// grouping keys, its group visits and its groups' output rows: the
/// executor interns each value in one table of every distinct value read
/// and keeps each group's (group, value) pairs in a set, so its time per
/// row grows with both tables, sharply once one outgrows the cache, and
/// with the values and pairs it inserts. The pairs are each group's
/// expected distinct values among its rows, drawn from `distinct_values`.
/// Example: 12,000,000 rows of 1,000,000 values in 100 groups (11.3 M
/// pairs) cost 59 + 13 × 10.0 + 207 × 0.8 + 79 × 0.96 + 356 × 1 M ÷ 12 M +
/// 13 × 11.3 M ÷ 12 M = 472 (measured 488 ns per row with the key and the
/// measure, charged 492).
pub(crate) fn exact_distinct_value_operations_per_row(
distinct_values: u64,
group_count: u64,
Expand All @@ -272,10 +329,17 @@ pub(crate) fn exact_distinct_value_operations_per_row(
let rows = rows.max(1) as f64;
let groups = group_count.max(1) as f64;
let pairs = groups * -values * (-(rows / groups) / values).exp_m1();
let table_values = values.clamp(EXACT_DISTINCT_FEWEST_VALUES, EXACT_DISTINCT_MOST_VALUES);
EXACT_DISTINCT_OPERATIONS_PER_ROW
+ EXACT_DISTINCT_LOOKUP_OPERATIONS_PER_SQRT_VALUE
* values.min(EXACT_DISTINCT_LOOKUP_MOST_VALUES).sqrt()
+ EXACT_DISTINCT_OPERATIONS_PER_INSERTED_ENTRY * (values.min(rows) + pairs) / rows
+ EXACT_DISTINCT_OPERATIONS_PER_VALUE_DOUBLING
* (table_values / EXACT_DISTINCT_FEWEST_VALUES).log2()
+ EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_VALUE_MISS
* cache_miss_fraction(table_values, EXACT_DISTINCT_VALUES_IN_CACHE)
+ EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_PAIR_MISS
* cache_miss_fraction(pairs, EXACT_DISTINCT_PAIRS_IN_CACHE)
+ (EXACT_DISTINCT_OPERATIONS_PER_INSERTED_VALUE * values.min(rows)
+ EXACT_DISTINCT_OPERATIONS_PER_INSERTED_PAIR * pairs)
/ rows
}

/// One counter update of any other sketch, per counter its row updates
Expand Down
137 changes: 135 additions & 2 deletions crates/cost/src/node/usage/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,14 @@ fn an_exact_distinct_count_costs_more_per_row_with_more_distinct_values() {
/// constants. Measured on the executor over 300,000 rows by `job` (10
/// groups): 60.2 ns per row of 100 distinct users and 81.3 of 1,000. With
/// the key and the visits also inside the count's fitted constant, they
/// were charged 83 and 97, 1.38x and 1.19x.
/// were charged 83 and 97, 1.38x and 1.19x. The count's terms are fitted
/// to within 0.76–1.32x of 119 measured counts, so the bound is that
/// range: they are charged 80 and 82. The bound is wider than 0.8–1.25
/// because the 100-user point is noisy: over the same 300,000 rows of 100
/// users, the count by `job` takes 60.2 ns per row and the one by `region`
/// (4 groups) 86.1, 1.43x apart at the same values, and the four grouped counts
/// over 100 values span 60–92. Every count under 1,000 values is charged
/// as at 1,000, so the 60.2 point is charged 1.33x, near the bound.
#[test]
fn a_grouped_exact_distinct_count_pays_its_key_and_visits_once() {
use asap_types::ir::operator::AggIntent;
Expand All @@ -985,12 +992,138 @@ fn a_grouped_exact_distinct_count_pays_its_key_and_visits_once() {
user_ids,
);
assert!(
(0.8..=1.25).contains(&(charged / measured)),
(0.75..=1.35).contains(&(charged / measured)),
"by job of {user_ids} users: charged {charged:.1} ns per row, measured {measured}"
);
}
}

/// The CPU operations per input row a query-time exact
/// `COUNT(DISTINCT user_id)` grouped by `keys` charges over `rows` rows of
/// `http` (10,000 series at one row per second each, so the rows span
/// `rows` ÷ 10,000 s; `region` takes 10 values, `service` 100 and
/// `instance` 10,000). `user_ids` declares the distinct `user_id`s and
/// `max_distinct_per_evaluation` the source's bound on each group's distinct
/// count; either may be absent.
fn http_distinct_count_operations_per_row(
keys: Vec<usize>,
rows: u64,
user_ids: Option<u64>,
max_distinct_per_evaluation: Option<u64>,
) -> f64 {
use asap_types::ir::operator::AggIntent;
use asap_types::ir::schema::DataType;
let settings = CostInputs::default();
let mut labels = serde_json::json!({"region": 10, "service": 100, "instance": 10_000});
if let Some(user_ids) = user_ids {
labels["user_id"] = user_ids.into();
}
let mut source = serde_json::json!({"series_count": 10_000, "sample_interval": 1000,
"distinct_values_per_label": labels});
if let Some(bound) = max_distinct_per_evaluation {
source["max_distinct_per_evaluation"] = bound.into();
}
let data: DataWorkload = serde_json::from_value(serde_json::json!({
"arrival": {"ingesting": {"rows_per_second": 10_000.0}},
"sources": {"http": source}
}))
.unwrap();
let schema = Schema::new(
[
("ts", DataType::Timestamp),
("region", DataType::Utf8),
("service", DataType::Utf8),
("instance", DataType::Utf8),
("user_id", DataType::Float64),
]
.map(|(name, dtype)| Field::new(name, FieldDataType::Plain(dtype), false))
.to_vec(),
);
let range = PhysicalASAPDAGNode {
output_schema: schema,
..node(Payload::NonASAP(NonASAPOp::TimeRange {
range: Duration::from_secs(rows / 10_000),
kind: TimeRangeKind::Range,
child: 0,
}))
};
let mut input = raw_rows(&range, "http", rows, rows / 10);
input.output_schema = &range.output_schema;
input.unchanged_columns = ["region", "service", "instance", "user_id"].into();
let aggregate = node(Payload::NonASAP(NonASAPOp::Aggregate {
reduction: Reduction::by(keys),
measures: vec![AggIntent::Cardinality { cols: vec![4] }],
output_names: vec![String::new()],
filters: vec![],
having: None,
child: 0,
}));
let usage =
node_resource_usage_and_cost(&aggregate, &[input], (1, 1.0), None, &data, &settings)
.unwrap();
usage.cpu_ops_per_second / rows as f64
}

/// An exact `COUNT(DISTINCT user_id)` looks each row's value up in a table
/// of every distinct value read, then its (group, value) pair in a set of
/// every pair kept: a lookup misses the cache once its table outgrows it,
/// so the time per row follows the values and the pairs, not the rows.
/// Measured on the executor over 12,000,000 rows of 10,000 series (10 rows
/// per series per batch, median of 3 runs on one pinned core): 1,000 users
/// by `instance` (10,000 groups, 7.0 M pairs) take 179 ns per row; 10,000
/// ungrouped 106; 1,000,000 ungrouped 381, by `service` 488 and 10,000,000
/// by (`region`, `service`) 811. Charged per square root of the values and
/// per inserted entry, they were 139, 99, 587, 665 and 1,141: 0.78x to
/// 1.54x.
#[test]
fn an_exact_distinct_count_costs_per_value_and_pair_it_looks_up() {
for (keys, user_ids, measured) in [
(vec![3], 1_000, 179.1),
(vec![], 10_000, 106.2),
(vec![], 1_000_000, 381.1),
(vec![2], 1_000_000, 488.4),
(vec![1, 2], 10_000_000, 811.3),
] {
let charged =
http_distinct_count_operations_per_row(keys.clone(), 12_000_000, Some(user_ids), None);
assert!(
(0.8..=1.25).contains(&(charged / measured)),
"by {keys:?} of {user_ids} users: charged {charged:.1} ns per row, measured {measured}"
);
}
}

/// `max_distinct_per_evaluation` bounds the distinct items of each group,
/// so a grouped count's table of every distinct value may hold that bound
/// times the groups: 10,000 `instance`s of 1,000 users each are 10,000,000
/// users. A workload whose users are shared across the groups declares the
/// column-wide `distinct_values_per_label`, which caps the table instead.
/// `COUNT(DISTINCT user_id)` by `instance` over 120,000,000 rows of
/// 1,000,000 users at most, with the per-group bound also 1,000,000, is
/// charged as over the 1,000,000 declared values; with the per-group bound
/// alone, as over the rows read (1,000,000 × 10,000 is more).
#[test]
fn an_exact_distinct_count_reads_at_most_its_column_s_declared_distinct_values() {
let declared_and_bounded = http_distinct_count_operations_per_row(
vec![3],
120_000_000,
Some(1_000_000),
Some(1_000_000),
);
let declared =
http_distinct_count_operations_per_row(vec![3], 120_000_000, Some(1_000_000), None);
let bounded =
http_distinct_count_operations_per_row(vec![3], 120_000_000, None, Some(1_000_000));
assert!(
(declared_and_bounded - declared).abs() < 1e-9 * declared,
"declared and bounded {declared_and_bounded:.1} ns per row, declared {declared:.1}"
);
assert!(
bounded > declared,
"bounded per group {bounded:.1} ns per row, declared {declared:.1}"
);
}

/// A KMV state compares each row's hash with the hashes it retains until
/// it holds `k` of them, then only hashes below its largest
/// (`asap_sketchlib`'s duplicate check). Over 300,000 rows of 100,000
Expand Down
9 changes: 6 additions & 3 deletions crates/integration-tests/tests/production_rollup_workloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ fn entry(accuracy: &AccuracyTarget, interval_ms: u32) -> QueryWorkloadEntry {
}

/// `http` arrives at 2e6 rows/s (W-RollupHeavy's table), 10,000 series,
/// a million distinct users at most per evaluation, and holds a minute of
/// rows (the catalog has no time index, so a query's time predicate is a
/// filter over every row held).
/// a million distinct users at most per evaluation in each group and in
/// the whole table (the users are shared across groups, so the column
/// takes a million values too), and holds a minute of rows (the catalog
/// has no time index, so a query's time predicate is a filter over every
/// row held).
fn data() -> DataWorkload {
serde_json::from_value(serde_json::json!({
"arrival": {"ingesting": {"rows_per_second": 2_000_000.0}},
Expand All @@ -61,6 +63,7 @@ fn data() -> DataWorkload {
"sample_interval": 5,
"column_signs": {"value": "non_negative"},
"max_distinct_per_evaluation": 1_000_000,
"distinct_values_per_label": {"user_id": 1_000_000},
"rows_held": 120_000_000,
}},
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"series_count": 10000,
"sample_interval": 5,
"column_signs": {"value": "non_negative"},
"max_distinct_per_evaluation": 1000000
"max_distinct_per_evaluation": 1000000,
"distinct_values_per_label": {"user_id": 1000000}
}
}
}
Expand Down
Loading