-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathbulk_data_service.cc
2739 lines (2339 loc) · 93.1 KB
/
bulk_data_service.cc
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2022, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "mysql/components/services/bulk_data_service.h"
#include <assert.h>
#include <cstdint>
#include "field_types.h"
#include "my_byteorder.h"
#include "my_time.h"
#include "mysql/components/service_implementation.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql_time.h"
#include "mysqld_error.h"
#include "sql-common/json_dom.h"
#include "sql-common/my_decimal.h"
#include "sql/field.h"
#include "sql/item_strfunc.h"
#include "sql/sql_class.h"
#include "sql/sql_gipk.h"
#include "sql/sql_time.h"
#include "sql/strfunc.h" // find_type2
#include "sql/tztime.h"
#include "vector-common/vector_conversion.h"
namespace Bulk_data_convert {
/** Log details of error during data conversion.
@param[in] text_col input column from CSV file
@param[in] mesg error message to append to */
static void log_conversion_error(const Column_text &text_col,
std::string mesg) {
std::ostringstream err_strm;
std::string in_value(text_col.m_data_ptr, text_col.m_data_len);
err_strm << "BULK LOAD Conversion: " << mesg << in_value;
LogErr(INFORMATION_LEVEL, ER_BULK_LOADER_INFO, err_strm.str().c_str());
}
/** Create an integer column converting data from CSV text.
@param[in] text_col input column in text read from CSV
@param[in] charset character set for the input column data
@param[in] field table column metadata
@param[in] write_in_buffer write integer data in column buffer
@param[out] sql_col converted column in MySQL storage format
@param[out] error_details the error details
@return error code. */
template <typename S, typename U>
static int format_int_column(const Column_text &text_col,
const CHARSET_INFO *charset, const Field *field,
bool write_in_buffer, Column_mysql &sql_col,
Bulk_load_error_location_details &error_details) {
int err = 0;
const char *end;
auto field_num = (const Field_num *)field;
bool is_unsigned = field_num->is_unsigned();
auto val = charset->cset->strntoull10rnd(charset, text_col.m_data_ptr,
text_col.m_data_len, is_unsigned,
&end, &err);
if (err != 0) {
error_details.column_type = "integer";
log_conversion_error(text_col, "Integer conversion failed for: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
if (is_unsigned && val > std::numeric_limits<U>::max()) {
error_details.column_type = "integer";
log_conversion_error(text_col, "Unsigned Integer out of range: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
if (!is_unsigned) {
auto signed_val = static_cast<int64_t>(val);
if (signed_val < std::numeric_limits<S>::min() ||
signed_val > std::numeric_limits<S>::max()) {
error_details.column_type = "integer";
log_conversion_error(text_col, "Integer out of range: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
}
sql_col.m_int_data = val;
/* Write the integer bytes in the buffer. */
if (write_in_buffer) {
/* This is written to temp files to be consumed later part of execution.
We don't bother about BE/LE order here. */
if (sql_col.m_type == MYSQL_TYPE_LONGLONG) {
memcpy(sql_col.get_data(), (void *)(&sql_col.m_int_data),
sizeof(uint64_t));
sql_col.m_data_len = sizeof(uint64_t);
return 0;
}
/* Unsigned integer less than or equal to four bytes. */
if (is_unsigned) {
/* Data is already checked to be within the range of S. */
auto data_4 = static_cast<uint32_t>(sql_col.m_int_data);
memcpy(sql_col.get_data(), (void *)(&data_4), sizeof(uint32_t));
sql_col.m_data_len = sizeof(uint32_t);
return 0;
}
/* Signed integer less than or equal to four bytes. */
auto signed_val = static_cast<int64_t>(sql_col.m_int_data);
/* Data is already checked to be within the range of S. */
auto data_4 = static_cast<int32_t>(signed_val);
memcpy(sql_col.get_data(), (void *)(&data_4), sizeof(int32_t));
sql_col.m_data_len = sizeof(int32_t);
}
return 0;
}
/** Create a blob column.
@param[in] field table column metadata
@param[in] from_cs charset of the input string from CSV.
@param[in] text_col input column in text read from CSV.
@param[out] sql_col converted column in MySQL storage format
@param[out] length_size number of bytes used to write the length
@param[out] error_details error location details
@return 0 on success, error code on failure. */
static int format_blob_column(Field *field, const CHARSET_INFO *from_cs,
const Column_text &text_col,
Column_mysql &sql_col, size_t &length_size,
Bulk_load_error_location_details &error_details) {
assert(!sql_col.m_is_null);
auto field_str = (const Field_str *)field;
const CHARSET_INFO *field_charset = field_str->charset();
switch (sql_col.m_type) {
case MYSQL_TYPE_TINY_BLOB:
length_size = 1;
break;
case MYSQL_TYPE_BLOB:
length_size = 2;
break;
case MYSQL_TYPE_MEDIUM_BLOB:
length_size = 3;
break;
case MYSQL_TYPE_GEOMETRY:
[[fallthrough]];
case MYSQL_TYPE_JSON:
[[fallthrough]];
case MYSQL_TYPE_VECTOR:
case MYSQL_TYPE_LONG_BLOB:
length_size = 4;
break;
default:
assert(0);
break;
}
char *field_begin = sql_col.get_data();
char *field_data = field_begin + length_size;
size_t copy_size = text_col.m_data_len;
if (text_col.is_ext()) {
/* Column data stored externally. */
memcpy(field_data, text_col.m_data_ptr, copy_size);
} else {
const char *error_pos = nullptr;
const char *convert_error_pos = nullptr;
const char *end_pos = nullptr;
const size_t nchars = text_col.m_data_len;
auto field_size = sql_col.m_data_len;
if (sql_col.m_type == MYSQL_TYPE_VECTOR) {
assert(!text_col.is_ext());
const size_t len = text_col.m_data_len;
uint32 input_dims = get_dimensions(len, Field_vector::precision);
if (input_dims > Field_vector::max_dimensions) {
error_details.m_column_length = len;
error_details.column_input_data = text_col.m_data_ptr;
return ER_TO_VECTOR_CONVERSION;
}
assert(input_dims != 0);
/* Refer to Item_func_from_vector::per_value_chars */
const uint32 per_value_chars = 16;
uint32 out_length = input_dims * per_value_chars;
#ifndef NDEBUG
const uint32 max_output_bytes =
(Field_vector::max_dimensions * per_value_chars);
assert(out_length <= max_output_bytes);
#endif /* NDEBUG */
/* Refer to Field_vector::store(). */
const char *from = text_col.m_data_ptr;
for (uint32 i = 0; i < input_dims; i++) {
float to_store = 0;
memcpy(&to_store, from + sizeof(float) * i, sizeof(float));
if (std::isnan(to_store) || std::isinf(to_store)) {
error_details.m_column_length = len;
error_details.column_input_data = text_col.m_data_ptr;
return ER_TO_VECTOR_CONVERSION;
}
}
auto ptr = std::make_unique<char[]>(out_length);
if (from_vector_to_string(text_col.m_data_ptr, input_dims, ptr.get(),
&out_length)) {
error_details.m_column_length = len;
error_details.column_input_data = text_col.m_data_ptr;
return ER_TO_VECTOR_CONVERSION;
}
}
if (field_charset == &my_charset_bin) {
/* If the charset of the field is binary, then the column data in the CSV
would also be binary. Don't do any charset conversions. */
if (text_col.m_data_len > sql_col.m_data_len) {
error_details.m_column_length = sql_col.m_data_len;
return ER_TOO_BIG_FIELDLENGTH;
}
memcpy(field_data, text_col.m_data_ptr, text_col.m_data_len);
copy_size = text_col.m_data_len;
} else {
copy_size = well_formed_copy_nchars(
field_charset, field_data, field_size, from_cs, text_col.m_data_ptr,
text_col.m_data_len, nchars, &error_pos, &convert_error_pos,
&end_pos);
if (error_pos != nullptr || convert_error_pos != nullptr) {
error_details.column_type = "text";
log_conversion_error(text_col, "Invalid Input String: ");
return ER_INVALID_CHARACTER_STRING;
}
if (end_pos < text_col.m_data_ptr + text_col.m_data_len) {
return ER_TOO_BIG_FIELDLENGTH;
}
}
if (sql_col.m_type == MYSQL_TYPE_JSON) {
std::string errmsg;
auto error_handler = [&errmsg](const char *err, size_t) { errmsg = err; };
auto depth_handler = []() {};
auto dom =
Json_dom::parse(field_data, copy_size, error_handler, depth_handler);
if (dom.get() == nullptr) {
error_details.m_error_mesg = errmsg;
return ER_BULK_JSON_INVALID;
}
String value;
Bulk_load::Json_serialization_error_handler error_object;
bool failure = json_binary::serialize(dom.get(), error_object, &value);
if (failure) {
/* failure */
error_details.m_error_mesg = error_object.get_error();
return ER_BULK_JSON_INVALID;
}
memcpy(field_data, value.ptr(), value.length());
copy_size = value.length();
}
}
switch (sql_col.m_type) {
case MYSQL_TYPE_TINY_BLOB:
*field_begin = static_cast<uint8_t>(copy_size);
break;
case MYSQL_TYPE_BLOB:
int2store(field_begin, static_cast<uint16_t>(copy_size));
break;
case MYSQL_TYPE_MEDIUM_BLOB:
int3store(field_begin, copy_size);
break;
case MYSQL_TYPE_GEOMETRY:
[[fallthrough]];
case MYSQL_TYPE_JSON:
[[fallthrough]];
case MYSQL_TYPE_VECTOR:
[[fallthrough]];
case MYSQL_TYPE_LONG_BLOB:
int4store(field_begin, copy_size);
break;
default:
assert(0);
break;
}
sql_col.m_data_len = copy_size;
return 0;
}
/** Create a char/varchar column converting data to MySQL storage format.
@param[in] text_col input column in text read from CSV
@param[in] charset character set for the input column data
@param[in] field table column metadata
@param[in] write_length write length of column data if variable length
@param[in] col_meta column metadata
@param[in] single_byte if true, allocation is done assuming single byte
return ER_TOO_BIG_FIELDLENGTH if not enough
@param[out] sql_col converted column in MySQL storage format
@param[out] length_size number of bytes used to write the length
@param[out] error_details error location details
@return error code. */
static int format_char_column(const Column_text &text_col,
const CHARSET_INFO *charset, const Field *field,
bool write_length, const Column_meta &col_meta,
bool single_byte, Column_mysql &sql_col,
size_t &length_size,
Bulk_load_error_location_details &error_details) {
auto field_str = (const Field_str *)field;
const CHARSET_INFO *field_charset = field_str->charset();
auto field_char_size = field_str->char_length_cache;
auto field_size = sql_col.m_data_len;
/* We consider character data as fixed length if it can be adjusted within
single byte char allocation, e.g. for CHAR(N), we take N bytes as the fixed
length and if it exceeds N bytes because of multi-byte characters we consider
it as variable length and write as varchar in length + data format. The idea
here is to avoid allocating too much fixed length unused space. */
bool fixed_length =
col_meta.m_is_fixed_len || col_meta.m_fixed_len_if_set_in_row;
length_size = 0;
if (write_length) {
length_size = col_meta.m_is_single_byte_len ? 1 : 2;
}
/* For non-key, fixed length char data adjusted within single byte length, we
skip writing length byte(s). In such case, row header is marked to indicate
that length bytes are not present for fixed length types. This added
complexity helps in saving temp storage size for fixed length char. */
bool no_length_char =
single_byte && col_meta.m_fixed_len_if_set_in_row && !col_meta.m_is_key;
if (col_meta.m_is_fixed_len || no_length_char) {
length_size = 0;
}
char *field_begin = sql_col.get_data();
char *field_data = field_begin + length_size;
const char *error_pos = nullptr;
const char *convert_error_pos = nullptr;
const char *end_pos = nullptr;
size_t copy_size{0};
if (text_col.is_ext()) {
assert(text_col.m_data_len == 20);
memcpy(field_data, text_col.m_data_ptr, text_col.m_data_len);
copy_size = text_col.m_data_len;
} else {
copy_size = well_formed_copy_nchars(
field_charset, field_data, field_size, charset, text_col.m_data_ptr,
text_col.m_data_len, field_char_size, &error_pos, &convert_error_pos,
&end_pos);
if (end_pos < text_col.m_data_ptr + text_col.m_data_len) {
/* The error is expected when fixed_length = true, where we try to adjust
the data within character length limit. The data could not be fit in
such limit here which is possible for multi-byte character set. We
return from here and retry with variable length format - mysql_format() */
if (fixed_length && single_byte) {
return ER_TOO_BIG_FIELDLENGTH;
}
error_details.column_type = "string";
log_conversion_error(text_col, "Input String too long: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
if (error_pos != nullptr || convert_error_pos != nullptr) {
error_details.column_type = "string";
log_conversion_error(text_col, "Invalid Input String: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
}
auto data_length = copy_size;
/* For char[] column need to fill padding characters. */
if (fixed_length && copy_size < field_size) {
size_t fill_size = field_size - copy_size;
char *fill_pos = field_data + copy_size;
field_charset->cset->fill(field_charset, fill_pos, fill_size,
field_charset->pad_char);
data_length = field_size;
}
sql_col.set_data(field_data);
sql_col.m_data_len = data_length;
if (length_size == 0) {
return 0;
}
assert(write_length);
/* Write length for varchar column. */
if (length_size == 1) {
*field_begin = static_cast<unsigned char>(data_length);
return 0;
}
assert(length_size == 2);
int2store(field_begin, static_cast<uint16_t>(data_length));
return 0;
}
/** Create a FLOAT column converting data to MySQL storage format.
@param[in] text_col input column in text read from CSV
@param[in] charset character set for the input column data
@param[in] field table column metadata
@param[out] sql_col converted column in MySQL storage format
@param[out] error_details the error details
@return error code. */
static int format_float_column(
const Column_text &text_col, const CHARSET_INFO *charset,
const Field *field, Column_mysql &sql_col,
Bulk_load_error_location_details &error_details) {
int conv_error;
const char *end;
double nr = my_strntod(charset, text_col.m_data_ptr, text_col.m_data_len,
&end, &conv_error);
const auto converted_len = (size_t)(end - text_col.m_data_ptr);
if (conv_error != 0 || end == text_col.m_data_ptr ||
converted_len != text_col.m_data_len) {
error_details.column_type = "float";
log_conversion_error(text_col, "Invalid Float Data: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
auto field_float = (const Field_float *)field;
if (field_float->is_unsigned() && nr < 0) {
error_details.column_type = "float";
log_conversion_error(text_col, "Signed Float for unsigned type: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
if (field_float->truncate(&nr, FLT_MAX) != Field_real::TR_OK) {
error_details.column_type = "float";
log_conversion_error(text_col, "Invalid value for type: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
float4store((uchar *)sql_col.get_data(), nr);
return 0;
}
/** Create a DOUBLE column converting data to MySQL storage format.
@param[in] text_col input column in text read from CSV
@param[in] charset character set for the input column data
@param[in] field table column metadata
@param[out] sql_col converted column in MySQL storage format
@param[out] error_details the error details
@return error code. */
static int format_double_column(
const Column_text &text_col, const CHARSET_INFO *charset,
const Field *field, Column_mysql &sql_col,
Bulk_load_error_location_details &error_details) {
int conv_error;
const char *end;
double nr = my_strntod(charset, text_col.m_data_ptr, text_col.m_data_len,
&end, &conv_error);
const auto converted_len = (size_t)(end - text_col.m_data_ptr);
if (conv_error != 0 || end == text_col.m_data_ptr ||
converted_len != text_col.m_data_len) {
error_details.column_type = "double";
log_conversion_error(text_col, "Invalid Float Data: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
auto field_double = (const Field_double *)field;
if (field_double->is_unsigned() && nr < 0) {
error_details.column_type = "double";
log_conversion_error(text_col, "Signed Double for unsigned type: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
if (field_double->truncate(&nr, DBL_MAX) != Field_real::TR_OK) {
error_details.column_type = "double";
log_conversion_error(text_col, "Invalid value for type: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
float8store((uchar *)sql_col.get_data(), nr);
return 0;
}
/** Create a DECIMAL column converting data to MySQL storage format.
@param[in] text_col input column in text read from CSV
@param[in] charset character set for the input column data
@param[in] field table column metadata
@param[out] sql_col converted column in MySQL storage format
@param[out] error_details the error details
@return error code. */
static int format_decimal_column(
const Column_text &text_col, const CHARSET_INFO *charset,
const Field *field, Column_mysql &sql_col,
Bulk_load_error_location_details &error_details) {
auto field_dec = (const Field_new_decimal *)field;
my_decimal decimal_value;
int err = str2my_decimal(
E_DEC_FATAL_ERROR & ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
text_col.m_data_ptr, text_col.m_data_len, charset, &decimal_value);
if (err == E_DEC_OK) {
auto precision = field_dec->precision;
auto scale = field_dec->dec;
assert(sql_col.m_data_len >= (size_t)decimal_bin_size(precision, scale));
auto field_begin = (unsigned char *)sql_col.get_data();
err = my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, &decimal_value,
field_begin, precision, scale);
}
if (err != E_DEC_OK) {
error_details.column_type = "decimal";
log_conversion_error(text_col, "Invalid Decimal Data: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
if (field_dec->is_unsigned() && decimal_value.sign()) {
error_details.column_type = "decimal";
log_conversion_error(text_col, "Signed Decimal for unsigned type: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
return 0;
}
/** Create a DATETIME column converting data to MySQL storage format.
@param[in] thd session THD
@param[in] text_col input column in text read from CSV
@param[in] charset character set for the input column data
@param[in] field table column metadata
@param[out] sql_col converted column in MySQL storage format
@param[out] error_details the error details
@return error code. */
int format_datetime_column(THD *thd, const Column_text &text_col,
const CHARSET_INFO *charset, const Field *field,
Column_mysql &sql_col,
Bulk_load_error_location_details &error_details) {
auto field_date = (const Field_temporal *)field;
auto flags = field_date->get_date_flags(thd);
MYSQL_TIME ltime;
MYSQL_TIME_STATUS status;
/* Convert input to MySQL TIME. */
bool res = str_to_datetime(charset, text_col.m_data_ptr, text_col.m_data_len,
<ime, flags, &status);
/* Adjust value to the column precision. */
if (!res && status.warnings == 0) {
res = my_datetime_adjust_frac(<ime, field_date->get_fractional_digits(),
&status.warnings, flags & TIME_FRAC_TRUNCATE);
}
/* Check for error in conversion. */
if (res || (status.warnings != 0)) {
error_details.column_type = "datetime";
log_conversion_error(text_col, "Invalid DATETIME: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
MYSQL_TIME *time = <ime;
MYSQL_TIME tz_ltime;
if (ltime.time_type == MYSQL_TIMESTAMP_DATETIME_TZ) {
tz_ltime = ltime;
time = &tz_ltime;
const Time_zone *tz = thd->time_zone();
if (convert_time_zone_displacement(tz, &tz_ltime)) {
error_details.column_type = "datetime";
log_conversion_error(text_col, "TZ displacement failed: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
/* Check for boundary conditions by converting to a timeval */
my_timeval tm_not_used;
res = datetime_with_no_zero_in_date_to_timeval(&tz_ltime, *tz, &tm_not_used,
&status.warnings);
if (res || status.warnings != 0) {
error_details.column_type = "datetime";
log_conversion_error(text_col, "TZ boundary check failed: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
}
auto packed = TIME_to_longlong_datetime_packed(*time);
auto field_begin = (unsigned char *)sql_col.get_data();
my_datetime_packed_to_binary(packed, field_begin,
field_date->get_fractional_digits());
return 0;
}
/** Create a DATE column converting data to MySQL storage format.
@param[in] thd session THD
@param[in] text_col input column in text read from CSV
@param[in] charset character set for the input column data
@param[in] field table column metadata
@param[out] sql_col converted column in MySQL storage format
@param[out] error_details error location details
@return error code. */
int format_date_column(THD *thd, const Column_text &text_col,
const CHARSET_INFO *charset, const Field *field,
Column_mysql &sql_col,
Bulk_load_error_location_details &error_details) {
auto field_date = (const Field_temporal *)field;
auto flags = field_date->get_date_flags(thd);
MYSQL_TIME ltime;
MYSQL_TIME_STATUS status;
/* Convert input to MySQL TIME. */
bool res = str_to_datetime(charset, text_col.m_data_ptr, text_col.m_data_len,
<ime, flags, &status);
/* Adjust value to the column precision. */
if (!res && status.warnings == 0) {
res = my_datetime_adjust_frac(<ime, field_date->get_fractional_digits(),
&status.warnings, flags & TIME_FRAC_TRUNCATE);
}
/* Check for error in conversion. */
if (res || (status.warnings != 0)) {
error_details.column_type = "date";
log_conversion_error(text_col, "Invalid DATE: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
MYSQL_TIME *time = <ime;
MYSQL_TIME tz_ltime;
if (ltime.time_type == MYSQL_TIMESTAMP_DATETIME_TZ) {
tz_ltime = ltime;
time = &tz_ltime;
const Time_zone *tz = thd->time_zone();
if (convert_time_zone_displacement(tz, &tz_ltime)) {
error_details.column_type = "date";
log_conversion_error(text_col, "TZ displacement failed: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
/* Check for boundary conditions by converting to a timeval */
my_timeval tm_not_used;
res = datetime_with_no_zero_in_date_to_timeval(&tz_ltime, *tz, &tm_not_used,
&status.warnings);
if (res || status.warnings != 0) {
error_details.column_type = "date";
log_conversion_error(text_col, "TZ boundary check failed: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
}
if (non_zero_time(*time)) {
error_details.column_type = "date";
log_conversion_error(text_col, "DATE includes TIME: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
/* Convert to storage format. */
auto field_begin = (unsigned char *)sql_col.get_data();
my_date_to_binary(time, field_begin);
return 0;
}
class Row_header {
public:
enum Flag {
/** If there is one or more NULL data in current row. */
HAS_NULL_DATA = 1,
/** Character data is fixed length. */
IS_FIXED_CHAR = 2,
/** Don't define flag beyond this maximum. */
FLAG_MAX = 16
};
/** Matches MAX_FIELDS in SQL. We need separate definition here as we have
array of this size allocated from stack. If SQL increases the value in future
we need to re-evaluate and possibly go for dynamic allocation. We don't want
to use dynamic allocation unconditionally as it impacts performance. */
const static size_t MAX_NULLABLE_BYTES = 512;
/** Construct header.
@param[in] row_meta row metadata. */
explicit Row_header(const Row_meta &row_meta);
/** Serialize header into a buffer.
@param[in,out] buffer buffer to write to
@param[in] length buffer length
@return true iff successful. */
bool serialize(char *buffer, size_t length);
/** De-Serialize header from a buffer.
@param[in] buffer buffer to write to
@param[in] length buffer length
@return true iff successful. */
bool deserialize(const char *buffer, size_t length);
/** Add length to row.
@param[in] add length to add */
void add_length(size_t add) { m_row_length += add; }
/** @return current row length. */
size_t get_row_length() const { return m_row_length; }
/** Set specific flag.
@param[in] flag flag to set */
void set(Flag flag) { m_flags |= static_cast<uint16_t>(1 << (flag - 1)); }
/** Check if a specific flag is set.
@param[in] flag flag to check
@return true iff set. */
bool is_set(Flag flag) const {
return ((m_flags & static_cast<uint16_t>(1 << (flag - 1))) > 0);
}
/** Set the column value as NULL in header.
@param[in] col_meta column metadata */
void set_column_null(const Column_meta &col_meta);
/** check if column value is NULL in header.
@param[in] col_meta column metadata
@return true iff NULL */
bool is_column_null(const Column_meta &col_meta) const;
/** @return total header length. */
size_t header_length() const {
return m_null_bitmap_length + sizeof(m_row_length) + sizeof(m_flags);
}
private:
/** NULL bitmap for the row. Needed only while sorting by key. */
std::array<unsigned char, MAX_NULLABLE_BYTES> m_null_bitmap;
/** Actual length of bitmap in bytes. Must be less than or equal to
MAX_NULLABLE_BYTES. */
size_t m_null_bitmap_length{};
/** Current row length. */
uint16_t m_row_length{};
/** Row flags : 2 bytes, maximum 16 flags */
uint16_t m_flags{};
};
Row_header::Row_header(const Row_meta &metadata) {
m_null_bitmap_length = metadata.m_bitmap_length;
memset(m_null_bitmap.data(), 0, m_null_bitmap_length);
m_row_length = 0;
m_flags = 0;
}
void Row_header::set_column_null(const Column_meta &col_meta) {
assert(col_meta.m_is_nullable);
unsigned char &null_byte = m_null_bitmap[col_meta.m_null_byte];
null_byte |= static_cast<unsigned char>(1 << col_meta.m_null_bit);
}
bool Row_header::is_column_null(const Column_meta &col_meta) const {
const unsigned char &null_byte = m_null_bitmap[col_meta.m_null_byte];
return ((null_byte & static_cast<unsigned char>(1 << col_meta.m_null_bit)) !=
0);
}
bool Row_header::serialize(char *buffer, size_t length) {
if (length < header_length()) {
return false;
}
int2store((uchar *)buffer, m_row_length);
buffer += sizeof(uint16_t);
int2store((uchar *)buffer, m_flags);
buffer += sizeof(uint16_t);
memcpy(buffer, m_null_bitmap.data(), m_null_bitmap_length);
return true;
}
bool Row_header::deserialize(const char *buffer, size_t length) {
if (length < header_length()) {
return false;
}
m_row_length = uint2korr((const uchar *)buffer);
buffer += sizeof(uint16_t);
m_flags = uint2korr((const uchar *)buffer);
buffer += sizeof(uint16_t);
auto dest = m_null_bitmap.data();
memcpy(dest, buffer, m_null_bitmap_length);
return true;
}
/** Create a TIME column converting data to MySQL storage format.
@param[in] thd session THD
@param[in] text_col input column in text read from CSV
@param[in] charset character set for the input column data
@param[in] field table column metadata
@param[out] sql_col converted column in MySQL storage format
@param[out] error_details the error details
@return error code. */
static int format_time_column(THD *thd, const Column_text &text_col,
const CHARSET_INFO *charset, const Field *field,
Column_mysql &sql_col,
Bulk_load_error_location_details &error_details) {
auto field_date = (const Field_temporal *)field;
auto flags = field_date->get_date_flags(thd);
MYSQL_TIME ltime;
MYSQL_TIME_STATUS status;
/* Convert input to MySQL TIME. */
bool res = str_to_time(charset, text_col.m_data_ptr, text_col.m_data_len,
<ime, flags, &status);
/* Adjust value to the column precision. */
if (!res && status.warnings == 0) {
res = my_datetime_adjust_frac(<ime, field_date->get_fractional_digits(),
&status.warnings, flags & TIME_FRAC_TRUNCATE);
}
/* Check for error in conversion. */
if (res || (status.warnings != 0)) {
error_details.column_type = "time";
log_conversion_error(text_col, "Invalid TIME: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
MYSQL_TIME *time = <ime;
MYSQL_TIME tz_ltime;
my_timeval tm;
if (ltime.time_type == MYSQL_TIMESTAMP_DATETIME_TZ) {
tz_ltime = ltime;
time = &tz_ltime;
const Time_zone *tz = thd->time_zone();
if (convert_time_zone_displacement(tz, &tz_ltime)) {
error_details.column_type = "time";
log_conversion_error(text_col, "TZ displacement failed: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
/* Check for boundary conditions by converting to a timeval */
res = datetime_with_no_zero_in_date_to_timeval(&tz_ltime, *tz, &tm,
&status.warnings);
if (res || status.warnings != 0) {
error_details.column_type = "time";
log_conversion_error(text_col, "TZ boundary check failed: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
}
if (non_zero_date(*time)) {
error_details.column_type = "time";
log_conversion_error(text_col, "TIME includes DATE: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
auto packed = TIME_to_longlong_time_packed(*time);
/* Convert to storage format. */
auto field_begin = (unsigned char *)sql_col.get_data();
my_time_packed_to_binary(packed, field_begin,
field_date->get_fractional_digits());
return 0;
}
/** Create a TIMESTAMP column converting data to MySQL storage format.
@param[in] thd session THD
@param[in] text_col input column in text read from CSV
@param[in] charset character set for the input column data
@param[in] field table column metadata
@param[out] sql_col converted column in MySQL storage format
@param[out] error_details the error details
@return error code. */
static int format_timestamp_column(
THD *thd, const Column_text &text_col, const CHARSET_INFO *charset,
const Field *field, Column_mysql &sql_col,
Bulk_load_error_location_details &error_details) {
auto field_date = (const Field_temporal *)field;
auto flags = field_date->get_date_flags(thd);
MYSQL_TIME ltime;
MYSQL_TIME_STATUS status;
/* Convert input to MySQL TIME. */
bool res = str_to_datetime(charset, text_col.m_data_ptr, text_col.m_data_len,
<ime, flags, &status);
/* Adjust value to the column precision. */
if (!res && status.warnings == 0) {
res = my_datetime_adjust_frac(<ime, field_date->get_fractional_digits(),
&status.warnings, flags & TIME_FRAC_TRUNCATE);
}
/* Check for error in conversion. */
if (res || (status.warnings != 0)) {
error_details.column_type = "timestamp";
log_conversion_error(text_col, "Invalid DATETIME: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
MYSQL_TIME *time = <ime;
MYSQL_TIME tz_ltime;
if (ltime.time_type == MYSQL_TIMESTAMP_DATETIME_TZ) {
tz_ltime = ltime;
time = &tz_ltime;
const Time_zone *tz = thd->time_zone();
if (convert_time_zone_displacement(tz, &tz_ltime)) {
error_details.column_type = "timestamp";
log_conversion_error(text_col, "TZ displacement failed: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
/* Check for boundary conditions by converting to a timeval */
my_timeval tm_not_used;
res = datetime_with_no_zero_in_date_to_timeval(&tz_ltime, *tz, &tm_not_used,
&status.warnings);
if (res || status.warnings != 0) {
error_details.column_type = "timestamp";
log_conversion_error(text_col, "TZ boundary check failed: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
}
my_timeval tm;
if (datetime_with_no_zero_in_date_to_timeval(time, *thd->time_zone(), &tm,
&status.warnings)) {
tm.m_tv_sec = tm.m_tv_usec = 0;
}
if (tm.m_tv_sec > TYPE_TIMESTAMP_MAX_VALUE) {
tm.m_tv_sec = tm.m_tv_usec = 0;
status.warnings |= MYSQL_TIME_WARN_OUT_OF_RANGE;
}
if (status.warnings != 0) {
error_details.column_type = "timestamp";
log_conversion_error(text_col, "Invalid TIMESTAMP: ");
return ER_LOAD_BULK_DATA_WRONG_VALUE_FOR_FIELD;
}
auto field_begin = (unsigned char *)sql_col.get_data();
my_timestamp_to_binary(&tm, field_begin, field_date->get_fractional_digits());
return 0;
}
/** Create a YEAR column converting data to MySQL storage format.
@param[in] text_col input column in text read from CSV
@param[in] charset character set for the input column data
@param[out] sql_col converted column in MySQL storage format
@param[out] error_details the error details
@return error code. */
static int format_year_column(const Column_text &text_col,
const CHARSET_INFO *charset,