proof_of_sql/sql/proof/
query_result.rs

1use crate::base::{
2    database::{ColumnCoercionError, OwnedTable, OwnedTableError, TableCoercionError},
3    proof::ProofError,
4    scalar::Scalar,
5};
6use snafu::Snafu;
7
8/// Verifiable query errors
9#[derive(Snafu, Debug)]
10pub enum QueryError {
11    /// The query result overflowed. This does not mean that the verification failed.
12    /// This just means that the database was supposed to respond with a result that was too large.
13    #[snafu(display("Overflow error"))]
14    Overflow,
15    /// The query result string could not be decoded. This does not mean that the verification failed.
16    /// This just means that the database was supposed to respond with a string that was not valid UTF-8.
17    #[snafu(display("String decode error"))]
18    InvalidString,
19    /// Decoding errors other than overflow and invalid string.
20    #[snafu(display("Miscellaneous decoding error"))]
21    MiscellaneousDecodingError,
22    /// Miscellaneous evaluation error.
23    #[snafu(display("Miscellaneous evaluation error"))]
24    MiscellaneousEvaluationError,
25    /// The proof failed to verify.
26    #[snafu(transparent)]
27    ProofError {
28        /// The underlying source error
29        source: ProofError,
30    },
31    /// The table data was invalid. This should never happen because this should get caught by the verifier before reaching this point.
32    #[snafu(transparent)]
33    InvalidTable {
34        /// The underlying source error
35        source: OwnedTableError,
36    },
37    /// The number of columns in the table was invalid.
38    #[snafu(display("Invalid number of columns"))]
39    InvalidColumnCount,
40}
41
42impl From<TableCoercionError> for QueryError {
43    fn from(error: TableCoercionError) -> Self {
44        match error {
45            TableCoercionError::ColumnCoercionError {
46                source: ColumnCoercionError::Overflow,
47            } => QueryError::Overflow,
48            TableCoercionError::ColumnCoercionError {
49                source: ColumnCoercionError::InvalidTypeCoercion,
50            } => ProofError::InvalidTypeCoercion.into(),
51            TableCoercionError::NameMismatch => ProofError::FieldNamesMismatch.into(),
52            TableCoercionError::ColumnCountMismatch => ProofError::FieldCountMismatch.into(),
53        }
54    }
55}
56
57/// The verified results of a query along with metadata produced by verification
58pub struct QueryData<S: Scalar> {
59    /// We use Apache Arrow's [`RecordBatch`] to represent a table
60    /// result so as to allow for easy interoperability with
61    /// Apache Arrow Flight.
62    ///
63    /// See `<https://voltrondata.com/blog/apache-arrow-flight-primer/>`
64    pub table: OwnedTable<S>,
65    /// Additionally, there is a 32-byte verification hash that is included with this table.
66    /// This hash provides evidence that the verification has been run.
67    pub verification_hash: [u8; 32],
68}
69
70/// The result of a query -- either an error or a table.
71pub type QueryResult<S> = Result<QueryData<S>, QueryError>;