diff --git a/Cargo.lock b/Cargo.lock index 23a219f310c..ed3853b703b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1657,7 +1657,7 @@ dependencies = [ "maybe-owned", "rustix 1.0.7", "rustix-linux-procfs", - "windows-sys 0.60.2", + "windows-sys 0.61.2", "winx", ] @@ -6772,7 +6772,7 @@ dependencies = [ "libc", "percent-encoding", "pin-project-lite", - "socket2 0.5.10", + "socket2 0.6.4", "tokio", "tower-service", "tracing", @@ -8044,7 +8044,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07033963ba89ebaf1584d767badaa2e8fcec21aedea6b8c0346d487d49c28667" dependencies = [ "cfg-if", - "windows-targets 0.53.5", + "windows-targets 0.48.5", ] [[package]] @@ -9348,7 +9348,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -11624,7 +11624,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac6c3320f9abac597dcbc668774ef006702672474aad53c6d596b62e487b40b1" dependencies = [ "heck 0.5.0", - "itertools 0.10.5", + "itertools 0.11.0", "log", "multimap", "once_cell", @@ -11672,7 +11672,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d" dependencies = [ "anyhow", - "itertools 0.10.5", + "itertools 0.11.0", "proc-macro2", "quote", "syn 2.0.117", @@ -11685,7 +11685,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9120690fafc389a67ba3803df527d0ec9cbbc9cc45e4cc20b332996dfb672425" dependencies = [ "anyhow", - "itertools 0.10.5", + "itertools 0.11.0", "proc-macro2", "quote", "syn 2.0.117", @@ -13241,7 +13241,7 @@ dependencies = [ "security-framework 3.7.0", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -14163,7 +14163,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52d1cfed4120b4d927bf7c0f86d2087a4a7d6027c906d9f9d525a80573b9be51" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -14734,7 +14734,7 @@ dependencies = [ "cfg-if", "libc", "psm", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -15359,7 +15359,7 @@ dependencies = [ "getrandom 0.3.4", "once_cell", "rustix 1.0.7", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] diff --git a/src/client/src/region.rs b/src/client/src/region.rs index 8f1de9ccfd0..13de37e7472 100644 --- a/src/client/src/region.rs +++ b/src/client/src/region.rs @@ -703,6 +703,10 @@ mod tests { let server = RegionTestServer::start().await; let requester = server.create_requester(ChannelConfig::default(), false, false); let request = api::v1::region::SketchRequest { + sampling: Some(api::v1::region::SketchSampling { + max_files: 512, + seed: 42, + }), region_id: 42, time_range: Some(api::v1::region::SketchTimeRange { start: 1001, @@ -717,6 +721,13 @@ mod tests { }], }; let expected = api::v1::region::SketchResponse { + sample: Some(api::v1::region::SketchSample { + candidate_files: 1000, + sampled_files: 512, + fingerprint: 42, + rows_squared_sum: 512.0, + max_file_rows: 1, + }), results: vec![api::v1::region::ColumnSketchResult { column_name: "__table_id".into(), column_id: 42, diff --git a/src/mito2/src/engine/sketch.rs b/src/mito2/src/engine/sketch.rs index 35b9e484461..45d82d34039 100644 --- a/src/mito2/src/engine/sketch.rs +++ b/src/mito2/src/engine/sketch.rs @@ -120,6 +120,7 @@ mod tests { let mut env = TestEnv::new().await; let engine = env.create_engine(Default::default()).await; let request = SketchRequest { + sampling: None, time_range: SketchTimeRange { start: Timestamp::new_microsecond(1001), end: Timestamp::new_microsecond(1000), diff --git a/src/store-api/src/sketch.rs b/src/store-api/src/sketch.rs index 29d9e4c8bc2..60250dbbd89 100644 --- a/src/store-api/src/sketch.rs +++ b/src/store-api/src/sketch.rs @@ -46,6 +46,26 @@ pub enum SketchQuery { Rank { value: Value }, } +/// Uniform sampling of physical files, without replacement, before index I/O. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct SketchSampling { + pub max_files: u32, + /// Stable seed shared by coverage and value requests. + pub seed: u64, +} + +/// File sample identity and unscaled row diagnostics for the requested window. +#[derive(Debug, Clone, PartialEq)] +pub struct SketchSample { + pub candidate_files: u64, + pub sampled_files: u64, + /// Identifies the selected immutable index generations. + pub fingerprint: u64, + /// Sum of squared per-file represented row counts, including zero contributions. + pub rows_squared_sum: f64, + pub max_file_rows: u64, +} + /// Selects whole time buckets inside a required range. The implementation validates request limits. /// A whole bucket may still have missing file statistics, reported separately in its coverage. #[derive(Debug, Clone, PartialEq)] @@ -55,6 +75,8 @@ pub struct SketchRequest { pub columns: Vec, /// Operations applied to each selected column. pub queries: Vec, + /// Absent means inspect all candidate files. Missing sampled files are never replaced. + pub sampling: Option, } impl SketchRequest { @@ -63,6 +85,8 @@ impl SketchRequest { pub fn validate(&self) -> Result<(), BoxedError> { let reason = if self.time_range.start >= self.time_range.end { Some("sketch time range must have start before end") + } else if self.sampling.is_some_and(|sampling| !(1..=100_000).contains(&sampling.max_files)) { + Some("sketch sample file limit must be in 1..=100000") } else if self.columns.is_empty() { Some("sketch request must select at least one column") } else if self.queries.is_empty() { @@ -171,6 +195,8 @@ pub struct SketchResponse { /// One result per requested column and operation. /// In request column order, then query order within each column. pub results: Vec, + /// Present when sampling was requested; estimates and represented rows remain unscaled. + pub sample: Option, } /// Optional engine capability for reading persisted sketches. Each call takes its own snapshot. @@ -205,6 +231,19 @@ mod tests { }, columns: vec!["value".to_string()], queries: vec![SketchQuery::DistinctCount], + sampling: None, + } + } + + #[test] + fn test_validate_sampling_limit() { + for (max_files, valid) in [(0, false), (1, true), (100_000, true), (100_001, false)] { + let mut request = request(); + request.sampling = Some(SketchSampling { + max_files, + seed: 42, + }); + assert_eq!(request.validate().is_ok(), valid, "{max_files}"); } } diff --git a/src/store-api/src/sketch/proto.rs b/src/store-api/src/sketch/proto.rs index d52fe7d86c0..e93613d2ae2 100644 --- a/src/store-api/src/sketch/proto.rs +++ b/src/store-api/src/sketch/proto.rs @@ -24,7 +24,7 @@ use datatypes::value::Value; use crate::sketch::{ ColumnSketchResult, SketchBucket, SketchGap, SketchGapReason, SketchQuery, SketchRequest, - SketchResponse, SketchTimeRange, SketchValue, + SketchResponse, SketchSample, SketchSampling, SketchTimeRange, SketchValue, }; use crate::storage::{FileId, RegionId}; @@ -138,6 +138,10 @@ impl TryFrom for SketchRequest { .time_range .ok_or_else(|| invalid("missing sketch time range"))? .try_into()?, + sampling: request.sampling.map(|sampling| SketchSampling { + max_files: sampling.max_files, + seed: sampling.seed, + }), columns: request.columns, queries: request .queries @@ -157,6 +161,10 @@ impl SketchRequest { Ok(pb::SketchRequest { region_id: region_id.as_u64(), time_range: Some(self.time_range.try_into()?), + sampling: self.sampling.map(|sampling| pb::SketchSampling { + max_files: sampling.max_files, + seed: sampling.seed, + }), columns: self.columns, queries: self.queries.into_iter().map(Into::into).collect(), }) @@ -291,7 +299,16 @@ impl TryFrom for pb::SketchResponse { }) }) .collect::>()?; - Ok(Self { results }) + Ok(Self { + results, + sample: response.sample.map(|sample| pb::SketchSample { + candidate_files: sample.candidate_files, + sampled_files: sample.sampled_files, + fingerprint: sample.fingerprint, + rows_squared_sum: sample.rows_squared_sum, + max_file_rows: sample.max_file_rows, + }), + }) } } @@ -359,7 +376,26 @@ impl TryFrom for SketchResponse { }) }) .collect::>()?; - Ok(Self { results }) + let sample = response + .sample + .map(|sample| { + if sample.sampled_files > sample.candidate_files + || (sample.candidate_files > 0 && sample.sampled_files == 0) + || !sample.rows_squared_sum.is_finite() + || sample.rows_squared_sum < 0.0 + { + return Err(invalid("invalid sketch file sample diagnostics")); + } + Ok(SketchSample { + candidate_files: sample.candidate_files, + sampled_files: sample.sampled_files, + fingerprint: sample.fingerprint, + rows_squared_sum: sample.rows_squared_sum, + max_file_rows: sample.max_file_rows, + }) + }) + .transpose()?; + Ok(Self { results, sample }) } } @@ -458,6 +494,10 @@ mod tests { fn test_request_roundtrip() { let region_id = RegionId::new(42, 7); let request = SketchRequest { + sampling: Some(SketchSampling { + max_files: 512, + seed: 42, + }), time_range: SketchTimeRange { start: Timestamp::new_microsecond(1001), end: Timestamp::new_microsecond(2003), @@ -493,6 +533,13 @@ mod tests { fn response() -> SketchResponse { SketchResponse { + sample: Some(SketchSample { + candidate_files: 1000, + sampled_files: 512, + fingerprint: 42, + rows_squared_sum: 512.0, + max_file_rows: 1, + }), results: vec![ColumnSketchResult { column_name: "__table_id".to_string(), column_id: 3,