From 93473b9c019c1a0a106d8457506a0baf020b02bd Mon Sep 17 00:00:00 2001 From: Milind Srivastava Date: Wed, 18 Nov 2026 10:25:14 -0500 Subject: [PATCH 1/2] fix(cost): print a summary build's operations per row as a whole number A KMV build's compared hashes per row are fractional, so its cost detail printed e.g. "300000 rows x 304.9256135696759 ops". Print the build's and the chain's operations per row, and the state allocation's, with `:.0`, as the hash aggregate's detail already does. Charged costs are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014qhwq4GZoWwtfFWWr9CKkr --- crates/cost/src/node/usage/mod.rs | 6 ++--- crates/cost/src/node/usage/tests.rs | 42 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 2a67c1468..803b97fb6 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -1131,7 +1131,7 @@ pub(crate) fn node_usage_and_cost_terms<'a>( let allocation_terms = state_allocation_terms(node.parameters.as_ref()); let allocation_ops = allocation_terms.operations(); let allocation_detail = if allocation_ops > 0.0 { - format!(", {allocation_ops} ops to allocate each state") + format!(", {allocation_ops:.0} ops to allocate each state") } else { String::new() }; @@ -1143,7 +1143,7 @@ pub(crate) fn node_usage_and_cost_terms<'a>( out, Ok(charged(terms, out.bytes, 0)), format!( - "build {} into {group_count} states: {} rows x {update_ops} ops, \ + "build {} into {group_count} states: {} rows x {update_ops:.0} ops, \ {group_visits:.0} group visits{allocation_detail}", summary_type_name(family), input.rows @@ -1243,7 +1243,7 @@ pub(crate) fn node_usage_and_cost_terms<'a>( )), format!( "build {} in tumbling windows of {tumbling_window_width_ms} ms for \ - {group_count} groups: {} rows x {update_ops} ops, {group_visits:.0} \ + {group_count} groups: {} rows x {update_ops:.0} ops, {group_visits:.0} \ group visits, {emitted_states:.1} states emitted", summary_type_name(family), input.rows diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 3568904d7..1c84524f5 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -1173,6 +1173,48 @@ fn a_kmv_build_costs_per_retained_hash_it_compares() { } } +/// A build's detail prints its operations per row as a whole number, as a +/// hash aggregate's does: a KMV's compared hashes per row are fractional +/// and printed in full made the planner's output unreadable and its +/// snapshots sensitive to float noise. +#[test] +fn a_build_detail_prints_whole_operations_per_row() { + use asap_types::summary_type::SketchAlgorithm; + let settings = CostInputs::default(); + let (data, schema) = sql_rows_of_t(100_000); + let range = PhysicalASAPDAGNode { + output_schema: schema, + ..node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })) + }; + let mut rows = raw_rows(&range, "t", 300_000, 300_000); + rows.output_schema = &range.output_schema; + rows.unchanged_columns = ["job", "region", "instance", "user_id", "value"].into(); + let build = PhysicalASAPDAGNode { + parameters: Some(SketchParams::Kmv { k: 4_323 }), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), + input: SummaryUpdate::column(ColumnRef::Named("user_id".into())), + reduction: Reduction::by(vec![]), + filter: None, + })) + }; + let detail = node_resource_usage_and_cost(&build, &[rows], (1, 1.0), None, &data, &settings) + .unwrap() + .cost + .detail; + let ops = detail + .split(" rows x ") + .nth(1) + .and_then(|rest| rest.split(" ops").next()) + .unwrap_or_else(|| panic!("no operations per row in {detail}")); + assert!(ops.parse::().is_ok(), "{detail}"); +} + /// A KMV build's time per row, measured on the executor (median of 3 runs /// on one pinned core) over `user_id`s drawn from 1,000,000 values, in 10 /// states by `job` and 1,000 by `instance`, from 1,200 to 60,000 rows per From 72ee6fe289b415c165e47ce92674ea2a62b4b046 Mon Sep 17 00:00:00 2001 From: Milind Srivastava Date: Wed, 18 Nov 2026 11:17:08 -0500 Subject: [PATCH 2/2] fix(cost): print operation counts to three decimals through one helper Review of #1417: `:.0` printed a count under 0.5 as "0 ops" while still charging it, and the detail no longer multiplied out to the charge. `operations_text` prints three decimals with trailing zeros dropped, so whole counts read as before ("121"), fractional ones keep their value ("304.926") and float noise does not show ("70"). The build, chain and allocation details use it, each pinned by its own test, over one shared fixture. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014qhwq4GZoWwtfFWWr9CKkr --- crates/cost/src/node/usage/mod.rs | 24 +++++-- crates/cost/src/node/usage/tests.rs | 99 ++++++++++++++++++++++------- 2 files changed, 96 insertions(+), 27 deletions(-) diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 803b97fb6..04f34963a 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -1131,7 +1131,10 @@ pub(crate) fn node_usage_and_cost_terms<'a>( let allocation_terms = state_allocation_terms(node.parameters.as_ref()); let allocation_ops = allocation_terms.operations(); let allocation_detail = if allocation_ops > 0.0 { - format!(", {allocation_ops:.0} ops to allocate each state") + format!( + ", {} ops to allocate each state", + operations_text(allocation_ops) + ) } else { String::new() }; @@ -1143,10 +1146,11 @@ pub(crate) fn node_usage_and_cost_terms<'a>( out, Ok(charged(terms, out.bytes, 0)), format!( - "build {} into {group_count} states: {} rows x {update_ops:.0} ops, \ + "build {} into {group_count} states: {} rows x {} ops, \ {group_visits:.0} group visits{allocation_detail}", summary_type_name(family), - input.rows + input.rows, + operations_text(update_ops) ), ) } @@ -1243,10 +1247,11 @@ pub(crate) fn node_usage_and_cost_terms<'a>( )), format!( "build {} in tumbling windows of {tumbling_window_width_ms} ms for \ - {group_count} groups: {} rows x {update_ops:.0} ops, {group_visits:.0} \ + {group_count} groups: {} rows x {} ops, {group_visits:.0} \ group visits, {emitted_states:.1} states emitted", summary_type_name(family), - input.rows + input.rows, + operations_text(update_ops) ), ) } @@ -1816,6 +1821,15 @@ fn merged_states(producers: &[ProducerOutput<'_>]) -> Result String { + let text = format!("{operations:.3}"); + text.trim_end_matches('0').trim_end_matches('.').to_string() +} + /// A summary type's name in reports: a sketch by its algorithm, e.g. /// `HydraCms`; an exact accumulator by its kind. // Any other type prints its Debug form, which names a new variant too, so diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 1c84524f5..37e778b0d 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -1173,13 +1173,14 @@ fn a_kmv_build_costs_per_retained_hash_it_compares() { } } -/// A build's detail prints its operations per row as a whole number, as a -/// hash aggregate's does: a KMV's compared hashes per row are fractional -/// and printed in full made the planner's output unreadable and its -/// snapshots sensitive to float noise. -#[test] -fn a_build_detail_prints_whole_operations_per_row() { - use asap_types::summary_type::SketchAlgorithm; +/// The cost detail of an `algorithm` sketch over `user_id` (`parameters`), built by a +/// `SummaryAgg` or, with `chain`, a 60 s tumbling-window chain, from +/// 300,000 rows of a 5 min time range over 100,000 users. +fn user_id_sketch_detail( + parameters: SketchParams, + algorithm: asap_types::summary_type::SketchAlgorithm, + chain: bool, +) -> String { let settings = CostInputs::default(); let (data, schema) = sql_rows_of_t(100_000); let range = PhysicalASAPDAGNode { @@ -1193,26 +1194,80 @@ fn a_build_detail_prints_whole_operations_per_row() { let mut rows = raw_rows(&range, "t", 300_000, 300_000); rows.output_schema = &range.output_schema; rows.unchanged_columns = ["job", "region", "instance", "user_id", "value"].into(); + let summary_type = FieldDataType::Sketch(algorithm); + let input = SummaryUpdate::column(ColumnRef::Named("user_id".into())); + let reduction = Reduction::by(vec![]); let build = PhysicalASAPDAGNode { - parameters: Some(SketchParams::Kmv { k: 4_323 }), - ..node(Payload::ASAP(ASAPOp::SummaryAgg { - child: 0, - summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), - input: SummaryUpdate::column(ColumnRef::Named("user_id".into())), - reduction: Reduction::by(vec![]), - filter: None, + parameters: Some(parameters), + ..node(Payload::ASAP(if chain { + ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type, + input, + reduction, + filter: None, + tumbling_window_width_ms: 60_000, + } + } else { + ASAPOp::SummaryAgg { + child: 0, + summary_type, + input, + reduction, + filter: None, + } })) }; - let detail = node_resource_usage_and_cost(&build, &[rows], (1, 1.0), None, &data, &settings) + node_resource_usage_and_cost(&build, &[rows], (1, 1.0), None, &data, &settings) .unwrap() .cost - .detail; - let ops = detail - .split(" rows x ") - .nth(1) - .and_then(|rest| rest.split(" ops").next()) - .unwrap_or_else(|| panic!("no operations per row in {detail}")); - assert!(ops.parse::().is_ok(), "{detail}"); + .detail +} + +/// An operation count prints to three decimals without trailing zeros: +/// whole counts as before, fractional ones with their value, float noise +/// hidden. +#[test] +fn an_operation_count_prints_to_three_decimals() { + assert_eq!(operations_text(121.0), "121"); + assert_eq!(operations_text(304.925_613_569_675_9), "304.926"); + assert_eq!(operations_text(69.999_933_338_068_93), "70"); + assert_eq!(operations_text(14.700_000_000_000_001), "14.7"); + assert_eq!(operations_text(0.3), "0.3"); +} + +/// A build's detail prints its fractional operations per row (a KMV's +/// compared hashes) through `operations_text`, not in full. +#[test] +fn a_build_detail_prints_its_operations_per_row_to_three_decimals() { + use asap_types::summary_type::SketchAlgorithm; + let detail = user_id_sketch_detail(SketchParams::Kmv { k: 4_323 }, SketchAlgorithm::Kmv, false); + assert!(detail.contains("300000 rows x 304.926 ops"), "{detail}"); +} + +/// A tumbling-window chain's detail prints its operations per row the same +/// way. +#[test] +fn a_chain_detail_prints_its_operations_per_row_to_three_decimals() { + use asap_types::summary_type::SketchAlgorithm; + let detail = user_id_sketch_detail(SketchParams::Kmv { k: 4_323 }, SketchAlgorithm::Kmv, true); + assert!(detail.contains("300000 rows x 714.976 ops"), "{detail}"); +} + +/// A Count-Min state's allocation, 7 counters at 2.1 operations each, is +/// 14.700000000000001 in floating point and prints as 14.7. +#[test] +fn a_state_allocation_prints_to_three_decimals() { + use asap_types::summary_type::SketchAlgorithm; + let detail = user_id_sketch_detail( + SketchParams::Cms { width: 7, depth: 1 }, + SketchAlgorithm::Cms, + false, + ); + assert!( + detail.contains(", 14.7 ops to allocate each state"), + "{detail}" + ); } /// A KMV build's time per row, measured on the executor (median of 3 runs