use crate::db::{
GroupedRow, PagedGroupedExecutionWithTrace, QueryError,
cursor::encode_cursor,
diagnostics::ExecutionTrace,
executor::{PageCursor, RuntimeGroupedRow, StructuralGroupedProjectionResult},
};
fn encode_grouped_page_cursor(cursor: Option<PageCursor>) -> Result<Option<Vec<u8>>, QueryError> {
cursor
.map(|token| {
let Some(token) = token.as_grouped() else {
return Err(QueryError::grouped_paged_emitted_scalar_continuation());
};
token.encode().map_err(|err| {
QueryError::serialize_internal(format!(
"failed to serialize grouped continuation cursor: {err}"
))
})
})
.transpose()
}
fn grouped_row_from_runtime_row(row: RuntimeGroupedRow) -> GroupedRow {
let (group_key, aggregate_values) = row.into_parts();
GroupedRow::new(group_key, aggregate_values)
}
fn grouped_rows_from_runtime_rows(rows: Vec<RuntimeGroupedRow>) -> Vec<GroupedRow> {
rows.into_iter().map(grouped_row_from_runtime_row).collect()
}
pub(in crate::db) fn finalize_structural_grouped_projection_result(
result: StructuralGroupedProjectionResult,
trace: Option<ExecutionTrace>,
) -> Result<PagedGroupedExecutionWithTrace, QueryError> {
let (rows, next_cursor) = result.into_parts();
let next_cursor = encode_grouped_page_cursor(next_cursor)?;
Ok(PagedGroupedExecutionWithTrace::new(
grouped_rows_from_runtime_rows(rows),
next_cursor,
trace,
))
}
pub(in crate::db) fn sql_grouped_cursor_from_bytes(cursor: Option<Vec<u8>>) -> Option<String> {
cursor.as_deref().map(encode_cursor)
}