proof_of_sql/sql/proof/
query_result.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
use crate::base::{
    database::{ColumnCoercionError, OwnedTable, OwnedTableError, TableCoercionError},
    proof::ProofError,
    scalar::Scalar,
};
use snafu::Snafu;

/// Verifiable query errors
#[derive(Snafu, Debug)]
pub enum QueryError {
    /// The query result overflowed. This does not mean that the verification failed.
    /// This just means that the database was supposed to respond with a result that was too large.
    #[snafu(display("Overflow error"))]
    Overflow,
    /// The query result string could not be decoded. This does not mean that the verification failed.
    /// This just means that the database was supposed to respond with a string that was not valid UTF-8.
    #[snafu(display("String decode error"))]
    InvalidString,
    /// Decoding errors other than overflow and invalid string.
    #[snafu(display("Miscellaneous decoding error"))]
    MiscellaneousDecodingError,
    /// Miscellaneous evaluation error.
    #[snafu(display("Miscellaneous evaluation error"))]
    MiscellaneousEvaluationError,
    /// The proof failed to verify.
    #[snafu(transparent)]
    ProofError {
        /// The underlying source error
        source: ProofError,
    },
    /// The table data was invalid. This should never happen because this should get caught by the verifier before reaching this point.
    #[snafu(transparent)]
    InvalidTable {
        /// The underlying source error
        source: OwnedTableError,
    },
    /// The number of columns in the table was invalid.
    #[snafu(display("Invalid number of columns"))]
    InvalidColumnCount,
}

impl From<TableCoercionError> for QueryError {
    fn from(error: TableCoercionError) -> Self {
        match error {
            TableCoercionError::ColumnCoercionError {
                source: ColumnCoercionError::Overflow,
            } => QueryError::Overflow,
            TableCoercionError::ColumnCoercionError {
                source: ColumnCoercionError::InvalidTypeCoercion,
            } => ProofError::InvalidTypeCoercion.into(),
            TableCoercionError::NameMismatch => ProofError::FieldNamesMismatch.into(),
            TableCoercionError::ColumnCountMismatch => ProofError::FieldCountMismatch.into(),
        }
    }
}

/// The verified results of a query along with metadata produced by verification
pub struct QueryData<S: Scalar> {
    /// We use Apache Arrow's [`RecordBatch`] to represent a table
    /// result so as to allow for easy interoperability with
    /// Apache Arrow Flight.
    ///
    /// See `<https://voltrondata.com/blog/apache-arrow-flight-primer/>`
    pub table: OwnedTable<S>,
    /// Additionally, there is a 32-byte verification hash that is included with this table.
    /// This hash provides evidence that the verification has been run.
    pub verification_hash: [u8; 32],
}

/// The result of a query -- either an error or a table.
pub type QueryResult<S> = Result<QueryData<S>, QueryError>;