proof_of_sql/sql/proof_exprs/
proof_expr.rs

1use crate::{
2    base::{
3        database::{Column, ColumnRef, ColumnType, LiteralValue, Table},
4        map::{IndexMap, IndexSet},
5        math::decimal::Precision,
6        proof::{PlaceholderResult, ProofError},
7        scalar::Scalar,
8    },
9    sql::proof::{FinalRoundBuilder, VerificationBuilder},
10};
11use bumpalo::Bump;
12use core::fmt::Debug;
13use sqlparser::ast::Ident;
14
15/// Provable AST column expression that evaluates to a `Column`
16#[enum_dispatch::enum_dispatch(DynProofExpr)]
17pub trait ProofExpr: Debug + Send + Sync {
18    /// Get the data type of the expression
19    fn data_type(&self) -> ColumnType;
20
21    /// This returns the result of evaluating the expression on the given table, and returns
22    /// a column of values. This result slice is guaranteed to have length `table_length`.
23    /// Implementations must ensure that the returned slice has length `table_length`.
24    fn first_round_evaluate<'a, S: Scalar>(
25        &self,
26        alloc: &'a Bump,
27        table: &Table<'a, S>,
28        params: &[LiteralValue],
29    ) -> PlaceholderResult<Column<'a, S>>;
30
31    /// Evaluate the expression, add components needed to prove it, and return thet resulting column
32    /// of values
33    fn final_round_evaluate<'a, S: Scalar>(
34        &self,
35        builder: &mut FinalRoundBuilder<'a, S>,
36        alloc: &'a Bump,
37        table: &Table<'a, S>,
38        params: &[LiteralValue],
39    ) -> PlaceholderResult<Column<'a, S>>;
40
41    /// Compute the evaluation of a multilinear extension from this expression
42    /// at the random sumcheck point and adds components needed to verify the expression to
43    /// [`VerificationBuilder<S>`]
44    fn verifier_evaluate<S: Scalar>(
45        &self,
46        builder: &mut impl VerificationBuilder<S>,
47        accessor: &IndexMap<Ident, S>,
48        chi_eval: S,
49        params: &[LiteralValue],
50    ) -> Result<S, ProofError>;
51
52    /// Insert in the [`IndexSet`] `columns` all the column
53    /// references in the `BoolExpr` or forwards the call to some
54    /// subsequent `bool_expr`
55    fn get_column_references(&self, columns: &mut IndexSet<ColumnRef>);
56}
57
58/// A trait for `ProofExpr`s that always return a decimal type
59pub(crate) trait DecimalProofExpr: ProofExpr {
60    /// Get the precision of the expression
61    ///
62    /// # Panics
63    /// This panics if the precision is invalid
64    fn precision(&self) -> Precision {
65        Precision::new(
66            self.data_type()
67                .precision_value()
68                .expect("Precision should be valid"),
69        )
70        .expect("Precision should be valid")
71    }
72
73    /// Get the scale of the expression
74    ///
75    /// # Panics
76    /// This panics if the scale is invalid
77    fn scale(&self) -> i8 {
78        self.data_type().scale().expect("Scale should be valid")
79    }
80}