use crate::db::cursor::encode_cursor;
use crate::db::{
GroupedRow, QueryError,
executor::{RuntimeGroupedRow, StructuralGroupedProjectionResult},
schema::AcceptedEnumCatalog,
session::response::output_row_from_runtime,
};
type FinalizedGroupedProjection = (Vec<GroupedRow>, Option<Vec<u8>>);
fn grouped_row_from_runtime_row(
catalog: &AcceptedEnumCatalog,
row: RuntimeGroupedRow,
) -> Result<GroupedRow, QueryError> {
let (group_key, aggregate_values) = row.into_group_key_and_aggregate_values();
let group_key = output_row_from_runtime(catalog, group_key)?;
let aggregate_values = output_row_from_runtime(catalog, aggregate_values)?;
Ok(GroupedRow::new(group_key, aggregate_values))
}
fn grouped_rows_from_runtime_rows(
catalog: &AcceptedEnumCatalog,
rows: Vec<RuntimeGroupedRow>,
) -> Result<Vec<GroupedRow>, QueryError> {
rows.into_iter()
.map(|row| grouped_row_from_runtime_row(catalog, row))
.collect()
}
pub(in crate::db) fn finalize_structural_grouped_projection_result(
result: StructuralGroupedProjectionResult,
) -> Result<FinalizedGroupedProjection, QueryError> {
let (rows, next_cursor, value_catalog) = result.into_rows_and_cursor();
let rows = grouped_rows_from_runtime_rows(value_catalog.enum_catalog(), rows)?;
Ok((rows, next_cursor))
}
pub(in crate::db) fn grouped_cursor_from_bytes(cursor: Option<Vec<u8>>) -> Option<String> {
cursor.as_deref().map(encode_cursor)
}