Skip to main content

gluesql_core/plan/statement/query/
distinct.rs

1use {
2    super::{ProjectPlan, SelectOrderByPlan},
3    serde::{Deserialize, Serialize},
4};
5
6#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub struct DistinctPlan {
8    pub input: DistinctInputPlan,
9}
10
11impl DistinctPlan {
12    pub(super) fn project(&self) -> &ProjectPlan {
13        match &self.input {
14            DistinctInputPlan::Project(project) => project,
15            DistinctInputPlan::SelectOrderBy(order_by) => &order_by.input,
16        }
17    }
18}
19
20#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
21pub enum DistinctInputPlan {
22    Project(ProjectPlan),
23    SelectOrderBy(SelectOrderByPlan),
24}
25
26#[cfg(test)]
27mod tests {
28    use {
29        super::{DistinctInputPlan, DistinctPlan},
30        crate::plan::{
31            ProjectInputPlan, ProjectPlan, ProjectionPlan, SelectOrderByPlan, SourcePlan,
32            TableAccessPlan, TableSourcePlan,
33        },
34    };
35
36    fn project_plan() -> ProjectPlan {
37        ProjectPlan {
38            input: ProjectInputPlan::Source(SourcePlan::Table(TableSourcePlan {
39                name: "Item".to_owned(),
40                alias: None,
41                access: TableAccessPlan::FullScan,
42            })),
43            projection: ProjectionPlan::SelectItems(Vec::new()),
44        }
45    }
46
47    #[test]
48    fn distinct_accepts_project_and_select_order_by_inputs() {
49        let distinct = DistinctPlan {
50            input: DistinctInputPlan::Project(project_plan()),
51        };
52        assert!(matches!(distinct.input, DistinctInputPlan::Project(_)));
53
54        let order_by = DistinctPlan {
55            input: DistinctInputPlan::SelectOrderBy(SelectOrderByPlan {
56                input: project_plan(),
57                exprs: Vec::new(),
58            }),
59        };
60        assert!(matches!(
61            order_by.input,
62            DistinctInputPlan::SelectOrderBy(_)
63        ));
64    }
65}