-
Notifications
You must be signed in to change notification settings - Fork 4k
GH-42018: Add numpy.StringDType support #48391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alippai
wants to merge
10
commits into
apache:main
Choose a base branch
from
alippai:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+272
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3b49f62
Fix StringDType helper declaration and initialize UTF8
alippai 6e4c3c6
Fix NumPy string dtype allocator guard
alippai a90ea23
Remove StringDType header comment
alippai 8729eb3
Format numpy_to_arrow include
alippai f49ba67
Run clang-format on numpy_to_arrow
alippai 050ca86
Handle missing NumPy dtypes module in StringDType tests
alippai da255c9
Make StringDType support unconditional
alippai 80a3aca
Remove StringDType endif comments
alippai bef2c71
Add StringDType mask coverage and sentinel test
alippai 166dd05
Merge branch 'apache:main' into main
alippai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| #include <limits> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
|
|
@@ -59,6 +60,8 @@ | |
| #include "arrow/python/type_traits.h" | ||
| #include "arrow/python/vendored/pythoncapi_compat.h" | ||
|
|
||
| #include <numpy/arrayobject.h> | ||
|
|
||
| namespace arrow { | ||
|
|
||
| using internal::checked_cast; | ||
|
|
@@ -74,6 +77,27 @@ using internal::NumPyTypeSize; | |
|
|
||
| namespace { | ||
|
|
||
| #if NPY_ABI_VERSION >= 0x02000000 | ||
| inline npy_string_allocator* ArrowNpyString_acquire_allocator( | ||
| const PyArray_StringDTypeObject* descr) { | ||
| using Func = npy_string_allocator* (*)(const PyArray_StringDTypeObject*); | ||
| return reinterpret_cast<Func>(PyArray_API[316])(descr); | ||
| } | ||
|
|
||
| inline void ArrowNpyString_release_allocator(npy_string_allocator* allocator) { | ||
| using Func = void (*)(npy_string_allocator*); | ||
| reinterpret_cast<Func>(PyArray_API[318])(allocator); | ||
| } | ||
|
|
||
| inline int ArrowNpyString_load(npy_string_allocator* allocator, | ||
| const npy_packed_static_string* packed, | ||
| npy_static_string* out) { | ||
| using Func = | ||
| int (*)(npy_string_allocator*, const npy_packed_static_string*, npy_static_string*); | ||
| return reinterpret_cast<Func>(PyArray_API[313])(allocator, packed, out); | ||
| } | ||
| #endif | ||
|
|
||
| Status AllocateNullBitmap(MemoryPool* pool, int64_t length, | ||
| std::shared_ptr<ResizableBuffer>* out) { | ||
| int64_t null_bytes = bit_util::BytesForBits(length); | ||
|
|
@@ -233,6 +257,13 @@ class NumPyConverter { | |
| Status Visit(const LargeStringType& type); | ||
| Status Visit(const StringViewType& type); | ||
|
|
||
| #if NPY_ABI_VERSION >= 0x02000000 | ||
| template <typename Builder> | ||
| Status AppendStringDTypeValues(Builder* builder); | ||
|
|
||
| Status ConvertStringDType(); | ||
| #endif | ||
|
|
||
| Status Visit(const StructType& type); | ||
|
|
||
| Status Visit(const FixedSizeBinaryType& type); | ||
|
|
@@ -338,6 +369,16 @@ Status NumPyConverter::Convert() { | |
| return Status::OK(); | ||
| } | ||
|
|
||
| if (IsStringDType(dtype_)) { | ||
| #if NPY_ABI_VERSION >= 0x02000000 | ||
| RETURN_NOT_OK(ConvertStringDType()); | ||
| return Status::OK(); | ||
| #else | ||
| return Status::NotImplemented( | ||
| "NumPy StringDType requires building PyArrow with NumPy >= 2.0"); | ||
| #endif | ||
| } | ||
|
|
||
| if (type_ == nullptr) { | ||
| return Status::Invalid("Must pass data type for non-object arrays"); | ||
| } | ||
|
|
@@ -815,6 +856,110 @@ Status NumPyConverter::Visit(const StringViewType& type) { | |
| return Status::OK(); | ||
| } | ||
|
|
||
| #if NPY_ABI_VERSION >= 0x02000000 | ||
| template <typename Builder> | ||
| Status NumPyConverter::AppendStringDTypeValues(Builder* builder) { | ||
| auto* descr = reinterpret_cast<PyArray_StringDTypeObject*>(dtype_); | ||
|
|
||
| npy_string_allocator* allocator = ArrowNpyString_acquire_allocator(descr); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI for other reviewers: this locks a mutex internally in NumPy. |
||
| if (allocator == nullptr) { | ||
| return Status::Invalid("Failed to acquire NumPy StringDType allocator"); | ||
| } | ||
|
|
||
| struct AllocatorGuard { | ||
| npy_string_allocator* ptr; | ||
| explicit AllocatorGuard(npy_string_allocator* p) : ptr(p) {} | ||
| ~AllocatorGuard() { | ||
| if (ptr != nullptr) { | ||
| ArrowNpyString_release_allocator(ptr); | ||
| } | ||
| } | ||
| } guard(allocator); | ||
|
|
||
| npy_static_string value = {0, nullptr}; | ||
| char* data = PyArray_BYTES(arr_); | ||
|
|
||
| if (mask_ != nullptr) { | ||
| Ndarray1DIndexer<uint8_t> mask_values(mask_); | ||
| for (int64_t i = 0; i < length_; ++i) { | ||
| if (mask_values[i]) { | ||
| RETURN_NOT_OK(builder->AppendNull()); | ||
| continue; | ||
| } | ||
|
|
||
| const auto* packed = | ||
| reinterpret_cast<const npy_packed_static_string*>(data + i * stride_); | ||
| const int is_null = ArrowNpyString_load(allocator, packed, &value); | ||
| if (is_null == -1) { | ||
| RETURN_IF_PYERROR(); | ||
| return Status::Invalid("Failed to unpack NumPy StringDType value"); | ||
| } | ||
| if (is_null) { | ||
| RETURN_NOT_OK(builder->AppendNull()); | ||
| } else { | ||
| RETURN_NOT_OK(builder->Append(std::string_view{value.buf, value.size})); | ||
| } | ||
| } | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| for (int64_t i = 0; i < length_; ++i) { | ||
| const auto* packed = reinterpret_cast<const npy_packed_static_string*>(data); | ||
| const int is_null = ArrowNpyString_load(allocator, packed, &value); | ||
| if (is_null == -1) { | ||
| RETURN_IF_PYERROR(); | ||
| return Status::Invalid("Failed to unpack NumPy StringDType value"); | ||
| } | ||
| if (is_null) { | ||
| RETURN_NOT_OK(builder->AppendNull()); | ||
| } else { | ||
| RETURN_NOT_OK(builder->Append(std::string_view{value.buf, value.size})); | ||
| } | ||
| data += stride_; | ||
| } | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status NumPyConverter::ConvertStringDType() { | ||
| util::InitializeUTF8(); | ||
|
|
||
| if (type_ == nullptr) { | ||
| type_ = utf8(); | ||
| } | ||
|
|
||
| switch (type_->id()) { | ||
| case Type::STRING: { | ||
| arrow::internal::ChunkedStringBuilder builder(kBinaryChunksize, pool_); | ||
| RETURN_NOT_OK(builder.Reserve(length_)); | ||
| RETURN_NOT_OK(AppendStringDTypeValues(&builder)); | ||
|
|
||
| ArrayVector chunks; | ||
| RETURN_NOT_OK(builder.Finish(&chunks)); | ||
| for (const auto& chunk : chunks) { | ||
| RETURN_NOT_OK(PushArray(chunk->data())); | ||
| } | ||
| return Status::OK(); | ||
| } | ||
| case Type::LARGE_STRING: { | ||
| LargeStringBuilder builder(pool_); | ||
| RETURN_NOT_OK(builder.Reserve(length_)); | ||
| RETURN_NOT_OK(AppendStringDTypeValues(&builder)); | ||
| return PushBuilderResult(&builder); | ||
| } | ||
| case Type::STRING_VIEW: { | ||
| StringViewBuilder builder(pool_); | ||
| RETURN_NOT_OK(builder.Reserve(length_)); | ||
| RETURN_NOT_OK(AppendStringDTypeValues(&builder)); | ||
| return PushBuilderResult(&builder); | ||
| } | ||
| default: | ||
| return Status::TypeError( | ||
| "NumPy StringDType can only be converted to Arrow string types"); | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| Status NumPyConverter::Visit(const StructType& type) { | ||
| std::vector<NumPyConverter> sub_converters; | ||
| std::vector<OwnedRefNoGIL> sub_arrays; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.