forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception.cpp
More file actions
385 lines (323 loc) · 16.2 KB
/
exception.cpp
File metadata and controls
385 lines (323 loc) · 16.2 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "duckdb/common/exception.hpp"
#include "duckdb/common/string_util.hpp"
#include "duckdb/common/to_string.hpp"
#include "duckdb/common/types.hpp"
#include "duckdb/common/exception/list.hpp"
#include "duckdb/parser/tableref.hpp"
#include "duckdb/planner/expression.hpp"
#ifdef DUCKDB_CRASH_ON_ASSERT
#include "duckdb/common/printer.hpp"
#include <stdio.h>
#include <stdlib.h>
#endif
#include "duckdb/common/stacktrace.hpp"
namespace duckdb {
Exception::Exception(ExceptionType exception_type, const string &message)
: std::runtime_error(ToJSON(exception_type, message)) {
}
Exception::Exception(ExceptionType exception_type, const string &message,
const unordered_map<string, string> &extra_info)
: std::runtime_error(ToJSON(exception_type, message, extra_info)) {
}
string Exception::ToJSON(ExceptionType type, const string &message) {
unordered_map<string, string> extra_info;
return ToJSON(type, message, extra_info);
}
string Exception::ToJSON(ExceptionType type, const string &message, const unordered_map<string, string> &extra_info) {
#ifndef DUCKDB_DEBUG_STACKTRACE
// by default we only enable stack traces for internal exceptions
if (type == ExceptionType::INTERNAL || type == ExceptionType::FATAL)
#endif
{
auto extended_extra_info = extra_info;
// We only want to add the stack trace pointers if they are not already present, otherwise the original
// stack traces are lost
if (extended_extra_info.find("stack_trace_pointers") == extended_extra_info.end() &&
extended_extra_info.find("stack_trace") == extended_extra_info.end()) {
extended_extra_info["stack_trace_pointers"] = StackTrace::GetStacktracePointers();
}
return StringUtil::ExceptionToJSONMap(type, message, extended_extra_info);
}
return StringUtil::ExceptionToJSONMap(type, message, extra_info);
}
bool Exception::UncaughtException() {
#if __cplusplus >= 201703L
return std::uncaught_exceptions() > 0;
#else
return std::uncaught_exception();
#endif
}
bool Exception::InvalidatesTransaction(ExceptionType exception_type) {
switch (exception_type) {
case ExceptionType::BINDER:
case ExceptionType::CATALOG:
case ExceptionType::CONNECTION:
case ExceptionType::PARAMETER_NOT_ALLOWED:
case ExceptionType::PARSER:
case ExceptionType::PERMISSION:
return false;
default:
return true;
}
}
bool Exception::InvalidatesDatabase(ExceptionType exception_type) {
switch (exception_type) {
case ExceptionType::FATAL:
return true;
default:
return false;
}
}
string Exception::GetStackTrace(idx_t max_depth) {
return StackTrace::GetStackTrace(max_depth);
}
string Exception::ConstructMessageRecursive(const string &msg, std::vector<ExceptionFormatValue> &values) {
#ifdef DEBUG
// Verify that we have the required amount of values for the message
idx_t parameter_count = 0;
for (idx_t i = 0; i + 1 < msg.size(); i++) {
if (msg[i] != '%') {
continue;
}
if (msg[i + 1] == '%') {
i++;
continue;
}
parameter_count++;
}
if (parameter_count != values.size()) {
throw InternalException("Primary exception: %s\nSecondary exception in ConstructMessageRecursive: Expected %d "
"parameters, received %d",
msg.c_str(), parameter_count, values.size());
}
#endif
return ExceptionFormatValue::Format(msg, values);
}
struct ExceptionEntry {
ExceptionType type;
char text[48];
};
static constexpr ExceptionEntry EXCEPTION_MAP[] = {{ExceptionType::INVALID, "Invalid"},
{ExceptionType::OUT_OF_RANGE, "Out of Range"},
{ExceptionType::CONVERSION, "Conversion"},
{ExceptionType::UNKNOWN_TYPE, "Unknown Type"},
{ExceptionType::DECIMAL, "Decimal"},
{ExceptionType::MISMATCH_TYPE, "Mismatch Type"},
{ExceptionType::DIVIDE_BY_ZERO, "Divide by Zero"},
{ExceptionType::OBJECT_SIZE, "Object Size"},
{ExceptionType::INVALID_TYPE, "Invalid type"},
{ExceptionType::SERIALIZATION, "Serialization"},
{ExceptionType::TRANSACTION, "TransactionContext"},
{ExceptionType::NOT_IMPLEMENTED, "Not implemented"},
{ExceptionType::EXPRESSION, "Expression"},
{ExceptionType::CATALOG, "Catalog"},
{ExceptionType::PARSER, "Parser"},
{ExceptionType::BINDER, "Binder"},
{ExceptionType::PLANNER, "Planner"},
{ExceptionType::SCHEDULER, "Scheduler"},
{ExceptionType::EXECUTOR, "Executor"},
{ExceptionType::CONSTRAINT, "Constraint"},
{ExceptionType::INDEX, "Index"},
{ExceptionType::STAT, "Stat"},
{ExceptionType::CONNECTION, "Connection"},
{ExceptionType::SYNTAX, "Syntax"},
{ExceptionType::SETTINGS, "Settings"},
{ExceptionType::OPTIMIZER, "Optimizer"},
{ExceptionType::NULL_POINTER, "NullPointer"},
{ExceptionType::IO, "IO"},
{ExceptionType::INTERRUPT, "INTERRUPT"},
{ExceptionType::FATAL, "FATAL"},
{ExceptionType::INTERNAL, "INTERNAL"},
{ExceptionType::INVALID_INPUT, "Invalid Input"},
{ExceptionType::OUT_OF_MEMORY, "Out of Memory"},
{ExceptionType::PERMISSION, "Permission"},
{ExceptionType::PARAMETER_NOT_RESOLVED, "Parameter Not Resolved"},
{ExceptionType::PARAMETER_NOT_ALLOWED, "Parameter Not Allowed"},
{ExceptionType::DEPENDENCY, "Dependency"},
{ExceptionType::MISSING_EXTENSION, "Missing Extension"},
{ExceptionType::HTTP, "HTTP"},
{ExceptionType::AUTOLOAD, "Extension Autoloading"},
{ExceptionType::SEQUENCE, "Sequence"},
{ExceptionType::INVALID_CONFIGURATION, "Invalid Configuration"}};
string Exception::ExceptionTypeToString(ExceptionType type) {
for (auto &e : EXCEPTION_MAP) {
if (e.type == type) {
return e.text;
}
}
return "Unknown";
}
ExceptionType Exception::StringToExceptionType(const string &type) {
for (auto &e : EXCEPTION_MAP) {
if (e.text == type) {
return e.type;
}
}
return ExceptionType::INVALID;
}
unordered_map<string, string> Exception::InitializeExtraInfo(const Expression &expr) {
return InitializeExtraInfo(expr.GetQueryLocation());
}
unordered_map<string, string> Exception::InitializeExtraInfo(const ParsedExpression &expr) {
return InitializeExtraInfo(expr.GetQueryLocation());
}
unordered_map<string, string> Exception::InitializeExtraInfo(const QueryErrorContext &error_context) {
return InitializeExtraInfo(error_context.query_location);
}
unordered_map<string, string> Exception::InitializeExtraInfo(const TableRef &ref) {
return InitializeExtraInfo(ref.query_location);
}
unordered_map<string, string> Exception::InitializeExtraInfo(optional_idx error_location) {
unordered_map<string, string> result;
SetQueryLocation(error_location, result);
return result;
}
bool Exception::IsExecutionError(ExceptionType type) {
switch (type) {
case ExceptionType::INVALID_INPUT:
case ExceptionType::OUT_OF_RANGE:
case ExceptionType::CONVERSION:
return true;
default:
return false;
}
}
unordered_map<string, string> Exception::InitializeExtraInfo(const string &subtype, optional_idx error_location) {
unordered_map<string, string> result;
result["error_subtype"] = subtype;
SetQueryLocation(error_location, result);
return result;
}
void Exception::SetQueryLocation(optional_idx error_location, unordered_map<string, string> &extra_info) {
if (error_location.IsValid()) {
extra_info["position"] = to_string(error_location.GetIndex());
}
}
InvalidTypeException::InvalidTypeException(PhysicalType type, const string &msg)
: Exception(ExceptionType::INVALID_TYPE, "Invalid Type [" + TypeIdToString(type) + "]: " + msg) {
}
InvalidTypeException::InvalidTypeException(const LogicalType &type, const string &msg)
: Exception(ExceptionType::INVALID_TYPE, "Invalid Type [" + type.ToString() + "]: " + msg) {
}
InvalidTypeException::InvalidTypeException(const string &msg) : Exception(ExceptionType::INVALID_TYPE, msg) {
}
TypeMismatchException::TypeMismatchException(const PhysicalType type_1, const PhysicalType type_2, const string &msg)
: Exception(ExceptionType::MISMATCH_TYPE,
"Type " + TypeIdToString(type_1) + " does not match with " + TypeIdToString(type_2) + ". " + msg) {
}
TypeMismatchException::TypeMismatchException(const LogicalType &type_1, const LogicalType &type_2, const string &msg)
: TypeMismatchException(optional_idx(), type_1, type_2, msg) {
}
TypeMismatchException::TypeMismatchException(optional_idx error_location, const LogicalType &type_1,
const LogicalType &type_2, const string &msg)
: Exception(ExceptionType::MISMATCH_TYPE,
"Type " + type_1.ToString() + " does not match with " + type_2.ToString() + ". " + msg,
Exception::InitializeExtraInfo(error_location)) {
}
TypeMismatchException::TypeMismatchException(const string &msg) : Exception(ExceptionType::MISMATCH_TYPE, msg) {
}
TransactionException::TransactionException(const string &msg) : Exception(ExceptionType::TRANSACTION, msg) {
}
NotImplementedException::NotImplementedException(const string &msg) : Exception(ExceptionType::NOT_IMPLEMENTED, msg) {
}
OutOfRangeException::OutOfRangeException(const string &msg) : Exception(ExceptionType::OUT_OF_RANGE, msg) {
}
OutOfRangeException::OutOfRangeException(const int64_t value, const PhysicalType orig_type, const PhysicalType new_type)
: Exception(ExceptionType::OUT_OF_RANGE, "Type " + TypeIdToString(orig_type) + " with value " +
to_string((intmax_t)value) +
" can't be cast because the value is out of range "
"for the destination type " +
TypeIdToString(new_type)) {
}
OutOfRangeException::OutOfRangeException(const double value, const PhysicalType orig_type, const PhysicalType new_type)
: Exception(ExceptionType::OUT_OF_RANGE, "Type " + TypeIdToString(orig_type) + " with value " + to_string(value) +
" can't be cast because the value is out of range "
"for the destination type " +
TypeIdToString(new_type)) {
}
OutOfRangeException::OutOfRangeException(const hugeint_t value, const PhysicalType orig_type,
const PhysicalType new_type)
: Exception(ExceptionType::OUT_OF_RANGE, "Type " + TypeIdToString(orig_type) + " with value " + value.ToString() +
" can't be cast because the value is out of range "
"for the destination type " +
TypeIdToString(new_type)) {
}
OutOfRangeException::OutOfRangeException(const PhysicalType var_type, const idx_t length)
: Exception(ExceptionType::OUT_OF_RANGE,
"The value is too long to fit into type " + TypeIdToString(var_type) + "(" + to_string(length) + ")") {
}
ConnectionException::ConnectionException(const string &msg) : Exception(ExceptionType::CONNECTION, msg) {
}
PermissionException::PermissionException(const string &msg) : Exception(ExceptionType::PERMISSION, msg) {
}
SyntaxException::SyntaxException(const string &msg) : Exception(ExceptionType::SYNTAX, msg) {
}
ExecutorException::ExecutorException(const string &msg) : Exception(ExceptionType::EXECUTOR, msg) {
}
ConstraintException::ConstraintException(const string &msg) : Exception(ExceptionType::CONSTRAINT, msg) {
}
DependencyException::DependencyException(const string &msg) : Exception(ExceptionType::DEPENDENCY, msg) {
}
IOException::IOException(const string &msg) : Exception(ExceptionType::IO, msg) {
}
IOException::IOException(const string &msg, const unordered_map<string, string> &extra_info)
: Exception(ExceptionType::IO, msg, extra_info) {
}
MissingExtensionException::MissingExtensionException(const string &msg)
: Exception(ExceptionType::MISSING_EXTENSION, msg) {
}
AutoloadException::AutoloadException(const string &extension_name, const string &message)
: Exception(ExceptionType::AUTOLOAD,
"An error occurred while trying to automatically install the required extension '" + extension_name +
"':\n" + message) {
}
SerializationException::SerializationException(const string &msg) : Exception(ExceptionType::SERIALIZATION, msg) {
}
SequenceException::SequenceException(const string &msg) : Exception(ExceptionType::SEQUENCE, msg) {
}
InterruptException::InterruptException() : Exception(ExceptionType::INTERRUPT, "Interrupted!") {
}
FatalException::FatalException(ExceptionType type, const string &msg) : Exception(type, msg) {
}
InternalException::InternalException(const string &msg) : Exception(ExceptionType::INTERNAL, msg) {
#ifdef DUCKDB_CRASH_ON_ASSERT
Printer::Print("ABORT THROWN BY INTERNAL EXCEPTION: " + msg + "\n" + StackTrace::GetStackTrace());
abort();
#endif
}
InvalidInputException::InvalidInputException(const string &msg) : Exception(ExceptionType::INVALID_INPUT, msg) {
}
InvalidInputException::InvalidInputException(const string &msg, const unordered_map<string, string> &extra_info)
: Exception(ExceptionType::INVALID_INPUT, msg, extra_info) {
}
InvalidConfigurationException::InvalidConfigurationException(const string &msg)
: Exception(ExceptionType::INVALID_CONFIGURATION, msg) {
}
InvalidConfigurationException::InvalidConfigurationException(const string &msg,
const unordered_map<string, string> &extra_info)
: Exception(ExceptionType::INVALID_CONFIGURATION, msg, extra_info) {
}
OutOfMemoryException::OutOfMemoryException(const string &msg)
: Exception(ExceptionType::OUT_OF_MEMORY, ExtendOutOfMemoryError(msg)) {
}
string OutOfMemoryException::ExtendOutOfMemoryError(const string &msg) {
string link = "https://duckdb.org/docs/stable/guides/performance/how_to_tune_workloads";
if (StringUtil::Contains(msg, link)) {
// already extended
return msg;
}
string new_msg = msg;
new_msg += "\n\nPossible solutions:\n";
new_msg += "* Reducing the number of threads (SET threads=X)\n";
new_msg += "* Disabling insertion-order preservation (SET preserve_insertion_order=false)\n";
new_msg += "* Increasing the memory limit (SET memory_limit='...GB')\n";
new_msg += "\nSee also " + link;
return new_msg;
}
ParameterNotAllowedException::ParameterNotAllowedException(const string &msg)
: Exception(ExceptionType::PARAMETER_NOT_ALLOWED, msg) {
}
ParameterNotResolvedException::ParameterNotResolvedException()
: Exception(ExceptionType::PARAMETER_NOT_RESOLVED, "Parameter types could not be resolved") {
}
} // namespace duckdb