proof_of_sql/sql/logical_plans/
plan.rs

1use super::Expr;
2use crate::base::database::{ColumnField, TableRef};
3use alloc::{boxed::Box, vec::Vec};
4use serde::{Deserialize, Serialize};
5
6/// Enum of logical plans that are either provable or supported in postprocessing
7#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
8pub enum LogicalPlan {
9    /// Empty
10    Empty(Empty),
11    /// Table scan
12    TableScan(TableScan),
13    /// Projection
14    Projection(Projection),
15    /// Filter
16    Filter(Filter),
17    /// Aggregate
18    Aggregate(Aggregate),
19    /// Sort
20    Sort(Sort),
21    /// Slice
22    Slice(Slice),
23    /// Join
24    Join(Join),
25    /// Union
26    Union(Union),
27}
28
29/// Empty
30/// e.g. SELECT 1
31#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
32pub struct Empty {}
33
34/// Table scan
35///
36/// e.g. SELECT * FROM t
37#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
38pub struct TableScan {
39    /// Table reference
40    pub table_ref: TableRef,
41}
42
43/// Projection
44/// e.g. SELECT a, b FROM <input>
45#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
46pub struct Projection {
47    /// Input plan
48    pub input: Box<LogicalPlan>,
49    /// Projection expressions
50    pub expr: Vec<Expr>,
51}
52
53/// Filter
54/// e.g. WHERE a > 5
55#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
56pub struct Filter {
57    /// Input plan
58    pub input: Box<LogicalPlan>,
59    /// Filter expression
60    pub filter: Expr,
61}
62
63/// Aggregate
64/// e.g. SELECT a, COUNT(b) FROM t GROUP BY a
65#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
66pub struct Aggregate {
67    /// Input plan
68    pub input: Box<LogicalPlan>,
69    /// Group by
70    pub group_by: Vec<Expr>,
71    /// Aggregate expressions
72    pub aggr_expr: Vec<Expr>,
73    /// Output schema
74    pub schema: Vec<ColumnField>,
75}
76
77/// Sort
78/// e.g. ORDER BY a ASC, b DESC
79#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
80pub struct Sort {
81    /// Input plan
82    pub input: Box<LogicalPlan>,
83    /// Sort expressions
84    pub expr: Vec<SortExpr>,
85}
86
87/// Sort expression
88#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
89pub struct SortExpr {
90    /// Expression
91    pub expr: Expr,
92    /// Direction
93    pub asc: bool,
94}
95
96/// Slice
97/// e.g. LIMIT 5 OFFSET 10
98#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
99pub struct Slice {
100    /// Input plan
101    pub input: Box<LogicalPlan>,
102    /// Maximum number of rows to return. None = no limit
103    pub limit: Option<u64>,
104    /// Offset value
105    pub offset: i64,
106}
107
108/// Join
109/// e.g. SELECT t1.a, t1.b, t2.c FROM t1 JOIN t2 ON t1.a = t2.a
110/// Note that we only support inner joins for now
111#[allow(clippy::struct_field_names)]
112#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
113pub struct Join {
114    /// Left input plan
115    pub left: Box<LogicalPlan>,
116    /// Right input plan
117    pub right: Box<LogicalPlan>,
118    /// Equijoin condition
119    pub on: Vec<(Expr, Expr)>,
120    /// Output schema
121    pub schema: Vec<ColumnField>,
122}
123
124/// Union
125/// e.g. SELECT a, b FROM t1 UNION ALL SELECT a, b FROM t2
126#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
127pub struct Union {
128    /// Input plans
129    pub inputs: Vec<LogicalPlan>,
130}