Skip to main content

densa_data/
error.rs

1use std::fmt;
2
3/// Why a table or view operation was refused. Nothing changes when one is returned.
4#[derive(Debug, Clone, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum DataError {
7    /// No column has this name.
8    UnknownColumn(String),
9    /// The key column must hold integers or strings.
10    UnsupportedKey(String),
11    /// Views and groups cannot read this column's data type (lists, structs, binary, ...).
12    UnsupportedColumn(String),
13    /// A filter's value or operation does not fit this column's type (text compared with a
14    /// number, `contains` on a number).
15    LiteralMismatch(String),
16    /// Sums and means need an integer, decimal or float column; this one holds something
17    /// else (text, booleans, dates, times).
18    NotNumeric(String),
19    /// Two columns of a group spec's result would have this name.
20    DuplicateName(String),
21    /// The batch has a different number of columns.
22    ColumnCount {
23        /// Columns in the table.
24        expected: usize,
25        /// Columns in the batch.
26        found: usize,
27    },
28    /// The batch's column at this index has another name or data type.
29    ColumnMismatch {
30        /// Index of the first differing column.
31        column: usize,
32    },
33    /// A row of the batch has no key.
34    NullKey {
35        /// The row, within the batch.
36        row: usize,
37    },
38    /// Keys to delete or find are not of the key column's kind (text for a number key).
39    KeyType,
40    /// More than 2^32 live keys, or rows in one batch.
41    TooManyRows,
42    /// Arrow could not build a batch: compacting a table, or assembling groups.
43    Arrow(String),
44}
45
46impl fmt::Display for DataError {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            Self::UnknownColumn(name) => write!(f, "there is no column named {name:?}"),
50            Self::UnsupportedKey(name) => {
51                write!(f, "key column {name:?} must hold integers or strings")
52            }
53            Self::UnsupportedColumn(name) => {
54                write!(
55                    f,
56                    "views and groups cannot read column {name:?} of this type"
57                )
58            }
59            Self::LiteralMismatch(name) => {
60                write!(f, "the filter does not fit the type of column {name:?}")
61            }
62            Self::NotNumeric(name) => {
63                write!(f, "column {name:?} is not numeric: it cannot be summed")
64            }
65            Self::DuplicateName(name) => write!(f, "two result columns are named {name:?}"),
66            Self::ColumnCount { expected, found } => {
67                write!(f, "the batch has {found} columns, the table {expected}")
68            }
69            Self::ColumnMismatch { column } => {
70                write!(
71                    f,
72                    "column {column} of the batch has another name or data type"
73                )
74            }
75            Self::NullKey { row } => write!(f, "row {row} of the batch has a null key"),
76            Self::KeyType => f.write_str("the keys are not of the key column's kind"),
77            Self::TooManyRows => f.write_str("more than 2^32 live keys, or rows in one batch"),
78            Self::Arrow(message) => write!(f, "Arrow could not build a batch: {message}"),
79        }
80    }
81}
82
83impl std::error::Error for DataError {}
84
85impl DataError {
86    pub(crate) fn arrow(error: arrow_schema::ArrowError) -> Self {
87        Self::Arrow(error.to_string())
88    }
89}