Skip to main content

linkrs_query/optimizer/cost_based/aggregate_strategy/
strategy.rs

1//! Aggregation strategy types and decision container.
2
3/// Aggregation policy type
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum AggregateStrategy {
6    /// Hash Aggregation – Storing Group Status Using a HashMap
7    /// Applicable to: cases where the number of group keys is high and there is sufficient memory available.
8    HashAggregate,
9    /// Sorting and Aggregation: First sort the data, then perform the aggregation.
10    /// Applicable to: inputs that are already sorted or nearly sorted, and in cases where memory is limited.
11    SortAggregate,
12    /// Stream Aggregation – An optimized aggregation method when the input data is already sorted.
13    /// Applicable to: The input data has already been sorted according to the grouping key.
14    StreamingAggregate,
15}
16
17impl AggregateStrategy {
18    /// Obtain the policy name
19    pub fn name(&self) -> &'static str {
20        match self {
21            AggregateStrategy::HashAggregate => "HashAggregate",
22            AggregateStrategy::SortAggregate => "SortAggregate",
23            AggregateStrategy::StreamingAggregate => "StreamingAggregate",
24        }
25    }
26}
27
28/// Aggregation policy decision-making
29#[derive(Debug, Clone)]
30pub struct AggregateStrategyDecision {
31    /// Selected Aggregation Strategy
32    pub strategy: AggregateStrategy,
33    /// Estimated number of output lines
34    pub estimated_output_rows: u64,
35    /// Estimated cost
36    pub estimated_cost: f64,
37    /// Estimated memory usage (in bytes)
38    pub estimated_memory_bytes: u64,
39    /// Reason for the choice
40    pub reason: SelectionReason,
41}
42
43/// Reasons for the choice of strategy
44#[derive(Debug, Clone)]
45pub enum SelectionReason {
46    /// The input is already sorted, and stream aggregation is being used.
47    InputAlreadySorted,
48    /// A high cardinality of the grouping key results in better performance for hash aggregation.
49    HighCardinality,
50    /// The base value of the grouping key is low, which makes sorting and aggregation more efficient.
51    LowCardinality,
52    /// Memory is limited; choose the sorting algorithm for aggregation.
53    MemoryConstrained,
54    /// With small amounts of data, hash aggregation is much simpler.
55    SmallDataSet,
56    /// Large volumes of data: Sorting and aggregation should be performed to avoid memory overflow.
57    LargeDataSet,
58    /// Cost-based decision-making
59    CostBased { hash_cost: f64, sort_cost: f64 },
60}