mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-28 05:34:02 +00:00
feat(sketch): expose deterministic file sampling over region RPC
Add optional file-sampling requests and unscaled sample diagnostics, validate limits, and preserve the fields through region RPC conversion. Pin the published proto revision in the standalone OSS workspace; retain Cargo-generated lockfile dependency-edge normalization. Signed-off-by: WenyXu <wenymedia@gmail.com>
This commit is contained in:
Generated
+11
-11
@@ -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",
|
||||
]
|
||||
|
||||
@@ -6799,7 +6799,7 @@ dependencies = [
|
||||
"libc",
|
||||
"percent-encoding",
|
||||
"pin-project-lite",
|
||||
"socket2 0.5.10",
|
||||
"socket2 0.6.4",
|
||||
"tokio",
|
||||
"tower-service",
|
||||
"tracing",
|
||||
@@ -8071,7 +8071,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]]
|
||||
@@ -9377,7 +9377,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]]
|
||||
@@ -11679,7 +11679,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",
|
||||
@@ -11727,7 +11727,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",
|
||||
@@ -11740,7 +11740,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",
|
||||
@@ -13297,7 +13297,7 @@ dependencies = [
|
||||
"security-framework 3.7.0",
|
||||
"security-framework-sys",
|
||||
"webpki-root-certs",
|
||||
"windows-sys 0.60.2",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -14220,7 +14220,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]]
|
||||
@@ -14790,7 +14790,7 @@ dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"psm",
|
||||
"windows-sys 0.60.2",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -15417,7 +15417,7 @@ dependencies = [
|
||||
"getrandom 0.3.4",
|
||||
"once_cell",
|
||||
"rustix 1.0.7",
|
||||
"windows-sys 0.60.2",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -708,6 +708,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,
|
||||
@@ -722,6 +726,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,
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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<String>,
|
||||
/// Operations applied to each selected column.
|
||||
pub queries: Vec<SketchQuery>,
|
||||
/// Absent means inspect all candidate files. Missing sampled files are never replaced.
|
||||
pub sampling: Option<SketchSampling>,
|
||||
}
|
||||
|
||||
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<ColumnSketchResult>,
|
||||
/// Present when sampling was requested; estimates and represented rows remain unscaled.
|
||||
pub sample: Option<SketchSample>,
|
||||
}
|
||||
|
||||
/// 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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<pb::SketchRequest> 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<SketchResponse> for pb::SketchResponse {
|
||||
})
|
||||
})
|
||||
.collect::<Result<_, BoxedError>>()?;
|
||||
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<pb::SketchResponse> for SketchResponse {
|
||||
})
|
||||
})
|
||||
.collect::<Result<_, BoxedError>>()?;
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user