proof_of_sql/sql/postprocessing/
owned_table_postprocessing.rs

1use super::{
2    GroupByPostprocessing, OrderByPostprocessing, PostprocessingResult, PostprocessingStep,
3    SelectPostprocessing, SlicePostprocessing,
4};
5use crate::base::{database::OwnedTable, scalar::Scalar};
6use serde::{Deserialize, Serialize};
7
8/// An enum for nodes that can apply postprocessing to a `OwnedTable`.
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
10pub enum OwnedTablePostprocessing {
11    /// Slice the `OwnedTable` with the given `SlicePostprocessing`.
12    Slice(SlicePostprocessing),
13    /// Order the `OwnedTable` with the given `OrderByPostprocessing`.
14    OrderBy(OrderByPostprocessing),
15    /// Select the `OwnedTable` with the given `SelectPostprocessing`.
16    Select(SelectPostprocessing),
17    /// Aggregate the `OwnedTable` with the given `GroupByPostprocessing`.
18    GroupBy(GroupByPostprocessing),
19}
20
21impl<S: Scalar> PostprocessingStep<S> for OwnedTablePostprocessing {
22    /// Apply the postprocessing step to the `OwnedTable` and return the result.
23    fn apply(&self, owned_table: OwnedTable<S>) -> PostprocessingResult<OwnedTable<S>> {
24        match self {
25            OwnedTablePostprocessing::Slice(slice_expr) => slice_expr.apply(owned_table),
26            OwnedTablePostprocessing::OrderBy(order_by_expr) => order_by_expr.apply(owned_table),
27            OwnedTablePostprocessing::Select(select_expr) => select_expr.apply(owned_table),
28            OwnedTablePostprocessing::GroupBy(group_by_expr) => group_by_expr.apply(owned_table),
29        }
30    }
31}
32
33impl OwnedTablePostprocessing {
34    /// Create a new `OwnedTablePostprocessing` with the given `SlicePostprocessing`.
35    #[must_use]
36    pub fn new_slice(slice_expr: SlicePostprocessing) -> Self {
37        Self::Slice(slice_expr)
38    }
39    /// Create a new `OwnedTablePostprocessing` with the given `OrderByPostprocessing`.
40    #[must_use]
41    pub fn new_order_by(order_by_expr: OrderByPostprocessing) -> Self {
42        Self::OrderBy(order_by_expr)
43    }
44    /// Create a new `OwnedTablePostprocessing` with the given `SelectPostprocessing`.
45    #[must_use]
46    pub fn new_select(select_expr: SelectPostprocessing) -> Self {
47        Self::Select(select_expr)
48    }
49    /// Create a new `OwnedTablePostprocessing` with the given `GroupByPostprocessing`.
50    #[must_use]
51    pub fn new_group_by(group_by_postprocessing: GroupByPostprocessing) -> Self {
52        Self::GroupBy(group_by_postprocessing)
53    }
54}
55
56/// Apply a list of postprocessing steps to an `OwnedTable`.
57pub fn apply_postprocessing_steps<S: Scalar>(
58    owned_table: OwnedTable<S>,
59    postprocessing_steps: &[OwnedTablePostprocessing],
60) -> PostprocessingResult<OwnedTable<S>> {
61    // Sadly try_fold() only works on Options
62    let mut current_table = owned_table;
63    for step in postprocessing_steps {
64        current_table = step.apply(current_table)?;
65    }
66    Ok(current_table)
67}