proof_of_sql/sql/proof_plans/
dyn_proof_plan.rs

1use super::{
2    EmptyExec, FilterExec, GroupByExec, ProjectionExec, SliceExec, SortMergeJoinExec, TableExec,
3    UnionExec,
4};
5use crate::{
6    base::{
7        database::{ColumnField, ColumnRef, OwnedTable, Table, TableEvaluation, TableRef},
8        map::{IndexMap, IndexSet},
9        proof::ProofError,
10        scalar::Scalar,
11    },
12    sql::{
13        proof::{
14            FinalRoundBuilder, FirstRoundBuilder, ProofPlan, ProverEvaluate, VerificationBuilder,
15        },
16        proof_exprs::{AliasedDynProofExpr, ColumnExpr, DynProofExpr, TableExpr},
17    },
18};
19use alloc::{boxed::Box, vec::Vec};
20use bumpalo::Bump;
21use serde::{Deserialize, Serialize};
22use sqlparser::ast::Ident;
23
24/// The query plan for proving a query
25#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
26#[enum_dispatch::enum_dispatch]
27pub enum DynProofPlan {
28    /// Source [`ProofPlan`] for (sub)queries without table source such as `SELECT "No table here" as msg;`
29    Empty(EmptyExec),
30    /// Source [`ProofPlan`] for (sub)queries with table source such as `SELECT col from tab;`
31    Table(TableExec),
32    /// Provable expressions for queries of the form
33    /// ```ignore
34    ///     SELECT <result_expr1>, ..., <result_exprN> FROM <table>
35    /// ```
36    Projection(ProjectionExec),
37    /// Provable expressions for queries of the form
38    /// ```ignore
39    ///     SELECT <group_by_expr1>, ..., <group_by_exprM>,
40    ///         SUM(<sum_expr1>.0) as <sum_expr1>.1, ..., SUM(<sum_exprN>.0) as <sum_exprN>.1,
41    ///         COUNT(*) as count_alias
42    ///     FROM <table>
43    ///     WHERE <where_clause>
44    ///     GROUP BY <group_by_expr1>, ..., <group_by_exprM>
45    /// ```
46    GroupBy(GroupByExec),
47    /// Provable expressions for queries of the form, where the result is sent in a dense form
48    /// ```ignore
49    ///     SELECT <result_expr1>, ..., <result_exprN> FROM <table> WHERE <where_clause>
50    /// ```
51    Filter(FilterExec),
52    /// `ProofPlan` for queries of the form
53    /// ```ignore
54    ///     <ProofPlan> LIMIT <fetch> [OFFSET <skip>]
55    /// ```
56    Slice(SliceExec),
57    /// `ProofPlan` for queries of the form
58    /// ```ignore
59    ///     <ProofPlan>
60    ///     UNION ALL
61    ///     <ProofPlan>
62    ///     ...
63    ///     UNION ALL
64    ///     <ProofPlan>
65    /// ```
66    Union(UnionExec),
67    /// `ProofPlan` for queries of the form
68    /// ```ignore
69    ///     <ProofPlan> INNER JOIN <ProofPlan>
70    ///     ON col1 = col2
71    /// ```
72    SortMergeJoin(SortMergeJoinExec),
73}
74
75impl DynProofPlan {
76    /// Creates a new empty plan.
77    #[must_use]
78    pub fn new_empty() -> Self {
79        Self::Empty(EmptyExec::new())
80    }
81
82    /// Creates a new table plan.
83    #[must_use]
84    pub fn new_table(table_ref: TableRef, schema: Vec<ColumnField>) -> Self {
85        Self::Table(TableExec::new(table_ref, schema))
86    }
87
88    /// Creates a new projection plan.
89    #[must_use]
90    pub fn new_projection(aliased_results: Vec<AliasedDynProofExpr>, input: DynProofPlan) -> Self {
91        Self::Projection(ProjectionExec::new(aliased_results, Box::new(input)))
92    }
93
94    /// Creates a new filter plan.
95    #[must_use]
96    pub fn new_filter(
97        aliased_results: Vec<AliasedDynProofExpr>,
98        input: TableExpr,
99        filter_expr: DynProofExpr,
100    ) -> Self {
101        Self::Filter(FilterExec::new(aliased_results, input, filter_expr))
102    }
103
104    /// Creates a new group by plan.
105    #[must_use]
106    pub fn new_group_by(
107        group_by_exprs: Vec<ColumnExpr>,
108        sum_expr: Vec<AliasedDynProofExpr>,
109        count_alias: Ident,
110        table: TableExpr,
111        where_clause: DynProofExpr,
112    ) -> Self {
113        Self::GroupBy(GroupByExec::new(
114            group_by_exprs,
115            sum_expr,
116            count_alias,
117            table,
118            where_clause,
119        ))
120    }
121
122    /// Creates a new slice plan.
123    #[must_use]
124    pub fn new_slice(input: DynProofPlan, skip: usize, fetch: Option<usize>) -> Self {
125        Self::Slice(SliceExec::new(Box::new(input), skip, fetch))
126    }
127
128    /// Creates a new union plan.
129    #[must_use]
130    pub fn new_union(inputs: Vec<DynProofPlan>, schema: Vec<ColumnField>) -> Self {
131        Self::Union(UnionExec::new(inputs, schema))
132    }
133}