use std::path::Path;
use anyhow::Result;
use serde_json::{json, Value};
use crate::meta::open;
struct Check {
name: &'static str,
ok: bool,
detail: String,
}
pub fn validate(path: &Path) -> Result<Value> {
let metadata = open(path)?;
let file_size = std::fs::metadata(path)?.len();
let file = metadata.file_metadata();
let mut checks = Vec::new();
let rows_in_groups: i64 = metadata.row_groups().iter().map(|g| g.num_rows()).sum();
checks.push(Check {
name: "row-count",
ok: rows_in_groups == file.num_rows(),
detail: format!(
"the footer says {} rows, the row groups hold {rows_in_groups}",
file.num_rows()
),
});
let mut values_in_groups: i64 = 0;
let mut group_rows_mismatch = Vec::new();
for (index, group) in metadata.row_groups().iter().enumerate() {
let first = group.columns().first().map(|c| c.num_values()).unwrap_or(0);
if group.columns().iter().any(|c| c.num_values() != first) {
group_rows_mismatch.push(index);
}
values_in_groups += first;
}
checks.push(Check {
name: "column-counts",
ok: group_rows_mismatch.is_empty(),
detail: if group_rows_mismatch.is_empty() {
format!(
"every column of a row group holds the same number of values ({values_in_groups})"
)
} else {
format!(
"these row groups have columns with different value counts: {group_rows_mismatch:?}"
)
},
});
let mut chunks: Vec<(u64, u64)> = Vec::new();
let mut out_of_bounds = Vec::new();
for group in metadata.row_groups() {
for chunk in group.columns() {
let (start, length) = chunk.byte_range();
let end = start.saturating_add(length);
if end > file_size {
out_of_bounds.push(chunk.column_path().string());
}
chunks.push((start, end));
}
}
checks.push(Check {
name: "within-file",
ok: out_of_bounds.is_empty(),
detail: if out_of_bounds.is_empty() {
format!("every column chunk fits inside the file ({file_size} bytes)")
} else {
format!("these column chunks point past the end of the file: {out_of_bounds:?}")
},
});
chunks.sort_unstable();
let mut overlapping = Vec::new();
for pair in chunks.windows(2) {
if pair[0].1 > pair[1].0 {
overlapping.push(format!("{} and {}", pair[0].1, pair[1].0));
}
}
checks.push(Check {
name: "no-overlap",
ok: overlapping.is_empty(),
detail: if overlapping.is_empty() {
"no two column chunks share bytes".to_string()
} else {
format!("{} overlapping byte ranges", overlapping.len())
},
});
let compressed: i64 = metadata
.row_groups()
.iter()
.flat_map(|group| group.columns().iter())
.map(|chunk| chunk.compressed_size())
.sum();
let footer_share = if file_size > 0 {
compressed as f64 / file_size as f64
} else {
0.0
};
checks.push(Check {
name: "sizes",
ok: compressed as u64 <= file_size,
detail: format!(
"the column chunks hold {compressed} bytes of the {file_size} byte file ({:.1}% of it, the rest is the footer)",
footer_share * 100.0
),
});
let ok = checks.iter().all(|check| check.ok);
Ok(json!({
"file": path.display().to_string(),
"ok": ok,
"checks": checks
.iter()
.map(|check| json!({
"name": check.name,
"ok": check.ok,
"detail": check.detail,
}))
.collect::<Vec<Value>>(),
}))
}