forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_to_arrow.cc
More file actions
1054 lines (910 loc) · 35.5 KB
/
Copy pathpython_to_arrow.cc
File metadata and controls
1054 lines (910 loc) · 35.5 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
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
// 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 "arrow/python/python_to_arrow.h"
#include "arrow/python/numpy_interop.h"
#include <datetime.h>
#include <algorithm>
#include <limits>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include "arrow/array.h"
#include "arrow/builder.h"
#include "arrow/status.h"
#include "arrow/table.h"
#include "arrow/type.h"
#include "arrow/type_traits.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/decimal.h"
#include "arrow/util/logging.h"
#include "arrow/python/decimal.h"
#include "arrow/python/helpers.h"
#include "arrow/python/inference.h"
#include "arrow/python/iterators.h"
#include "arrow/python/numpy_convert.h"
#include "arrow/python/type_traits.h"
#include "arrow/python/util/datetime.h"
namespace arrow {
using internal::checked_cast;
namespace py {
// ----------------------------------------------------------------------
// Sequence converter base and CRTP "middle" subclasses
class SeqConverter;
// Forward-declare converter factory
Status GetConverter(const std::shared_ptr<DataType>& type, bool from_pandas,
bool strict_conversions, std::unique_ptr<SeqConverter>* out);
// Marshal Python sequence (list, tuple, etc.) to Arrow array
class SeqConverter {
public:
virtual ~SeqConverter() = default;
// Initialize the sequence converter with an ArrayBuilder created
// externally. The reason for this interface is that we have
// arrow::MakeBuilder which also creates child builders for nested types, so
// we have to pass in the child builders to child SeqConverter in the case of
// converting Python objects to Arrow nested types
virtual Status Init(ArrayBuilder* builder) = 0;
// Append a single (non-sequence) Python datum to the underlying builder,
// virtual function
virtual Status AppendSingleVirtual(PyObject* obj) = 0;
// Append the contents of a Python sequence to the underlying builder,
// virtual version
virtual Status AppendMultiple(PyObject* seq, int64_t size) = 0;
// Append the contents of a Python sequence to the underlying builder,
// virtual version
virtual Status AppendMultipleMasked(PyObject* seq, PyObject* mask, int64_t size) = 0;
virtual Status GetResult(std::vector<std::shared_ptr<Array>>* chunks) {
*chunks = chunks_;
// Still some accumulated data in the builder. If there are no chunks, we
// always call Finish to deal with the edge case where a size-0 sequence
// was converted with a specific output type, like array([], type=t)
if (chunks_.size() == 0 || builder_->length() > 0) {
std::shared_ptr<Array> last_chunk;
RETURN_NOT_OK(builder_->Finish(&last_chunk));
chunks->emplace_back(std::move(last_chunk));
}
return Status::OK();
}
ArrayBuilder* builder() const { return builder_; }
protected:
ArrayBuilder* builder_;
bool unfinished_builder_;
std::vector<std::shared_ptr<Array>> chunks_;
};
enum class NullCoding : char { NONE_ONLY, PANDAS_SENTINELS };
template <NullCoding kind>
struct NullChecker {};
template <>
struct NullChecker<NullCoding::NONE_ONLY> {
static inline bool Check(PyObject* obj) { return obj == Py_None; }
};
template <>
struct NullChecker<NullCoding::PANDAS_SENTINELS> {
static inline bool Check(PyObject* obj) { return internal::PandasObjectIsNull(obj); }
};
// ----------------------------------------------------------------------
// Helper templates to append PyObject* to builder for each target conversion
// type
template <typename Type, typename Enable = void>
struct Unbox {};
template <typename Type>
struct Unbox<Type, enable_if_integer<Type>> {
using BuilderType = typename TypeTraits<Type>::BuilderType;
static inline Status Append(BuilderType* builder, PyObject* obj) {
typename Type::c_type value;
RETURN_NOT_OK(internal::CIntFromPython(obj, &value));
return builder->Append(value);
}
};
template <>
struct Unbox<HalfFloatType> {
static inline Status Append(HalfFloatBuilder* builder, PyObject* obj) {
npy_half val;
RETURN_NOT_OK(PyFloat_AsHalf(obj, &val));
return builder->Append(val);
}
};
template <>
struct Unbox<FloatType> {
static inline Status Append(FloatBuilder* builder, PyObject* obj) {
if (internal::PyFloatScalar_Check(obj)) {
float val = static_cast<float>(PyFloat_AsDouble(obj));
RETURN_IF_PYERROR();
return builder->Append(val);
} else if (internal::PyIntScalar_Check(obj)) {
float val = 0;
RETURN_NOT_OK(internal::IntegerScalarToFloat32Safe(obj, &val));
return builder->Append(val);
} else {
return internal::InvalidValue(obj, "tried to convert to float32");
}
}
};
template <>
struct Unbox<DoubleType> {
static inline Status Append(DoubleBuilder* builder, PyObject* obj) {
if (PyFloat_Check(obj)) {
double val = PyFloat_AS_DOUBLE(obj);
return builder->Append(val);
} else if (internal::PyFloatScalar_Check(obj)) {
// Other kinds of float-y things
double val = PyFloat_AsDouble(obj);
RETURN_IF_PYERROR();
return builder->Append(val);
} else if (internal::PyIntScalar_Check(obj)) {
double val = 0;
RETURN_NOT_OK(internal::IntegerScalarToDoubleSafe(obj, &val));
return builder->Append(val);
} else {
return internal::InvalidValue(obj, "tried to convert to double");
}
}
};
// We use CRTP to avoid virtual calls to the AppendItem(), AppendNull(), and
// IsNull() on the hot path
template <typename Type, class Derived,
NullCoding null_coding = NullCoding::PANDAS_SENTINELS>
class TypedConverter : public SeqConverter {
public:
using BuilderType = typename TypeTraits<Type>::BuilderType;
Status Init(ArrayBuilder* builder) override {
builder_ = builder;
DCHECK_NE(builder_, nullptr);
typed_builder_ = checked_cast<BuilderType*>(builder);
return Status::OK();
}
bool CheckNull(PyObject* obj) const { return NullChecker<null_coding>::Check(obj); }
// Append a missing item (default implementation)
Status AppendNull() { return this->typed_builder_->AppendNull(); }
// This is overridden in several subclasses, but if an Unbox implementation
// is defined, it will be used here
Status AppendItem(PyObject* obj) { return Unbox<Type>::Append(typed_builder_, obj); }
Status AppendSingle(PyObject* obj) {
auto self = checked_cast<Derived*>(this);
return CheckNull(obj) ? self->AppendNull() : self->AppendItem(obj);
}
Status AppendSingleVirtual(PyObject* obj) override { return AppendSingle(obj); }
Status AppendMultiple(PyObject* obj, int64_t size) override {
/// Ensure we've allocated enough space
RETURN_NOT_OK(this->typed_builder_->Reserve(size));
// Iterate over the items adding each one
auto self = checked_cast<Derived*>(this);
return internal::VisitSequence(obj, [self](PyObject* item, bool* /* unused */) {
return self->AppendSingle(item);
});
}
Status AppendMultipleMasked(PyObject* obj, PyObject* mask, int64_t size) override {
/// Ensure we've allocated enough space
RETURN_NOT_OK(this->typed_builder_->Reserve(size));
// Iterate over the items adding each one
auto self = checked_cast<Derived*>(this);
return internal::VisitSequenceMasked(
obj, mask, [self](PyObject* item, bool is_masked, bool* /* unused */) {
if (is_masked) {
return self->AppendNull();
} else {
// This will also apply the null-checking convention in the event
// that the value is not masked
return self->AppendSingle(item);
}
});
}
protected:
BuilderType* typed_builder_;
};
// ----------------------------------------------------------------------
// Sequence converter for null type
class NullConverter : public TypedConverter<NullType, NullConverter> {
public:
Status AppendItem(PyObject* obj) {
return internal::InvalidValue(obj, "converting to null type");
}
};
// ----------------------------------------------------------------------
// Sequence converter for boolean type
class BoolConverter : public TypedConverter<BooleanType, BoolConverter> {
public:
Status AppendItem(PyObject* obj) {
if (obj == Py_True) {
return typed_builder_->Append(true);
} else if (obj == Py_False) {
return typed_builder_->Append(false);
} else {
return internal::InvalidValue(obj, "tried to convert to boolean");
}
}
};
// ----------------------------------------------------------------------
// Sequence converter template for numeric (integer and floating point) types
template <typename Type, NullCoding null_coding>
class NumericConverter
: public TypedConverter<Type, NumericConverter<Type, null_coding>, null_coding> {};
// ----------------------------------------------------------------------
// Sequence converters for temporal types
class Date32Converter : public TypedConverter<Date32Type, Date32Converter> {
public:
Status AppendItem(PyObject* obj) {
int32_t t;
if (PyDate_Check(obj)) {
auto pydate = reinterpret_cast<PyDateTime_Date*>(obj);
t = static_cast<int32_t>(PyDate_to_days(pydate));
} else {
RETURN_NOT_OK(internal::CIntFromPython(obj, &t, "Integer too large for date32"));
}
return typed_builder_->Append(t);
}
};
class Date64Converter : public TypedConverter<Date64Type, Date64Converter> {
public:
Status AppendItem(PyObject* obj) {
int64_t t;
if (PyDate_Check(obj)) {
auto pydate = reinterpret_cast<PyDateTime_Date*>(obj);
t = PyDate_to_ms(pydate);
} else {
RETURN_NOT_OK(internal::CIntFromPython(obj, &t, "Integer too large for date64"));
}
return typed_builder_->Append(t);
}
};
class Time32Converter : public TypedConverter<Time32Type, Time32Converter> {
public:
explicit Time32Converter(TimeUnit::type unit) : unit_(unit) {}
Status AppendItem(PyObject* obj) {
// TODO(kszucs): option for strict conversion?
int32_t t;
if (PyTime_Check(obj)) {
// datetime.time stores microsecond resolution
switch (unit_) {
case TimeUnit::SECOND:
t = static_cast<int32_t>(PyTime_to_s(obj));
break;
case TimeUnit::MILLI:
t = static_cast<int32_t>(PyTime_to_ms(obj));
break;
default:
return Status::UnknownError("Invalid time unit");
}
return typed_builder_->Append(t);
} else {
return internal::InvalidValue(obj, "converting to time32");
}
}
private:
TimeUnit::type unit_;
};
class Time64Converter : public TypedConverter<Time64Type, Time64Converter> {
public:
explicit Time64Converter(TimeUnit::type unit) : unit_(unit) {}
Status AppendItem(PyObject* obj) {
int64_t t;
if (PyTime_Check(obj)) {
// datetime.time stores microsecond resolution
switch (unit_) {
case TimeUnit::MICRO:
t = PyTime_to_us(obj);
break;
case TimeUnit::NANO:
t = PyTime_to_ns(obj);
break;
default:
return Status::UnknownError("Invalid time unit");
}
return typed_builder_->Append(t);
} else {
return internal::InvalidValue(obj, "converting to time64");
}
}
private:
TimeUnit::type unit_;
};
class TimestampConverter : public TypedConverter<TimestampType, TimestampConverter> {
public:
explicit TimestampConverter(TimeUnit::type unit) : unit_(unit) {}
Status AppendItem(PyObject* obj) {
int64_t t;
if (PyDateTime_Check(obj)) {
auto pydatetime = reinterpret_cast<PyDateTime_DateTime*>(obj);
switch (unit_) {
case TimeUnit::SECOND:
t = PyDateTime_to_s(pydatetime);
break;
case TimeUnit::MILLI:
t = PyDateTime_to_ms(pydatetime);
break;
case TimeUnit::MICRO:
t = PyDateTime_to_us(pydatetime);
break;
case TimeUnit::NANO:
t = PyDateTime_to_ns(pydatetime);
break;
default:
return Status::UnknownError("Invalid time unit");
}
} else if (PyArray_CheckAnyScalarExact(obj)) {
// numpy.datetime64
using traits = internal::npy_traits<NPY_DATETIME>;
std::shared_ptr<DataType> type;
RETURN_NOT_OK(NumPyDtypeToArrow(PyArray_DescrFromScalar(obj), &type));
if (type->id() != Type::TIMESTAMP) {
return Status::Invalid("Expected np.datetime64 but got: ", type->ToString());
}
const TimestampType& ttype = checked_cast<const TimestampType&>(*type);
if (unit_ != ttype.unit()) {
return Status::NotImplemented(
"Cannot convert NumPy datetime64 objects with differing unit");
}
t = reinterpret_cast<PyDatetimeScalarObject*>(obj)->obval;
if (traits::isnull(t)) {
// checks numpy NaT sentinel after conversion
return typed_builder_->AppendNull();
}
} else {
RETURN_NOT_OK(internal::CIntFromPython(obj, &t));
}
return typed_builder_->Append(t);
}
private:
TimeUnit::type unit_;
};
// ----------------------------------------------------------------------
// Sequence converters for Binary, FixedSizeBinary, String
namespace detail {
template <typename BuilderType>
inline Status AppendPyString(BuilderType* builder, const PyBytesView& view,
bool* is_full) {
if (view.size > BuilderType::memory_limit()) {
return Status::Invalid("string too large for datatype");
}
DCHECK_GE(view.size, 0);
// Did we reach the builder size limit?
if (ARROW_PREDICT_FALSE(builder->value_data_length() + view.size >
BuilderType::memory_limit())) {
*is_full = true;
return Status::OK();
}
RETURN_NOT_OK(builder->Append(::arrow::util::string_view(view.bytes, view.size)));
*is_full = false;
return Status::OK();
}
inline Status BuilderAppend(BinaryBuilder* builder, PyObject* obj, bool* is_full) {
PyBytesView view;
RETURN_NOT_OK(view.FromString(obj));
return AppendPyString(builder, view, is_full);
}
inline Status BuilderAppend(LargeBinaryBuilder* builder, PyObject* obj, bool* is_full) {
PyBytesView view;
RETURN_NOT_OK(view.FromString(obj));
return AppendPyString(builder, view, is_full);
}
inline Status BuilderAppend(FixedSizeBinaryBuilder* builder, PyObject* obj,
bool* is_full) {
PyBytesView view;
RETURN_NOT_OK(view.FromString(obj));
const auto expected_length =
checked_cast<const FixedSizeBinaryType&>(*builder->type()).byte_width();
if (ARROW_PREDICT_FALSE(view.size != expected_length)) {
std::stringstream ss;
ss << "expected to be length " << expected_length << " was " << view.size;
return internal::InvalidValue(obj, ss.str());
}
return AppendPyString(builder, view, is_full);
}
} // namespace detail
template <typename Type>
class BinaryLikeConverter : public TypedConverter<Type, BinaryLikeConverter<Type>> {
public:
Status AppendItem(PyObject* obj) {
// Accessing members of the templated base requires using this-> here
bool is_full = false;
RETURN_NOT_OK(detail::BuilderAppend(this->typed_builder_, obj, &is_full));
// Exceeded capacity of builder
if (ARROW_PREDICT_FALSE(is_full)) {
std::shared_ptr<Array> chunk;
RETURN_NOT_OK(this->typed_builder_->Finish(&chunk));
this->chunks_.emplace_back(std::move(chunk));
// Append the item now that the builder has been reset
return detail::BuilderAppend(this->typed_builder_, obj, &is_full);
}
return Status::OK();
}
};
class BytesConverter : public BinaryLikeConverter<BinaryType> {};
class LargeBytesConverter : public BinaryLikeConverter<LargeBinaryType> {};
class FixedWidthBytesConverter : public BinaryLikeConverter<FixedSizeBinaryType> {};
// For String/UTF8, if strict_conversions enabled, we reject any non-UTF8,
// otherwise we allow but return results as BinaryArray
template <typename TypeClass, bool STRICT>
class StringConverter
: public TypedConverter<TypeClass, StringConverter<TypeClass, STRICT>> {
public:
StringConverter() : binary_count_(0) {}
Status Append(PyObject* obj, bool* is_full) {
if (STRICT) {
// Force output to be unicode / utf8 and validate that any binary values
// are utf8
bool is_utf8 = false;
RETURN_NOT_OK(string_view_.FromString(obj, &is_utf8));
if (!is_utf8) {
return internal::InvalidValue(obj, "was not a utf8 string");
}
} else {
// Non-strict conversion; keep track of whether values are unicode or
// bytes; if any bytes are observe, the result will be bytes
if (PyUnicode_Check(obj)) {
RETURN_NOT_OK(string_view_.FromUnicode(obj));
} else {
// If not unicode or bytes, FromBinary will error
RETURN_NOT_OK(string_view_.FromBinary(obj));
++binary_count_;
}
}
return detail::AppendPyString(this->typed_builder_, string_view_, is_full);
}
Status AppendItem(PyObject* obj) {
bool is_full = false;
RETURN_NOT_OK(Append(obj, &is_full));
// Exceeded capacity of builder
if (ARROW_PREDICT_FALSE(is_full)) {
std::shared_ptr<Array> chunk;
RETURN_NOT_OK(this->typed_builder_->Finish(&chunk));
this->chunks_.emplace_back(std::move(chunk));
// Append the item now that the builder has been reset
RETURN_NOT_OK(Append(obj, &is_full));
}
return Status::OK();
}
virtual Status GetResult(std::vector<std::shared_ptr<Array>>* out) {
RETURN_NOT_OK(SeqConverter::GetResult(out));
// If we saw any non-unicode, cast results to BinaryArray
if (binary_count_) {
// We should have bailed out earlier
DCHECK(!STRICT);
using EquivalentBinaryType = typename TypeClass::EquivalentBinaryType;
using EquivalentBinaryArray = typename TypeTraits<EquivalentBinaryType>::ArrayType;
for (size_t i = 0; i < out->size(); ++i) {
auto binary_data = (*out)[i]->data()->Copy();
binary_data->type = TypeTraits<EquivalentBinaryType>::type_singleton();
(*out)[i] = std::make_shared<EquivalentBinaryArray>(binary_data);
}
}
return Status::OK();
}
private:
// Create a single instance of PyBytesView here to prevent unnecessary object
// creation/destruction
PyBytesView string_view_;
int64_t binary_count_;
};
// ----------------------------------------------------------------------
// Convert lists (NumPy arrays containing lists or ndarrays as values)
template <typename TypeClass>
class ListConverter : public TypedConverter<TypeClass, ListConverter<TypeClass>> {
public:
using BuilderType = typename TypeTraits<TypeClass>::BuilderType;
explicit ListConverter(bool from_pandas, bool strict_conversions)
: from_pandas_(from_pandas), strict_conversions_(strict_conversions) {}
Status Init(ArrayBuilder* builder) {
this->builder_ = builder;
this->typed_builder_ = checked_cast<BuilderType*>(builder);
value_type_ = checked_cast<const TypeClass&>(*builder->type()).value_type();
RETURN_NOT_OK(
GetConverter(value_type_, from_pandas_, strict_conversions_, &value_converter_));
return value_converter_->Init(this->typed_builder_->value_builder());
}
template <int NUMPY_TYPE, typename Type>
Status AppendNdarrayTypedItem(PyArrayObject* arr);
Status AppendNdarrayItem(PyObject* arr);
Status AppendItem(PyObject* obj) {
RETURN_NOT_OK(this->typed_builder_->Append());
if (PyArray_Check(obj)) {
return AppendNdarrayItem(obj);
}
const auto list_size = static_cast<int64_t>(PySequence_Size(obj));
if (ARROW_PREDICT_FALSE(list_size == -1)) {
RETURN_IF_PYERROR();
}
return value_converter_->AppendMultiple(obj, list_size);
}
// virtual Status GetResult(std::vector<std::shared_ptr<Array>>* chunks) {
// // TODO: Handle chunked children
// return SeqConverter::GetResult(chunks);
// }
protected:
std::shared_ptr<DataType> value_type_;
std::unique_ptr<SeqConverter> value_converter_;
bool from_pandas_;
bool strict_conversions_;
};
template <typename TypeClass>
template <int NUMPY_TYPE, typename Type>
Status ListConverter<TypeClass>::AppendNdarrayTypedItem(PyArrayObject* arr) {
using traits = internal::npy_traits<NUMPY_TYPE>;
using T = typename traits::value_type;
using ValueBuilderType = typename TypeTraits<Type>::BuilderType;
const bool null_sentinels_possible =
// Always treat Numpy's NaT as null
NUMPY_TYPE == NPY_DATETIME ||
// Observing pandas's null sentinels
(from_pandas_ && traits::supports_nulls);
auto child_builder = checked_cast<ValueBuilderType*>(value_converter_->builder());
// TODO(wesm): Vector append when not strided
Ndarray1DIndexer<T> values(arr);
if (null_sentinels_possible) {
for (int64_t i = 0; i < values.size(); ++i) {
if (traits::isnull(values[i])) {
RETURN_NOT_OK(child_builder->AppendNull());
} else {
RETURN_NOT_OK(child_builder->Append(values[i]));
}
}
} else {
for (int64_t i = 0; i < values.size(); ++i) {
RETURN_NOT_OK(child_builder->Append(values[i]));
}
}
return Status::OK();
}
// If the value type does not match the expected NumPy dtype, then fall through
// to a slower PySequence-based path
#define LIST_FAST_CASE(TYPE, NUMPY_TYPE, ArrowType) \
case Type::TYPE: { \
if (PyArray_DESCR(arr)->type_num != NUMPY_TYPE) { \
return value_converter_->AppendMultiple(obj, value_length); \
} \
return AppendNdarrayTypedItem<NUMPY_TYPE, ArrowType>(arr); \
}
// Use internal::VisitSequence, fast for NPY_OBJECT but slower otherwise
#define LIST_SLOW_CASE(TYPE) \
case Type::TYPE: { \
return value_converter_->AppendMultiple(obj, value_length); \
}
template <typename TypeClass>
Status ListConverter<TypeClass>::AppendNdarrayItem(PyObject* obj) {
PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(obj);
if (PyArray_NDIM(arr) != 1) {
return Status::Invalid("Can only convert 1-dimensional array values");
}
const int64_t value_length = PyArray_SIZE(arr);
switch (value_type_->id()) {
LIST_SLOW_CASE(NA)
LIST_FAST_CASE(UINT8, NPY_UINT8, UInt8Type)
LIST_FAST_CASE(INT8, NPY_INT8, Int8Type)
LIST_FAST_CASE(UINT16, NPY_UINT16, UInt16Type)
LIST_FAST_CASE(INT16, NPY_INT16, Int16Type)
LIST_FAST_CASE(UINT32, NPY_UINT32, UInt32Type)
LIST_FAST_CASE(INT32, NPY_INT32, Int32Type)
LIST_FAST_CASE(UINT64, NPY_UINT64, UInt64Type)
LIST_FAST_CASE(INT64, NPY_INT64, Int64Type)
LIST_SLOW_CASE(DATE32)
LIST_SLOW_CASE(DATE64)
LIST_SLOW_CASE(TIME32)
LIST_SLOW_CASE(TIME64)
LIST_FAST_CASE(TIMESTAMP, NPY_DATETIME, TimestampType)
LIST_FAST_CASE(HALF_FLOAT, NPY_FLOAT16, HalfFloatType)
LIST_FAST_CASE(FLOAT, NPY_FLOAT, FloatType)
LIST_FAST_CASE(DOUBLE, NPY_DOUBLE, DoubleType)
LIST_SLOW_CASE(BINARY)
LIST_SLOW_CASE(FIXED_SIZE_BINARY)
LIST_SLOW_CASE(STRING)
case Type::LIST: {
if (PyArray_DESCR(arr)->type_num != NPY_OBJECT) {
return Status::Invalid(
"Can only convert list types from NumPy object "
"array input");
}
return internal::VisitSequence(obj, [this](PyObject* item, bool*) {
return value_converter_->AppendSingleVirtual(item);
});
}
default: {
return Status::TypeError("Unknown list item type: ", value_type_->ToString());
}
}
}
// ----------------------------------------------------------------------
// Convert structs
class StructConverter : public TypedConverter<StructType, StructConverter> {
public:
explicit StructConverter(bool from_pandas, bool strict_conversions)
: from_pandas_(from_pandas), strict_conversions_(strict_conversions) {}
Status Init(ArrayBuilder* builder) {
builder_ = builder;
typed_builder_ = checked_cast<StructBuilder*>(builder);
const auto& struct_type = checked_cast<const StructType&>(*builder->type());
num_fields_ = typed_builder_->num_fields();
DCHECK_EQ(num_fields_, struct_type.num_children());
field_name_list_.reset(PyList_New(num_fields_));
RETURN_IF_PYERROR();
// Initialize the child converters and field names
for (int i = 0; i < num_fields_; i++) {
const std::string& field_name(struct_type.child(i)->name());
std::shared_ptr<DataType> field_type(struct_type.child(i)->type());
std::unique_ptr<SeqConverter> value_converter;
RETURN_NOT_OK(
GetConverter(field_type, from_pandas_, strict_conversions_, &value_converter));
RETURN_NOT_OK(value_converter->Init(typed_builder_->field_builder(i)));
value_converters_.push_back(std::move(value_converter));
// Store the field name as a PyObject, for dict matching
PyObject* nameobj =
PyUnicode_FromStringAndSize(field_name.c_str(), field_name.size());
RETURN_IF_PYERROR();
PyList_SET_ITEM(field_name_list_.obj(), i, nameobj);
}
return Status::OK();
}
Status AppendItem(PyObject* obj) {
RETURN_NOT_OK(typed_builder_->Append());
// Note heterogenous sequences are not allowed
if (ARROW_PREDICT_FALSE(source_kind_ == UNKNOWN)) {
if (PyDict_Check(obj)) {
source_kind_ = DICTS;
} else if (PyTuple_Check(obj)) {
source_kind_ = TUPLES;
}
}
if (PyDict_Check(obj) && source_kind_ == DICTS) {
return AppendDictItem(obj);
} else if (PyTuple_Check(obj) && source_kind_ == TUPLES) {
return AppendTupleItem(obj);
} else {
return Status::TypeError("Expected sequence of dicts or tuples for struct type");
}
}
// Append a missing item
Status AppendNull() {
RETURN_NOT_OK(typed_builder_->AppendNull());
// Need to also insert a missing item on all child builders
// (compare with ListConverter)
for (int i = 0; i < num_fields_; i++) {
RETURN_NOT_OK(value_converters_[i]->AppendSingleVirtual(Py_None));
}
return Status::OK();
}
protected:
Status AppendDictItem(PyObject* obj) {
// NOTE we're ignoring any extraneous dict items
for (int i = 0; i < num_fields_; i++) {
PyObject* nameobj = PyList_GET_ITEM(field_name_list_.obj(), i);
PyObject* valueobj = PyDict_GetItem(obj, nameobj); // borrowed
RETURN_IF_PYERROR();
RETURN_NOT_OK(
value_converters_[i]->AppendSingleVirtual(valueobj ? valueobj : Py_None));
}
return Status::OK();
}
Status AppendTupleItem(PyObject* obj) {
if (PyTuple_GET_SIZE(obj) != num_fields_) {
return Status::Invalid("Tuple size must be equal to number of struct fields");
}
for (int i = 0; i < num_fields_; i++) {
PyObject* valueobj = PyTuple_GET_ITEM(obj, i);
RETURN_NOT_OK(value_converters_[i]->AppendSingleVirtual(valueobj));
}
return Status::OK();
}
std::vector<std::unique_ptr<SeqConverter>> value_converters_;
OwnedRef field_name_list_;
int num_fields_;
// Whether we're converting from a sequence of dicts or tuples
enum { UNKNOWN, DICTS, TUPLES } source_kind_ = UNKNOWN;
bool from_pandas_;
bool strict_conversions_;
};
class DecimalConverter : public TypedConverter<arrow::Decimal128Type, DecimalConverter> {
public:
using BASE = TypedConverter<arrow::Decimal128Type, DecimalConverter>;
Status Init(ArrayBuilder* builder) override {
RETURN_NOT_OK(BASE::Init(builder));
decimal_type_ = checked_cast<const DecimalType*>(typed_builder_->type().get());
return Status::OK();
}
Status AppendItem(PyObject* obj) {
Decimal128 value;
RETURN_NOT_OK(internal::DecimalFromPyObject(obj, *decimal_type_, &value));
return typed_builder_->Append(value);
}
private:
const DecimalType* decimal_type_ = nullptr;
};
#define NUMERIC_CONVERTER(TYPE_ENUM, TYPE) \
case Type::TYPE_ENUM: \
if (from_pandas) { \
*out = std::unique_ptr<SeqConverter>( \
new NumericConverter<TYPE, NullCoding::PANDAS_SENTINELS>); \
} else { \
*out = std::unique_ptr<SeqConverter>( \
new NumericConverter<TYPE, NullCoding::NONE_ONLY>); \
} \
break;
#define SIMPLE_CONVERTER_CASE(TYPE_ENUM, TYPE_CLASS) \
case Type::TYPE_ENUM: \
*out = std::unique_ptr<SeqConverter>(new TYPE_CLASS); \
break;
// Dynamic constructor for sequence converters
Status GetConverter(const std::shared_ptr<DataType>& type, bool from_pandas,
bool strict_conversions, std::unique_ptr<SeqConverter>* out) {
switch (type->id()) {
SIMPLE_CONVERTER_CASE(NA, NullConverter);
SIMPLE_CONVERTER_CASE(BOOL, BoolConverter);
NUMERIC_CONVERTER(INT8, Int8Type);
NUMERIC_CONVERTER(INT16, Int16Type);
NUMERIC_CONVERTER(INT32, Int32Type);
NUMERIC_CONVERTER(INT64, Int64Type);
NUMERIC_CONVERTER(UINT8, UInt8Type);
NUMERIC_CONVERTER(UINT16, UInt16Type);
NUMERIC_CONVERTER(UINT32, UInt32Type);
NUMERIC_CONVERTER(UINT64, UInt64Type);
NUMERIC_CONVERTER(HALF_FLOAT, HalfFloatType);
NUMERIC_CONVERTER(FLOAT, FloatType);
NUMERIC_CONVERTER(DOUBLE, DoubleType);
SIMPLE_CONVERTER_CASE(DECIMAL, DecimalConverter);
SIMPLE_CONVERTER_CASE(BINARY, BytesConverter);
SIMPLE_CONVERTER_CASE(LARGE_BINARY, LargeBytesConverter);
SIMPLE_CONVERTER_CASE(FIXED_SIZE_BINARY, FixedWidthBytesConverter);
SIMPLE_CONVERTER_CASE(DATE32, Date32Converter);
SIMPLE_CONVERTER_CASE(DATE64, Date64Converter);
case Type::STRING:
if (strict_conversions) {
*out = std::unique_ptr<SeqConverter>(new StringConverter<StringType, true>());
} else {
*out = std::unique_ptr<SeqConverter>(new StringConverter<StringType, false>());
}
break;
case Type::LARGE_STRING:
if (strict_conversions) {
*out =
std::unique_ptr<SeqConverter>(new StringConverter<LargeStringType, true>());
} else {
*out =
std::unique_ptr<SeqConverter>(new StringConverter<LargeStringType, false>());
}
break;
case Type::TIME32: {
*out = std::unique_ptr<SeqConverter>(
new Time32Converter(checked_cast<const Time32Type&>(*type).unit()));
break;
}
case Type::TIME64: {
*out = std::unique_ptr<SeqConverter>(
new Time64Converter(checked_cast<const Time64Type&>(*type).unit()));
break;
}
case Type::TIMESTAMP: {
*out = std::unique_ptr<SeqConverter>(
new TimestampConverter(checked_cast<const TimestampType&>(*type).unit()));
break;
}
case Type::LIST:
*out = std::unique_ptr<SeqConverter>(
new ListConverter<ListType>(from_pandas, strict_conversions));
break;
case Type::LARGE_LIST:
*out = std::unique_ptr<SeqConverter>(
new ListConverter<LargeListType>(from_pandas, strict_conversions));
break;
case Type::STRUCT:
*out = std::unique_ptr<SeqConverter>(
new StructConverter(from_pandas, strict_conversions));
break;
default:
return Status::NotImplemented("Sequence converter for type ", type->ToString(),
" not implemented");
}
return Status::OK();
}
// ----------------------------------------------------------------------
// Convert *obj* to a sequence if necessary
// Fill *size* to its length. If >= 0 on entry, *size* is an upper size
// bound that may lead to truncation.
Status ConvertToSequenceAndInferSize(PyObject* obj, PyObject** seq, int64_t* size) {
if (PySequence_Check(obj)) {
// obj is already a sequence
int64_t real_size = static_cast<int64_t>(PySequence_Size(obj));
if (*size < 0) {
*size = real_size;
} else {
*size = std::min(real_size, *size);
}
Py_INCREF(obj);
*seq = obj;
} else if (*size < 0) {
// unknown size, exhaust iterator
*seq = PySequence_List(obj);
RETURN_IF_PYERROR();
*size = static_cast<int64_t>(PyList_GET_SIZE(*seq));
} else {
// size is known but iterator could be infinite
Py_ssize_t i, n = *size;
PyObject* iter = PyObject_GetIter(obj);
RETURN_IF_PYERROR();
OwnedRef iter_ref(iter);
PyObject* lst = PyList_New(n);
RETURN_IF_PYERROR();
for (i = 0; i < n; i++) {
PyObject* item = PyIter_Next(iter);
if (!item) break;
PyList_SET_ITEM(lst, i, item);
}
// Shrink list if len(iterator) < size
if (i < n && PyList_SetSlice(lst, i, n, NULL)) {
Py_DECREF(lst);
return Status::UnknownError("failed to resize list");
}
*seq = lst;
*size = std::min<int64_t>(i, *size);
}
return Status::OK();
}
Status ConvertPySequence(PyObject* sequence_source, PyObject* mask,
const PyConversionOptions& options,
std::shared_ptr<ChunkedArray>* out) {
PyAcquireGIL lock;
PyDateTime_IMPORT;
PyObject* seq;
OwnedRef tmp_seq_nanny;
std::shared_ptr<DataType> real_type;
int64_t size = options.size;
RETURN_NOT_OK(ConvertToSequenceAndInferSize(sequence_source, &seq, &size));
tmp_seq_nanny.reset(seq);
// In some cases, type inference may be "loose", like strings. If the user