proof_of_sql/sql/postprocessing/
owned_table_postprocessing.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use super::{
    GroupByPostprocessing, OrderByPostprocessing, PostprocessingResult, PostprocessingStep,
    SelectPostprocessing, SlicePostprocessing,
};
use crate::base::{database::OwnedTable, scalar::Scalar};
use serde::{Deserialize, Serialize};

/// An enum for nodes that can apply postprocessing to a `OwnedTable`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum OwnedTablePostprocessing {
    /// Slice the `OwnedTable` with the given `SlicePostprocessing`.
    Slice(SlicePostprocessing),
    /// Order the `OwnedTable` with the given `OrderByPostprocessing`.
    OrderBy(OrderByPostprocessing),
    /// Select the `OwnedTable` with the given `SelectPostprocessing`.
    Select(SelectPostprocessing),
    /// Aggregate the `OwnedTable` with the given `GroupByPostprocessing`.
    GroupBy(GroupByPostprocessing),
}

impl<S: Scalar> PostprocessingStep<S> for OwnedTablePostprocessing {
    /// Apply the postprocessing step to the `OwnedTable` and return the result.
    fn apply(&self, owned_table: OwnedTable<S>) -> PostprocessingResult<OwnedTable<S>> {
        match self {
            OwnedTablePostprocessing::Slice(slice_expr) => slice_expr.apply(owned_table),
            OwnedTablePostprocessing::OrderBy(order_by_expr) => order_by_expr.apply(owned_table),
            OwnedTablePostprocessing::Select(select_expr) => select_expr.apply(owned_table),
            OwnedTablePostprocessing::GroupBy(group_by_expr) => group_by_expr.apply(owned_table),
        }
    }
}

impl OwnedTablePostprocessing {
    /// Create a new `OwnedTablePostprocessing` with the given `SlicePostprocessing`.
    pub fn new_slice(slice_expr: SlicePostprocessing) -> Self {
        Self::Slice(slice_expr)
    }
    /// Create a new `OwnedTablePostprocessing` with the given `OrderByPostprocessing`.
    pub fn new_order_by(order_by_expr: OrderByPostprocessing) -> Self {
        Self::OrderBy(order_by_expr)
    }
    /// Create a new `OwnedTablePostprocessing` with the given `SelectPostprocessing`.
    pub fn new_select(select_expr: SelectPostprocessing) -> Self {
        Self::Select(select_expr)
    }
    /// Create a new `OwnedTablePostprocessing` with the given `GroupByPostprocessing`.
    pub fn new_group_by(group_by_postprocessing: GroupByPostprocessing) -> Self {
        Self::GroupBy(group_by_postprocessing)
    }
}

/// Apply a list of postprocessing steps to an `OwnedTable`.
pub fn apply_postprocessing_steps<S: Scalar>(
    owned_table: OwnedTable<S>,
    postprocessing_steps: &[OwnedTablePostprocessing],
) -> PostprocessingResult<OwnedTable<S>> {
    // Sadly try_fold() only works on Options
    let mut current_table = owned_table;
    for step in postprocessing_steps {
        current_table = step.apply(current_table)?;
    }
    Ok(current_table)
}