use std::collections::HashMap;
use crate::executor::streaming::helpers::accumulator_states::{
decode_partial_with_args, AggregateAccumulator,
};
use crate::executor::streaming::spill::{HashPartitionSpiller, SpilledRun};
use linkrs_core::columnar::{MaterializedBatch, RowKey};
use linkrs_core::types::expr::Expression;
use linkrs_core::types::operators::AggregateFunction;
use linkrs_core::Value;
pub const ACCUMULATOR_OVERHEAD_BYTES: usize = 64;
#[derive(Debug)]
pub struct AggregateState {
pub group_map: HashMap<RowKey, Vec<AggregateAccumulator>>,
pub accumulator_overhead: usize,
pub result_batch: MaterializedBatch,
pub emitted_offset: usize,
pub partition_spiller: Option<HashPartitionSpiller>,
pub spilled_runs: Vec<Option<SpilledRun>>,
pub current_partition: usize,
pub has_spilled: bool,
pub output_complete: bool,
pub col_names: Vec<String>,
}
#[derive(Debug)]
pub struct GroupByState {
pub batch: MaterializedBatch,
pub col_names: Vec<String>,
pub result_batch: MaterializedBatch,
pub emitted_offset: usize,
pub partition_spiller: Option<HashPartitionSpiller>,
pub spilled_runs: Vec<Option<SpilledRun>>,
pub current_partition: usize,
pub has_spilled: bool,
pub output_complete: bool,
}
#[derive(Debug)]
pub struct PartialAggregateState {
pub group_map: HashMap<RowKey, Vec<AggregateAccumulator>>,
pub col_names: Vec<String>,
pub result_batch: MaterializedBatch,
pub emitted_offset: usize,
}
#[derive(Debug)]
pub struct FinalAggregateState {
pub group_map: HashMap<RowKey, Vec<AggregateAccumulator>>,
pub col_names: Vec<String>,
pub result_batch: MaterializedBatch,
pub emitted_offset: usize,
}
pub(crate) fn value_to_partial_accumulator(
func: &AggregateFunction,
args: &[Expression],
value: &Value,
) -> Option<AggregateAccumulator> {
decode_partial_with_args(func, args, value)
}