MCPcopy Create free account
hub / github.com/apache/arrow / VisitString

Method VisitString

python/pyarrow/src/arrow/python/numpy_to_arrow.cc:762–841  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

760
761template <typename T>
762Status NumPyConverter::VisitString(T* builder) {
763 if (dtype_->type_num == NPY_VSTRING) {
764 // Acquires a lock, so must stay ahead of the gil_lock below
765 return VisitStringDType(builder);
766 }
767
768 auto data = reinterpret_cast<const uint8_t*>(PyArray_DATA(arr_));
769
770 char numpy_byteorder = dtype_->byteorder;
771
772 // For Python C API, -1 is little-endian, 1 is big-endian
773#if ARROW_LITTLE_ENDIAN
774 // Yield little-endian from both '|' (native) and '<'
775 int byteorder = numpy_byteorder == '>' ? 1 : -1;
776#else
777 // Yield big-endian from both '|' (native) and '>'
778 int byteorder = numpy_byteorder == '<' ? -1 : 1;
779#endif
780
781 PyAcquireGIL gil_lock;
782
783 const bool is_binary_type = dtype_->type_num == NPY_STRING;
784 const bool is_unicode_type = dtype_->type_num == NPY_UNICODE;
785
786 if (!is_binary_type && !is_unicode_type) {
787 const bool is_float_type = dtype_->kind == 'f';
788 if (from_pandas_ && is_float_type) {
789 // in case of from_pandas=True, accept an all-NaN float array as input
790 RETURN_NOT_OK(NumPyNullsConverter::Convert(pool_, arr_, from_pandas_, &null_bitmap_,
791 &null_count_));
792 if (null_count_ == length_) {
793 auto arr = std::make_shared<NullArray>(length_);
794 compute::ExecContext context(pool_);
795 ARROW_ASSIGN_OR_RAISE(
796 std::shared_ptr<Array> out,
797 compute::Cast(*arr, arrow::utf8(), cast_options_, &context));
798 out_arrays_.emplace_back(out);
799 return Status::OK();
800 }
801 }
802 std::string dtype_string;
803 RETURN_NOT_OK(internal::PyObject_StdStringStr(reinterpret_cast<PyObject*>(dtype_),
804 &dtype_string));
805 return Status::TypeError("Expected a string or bytes dtype, got ", dtype_string);
806 }
807
808 auto AppendNonNullValue = [&](const uint8_t* data) {
809 if (is_binary_type) {
810 if (ARROW_PREDICT_TRUE(util::ValidateUTF8(data, itemsize_))) {
811 return builder->Append(data, static_cast<int32_t>(itemsize_));
812 } else {
813 return Status::Invalid("Encountered non-UTF8 binary value: ",
814 HexEncode(data, itemsize_));
815 }
816 } else {
817 // is_unicode_type case
818 return AppendUTF32(reinterpret_cast<const char*>(data), itemsize_, byteorder,
819 builder);

Callers

nothing calls this directly

Calls 13

CastFunction · 0.85
PyObject_StdStringStrFunction · 0.85
HexEncodeFunction · 0.85
AppendUTF32Function · 0.85
emplace_backMethod · 0.80
ConvertFunction · 0.50
ARROW_ASSIGN_OR_RAISEFunction · 0.50
OKFunction · 0.50
TypeErrorFunction · 0.50
ValidateUTF8Function · 0.50
InvalidFunction · 0.50
AppendMethod · 0.45

Tested by

no test coverage detected