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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use crate::common::symbol::Symbol;
/// Errors produced during the binding phase.
///
/// Binding validates names against the catalog and resolves references.
/// All errors carry [`Symbol`]s — resolve via the interner for display.
#[derive(Debug, Clone, PartialEq)]
pub enum BindError {
/// A `CREATE DATABASE` was attempted but the database already exists
/// and `IF NOT EXISTS` was not specified.
DatabaseAlreadyExists(Symbol),
/// The role specified in `OWNER` does not exist in the catalog.
RoleNotFound(Symbol),
/// The tablespace specified in `TABLESPACE` does not exist in the catalog.
TablespaceNotFound(Symbol),
/// `CONNECTION LIMIT` was given a value below -1.
InvalidConnectionLimit(i64),
/// A `CREATE SCHEMA` was attempted but the schema already exists
/// and `IF NOT EXISTS` was not specified.
SchemaAlreadyExists(Symbol),
/// A `CREATE SCHEMA` referenced a database that does not exist.
DatabaseNotFound(Symbol),
/// A `CREATE TABLE` referenced a schema that does not exist.
SchemaNotFound(Symbol),
/// A `CREATE TABLE` was attempted but the table already exists
/// and `IF NOT EXISTS` was not specified.
TableAlreadyExists(Symbol),
/// A `CREATE TABLE` defined the same column name more than once.
DuplicateColumnName(Symbol),
/// A table-level constraint (e.g. `PRIMARY KEY (col)`, `UNIQUE (col)`,
/// `FOREIGN KEY (col) ...`) referenced a column name that is not
/// defined on this table.
ColumnNotFound(Symbol),
/// More than one `PRIMARY KEY` was specified for a table — either
/// via multiple column-level `PRIMARY KEY` constraints, multiple
/// table-level `PRIMARY KEY` constraints, or a combination of both.
MultiplePrimaryKeys,
/// A table referenced by a query does not exist in the catalog.
TableNotFound(Symbol),
/// The query specifies an INSERT source that is not supported.
UnsupportedInsertSource,
/// The query contains an expression that is not supported.
UnsupportedExpression,
/// The query contains an ON CONFLICT clause that is not supported.
UnsupportedOnConflict,
/// The query contains a RETURNING clause that is not supported.
UnsupportedReturning,
/// The number of target columns does not match the number of source columns.
ColumnCountMismatch { expected: usize, found: usize },
/// An `INSERT` left a `NOT NULL` column with no supplied value.
///
/// Distinct from [`BindError::ColumnCountMismatch`] — the row had the
/// right number of values, but a required column simply wasn't among
/// the columns targeted by the statement.
MissingNotNullColumn(Symbol),
/// A `SELECT` statement feature which is not yet implemented.
UnsupportedSelect,
/// A supplied value is incompatible with the column's declared data type.
///
/// `col` — the column's interned name symbol
/// `row` — 0-based row index in the VALUES list (for diagnostics)
/// `expected` — human-readable name of the declared type
/// `got` — human-readable description of the actual value kind
TypeMismatch {
col: Symbol,
row: usize,
expected: &'static str,
got: &'static str,
},
/// Division by zero attempted in an expression evaluation.
DivisionByZero,
}
impl std::fmt::Display for BindError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BindError::DatabaseAlreadyExists(s) => {
write!(f, "database {:?} already exists", s)
}
BindError::RoleNotFound(s) => {
write!(f, "role {:?} does not exist", s)
}
BindError::TablespaceNotFound(s) => {
write!(f, "tablespace {:?} does not exist", s)
}
BindError::InvalidConnectionLimit(n) => {
write!(f, "invalid connection limit {}, must be >= -1", n)
}
BindError::SchemaAlreadyExists(s) => {
write!(f, "schema {:?} already exist", s)
}
BindError::DatabaseNotFound(s) => {
write!(f, "database {:?} does not exist", s)
}
BindError::SchemaNotFound(s) => {
write!(f, "schema {:?} does not exist", s)
}
BindError::TableAlreadyExists(s) => {
write!(f, "table {:?} already exist", s)
}
BindError::DuplicateColumnName(s) => {
write!(f, "column {:?} specified more than once", s)
}
BindError::ColumnNotFound(s) => {
write!(f, "column {:?} does not exist", s)
}
BindError::MultiplePrimaryKeys => {
write!(f, "multiple primary keys for table specified")
}
BindError::TableNotFound(s) => {
write!(f, "table {:?} does not exist", s)
}
BindError::UnsupportedInsertSource => {
write!(f, "unsupported insert source")
}
BindError::UnsupportedExpression => {
write!(f, "unsupported expression")
}
BindError::UnsupportedOnConflict => {
write!(f, "unsupported ON CONFLICT clause")
}
BindError::UnsupportedReturning => {
write!(f, "unsupported RETURNING clause")
}
BindError::ColumnCountMismatch { expected, found } => {
write!(
f,
"column count mismatch: expected {}, found {}",
expected, found
)
}
BindError::MissingNotNullColumn(s) => {
write!(f, "column {:?} is NOT NULL but was not given a value", s)
}
BindError::UnsupportedSelect => {
write!(f, "unsupported feature request for select")
}
BindError::TypeMismatch {
col,
row,
expected,
got,
} => {
write!(
f,
"type mismatch at row {}: column {:?} expects {} but got {}",
row, col, expected, got
)
}
BindError::DivisionByZero => {
write!(f, "division by zero")
}
}
}
}
impl BindError {
pub fn format(&self, interner: &crate::common::Interner) -> String {
match self {
BindError::DatabaseAlreadyExists(s) => {
format!("database \"{}\" already exists", interner.resolve(*s))
}
BindError::RoleNotFound(s) => {
format!("role \"{}\" does not exist", interner.resolve(*s))
}
BindError::TablespaceNotFound(s) => {
format!("tablespace \"{}\" does not exist", interner.resolve(*s))
}
BindError::InvalidConnectionLimit(n) => {
format!("invalid connection limit {}, must be >= -1", n)
}
BindError::SchemaAlreadyExists(s) => {
format!("schema \"{}\" already exists", interner.resolve(*s))
}
BindError::DatabaseNotFound(s) => {
format!("database \"{}\" does not exist", interner.resolve(*s))
}
BindError::SchemaNotFound(s) => {
format!("schema \"{}\" does not exist", interner.resolve(*s))
}
BindError::TableAlreadyExists(s) => {
format!("table \"{}\" already exists", interner.resolve(*s))
}
BindError::DuplicateColumnName(s) => {
format!(
"column \"{}\" specified more than once",
interner.resolve(*s)
)
}
BindError::ColumnNotFound(s) => {
format!("column \"{}\" does not exist", interner.resolve(*s))
}
BindError::MultiplePrimaryKeys => {
"multiple primary keys for table specified".to_string()
}
BindError::TableNotFound(s) => {
format!("table \"{}\" does not exist", interner.resolve(*s))
}
BindError::UnsupportedInsertSource => "unsupported insert source".to_string(),
BindError::UnsupportedExpression => "unsupported expression".to_string(),
BindError::UnsupportedOnConflict => "unsupported ON CONFLICT clause".to_string(),
BindError::UnsupportedReturning => "unsupported RETURNING clause".to_string(),
BindError::ColumnCountMismatch { expected, found } => {
format!(
"column count mismatch: expected {}, found {}",
expected, found
)
}
BindError::MissingNotNullColumn(s) => {
format!(
"column \"{}\" is NOT NULL but was not given a value",
interner.resolve(*s)
)
}
BindError::UnsupportedSelect => "unsupported feature request for select".to_string(),
BindError::TypeMismatch {
col,
row,
expected,
got,
} => {
format!(
"type mismatch at row {}: column \"{}\" expects {} but got {}",
row,
interner.resolve(*col),
expected,
got
)
}
BindError::DivisionByZero => "division by zero".to_string(),
}
}
}
impl std::error::Error for BindError {}