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