proof_of_sql/sql/proof_plans/
dyn_proof_plan.rs1use super::{
2 AggregateExec, EmptyExec, FilterExec, GroupByExec, LegacyFilterExec, ProjectionExec, SliceExec,
3 SortMergeJoinExec, TableExec, UnionExec,
4};
5use crate::{
6 base::{
7 database::{ColumnField, ColumnRef, LiteralValue, Table, TableEvaluation, TableRef},
8 map::{IndexMap, IndexSet},
9 proof::{PlaceholderResult, ProofError},
10 scalar::Scalar,
11 },
12 sql::{
13 proof::{
14 FinalRoundBuilder, FirstRoundBuilder, ProofPlan, ProverEvaluate, VerificationBuilder,
15 },
16 proof_exprs::{AliasedDynProofExpr, ColumnExpr, DynProofExpr, TableExpr},
17 AnalyzeResult,
18 },
19};
20use alloc::{boxed::Box, vec::Vec};
21use bumpalo::Bump;
22use serde::{Deserialize, Serialize};
23use sqlparser::ast::Ident;
24
25#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
27#[enum_dispatch::enum_dispatch]
28pub enum DynProofPlan {
29 Empty(EmptyExec),
31 Table(TableExec),
33 Projection(ProjectionExec),
38 GroupBy(GroupByExec),
48 Aggregate(AggregateExec),
59 LegacyFilter(LegacyFilterExec),
64 Filter(FilterExec),
70 Slice(SliceExec),
75 Union(UnionExec),
85 SortMergeJoin(SortMergeJoinExec),
91}
92
93impl DynProofPlan {
94 #[must_use]
96 pub fn new_empty() -> Self {
97 Self::Empty(EmptyExec::new())
98 }
99
100 #[must_use]
102 pub fn new_table(table_ref: TableRef, schema: Vec<ColumnField>) -> Self {
103 Self::Table(TableExec::new(table_ref, schema))
104 }
105
106 #[must_use]
108 pub fn new_projection(aliased_results: Vec<AliasedDynProofExpr>, input: DynProofPlan) -> Self {
109 Self::Projection(ProjectionExec::new(aliased_results, Box::new(input)))
110 }
111
112 #[must_use]
114 pub fn new_legacy_filter(
115 aliased_results: Vec<AliasedDynProofExpr>,
116 input: TableExpr,
117 filter_expr: DynProofExpr,
118 ) -> Self {
119 Self::LegacyFilter(LegacyFilterExec::new(aliased_results, input, filter_expr))
120 }
121
122 #[must_use]
124 pub fn try_new_group_by(
125 group_by_exprs: Vec<ColumnExpr>,
126 sum_expr: Vec<AliasedDynProofExpr>,
127 count_alias: Ident,
128 table: TableExpr,
129 where_clause: DynProofExpr,
130 ) -> Option<Self> {
131 GroupByExec::try_new(group_by_exprs, sum_expr, count_alias, table, where_clause)
132 .map(Self::GroupBy)
133 }
134
135 pub fn try_new_aggregate(
137 group_by_exprs: Vec<AliasedDynProofExpr>,
138 sum_expr: Vec<AliasedDynProofExpr>,
139 count_alias: Ident,
140 input: DynProofPlan,
141 where_clause: DynProofExpr,
142 ) -> Result<Self, super::AggregateExecError> {
143 AggregateExec::try_new(
144 group_by_exprs,
145 sum_expr,
146 count_alias,
147 Box::new(input),
148 where_clause,
149 )
150 .map(Self::Aggregate)
151 }
152
153 #[must_use]
155 pub fn new_slice(input: DynProofPlan, skip: usize, fetch: Option<usize>) -> Self {
156 Self::Slice(SliceExec::new(Box::new(input), skip, fetch))
157 }
158
159 pub fn try_new_union(inputs: Vec<DynProofPlan>) -> AnalyzeResult<Self> {
161 UnionExec::try_new(inputs).map(Self::Union)
162 }
163
164 #[must_use]
166 pub fn new_filter(
167 aliased_results: Vec<AliasedDynProofExpr>,
168 input: DynProofPlan,
169 filter_expr: DynProofExpr,
170 ) -> Self {
171 Self::Filter(FilterExec::new(
172 aliased_results,
173 Box::new(input),
174 filter_expr,
175 ))
176 }
177
178 pub(crate) fn get_column_result_fields_as_references(&self) -> IndexSet<ColumnRef> {
180 self.get_column_result_fields()
181 .into_iter()
182 .map(|f| ColumnRef::new(TableRef::from_names(None, ""), f.name(), f.data_type()))
183 .collect()
184 }
185}