use rudb_common::{LogicalType, Value};
use rudb_vector::Chunk;
#[derive(Debug, Clone)]
pub struct QueryResult {
names: Vec<String>,
types: Vec<LogicalType>,
chunks: Vec<Chunk>,
rows: usize,
}
impl QueryResult {
#[must_use]
pub(crate) fn new(names: Vec<String>, types: Vec<LogicalType>, chunks: Vec<Chunk>) -> Self {
let rows = chunks.iter().map(Chunk::len).sum();
Self { names, types, chunks, rows }
}
#[must_use]
pub fn names(&self) -> &[String] {
&self.names
}
#[must_use]
pub fn types(&self) -> &[LogicalType] {
&self.types
}
#[must_use]
pub fn width(&self) -> usize {
self.names.len()
}
#[must_use]
pub fn len(&self) -> usize {
self.rows
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.rows == 0
}
#[must_use]
pub fn chunks(&self) -> &[Chunk] {
&self.chunks
}
#[must_use]
pub fn value_at(&self, row: usize, column: usize) -> Value {
let mut remaining = row;
for chunk in &self.chunks {
if remaining < chunk.len() {
return chunk.value_at(remaining, column);
}
remaining -= chunk.len();
}
Value::Null
}
#[must_use]
pub fn row(&self, row: usize) -> Option<Vec<Value>> {
let mut remaining = row;
for chunk in &self.chunks {
if remaining < chunk.len() {
return Some(chunk.row(remaining).collect());
}
remaining -= chunk.len();
}
None
}
pub fn rows(&self) -> impl Iterator<Item = Vec<Value>> + '_ {
self.chunks.iter().flat_map(|chunk| (0..chunk.len()).map(|row| chunk.row(row).collect()))
}
}