use arrow_array::RecordBatch;
use crate::row_ids::_helpers::digest::digest_row_id;
use crate::row_ids::_helpers::json_values::json_values;
pub fn key_row_ids(batch: &RecordBatch, key: &[(String, usize)]) -> Result<Vec<String>, String> {
let mut sorted: Vec<&(String, usize)> = key.iter().collect();
sorted.sort_by(|left, right| left.0.cmp(&right.0));
let mut parts: Vec<(String, Vec<String>)> = Vec::with_capacity(sorted.len());
for (name, index) in sorted {
let name = serde_json::to_string(name).map_err(|error| error.to_string())?;
parts.push((name, json_values(batch.column(*index))?));
}
Ok((0..batch.num_rows())
.map(|row| digest_row_id(key_json(&parts, row).as_bytes()))
.collect())
}
fn key_json(parts: &[(String, Vec<String>)], row: usize) -> String {
let mut fields: Vec<String> = Vec::with_capacity(parts.len());
for (name, values) in parts {
fields.push(format!("{name}:{}", values[row]));
}
format!("{{{}}}", fields.join(","))
}