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
use crate::{
base::{
database::{Column, ColumnRef, ColumnType, LiteralValue, Table},
map::{IndexMap, IndexSet},
math::decimal::Precision,
proof::{PlaceholderResult, ProofError},
scalar::Scalar,
},
sql::proof::{FinalRoundBuilder, VerificationBuilder},
};
use bumpalo::Bump;
use core::fmt::Debug;
use sqlparser::ast::Ident;
/// Provable AST column expression that evaluates to a `Column`
#[enum_dispatch::enum_dispatch(DynProofExpr)]
pub trait ProofExpr: Debug + Send + Sync {
/// Get the data type of the expression
fn data_type(&self) -> ColumnType;
/// This returns the result of evaluating the expression on the given table, and returns
/// a column of values. This result slice is guaranteed to have length `table_length`.
/// Implementations must ensure that the returned slice has length `table_length`.
fn first_round_evaluate<'a, S: Scalar>(
&self,
alloc: &'a Bump,
table: &Table<'a, S>,
params: &[LiteralValue],
) -> PlaceholderResult<Column<'a, S>>;
/// Evaluate the expression, add components needed to prove it, and return thet resulting column
/// of values
fn final_round_evaluate<'a, S: Scalar>(
&self,
builder: &mut FinalRoundBuilder<'a, S>,
alloc: &'a Bump,
table: &Table<'a, S>,
params: &[LiteralValue],
) -> PlaceholderResult<Column<'a, S>>;
/// Compute the evaluation of a multilinear extension from this expression
/// at the random sumcheck point and adds components needed to verify the expression to
/// [`VerificationBuilder<S>`]
fn verifier_evaluate<S: Scalar>(
&self,
builder: &mut impl VerificationBuilder<S>,
accessor: &IndexMap<Ident, S>,
chi_eval: S,
params: &[LiteralValue],
) -> Result<S, ProofError>;
/// Insert in the [`IndexSet`] `columns` all the column
/// references in the `BoolExpr` or forwards the call to some
/// subsequent `bool_expr`
fn get_column_references(&self, columns: &mut IndexSet<ColumnRef>);
}
/// A trait for `ProofExpr`s that always return a decimal type
pub(crate) trait DecimalProofExpr: ProofExpr {
/// Get the precision of the expression
///
/// # Panics
/// This panics if the precision is invalid
fn precision(&self) -> Precision {
Precision::new(
self.data_type()
.precision_value()
.expect("Precision should be valid"),
)
.expect("Precision should be valid")
}
/// Get the scale of the expression
///
/// # Panics
/// This panics if the scale is invalid
fn scale(&self) -> i8 {
self.data_type().scale().expect("Scale should be valid")
}
}