-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathquery_expr.rs
170 lines (162 loc) · 6.77 KB
/
query_expr.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use super::{EnrichedExpr, FilterExecBuilder, QueryContextBuilder};
use crate::{
base::{commitment::Commitment, database::SchemaAccessor},
sql::{
parse::ConversionResult,
postprocessing::{
GroupByPostprocessing, OrderByPostprocessing, OwnedTablePostprocessing,
SelectPostprocessing, SlicePostprocessing,
},
proof_plans::{DynProofPlan, GroupByExec},
},
};
use alloc::{fmt, vec, vec::Vec};
use proof_of_sql_parser::{intermediate_ast::SetExpression, Identifier, SelectStatement};
use serde::{Deserialize, Serialize};
#[derive(PartialEq, Serialize, Deserialize)]
/// A `QueryExpr` represents a Proof of SQL query that can be executed against a database.
/// It consists of a `DynProofPlan` for provable components and a vector of `OwnedTablePostprocessing` for the rest.
pub struct QueryExpr<C: Commitment> {
proof_expr: DynProofPlan<C>,
postprocessing: Vec<OwnedTablePostprocessing>,
}
// Implements fmt::Debug to aid in debugging QueryExpr.
// Prints filter and postprocessing fields in a readable format.
impl<C: Commitment> fmt::Debug for QueryExpr<C> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"QueryExpr \n[{:#?},\n{:#?}\n]",
self.proof_expr, self.postprocessing
)
}
}
impl<C: Commitment> QueryExpr<C> {
/// Creates a new `QueryExpr` with the given `DynProofPlan` and `OwnedTablePostprocessing`.
pub fn new(proof_expr: DynProofPlan<C>, postprocessing: Vec<OwnedTablePostprocessing>) -> Self {
Self {
proof_expr,
postprocessing,
}
}
/// Parse an intermediate AST `SelectStatement` into a `QueryExpr`.
pub fn try_new(
ast: SelectStatement,
default_schema: Identifier,
schema_accessor: &dyn SchemaAccessor,
) -> ConversionResult<Self> {
let context = match *ast.expr {
SetExpression::Query {
result_exprs,
from,
where_expr,
group_by,
} => QueryContextBuilder::new(schema_accessor)
.visit_table_expr(&from, default_schema)
.visit_group_by_exprs(group_by)?
.visit_result_exprs(result_exprs)?
.visit_where_expr(where_expr)?
.visit_order_by_exprs(ast.order_by)
.visit_slice_expr(ast.slice)
.build()?,
};
let result_aliased_exprs = context.get_aliased_result_exprs()?.to_vec();
let group_by = context.get_group_by_exprs();
// Figure out the basic postprocessing steps.
let mut postprocessing = vec![];
let order_bys = context.get_order_by_exprs()?;
if !order_bys.is_empty() {
postprocessing.push(OwnedTablePostprocessing::new_order_by(
OrderByPostprocessing::new(order_bys.clone()),
));
}
if let Some(slice) = context.get_slice_expr() {
postprocessing.push(OwnedTablePostprocessing::new_slice(
SlicePostprocessing::new(Some(slice.number_rows), Some(slice.offset_value)),
));
}
if context.has_agg() {
if let Some(group_by_expr) = Option::<GroupByExec<C>>::try_from(&context)? {
Ok(Self {
proof_expr: DynProofPlan::GroupBy(group_by_expr),
postprocessing,
})
} else {
let raw_enriched_exprs = result_aliased_exprs
.iter()
.map(|aliased_expr| EnrichedExpr {
residue_expression: aliased_expr.clone(),
dyn_proof_expr: None,
})
.collect::<Vec<_>>();
let filter = FilterExecBuilder::new(context.get_column_mapping())
.add_table_expr(*context.get_table_ref())
.add_where_expr(context.get_where_expr().clone())?
.add_result_columns(&raw_enriched_exprs)
.build();
let group_by_postprocessing =
GroupByPostprocessing::try_new(group_by.to_vec(), result_aliased_exprs)?;
postprocessing.insert(
0,
OwnedTablePostprocessing::new_group_by(group_by_postprocessing.clone()),
);
let remainder_exprs = group_by_postprocessing.remainder_exprs();
// Check whether we need to do select postprocessing.
// That is, if any of them is not simply a column reference.
if remainder_exprs
.iter()
.any(|expr| expr.try_as_identifier().is_none())
{
postprocessing.insert(
1,
OwnedTablePostprocessing::new_select(SelectPostprocessing::new(
remainder_exprs.to_vec(),
)),
);
}
Ok(Self {
proof_expr: DynProofPlan::Filter(filter),
postprocessing,
})
}
} else {
// No group by, so we need to do a filter.
let column_mapping = context.get_column_mapping();
let enriched_exprs = result_aliased_exprs
.iter()
.map(|aliased_expr| EnrichedExpr::new(aliased_expr.clone(), &column_mapping))
.collect::<Vec<_>>();
let select_exprs = enriched_exprs
.iter()
.map(|enriched_expr| enriched_expr.residue_expression.clone())
.collect::<Vec<_>>();
let filter = FilterExecBuilder::new(context.get_column_mapping())
.add_table_expr(*context.get_table_ref())
.add_where_expr(context.get_where_expr().clone())?
.add_result_columns(&enriched_exprs)
.build();
// Check whether we need to do select postprocessing.
if select_exprs
.iter()
.any(|expr| expr.try_as_identifier().is_none())
{
postprocessing.insert(
0,
OwnedTablePostprocessing::new_select(SelectPostprocessing::new(select_exprs)),
);
}
Ok(Self {
proof_expr: DynProofPlan::Filter(filter),
postprocessing,
})
}
}
/// Immutable access to this query's provable filter expression.
pub fn proof_expr(&self) -> &DynProofPlan<C> {
&self.proof_expr
}
/// Immutable access to this query's post-proof result transform expression.
pub fn postprocessing(&self) -> &[OwnedTablePostprocessing] {
&self.postprocessing
}
}