Repository navigation
Expand file tree
/
Copy pathdecoding.cpp
More file actions
518 lines (491 loc) · 16.1 KB
/
Copy pathdecoding.cpp
File metadata and controls
518 lines (491 loc) · 16.1 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include <teaql/provider_sqlite/detail/decoding.hpp>
#include <teaql/core/detail/text.hpp>
#include <sqlite3.h>
#include <array>
#include <charconv>
#include <stdexcept>
namespace teaql::detail {
namespace {
using Decoded = Result<Value, MutationExecutorError>;
template <class T>
void append_number(String& out, T value) {
char buffer[32];
auto end = std::to_chars(buffer, buffer + sizeof(buffer), value).ptr;
out.append(buffer, end);
}
Decoded invalid(Memory r, std::string_view type, std::string_view message) {
String text("invalid sqlite ", r);
text += type;
text += ": ";
text += message;
return Decoded::err(r, MutationExecutorError::bind(r, text));
}
std::optional<String> utf8_error(Memory r, std::string_view bytes) {
if (auto error = SqliteUtf8Error::validate(bytes)) {
return error->to_string(r);
}
return {};
}
enum class TimeError { Range, Invalid, Short, Long };
std::string_view time_error(TimeError e) {
switch (e) {
case TimeError::Range:
return "input is out of range";
case TimeError::Invalid:
return "input contains invalid characters";
case TimeError::Short:
return "premature end of input";
case TimeError::Long:
return "trailing input";
}
std::terminate();
}
// Implements the provider's chrono format items. Field range validation happens
// while scanning; calendar validation happens after checking trailing input.
struct TimeParser {
std::string_view s;
std::int32_t year = 0;
unsigned month = 0, day = 0, hour = 0, minute = 0, second = 0, millis = 0;
void space() {
trim_start(s);
}
bool take(char c) {
if (s.empty() || s.front() != c) {
return false;
}
s.remove_prefix(1);
return true;
}
void literal(char c) {
if (s.empty()) {
throw TimeError::Short;
}
if (!take(c)) {
throw TimeError::Invalid;
}
}
std::int64_t number(std::size_t width, unsigned minimum = 1, bool skip = true) {
if (skip) {
space();
}
if (s.size() < minimum) {
throw TimeError::Short;
}
std::int64_t value = 0;
std::size_t n = 0;
while (n < s.size() && n < width && s[n] >= '0' && s[n] <= '9') {
auto digit = s[n] - '0';
if (value > (INT64_MAX - digit) / 10) {
throw TimeError::Range;
}
value = value * 10 + digit;
++n;
}
if (n < minimum) {
throw TimeError::Invalid;
}
s.remove_prefix(n);
return value;
}
unsigned field(unsigned maximum, unsigned minimum = 0) {
auto value = number(2);
if (value < minimum || value > maximum) {
throw TimeError::Range;
}
return unsigned(value);
}
void date() {
space();
bool negative = take('-');
bool signed_year = negative || take('+');
auto value = number(signed_year ? s.size() : 4, 1, false);
if (negative) {
value = -value;
}
if (value < INT32_MIN || value > INT32_MAX) {
throw TimeError::Range;
}
year = std::int32_t(value);
literal('-');
month = field(12, 1);
literal('-');
day = field(31, 1);
}
void time() {
space();
hour = field(23);
literal(':');
minute = field(59);
literal(':');
second = field(60);
if (take('.')) {
auto start = s.size();
auto nanos = number(9, 1, false);
auto digits = start - s.size();
for (; digits < 9; ++digits) {
nanos *= 10;
}
millis = unsigned(nanos / 1000000);
while (!s.empty() && s.front() >= '0' && s.front() <= '9') {
s.remove_prefix(1);
}
}
}
int offset() {
space();
if (take('Z') || take('z')) {
return 0;
}
bool negative = take('-');
if (!negative && s.starts_with("−")) {
negative = true;
s.remove_prefix(3);
}
if (!negative) {
literal('+');
}
auto h = number(2, 2, false);
for (;;) {
space();
if (!take(':')) {
break;
}
}
auto m = s.empty() ? 0 : number(2, 2, false);
if (h > 23 || m > 59) {
throw TimeError::Range;
}
return int(h * 3600 + m * 60) * (negative ? -1 : 1);
}
Date finish_date() {
if (!s.empty()) {
throw TimeError::Long;
}
auto result = Date::from_ymd(year, month, day);
if (!result) {
throw TimeError::Range;
}
return *result;
}
Timestamp finish_time(int offset = 0) {
auto result = finish_date().epoch_days() * 86400000 +
(std::int64_t(hour) * 3600 + minute * 60 + second - offset) * 1000 + millis;
auto calendar_millis = result - (second == 60 ? 1000 : 0);
auto days = calendar_millis / 86400000 - (calendar_millis % 86400000 < 0);
if (!Date::from_epoch_days(days)) {
throw TimeError::Range;
}
return Timestamp(result);
}
};
}
std::optional<Timestamp> parse_fixed_sqlite_timestamp(std::string_view s) {
if (s.size() < 19 || s[4] != '-' || s[7] != '-' || (s[10] != ' ' && s[10] != 'T') ||
s[13] != ':' || s[16] != ':') {
return {};
}
// The source's then_some eagerly evaluates u8 subtraction, even for an
// invalid digit. Preserve its checked Debug failure and short-circuit order.
auto digits = [&](unsigned start, unsigned length) -> std::optional<unsigned> {
unsigned value = 0;
for (unsigned i = start; i < start + length; ++i) {
auto byte = static_cast<unsigned char>(s[i]);
#ifndef NDEBUG
if (byte < '0') {
throw std::out_of_range("attempt to subtract with overflow");
}
#endif
if (byte < '0' || byte > '9') {
return {};
}
value = value * 10 + byte - '0';
}
return value;
};
auto year = digits(0, 4);
if (!year) {
return {};
}
auto month = digits(5, 2);
if (!month) {
return {};
}
auto day = digits(8, 2);
if (!day || !Date::from_ymd(static_cast<std::int32_t>(*year), *month, *day)) {
return {};
}
auto hour = digits(11, 2);
if (!hour) {
return {};
}
auto minute = digits(14, 2);
if (!minute) {
return {};
}
auto second = digits(17, 2);
if (!second) {
return {};
}
if (s[17] == '6') {
return {}; // Fast path excludes leap seconds.
}
std::array<char, 19> base{};
std::copy_n(s.begin(), 19, base.begin());
base[10] = ' ';
auto time = Timestamp::parse(std::string_view(base.data(), base.size()));
if (!time) {
return {};
}
TimeParser p{s.substr(19)};
unsigned millis = 0;
try {
if (p.take('.')) {
auto start = p.s.size();
auto nanos = p.number(9, 1, false);
auto digits = start - p.s.size();
for (; digits < 9; ++digits) {
nanos *= 10;
}
millis = unsigned(nanos / 1000000);
while (!p.s.empty() && p.s.front() >= '0' && p.s.front() <= '9') {
p.s.remove_prefix(1);
}
}
int offset = 0;
if (!p.s.empty()) {
#ifndef NDEBUG
// signed_offset also eagerly computes both byte subtractions.
auto suffix = p.s;
bool fixed_offset = (suffix.size() == 3 || suffix.size() == 5 ||
(suffix.size() == 6 && suffix[3] == ':')) &&
(suffix.front() == '+' || suffix.front() == '-');
if (fixed_offset) {
for (unsigned index : {1, 2}) {
if (static_cast<unsigned char>(suffix[index]) < '0') {
throw std::out_of_range("attempt to subtract with overflow");
}
}
if (static_cast<unsigned char>(suffix[1]) <= '9' &&
static_cast<unsigned char>(suffix[2]) <= '9' && suffix.size() > 3) {
auto start = suffix.size() == 6 ? 4 : 3;
for (auto index : {start, start + 1}) {
if (static_cast<unsigned char>(suffix[index]) < '0') {
throw std::out_of_range("attempt to subtract with overflow");
}
}
}
}
#endif
if (p.take('Z') || p.take('z')) {
if (!p.s.empty()) {
return {};
}
} else {
const bool negative = p.take('-');
if (!negative && !p.take('+')) {
return {};
}
auto hours = p.number(2, 2, false);
std::int64_t minutes = 0;
if (!p.s.empty()) {
p.take(':');
minutes = p.number(2, 2, false);
}
if (!p.s.empty() || hours > 23 || minutes > 59) {
return {};
}
offset = int(hours * 3600 + minutes * 60) * (negative ? -1 : 1);
}
}
return Timestamp(time->as_millis() + millis - std::int64_t(offset) * 1000);
} catch (TimeError) {
return {};
}
}
namespace {
Decoded timestamp(Memory r, std::string_view s) {
if (auto value = parse_fixed_sqlite_timestamp(s)) {
return Decoded::ok(r, Value(r, *value));
}
if (auto value = Timestamp::parse(s)) {
return Decoded::ok(r, Value(r, *value));
}
try {
TimeParser p{s};
p.date();
p.time();
auto offset = p.offset();
return Decoded::ok(r, Value(r, p.finish_time(offset)));
} catch (TimeError) {
}
try {
TimeParser p{s};
p.date();
p.time();
return Decoded::ok(r, Value(r, p.finish_time()));
} catch (TimeError e) {
return invalid(r, "timestamp", time_error(e));
}
}
Vector<String> names(Memory r, sqlite3_stmt& stmt) {
Vector<String> result(r);
for (int i = 0; i < sqlite3_column_count(&stmt); ++i) {
auto* name = sqlite3_column_name(&stmt, i);
if (!name) {
throw std::bad_alloc();
}
result.emplace_back(name);
}
return result;
}
}
SqliteDecodeKind sqlite_decode_kind(std::optional<std::string_view> declaration) {
if (!declaration) {
return SqliteDecodeKind::Infer;
}
auto base = declaration->substr(0, declaration->find('('));
trim(base);
auto is = [&](std::string_view type) {
if (base.size() != type.size()) {
return false;
}
for (std::size_t i = 0; i < base.size(); ++i) {
auto c = base[i];
if (c >= 'a' && c <= 'z') {
c = char(c - 'a' + 'A');
}
if (c != type[i]) {
return false;
}
}
return true;
};
if (is("BOOL") || is("BOOLEAN")) {
return SqliteDecodeKind::Bool;
}
if (is("NUMERIC") || is("DECIMAL")) {
return SqliteDecodeKind::Decimal;
}
if (is("JSON")) {
return SqliteDecodeKind::Json;
}
if (is("DATE")) {
return SqliteDecodeKind::Date;
}
if (is("TIMESTAMP") || is("DATETIME")) {
return SqliteDecodeKind::Timestamp;
}
if (is("TEXT") || is("VARCHAR") || is("CHAR") || is("CLOB")) {
return SqliteDecodeKind::Text;
}
return SqliteDecodeKind::Infer;
}
std::optional<String> sqlite_utf8_error(Memory r, std::string_view text) {
return utf8_error(r, text);
}
SqliteRowLayout::SqliteRowLayout(std::allocator_arg_t, Allocator a, sqlite3_stmt& statement)
: columns_(std::allocator_arg, a, names(a.resource(), statement)), kinds_(a.resource()) {
for (int i = 0; i < sqlite3_column_count(&statement); ++i) {
auto* type = sqlite3_column_decltype(&statement, i);
kinds_.push_back(
sqlite_decode_kind(type ? std::optional<std::string_view>(type) : std::nullopt));
}
}
SqliteRowLayout::SqliteRowLayout(std::allocator_arg_t, Allocator a, const SqliteRowLayout& other)
: columns_(std::allocator_arg, a, other.columns_), kinds_(other.kinds_, a.resource()) {}
Decoded decode_sqlite_text(Memory r, std::string_view s, SqliteDecodeKind kind) {
if (auto error = utf8_error(r, s)) {
return invalid(r, "text", *error);
}
switch (kind) {
case SqliteDecodeKind::Decimal: {
auto value = Decimal::parse_result(r, s);
if (!value) {
return invalid(r, "decimal", value.error().to_string(r));
}
return Decoded::ok(r, Value(r, value.value()));
}
case SqliteDecodeKind::Json: {
auto value = Json::parse_result(r, s);
if (!value) {
return invalid(r, "json value", value.error().to_string(r));
}
return Decoded::ok(r, Value(r, std::move(value).value()));
}
case SqliteDecodeKind::Date:
try {
TimeParser p{s};
p.date();
return Decoded::ok(r, Value(r, p.finish_date()));
} catch (TimeError e) {
return invalid(r, "date", time_error(e));
}
case SqliteDecodeKind::Timestamp:
return timestamp(r, s);
case SqliteDecodeKind::Infer:
if (auto date = Date::parse(s)) {
return Decoded::ok(r, Value(r, *date));
}
if (auto value = Timestamp::parse(s)) {
return Decoded::ok(r, Value(r, *value));
}
[[fallthrough]];
case SqliteDecodeKind::Bool:
case SqliteDecodeKind::Text:
return Decoded::ok(r, Value(r, s));
}
std::terminate();
}
Result<CompactRow, MutationExecutorError>
decode_sqlite_row(Memory r, sqlite3_stmt& statement, const SqliteRowLayout& layout) {
using Output = Result<CompactRow, MutationExecutorError>;
Vector<Value> values(r);
values.reserve(layout.kinds().size());
for (std::size_t i = 0; i < layout.kinds().size(); ++i) {
if (i >= std::size_t(sqlite3_column_count(&statement))) {
return Output::err(
r, MutationExecutorError::sqlite(r, SqliteDriverError::invalid_column_index(r, i)));
}
auto column = int(i);
auto kind = layout.kinds()[i];
switch (sqlite3_column_type(&statement, column)) {
case SQLITE_NULL:
values.emplace_back();
break;
case SQLITE_INTEGER:
if (kind == SqliteDecodeKind::Bool) {
values.emplace_back(sqlite3_column_int64(&statement, column) != 0);
} else {
values.emplace_back(std::int64_t(sqlite3_column_int64(&statement, column)));
}
break;
case SQLITE_FLOAT:
values.emplace_back(sqlite3_column_double(&statement, column));
break;
case SQLITE_TEXT: {
auto* text = sqlite3_column_text(&statement, column);
if (!text) {
return Output::err(r,
MutationExecutorError::sqlite(r, SQLITE_NOMEM, "out of memory"));
}
auto decoded =
decode_sqlite_text(r,
std::string_view(reinterpret_cast<const char*>(text),
sqlite3_column_bytes(&statement, column)),
kind);
if (!decoded) {
return Output::err(r, std::move(decoded).error());
}
values.push_back(std::move(decoded).value());
break;
}
case SQLITE_BLOB:
return Output::err(r, MutationExecutorError::unsupported_column_type(r, "BLOB"));
default:
throw std::logic_error("unknown SQLite storage class");
}
}
return Output::ok(
r, CompactRow(std::allocator_arg, Allocator(r), layout.columns(), std::move(values)));
}
}