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
|
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/csv/converter.h"
#include <array>
#include <cstring>
#include <limits>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/array/builder_binary.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/array/builder_decimal.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/array/builder_dict.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/array/builder_primitive.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/csv/parser.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/status.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/type.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/type_fwd.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/type_traits.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/checked_cast.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/decimal.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/trie.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/utf8_internal.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/value_parsing.h" // IWYU pragma: keep
namespace arrow20 {
namespace csv {
using internal::checked_cast;
using internal::Trie;
using internal::TrieBuilder;
namespace {
Status GenericConversionError(const std::shared_ptr<DataType>& type, const uint8_t* data,
uint32_t size) {
return Status::Invalid("CSV conversion error to ", type->ToString(),
": invalid value '",
std::string(reinterpret_cast<const char*>(data), size), "'");
}
inline bool IsWhitespace(uint8_t c) {
if (ARROW_PREDICT_TRUE(c > ' ')) {
return false;
}
return c == ' ' || c == '\t';
}
// Updates data_inout and size_inout to not include leading/trailing whitespace
// characters.
inline void TrimWhiteSpace(const uint8_t** data_inout, uint32_t* size_inout) {
const uint8_t*& data = *data_inout;
uint32_t& size = *size_inout;
// Skip trailing whitespace
if (ARROW_PREDICT_TRUE(size > 0) && ARROW_PREDICT_FALSE(IsWhitespace(data[size - 1]))) {
const uint8_t* p = data + size - 1;
while (size > 0 && IsWhitespace(*p)) {
--size;
--p;
}
}
// Skip leading whitespace
if (ARROW_PREDICT_TRUE(size > 0) && ARROW_PREDICT_FALSE(IsWhitespace(data[0]))) {
while (size > 0 && IsWhitespace(*data)) {
--size;
++data;
}
}
}
Status InitializeTrie(const std::vector<std::string>& inputs, Trie* trie) {
TrieBuilder builder;
for (const auto& s : inputs) {
RETURN_NOT_OK(builder.Append(s, true /* allow_duplicates */));
}
*trie = builder.Finish();
return Status::OK();
}
// Presize a builder based on parser contents
template <typename BuilderType>
Status PresizeBuilder(const BlockParser& parser, BuilderType* builder) {
RETURN_NOT_OK(builder->Resize(parser.num_rows()));
if constexpr (is_base_binary_type<typename BuilderType::TypeClass>::value) {
return builder->ReserveData(parser.num_bytes());
} else {
return Status::OK();
}
}
/////////////////////////////////////////////////////////////////////////
// Per-type value decoders
struct ValueDecoder {
explicit ValueDecoder(const std::shared_ptr<DataType>& type,
const ConvertOptions& options)
: type_(type), options_(options) {}
Status Initialize() {
// TODO no need to build a separate Trie for each instance
return InitializeTrie(options_.null_values, &null_trie_);
}
bool IsNull(const uint8_t* data, uint32_t size, bool quoted) {
if (quoted && !options_.quoted_strings_can_be_null) {
return false;
}
return null_trie_.Find(std::string_view(reinterpret_cast<const char*>(data), size)) >=
0;
}
protected:
Trie null_trie_;
const std::shared_ptr<DataType> type_;
const ConvertOptions& options_;
};
//
// Value decoder for fixed-size binary
//
struct FixedSizeBinaryValueDecoder : public ValueDecoder {
using value_type = const uint8_t*;
explicit FixedSizeBinaryValueDecoder(const std::shared_ptr<DataType>& type,
const ConvertOptions& options)
: ValueDecoder(type, options),
byte_width_(checked_cast<const FixedSizeBinaryType&>(*type).byte_width()) {}
Status Decode(const uint8_t* data, uint32_t size, bool quoted, value_type* out) {
if (ARROW_PREDICT_FALSE(size != byte_width_)) {
return Status::Invalid("CSV conversion error to ", type_->ToString(), ": got a ",
size, "-byte long string");
}
*out = data;
return Status::OK();
}
protected:
const uint32_t byte_width_;
};
//
// Value decoder for variable-size binary
//
template <bool CheckUTF8>
struct BinaryValueDecoder : public ValueDecoder {
using value_type = std::string_view;
using ValueDecoder::ValueDecoder;
Status Initialize() {
util::InitializeUTF8();
return ValueDecoder::Initialize();
}
Status Decode(const uint8_t* data, uint32_t size, bool quoted, value_type* out) {
if (CheckUTF8 && ARROW_PREDICT_FALSE(!util::ValidateUTF8Inline(data, size))) {
return Status::Invalid("CSV conversion error to ", type_->ToString(),
": invalid UTF8 data");
}
*out = {reinterpret_cast<const char*>(data), size};
return Status::OK();
}
bool IsNull(const uint8_t* data, uint32_t size, bool quoted) {
return options_.strings_can_be_null &&
(!quoted || options_.quoted_strings_can_be_null) &&
ValueDecoder::IsNull(data, size, false /* quoted */);
}
};
//
// Value decoder for integers, floats and temporals
//
template <typename T>
static arrow20::internal::StringConverter<T> MakeStringConverter(
const ConvertOptions& options) {
if constexpr (is_floating_type<T>::value) {
return arrow20::internal::StringConverter<T>{options.decimal_point};
} else {
return arrow20::internal::StringConverter<T>{};
}
}
template <typename T>
struct NumericValueDecoder : public ValueDecoder {
using value_type = typename T::c_type;
NumericValueDecoder(const std::shared_ptr<DataType>& type,
const ConvertOptions& options)
: ValueDecoder(type, options),
concrete_type_(checked_cast<const T&>(*type)),
string_converter_(MakeStringConverter<T>(options)) {}
Status Decode(const uint8_t* data, uint32_t size, bool quoted, value_type* out) {
// XXX should quoted values be allowed at all?
TrimWhiteSpace(&data, &size);
if (ARROW_PREDICT_FALSE(!string_converter_.Convert(
concrete_type_, reinterpret_cast<const char*>(data), size, out))) {
return GenericConversionError(type_, data, size);
}
return Status::OK();
}
protected:
const T& concrete_type_;
arrow20::internal::StringConverter<T> string_converter_;
};
//
// Value decoder for booleans
//
struct BooleanValueDecoder : public ValueDecoder {
using value_type = bool;
using ValueDecoder::ValueDecoder;
Status Initialize() {
// TODO no need to build separate Tries for each instance
RETURN_NOT_OK(InitializeTrie(options_.true_values, &true_trie_));
RETURN_NOT_OK(InitializeTrie(options_.false_values, &false_trie_));
return ValueDecoder::Initialize();
}
Status Decode(const uint8_t* data, uint32_t size, bool quoted, value_type* out) {
// XXX should quoted values be allowed at all?
if (false_trie_.Find(std::string_view(reinterpret_cast<const char*>(data), size)) >=
0) {
*out = false;
return Status::OK();
}
if (ARROW_PREDICT_TRUE(true_trie_.Find(std::string_view(
reinterpret_cast<const char*>(data), size)) >= 0)) {
*out = true;
return Status::OK();
}
return GenericConversionError(type_, data, size);
}
protected:
Trie true_trie_;
Trie false_trie_;
};
//
// Value decoder for decimals
//
struct DecimalValueDecoder : public ValueDecoder {
using value_type = Decimal128;
explicit DecimalValueDecoder(const std::shared_ptr<DataType>& type,
const ConvertOptions& options)
: ValueDecoder(type, options),
decimal_type_(internal::checked_cast<const DecimalType&>(*type_)),
type_precision_(decimal_type_.precision()),
type_scale_(decimal_type_.scale()) {}
Status Decode(const uint8_t* data, uint32_t size, bool quoted, value_type* out) {
TrimWhiteSpace(&data, &size);
Decimal128 decimal;
int32_t precision, scale;
std::string_view view(reinterpret_cast<const char*>(data), size);
RETURN_NOT_OK(Decimal128::FromString(view, &decimal, &precision, &scale));
if (precision > type_precision_) {
return Status::Invalid("Error converting '", view, "' to ", type_->ToString(),
": precision not supported by type.");
}
if (scale != type_scale_) {
ARROW_ASSIGN_OR_RAISE(*out, decimal.Rescale(scale, type_scale_));
} else {
*out = std::move(decimal);
}
return Status::OK();
}
protected:
const DecimalType& decimal_type_;
const int32_t type_precision_;
const int32_t type_scale_;
};
//
// Value decoder wrapper for decimals with a non-default decimal point
//
template <typename WrappedDecoder>
struct CustomDecimalPointValueDecoder : public ValueDecoder {
using value_type = typename WrappedDecoder::value_type;
explicit CustomDecimalPointValueDecoder(const std::shared_ptr<DataType>& type,
const ConvertOptions& options)
: ValueDecoder(type, options), wrapped_decoder_(type, options) {}
Status Initialize() {
RETURN_NOT_OK(wrapped_decoder_.Initialize());
for (int i = 0; i < 256; ++i) {
mapping_[i] = i;
}
mapping_[options_.decimal_point] = '.';
mapping_['.'] = options_.decimal_point; // error out on standard decimal point
temp_.resize(30);
return Status::OK();
}
Status Decode(const uint8_t* data, uint32_t size, bool quoted, value_type* out) {
if (ARROW_PREDICT_FALSE(size > temp_.size())) {
temp_.resize(size);
}
uint8_t* temp_data = temp_.data();
for (uint32_t i = 0; i < size; ++i) {
temp_data[i] = mapping_[data[i]];
}
if (ARROW_PREDICT_FALSE(
!wrapped_decoder_.Decode(temp_data, size, quoted, out).ok())) {
return GenericConversionError(type_, data, size);
}
return Status::OK();
}
bool IsNull(const uint8_t* data, uint32_t size, bool quoted) {
return wrapped_decoder_.IsNull(data, size, quoted);
}
protected:
WrappedDecoder wrapped_decoder_;
std::array<uint8_t, 256> mapping_;
std::vector<uint8_t> temp_;
};
//
// Value decoders for timestamps
//
struct InlineISO8601ValueDecoder : public ValueDecoder {
using value_type = int64_t;
explicit InlineISO8601ValueDecoder(const std::shared_ptr<DataType>& type,
const ConvertOptions& options)
: ValueDecoder(type, options),
unit_(checked_cast<const TimestampType&>(*type_).unit()),
expect_timezone_(!checked_cast<const TimestampType&>(*type_).timezone().empty()) {
}
Status Decode(const uint8_t* data, uint32_t size, bool quoted, value_type* out) {
bool zone_offset_present = false;
if (ARROW_PREDICT_FALSE(
!internal::ParseTimestampISO8601(reinterpret_cast<const char*>(data), size,
unit_, out, &zone_offset_present))) {
return GenericConversionError(type_, data, size);
}
if (zone_offset_present != expect_timezone_) {
if (expect_timezone_) {
return Status::Invalid("CSV conversion error to ", type_->ToString(),
": expected a zone offset in '",
std::string(reinterpret_cast<const char*>(data), size),
"'. If these timestamps are in local time, parse them as "
"timestamps without timezone, then call assume_timezone.");
} else {
return Status::Invalid("CSV conversion error to ", type_->ToString(),
": expected no zone offset in '",
std::string(reinterpret_cast<const char*>(data), size),
"'");
}
}
return Status::OK();
}
protected:
TimeUnit::type unit_;
bool expect_timezone_;
};
struct SingleParserTimestampValueDecoder : public ValueDecoder {
using value_type = int64_t;
explicit SingleParserTimestampValueDecoder(const std::shared_ptr<DataType>& type,
const ConvertOptions& options)
: ValueDecoder(type, options),
unit_(checked_cast<const TimestampType&>(*type_).unit()),
expect_timezone_(!checked_cast<const TimestampType&>(*type_).timezone().empty()),
parser_(*options_.timestamp_parsers[0]) {}
Status Decode(const uint8_t* data, uint32_t size, bool quoted, value_type* out) {
bool zone_offset_present = false;
if (ARROW_PREDICT_FALSE(!parser_(reinterpret_cast<const char*>(data), size, unit_,
out, &zone_offset_present))) {
return GenericConversionError(type_, data, size);
}
if (zone_offset_present != expect_timezone_) {
if (expect_timezone_) {
return Status::Invalid("CSV conversion error to ", type_->ToString(),
": expected a zone offset in '",
std::string(reinterpret_cast<const char*>(data), size),
"'. If these timestamps are in local time, parse them as "
"timestamps without timezone, then call assume_timezone. "
"If using strptime, ensure '%z' is in the format string.");
} else {
return Status::Invalid("CSV conversion error to ", type_->ToString(),
": expected no zone offset in '",
std::string(reinterpret_cast<const char*>(data), size),
"'");
}
}
return Status::OK();
}
protected:
TimeUnit::type unit_;
bool expect_timezone_;
const TimestampParser& parser_;
};
struct MultipleParsersTimestampValueDecoder : public ValueDecoder {
using value_type = int64_t;
explicit MultipleParsersTimestampValueDecoder(const std::shared_ptr<DataType>& type,
const ConvertOptions& options)
: ValueDecoder(type, options),
unit_(checked_cast<const TimestampType&>(*type_).unit()),
expect_timezone_(!checked_cast<const TimestampType&>(*type_).timezone().empty()),
parsers_(GetParsers(options_)) {}
Status Decode(const uint8_t* data, uint32_t size, bool quoted, value_type* out) {
bool zone_offset_present = false;
for (const auto& parser : parsers_) {
if (parser->operator()(reinterpret_cast<const char*>(data), size, unit_, out,
&zone_offset_present) &&
zone_offset_present == expect_timezone_) {
return Status::OK();
}
}
return GenericConversionError(type_, data, size);
}
protected:
using ParserVector = std::vector<const TimestampParser*>;
static ParserVector GetParsers(const ConvertOptions& options) {
ParserVector parsers(options.timestamp_parsers.size());
for (size_t i = 0; i < options.timestamp_parsers.size(); ++i) {
parsers[i] = options.timestamp_parsers[i].get();
}
return parsers;
}
TimeUnit::type unit_;
bool expect_timezone_;
std::vector<const TimestampParser*> parsers_;
};
/////////////////////////////////////////////////////////////////////////
// Concrete Converter hierarchy
class ConcreteConverter : public Converter {
public:
using Converter::Converter;
};
class ConcreteDictionaryConverter : public DictionaryConverter {
public:
using DictionaryConverter::DictionaryConverter;
};
//
// Concrete Converter for nulls
//
class NullConverter : public ConcreteConverter {
public:
NullConverter(const std::shared_ptr<DataType>& type, const ConvertOptions& options,
MemoryPool* pool)
: ConcreteConverter(type, options, pool), decoder_(type_, options_) {}
Result<std::shared_ptr<Array>> Convert(const BlockParser& parser,
int32_t col_index) override {
NullBuilder builder(pool_);
auto visit = [&](const uint8_t* data, uint32_t size, bool quoted) -> Status {
if (ARROW_PREDICT_TRUE(decoder_.IsNull(data, size, quoted))) {
return builder.AppendNull();
} else {
return GenericConversionError(type_, data, size);
}
};
RETURN_NOT_OK(parser.VisitColumn(col_index, visit));
std::shared_ptr<Array> res;
RETURN_NOT_OK(builder.Finish(&res));
return res;
}
protected:
Status Initialize() override { return decoder_.Initialize(); }
ValueDecoder decoder_;
};
//
// Concrete Converter for primitives
//
template <typename T, typename ValueDecoderType>
class PrimitiveConverter : public ConcreteConverter {
public:
PrimitiveConverter(const std::shared_ptr<DataType>& type, const ConvertOptions& options,
MemoryPool* pool)
: ConcreteConverter(type, options, pool), decoder_(type_, options_) {}
Result<std::shared_ptr<Array>> Convert(const BlockParser& parser,
int32_t col_index) override {
using BuilderType = typename TypeTraits<T>::BuilderType;
using value_type = typename ValueDecoderType::value_type;
BuilderType builder(type_, pool_);
RETURN_NOT_OK(PresizeBuilder(parser, &builder));
auto visit = [&](const uint8_t* data, uint32_t size, bool quoted) -> Status {
if (decoder_.IsNull(data, size, quoted /* quoted */)) {
return builder.AppendNull();
}
value_type value{};
RETURN_NOT_OK(decoder_.Decode(data, size, quoted, &value));
builder.UnsafeAppend(value);
return Status::OK();
};
RETURN_NOT_OK(parser.VisitColumn(col_index, visit));
std::shared_ptr<Array> res;
RETURN_NOT_OK(builder.Finish(&res));
return res;
}
protected:
Status Initialize() override { return decoder_.Initialize(); }
ValueDecoderType decoder_;
};
//
// Concrete Converter for dictionaries
//
template <typename T, typename ValueDecoderType>
class TypedDictionaryConverter : public ConcreteDictionaryConverter {
public:
TypedDictionaryConverter(const std::shared_ptr<DataType>& value_type,
const ConvertOptions& options, MemoryPool* pool)
: ConcreteDictionaryConverter(value_type, options, pool),
decoder_(value_type, options_) {}
Result<std::shared_ptr<Array>> Convert(const BlockParser& parser,
int32_t col_index) override {
// We use a fixed index width so that all column chunks get the same index type
using BuilderType = Dictionary32Builder<T>;
using value_type = typename ValueDecoderType::value_type;
BuilderType builder(value_type_, pool_);
RETURN_NOT_OK(PresizeBuilder(parser, &builder));
auto visit = [&](const uint8_t* data, uint32_t size, bool quoted) -> Status {
if (decoder_.IsNull(data, size, quoted /* quoted */)) {
return builder.AppendNull();
}
if (ARROW_PREDICT_FALSE(builder.dictionary_length() > max_cardinality_)) {
return Status::IndexError("Dictionary length exceeded max cardinality");
}
value_type value{};
RETURN_NOT_OK(decoder_.Decode(data, size, quoted, &value));
return builder.Append(value);
};
RETURN_NOT_OK(parser.VisitColumn(col_index, visit));
std::shared_ptr<Array> res;
RETURN_NOT_OK(builder.Finish(&res));
return res;
}
void SetMaxCardinality(int32_t max_length) override { max_cardinality_ = max_length; }
protected:
Status Initialize() override {
util::InitializeUTF8();
return decoder_.Initialize();
}
ValueDecoderType decoder_;
int32_t max_cardinality_ = std::numeric_limits<int32_t>::max();
};
//
// Concrete Converter factory for timestamps
//
template <template <typename, typename> class ConverterType>
std::shared_ptr<Converter> MakeTimestampConverter(const std::shared_ptr<DataType>& type,
const ConvertOptions& options,
MemoryPool* pool) {
if (options.timestamp_parsers.size() == 0) {
// Default to ISO-8601
return std::make_shared<ConverterType<TimestampType, InlineISO8601ValueDecoder>>(
type, options, pool);
} else if (options.timestamp_parsers.size() == 1) {
// Single user-supplied converter
return std::make_shared<
ConverterType<TimestampType, SingleParserTimestampValueDecoder>>(type, options,
pool);
} else {
// Multiple converters, must iterate for each value
return std::make_shared<
ConverterType<TimestampType, MultipleParsersTimestampValueDecoder>>(type, options,
pool);
}
}
//
// Concrete Converter factory for reals
//
template <typename ConverterType, template <typename...> class ConcreteConverterType,
typename Type, typename DecoderType>
std::shared_ptr<ConverterType> MakeRealConverter(const std::shared_ptr<DataType>& type,
const ConvertOptions& options,
MemoryPool* pool) {
if (options.decimal_point == '.') {
return std::make_shared<ConcreteConverterType<Type, DecoderType>>(type, options,
pool);
}
return std::make_shared<
ConcreteConverterType<Type, CustomDecimalPointValueDecoder<DecoderType>>>(
type, options, pool);
}
} // namespace
/////////////////////////////////////////////////////////////////////////
// Base Converter class implementation
Converter::Converter(const std::shared_ptr<DataType>& type, const ConvertOptions& options,
MemoryPool* pool)
: options_(options), pool_(pool), type_(type) {}
DictionaryConverter::DictionaryConverter(const std::shared_ptr<DataType>& value_type,
const ConvertOptions& options, MemoryPool* pool)
: Converter(dictionary(int32(), value_type), options, pool),
value_type_(value_type) {}
Result<std::shared_ptr<Converter>> Converter::Make(const std::shared_ptr<DataType>& type,
const ConvertOptions& options,
MemoryPool* pool) {
std::shared_ptr<Converter> ptr;
switch (type->id()) {
#define CONVERTER_CASE(TYPE_ID, CONVERTER_TYPE) \
case TYPE_ID: \
ptr.reset(new CONVERTER_TYPE(type, options, pool)); \
break;
#define NUMERIC_CONVERTER_CASE(TYPE_ID, TYPE_CLASS) \
CONVERTER_CASE(TYPE_ID, \
(PrimitiveConverter<TYPE_CLASS, NumericValueDecoder<TYPE_CLASS>>))
#define REAL_CONVERTER_CASE(TYPE_ID, TYPE_CLASS, DECODER) \
case TYPE_ID: \
ptr = MakeRealConverter<Converter, PrimitiveConverter, TYPE_CLASS, DECODER>( \
type, options, pool); \
break;
CONVERTER_CASE(Type::NA, NullConverter)
NUMERIC_CONVERTER_CASE(Type::INT8, Int8Type)
NUMERIC_CONVERTER_CASE(Type::INT16, Int16Type)
NUMERIC_CONVERTER_CASE(Type::INT32, Int32Type)
NUMERIC_CONVERTER_CASE(Type::INT64, Int64Type)
NUMERIC_CONVERTER_CASE(Type::UINT8, UInt8Type)
NUMERIC_CONVERTER_CASE(Type::UINT16, UInt16Type)
NUMERIC_CONVERTER_CASE(Type::UINT32, UInt32Type)
NUMERIC_CONVERTER_CASE(Type::UINT64, UInt64Type)
NUMERIC_CONVERTER_CASE(Type::FLOAT, FloatType)
NUMERIC_CONVERTER_CASE(Type::DOUBLE, DoubleType)
REAL_CONVERTER_CASE(Type::DECIMAL, Decimal128Type, DecimalValueDecoder)
NUMERIC_CONVERTER_CASE(Type::DATE32, Date32Type)
NUMERIC_CONVERTER_CASE(Type::DATE64, Date64Type)
NUMERIC_CONVERTER_CASE(Type::TIME32, Time32Type)
NUMERIC_CONVERTER_CASE(Type::TIME64, Time64Type)
CONVERTER_CASE(Type::BOOL, (PrimitiveConverter<BooleanType, BooleanValueDecoder>))
CONVERTER_CASE(Type::BINARY,
(PrimitiveConverter<BinaryType, BinaryValueDecoder<false>>))
CONVERTER_CASE(Type::LARGE_BINARY,
(PrimitiveConverter<LargeBinaryType, BinaryValueDecoder<false>>))
CONVERTER_CASE(Type::FIXED_SIZE_BINARY,
(PrimitiveConverter<FixedSizeBinaryType, FixedSizeBinaryValueDecoder>))
case Type::TIMESTAMP:
ptr = MakeTimestampConverter<PrimitiveConverter>(type, options, pool);
break;
case Type::STRING:
if (options.check_utf8) {
ptr = std::make_shared<PrimitiveConverter<StringType, BinaryValueDecoder<true>>>(
type, options, pool);
} else {
ptr = std::make_shared<PrimitiveConverter<StringType, BinaryValueDecoder<false>>>(
type, options, pool);
}
break;
case Type::LARGE_STRING:
if (options.check_utf8) {
ptr = std::make_shared<
PrimitiveConverter<LargeStringType, BinaryValueDecoder<true>>>(type, options,
pool);
} else {
ptr = std::make_shared<
PrimitiveConverter<LargeStringType, BinaryValueDecoder<false>>>(type, options,
pool);
}
break;
case Type::DICTIONARY: {
const auto& dict_type = checked_cast<const DictionaryType&>(*type);
if (dict_type.index_type()->id() != Type::INT32) {
return Status::NotImplemented(
"CSV conversion to dictionary only supported for int32 indices, "
"got ",
type->ToString());
}
return DictionaryConverter::Make(dict_type.value_type(), options, pool);
}
default: {
return Status::NotImplemented("CSV conversion to ", type->ToString(),
" is not supported");
}
#undef CONVERTER_CASE
#undef NUMERIC_CONVERTER_CASE
#undef REAL_CONVERTER_CASE
}
RETURN_NOT_OK(ptr->Initialize());
return ptr;
}
Result<std::shared_ptr<DictionaryConverter>> DictionaryConverter::Make(
const std::shared_ptr<DataType>& type, const ConvertOptions& options,
MemoryPool* pool) {
std::shared_ptr<DictionaryConverter> ptr;
switch (type->id()) {
#define CONVERTER_CASE(TYPE_ID, TYPE, VALUE_DECODER_TYPE) \
case TYPE_ID: \
ptr.reset( \
new TypedDictionaryConverter<TYPE, VALUE_DECODER_TYPE>(type, options, pool)); \
break;
#define REAL_CONVERTER_CASE(TYPE_ID, TYPE_CLASS, DECODER) \
case TYPE_ID: \
ptr = MakeRealConverter<DictionaryConverter, TypedDictionaryConverter, TYPE_CLASS, \
DECODER>(type, options, pool); \
break;
// XXX Are 32-bit types useful?
CONVERTER_CASE(Type::INT32, Int32Type, NumericValueDecoder<Int32Type>)
CONVERTER_CASE(Type::INT64, Int64Type, NumericValueDecoder<Int64Type>)
CONVERTER_CASE(Type::UINT32, UInt32Type, NumericValueDecoder<UInt32Type>)
CONVERTER_CASE(Type::UINT64, UInt64Type, NumericValueDecoder<UInt64Type>)
CONVERTER_CASE(Type::FLOAT, FloatType, NumericValueDecoder<FloatType>)
CONVERTER_CASE(Type::DOUBLE, DoubleType, NumericValueDecoder<DoubleType>)
REAL_CONVERTER_CASE(Type::DECIMAL, Decimal128Type, DecimalValueDecoder)
CONVERTER_CASE(Type::FIXED_SIZE_BINARY, FixedSizeBinaryType,
FixedSizeBinaryValueDecoder)
CONVERTER_CASE(Type::BINARY, BinaryType, BinaryValueDecoder<false>)
CONVERTER_CASE(Type::LARGE_BINARY, LargeBinaryType, BinaryValueDecoder<false>)
case Type::STRING:
if (options.check_utf8) {
ptr = std::make_shared<
TypedDictionaryConverter<StringType, BinaryValueDecoder<true>>>(type, options,
pool);
} else {
ptr = std::make_shared<
TypedDictionaryConverter<StringType, BinaryValueDecoder<false>>>(
type, options, pool);
}
break;
case Type::LARGE_STRING:
if (options.check_utf8) {
ptr = std::make_shared<
TypedDictionaryConverter<LargeStringType, BinaryValueDecoder<true>>>(
type, options, pool);
} else {
ptr = std::make_shared<
TypedDictionaryConverter<LargeStringType, BinaryValueDecoder<false>>>(
type, options, pool);
}
break;
default: {
return Status::NotImplemented("CSV dictionary conversion to ", type->ToString(),
" is not supported");
}
#undef CONVERTER_CASE
#undef REAL_CONVERTER_CASE
}
RETURN_NOT_OK(ptr->Initialize());
return ptr;
}
} // namespace csv
} // namespace arrow20
|