proof_of_sql/sql/parse/
error.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
171
172
173
174
175
176
177
178
179
180
use crate::base::{
    database::{ColumnOperationError, ColumnType},
    math::decimal::DecimalError,
};
use alloc::{
    boxed::Box,
    format,
    string::{String, ToString},
};
use core::result::Result;
use proof_of_sql_parser::{
    intermediate_decimal::IntermediateDecimalError, posql_time::PoSQLTimestampError, Identifier,
    ResourceId,
};
use snafu::Snafu;

/// Errors from converting an intermediate AST into a provable AST.
#[derive(Snafu, Debug, PartialEq, Eq)]
pub enum ConversionError {
    #[snafu(display("Column '{identifier}' was not found in table '{resource_id}'"))]
    /// The column is missing in the table
    MissingColumn {
        /// The missing column identifier
        identifier: Box<Identifier>,
        /// The table resource id
        resource_id: Box<ResourceId>,
    },

    #[snafu(display("Column '{identifier}' was not found"))]
    /// The column is missing (without table information)
    MissingColumnWithoutTable {
        /// The missing column identifier
        identifier: Box<Identifier>,
    },

    #[snafu(display("Expected '{expected}' but found '{actual}'"))]
    /// Invalid data type received
    InvalidDataType {
        /// Expected data type
        expected: ColumnType,
        /// Actual data type found
        actual: ColumnType,
    },

    #[snafu(display("Left side has '{left_type}' type but right side has '{right_type}' type"))]
    /// Data types do not match
    DataTypeMismatch {
        /// The left side datatype
        left_type: String,
        /// The right side datatype
        right_type: String,
    },

    #[snafu(display("Columns have different lengths: {len_a} != {len_b}"))]
    /// Two columns do not have the same length
    DifferentColumnLength {
        /// The length of the first column
        len_a: usize,
        /// The length of the second column
        len_b: usize,
    },

    #[snafu(display("Multiple result columns with the same alias '{alias}' have been found."))]
    /// Duplicate alias in result columns
    DuplicateResultAlias {
        /// The duplicate alias
        alias: String,
    },

    #[snafu(display(
        "A WHERE clause must has boolean type. It is currently of type '{datatype}'."
    ))]
    /// WHERE clause is not boolean
    NonbooleanWhereClause {
        /// The actual datatype of the WHERE clause
        datatype: ColumnType,
    },

    #[snafu(display(
        "Invalid order by: alias '{alias}' does not appear in the result expressions."
    ))]
    /// ORDER BY clause references a non-existent alias
    InvalidOrderBy {
        /// The non-existent alias in the ORDER BY clause
        alias: String,
    },

    #[snafu(display(
        "Invalid group by: column '{column}' must appear in the group by expression."
    ))]
    /// GROUP BY clause references a non-existent column
    InvalidGroupByColumnRef {
        /// The non-existent column in the GROUP BY clause
        column: String,
    },

    #[snafu(display("Invalid expression: {expression}"))]
    /// General error for invalid expressions
    InvalidExpression {
        /// The invalid expression error
        expression: String,
    },

    #[snafu(display("Encountered parsing error: {error}"))]
    /// General parsing error
    ParseError {
        /// The underlying error
        error: String,
    },

    #[snafu(transparent)]
    /// Errors related to decimal operations
    DecimalConversionError {
        /// The underlying source error
        source: DecimalError,
    },

    /// Errors related to timestamp parsing
    #[snafu(context(false), display("Timestamp conversion error: {source}"))]
    TimestampConversionError {
        /// The underlying source error
        source: PoSQLTimestampError,
    },

    /// Errors related to column operations
    #[snafu(transparent)]
    ColumnOperationError {
        /// The underlying source error
        source: ColumnOperationError,
    },

    /// Errors related to postprocessing
    #[snafu(transparent)]
    PostprocessingError {
        /// The underlying source error
        source: crate::sql::postprocessing::PostprocessingError,
    },

    #[snafu(display("Query not provable because: {error}"))]
    /// Query requires unprovable feature
    Unprovable {
        /// The underlying error
        error: String,
    },
}

impl From<String> for ConversionError {
    fn from(value: String) -> Self {
        ConversionError::ParseError { error: value }
    }
}

impl From<ConversionError> for String {
    fn from(error: ConversionError) -> Self {
        error.to_string()
    }
}

impl From<IntermediateDecimalError> for ConversionError {
    fn from(err: IntermediateDecimalError) -> ConversionError {
        ConversionError::DecimalConversionError {
            source: DecimalError::IntermediateDecimalConversionError { source: err },
        }
    }
}

impl ConversionError {
    /// Returns a `ConversionError::InvalidExpression` for non-numeric types used in numeric aggregation functions.
    pub fn non_numeric_expr_in_agg<S: Into<String>>(dtype: S, func: S) -> Self {
        ConversionError::InvalidExpression {
            expression: format!(
                "cannot use expression of type '{}' with numeric aggregation function '{}'",
                dtype.into().to_lowercase(),
                func.into().to_lowercase()
            ),
        }
    }
}

pub type ConversionResult<T> = Result<T, ConversionError>;