You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "paleolimbot (via GitHub)" <gi...@apache.org> on 2024/01/04 18:03:19 UTC
[PR] GH-38717: [C++] Add ImportChunkedArray and ExportChunkedArray to/from ArrowArrayStream [arrow]
paleolimbot opened a new pull request, #39455:
URL: https://github.com/apache/arrow/pull/39455
### Rationale for this change
The `ChunkedArray` has no equivalent in the C data interface; however, it is the primary array structure that higher level bindings interact with (because it is a column in a `Table`). In the Python capsule interface, this means that ChunkedArrays always require a workaround involving loops in Python.
### What changes are included in this PR?
- Added `ImportChunkedArray()` and `ExportChunkedArray()`
- Generalized the classes that support import/export to relax the assumption that every `ArrowArray` in an `ArrowArrayStream` is a `RecordBatch`.
### Are these changes tested?
TODO
### Are there any user-facing changes?
Yes, two new functions are added to bridge.h.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-38717: [C++] Add ImportChunkedArray and ExportChunkedArray to/from ArrowArrayStream [arrow]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #39455:
URL: https://github.com/apache/arrow/pull/39455#discussion_r1446405952
##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -1979,13 +1979,61 @@ Result<std::shared_ptr<RecordBatch>> ImportDeviceRecordBatch(
namespace {
+template <typename T>
Review Comment:
You don't need this, those are just different signatures.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-38717: [C++] Add ImportChunkedArray and ExportChunkedArray to/from ArrowArrayStream [arrow]
Posted by "conbench-apache-arrow[bot] (via GitHub)" <gi...@apache.org>.
conbench-apache-arrow[bot] commented on PR #39455:
URL: https://github.com/apache/arrow/pull/39455#issuecomment-1932764691
After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit 478755f0ef79a921aaa14822c8829c122bbbe92e.
There were no benchmark performance regressions. 🎉
The [full Conbench report](https://github.com/apache/arrow/runs/21335030622) has more details. It also includes information about 1 possible false positive for unstable benchmarks that are known to sometimes produce them.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-38717: [C++] Add ImportChunkedArray and ExportChunkedArray to/from ArrowArrayStream [arrow]
Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #39455:
URL: https://github.com/apache/arrow/pull/39455#discussion_r1469595392
##########
cpp/src/arrow/c/bridge_test.cc:
##########
@@ -4506,6 +4554,29 @@ class TestArrayStreamRoundtrip : public BaseArrayStreamTest {
ASSERT_TRUE(weak_reader.expired());
}
+ void Roundtrip(std::shared_ptr<ChunkedArray> src,
+ std::function<void(const std::shared_ptr<ChunkedArray>&)> check_func) {
+ ArrowArrayStream c_stream;
+
+ // One original copy which to compare the result, one copy held by the stream
+ std::weak_ptr<ChunkedArray> weak_src(src);
+ ASSERT_EQ(weak_src.use_count(), 2);
+
+ ASSERT_OK(ExportChunkedArray(std::move(src), &c_stream));
+ ASSERT_FALSE(ArrowArrayStreamIsReleased(&c_stream));
+
+ {
+ ASSERT_OK_AND_ASSIGN(auto dst, ImportChunkedArray(&c_stream));
+ // Stream was moved, consumed, and released
+ ASSERT_TRUE(ArrowArrayStreamIsReleased(&c_stream));
+
+ // Stream was released by ImportChunkedArray but original copy remains
+ ASSERT_EQ(weak_src.use_count(), 1);
Review Comment:
I copied it from the previous test...I'm happy to remove it!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-38717: [C++] Add ImportChunkedArray and ExportChunkedArray to/from ArrowArrayStream [arrow]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #39455:
URL: https://github.com/apache/arrow/pull/39455#discussion_r1469772299
##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -1979,13 +1979,49 @@ Result<std::shared_ptr<RecordBatch>> ImportDeviceRecordBatch(
namespace {
+Status ExportStreamSchema(const std::shared_ptr<RecordBatchReader>& src,
+ struct ArrowSchema* out_schema) {
+ return ExportSchema(*src->schema(), out_schema);
+}
+
+Status ExportStreamSchema(const std::shared_ptr<ChunkedArray>& src,
+ struct ArrowSchema* out_schema) {
+ return ExportType(*src->type(), out_schema);
+}
+
+Status ExportStreamNext(const std::shared_ptr<RecordBatchReader>& src, int i,
Review Comment:
There is no guarantee that the number of batches fits in 31 bits, is there? (though of course the converse is highly unlikely...)
Let's just make these `int64_t` for peace of mind :-)
##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -1979,13 +1979,49 @@ Result<std::shared_ptr<RecordBatch>> ImportDeviceRecordBatch(
namespace {
+Status ExportStreamSchema(const std::shared_ptr<RecordBatchReader>& src,
+ struct ArrowSchema* out_schema) {
+ return ExportSchema(*src->schema(), out_schema);
+}
+
+Status ExportStreamSchema(const std::shared_ptr<ChunkedArray>& src,
+ struct ArrowSchema* out_schema) {
+ return ExportType(*src->type(), out_schema);
+}
+
+Status ExportStreamNext(const std::shared_ptr<RecordBatchReader>& src, int i,
+ struct ArrowArray* out_array) {
+ std::shared_ptr<RecordBatch> batch;
+ RETURN_NOT_OK(src->ReadNext(&batch));
+ if (batch == nullptr) {
+ // End of stream
+ ArrowArrayMarkReleased(out_array);
+ return Status::OK();
+ } else {
+ return ExportRecordBatch(*batch, out_array);
+ }
+}
+
+Status ExportStreamNext(const std::shared_ptr<ChunkedArray>& src, int i,
+ struct ArrowArray* out_array) {
+ if (i >= src->num_chunks()) {
+ // End of stream
+ ArrowArrayMarkReleased(out_array);
+ return Status::OK();
+ } else {
+ return ExportArray(*src->chunk(i), out_array);
+ }
+}
+
+template <typename T>
class ExportedArrayStream {
public:
struct PrivateData {
- explicit PrivateData(std::shared_ptr<RecordBatchReader> reader)
- : reader_(std::move(reader)) {}
+ explicit PrivateData(std::shared_ptr<T> reader)
+ : reader_(std::move(reader)), batch_num_(0) {}
- std::shared_ptr<RecordBatchReader> reader_;
+ std::shared_ptr<T> reader_;
+ int batch_num_;
Review Comment:
`int64_t` as well?
##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -2180,15 +2211,119 @@ class ArrayStreamBatchReader : public RecordBatchReader {
return {code, last_error ? std::string(last_error) : ""};
}
+ private:
mutable struct ArrowArrayStream stream_;
+};
+
+class ArrayStreamBatchReader : public RecordBatchReader, public ArrayStreamReader {
+ public:
+ explicit ArrayStreamBatchReader(struct ArrowArrayStream* stream)
+ : ArrayStreamReader(stream) {}
+
+ Status Init() {
+ ARROW_ASSIGN_OR_RAISE(schema_, ReadSchema());
+ return Status::OK();
+ }
+
+ std::shared_ptr<Schema> schema() const override { return schema_; }
+
+ Status ReadNext(std::shared_ptr<RecordBatch>* batch) override {
+ ARROW_RETURN_NOT_OK(CheckNotReleased());
+
+ struct ArrowArray c_array;
+ ARROW_RETURN_NOT_OK(ReadNextArrayInternal(&c_array));
+
+ if (ArrowArrayIsReleased(&c_array)) {
+ // End of stream
+ batch->reset();
+ return Status::OK();
+ } else {
+ return ImportRecordBatch(&c_array, schema_).Value(batch);
+ }
+ }
+
+ Status Close() override {
+ ReleaseStream();
+ return Status::OK();
+ }
+
+ private:
std::shared_ptr<Schema> schema_;
};
+class ArrayStreamArrayReader : public ArrayStreamReader {
+ public:
+ explicit ArrayStreamArrayReader(struct ArrowArrayStream* stream)
+ : ArrayStreamReader(stream) {}
+
+ Status Init() {
+ ARROW_ASSIGN_OR_RAISE(field_, ReadField());
+ return Status::OK();
+ }
+
+ std::shared_ptr<DataType> data_type() const { return field_->type(); }
+
+ Status ReadNext(std::shared_ptr<Array>* array) {
+ ARROW_RETURN_NOT_OK(CheckNotReleased());
+
+ struct ArrowArray c_array;
+ ARROW_RETURN_NOT_OK(ReadNextArrayInternal(&c_array));
+
+ if (ArrowArrayIsReleased(&c_array)) {
+ // End of stream
+ array->reset();
+ return Status::OK();
+ } else {
+ return ImportArray(&c_array, field_->type()).Value(array);
+ }
+ }
+
+ Status Close() {
+ ReleaseStream();
+ return Status::OK();
+ }
+
+ private:
+ std::shared_ptr<Field> field_;
+};
+
} // namespace
Result<std::shared_ptr<RecordBatchReader>> ImportRecordBatchReader(
struct ArrowArrayStream* stream) {
- return ArrayStreamBatchReader::Make(stream);
+ if (ArrowArrayStreamIsReleased(stream)) {
+ return Status::Invalid("Cannot import released ArrowArrayStream");
+ }
+
+ auto reader = std::make_shared<ArrayStreamBatchReader>(stream);
+ ARROW_RETURN_NOT_OK(reader->Init());
+ return reader;
+}
+
+Result<std::shared_ptr<ChunkedArray>> ImportChunkedArray(
+ struct ArrowArrayStream* stream) {
+ if (ArrowArrayStreamIsReleased(stream)) {
+ return Status::Invalid("Cannot import released ArrowArrayStream");
+ }
+
+ auto reader = std::make_shared<ArrayStreamArrayReader>(stream);
+ ARROW_RETURN_NOT_OK(reader->Init());
+
+ std::shared_ptr<DataType> data_type = reader->data_type();
+
+ ArrayVector chunks;
+ std::shared_ptr<Array> chunk;
+ while (true) {
+ ARROW_RETURN_NOT_OK(reader->ReadNext(&chunk));
+ if (!chunk) {
+ break;
+ }
+
+ chunks.push_back(std::move(chunk));
+ }
+
+ ARROW_RETURN_NOT_OK(reader->Close());
+ return ChunkedArray::Make(chunks, data_type);
Review Comment:
Nit: avoid copying these variables
```suggestion
return ChunkedArray::Make(std::move(chunks), std::move(data_type));
```
##########
cpp/src/arrow/c/bridge_test.cc:
##########
@@ -4467,6 +4478,43 @@ TEST_F(TestArrayStreamExport, Errors) {
ASSERT_EQ(EINVAL, c_stream.get_next(&c_stream, &c_array));
}
+TEST_F(TestArrayStreamExport, ChunkedArrayExport) {
+ ASSERT_OK_AND_ASSIGN(auto chunked_array,
+ ChunkedArray::Make({ArrayFromJSON(int32(), "[1, 2]"),
+ ArrayFromJSON(int32(), "[4, 5, null]")}));
Review Comment:
Can you also test a chunked array with zero chunks at some point? (here and for roundtripping)
##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -2180,15 +2211,119 @@ class ArrayStreamBatchReader : public RecordBatchReader {
return {code, last_error ? std::string(last_error) : ""};
}
+ private:
mutable struct ArrowArrayStream stream_;
+};
+
+class ArrayStreamBatchReader : public RecordBatchReader, public ArrayStreamReader {
+ public:
+ explicit ArrayStreamBatchReader(struct ArrowArrayStream* stream)
+ : ArrayStreamReader(stream) {}
+
+ Status Init() {
+ ARROW_ASSIGN_OR_RAISE(schema_, ReadSchema());
+ return Status::OK();
+ }
+
+ std::shared_ptr<Schema> schema() const override { return schema_; }
+
+ Status ReadNext(std::shared_ptr<RecordBatch>* batch) override {
+ ARROW_RETURN_NOT_OK(CheckNotReleased());
+
+ struct ArrowArray c_array;
+ ARROW_RETURN_NOT_OK(ReadNextArrayInternal(&c_array));
+
+ if (ArrowArrayIsReleased(&c_array)) {
+ // End of stream
+ batch->reset();
+ return Status::OK();
+ } else {
+ return ImportRecordBatch(&c_array, schema_).Value(batch);
+ }
+ }
+
+ Status Close() override {
+ ReleaseStream();
+ return Status::OK();
+ }
+
+ private:
std::shared_ptr<Schema> schema_;
};
+class ArrayStreamArrayReader : public ArrayStreamReader {
+ public:
+ explicit ArrayStreamArrayReader(struct ArrowArrayStream* stream)
+ : ArrayStreamReader(stream) {}
+
+ Status Init() {
+ ARROW_ASSIGN_OR_RAISE(field_, ReadField());
+ return Status::OK();
+ }
+
+ std::shared_ptr<DataType> data_type() const { return field_->type(); }
+
+ Status ReadNext(std::shared_ptr<Array>* array) {
+ ARROW_RETURN_NOT_OK(CheckNotReleased());
+
+ struct ArrowArray c_array;
+ ARROW_RETURN_NOT_OK(ReadNextArrayInternal(&c_array));
+
+ if (ArrowArrayIsReleased(&c_array)) {
+ // End of stream
+ array->reset();
+ return Status::OK();
+ } else {
+ return ImportArray(&c_array, field_->type()).Value(array);
+ }
+ }
+
+ Status Close() {
Review Comment:
Not sure this is useful since the `ArrayStreamReader` destructor already calls `ReleaseStream`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-38717: [C++] Add ImportChunkedArray and ExportChunkedArray to/from ArrowArrayStream [arrow]
Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #39455:
URL: https://github.com/apache/arrow/pull/39455#discussion_r1450935696
##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -2152,6 +2211,27 @@ class ArrayStreamBatchReader : public RecordBatchReader {
}
private:
+ template <typename BatchT>
Review Comment:
That is definitely a better idea! I reverted my initial attempt (which led to segfaults in Python) and will work through a second attempt more carefully 🙂 .
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-38717: [C++] Add ImportChunkedArray and ExportChunkedArray to/from ArrowArrayStream [arrow]
Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on PR #39455:
URL: https://github.com/apache/arrow/pull/39455#issuecomment-1914704447
@pitrou Sorry this took me a while to circle back to, but I took a stab at refactoring the "array" and "batch" readers in the way you described. No rush, but it's ready for another look whenever you are!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-38717: [C++] Add ImportChunkedArray and ExportChunkedArray to/from ArrowArrayStream [arrow]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #39455:
URL: https://github.com/apache/arrow/pull/39455#discussion_r1446406409
##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -1979,13 +1979,61 @@ Result<std::shared_ptr<RecordBatch>> ImportDeviceRecordBatch(
namespace {
+template <typename T>
+static inline Status ExportStreamSchema(const std::shared_ptr<T>& src,
+ struct ArrowSchema* out_schema);
+
+template <>
+inline Status ExportStreamSchema(const std::shared_ptr<RecordBatchReader>& src,
+ struct ArrowSchema* out_schema) {
+ return ExportSchema(*src->schema(), out_schema);
+}
+
+template <>
+inline Status ExportStreamSchema(const std::shared_ptr<ChunkedArray>& src,
+ struct ArrowSchema* out_schema) {
+ return ExportType(*src->type(), out_schema);
+}
Review Comment:
```suggestion
Status ExportStreamSchema(const std::shared_ptr<RecordBatchReader>& src,
struct ArrowSchema* out_schema) {
return ExportSchema(*src->schema(), out_schema);
}
Status ExportStreamSchema(const std::shared_ptr<ChunkedArray>& src,
struct ArrowSchema* out_schema) {
return ExportType(*src->type(), out_schema);
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-38717: [C++] Add ImportChunkedArray and ExportChunkedArray to/from ArrowArrayStream [arrow]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #39455:
URL: https://github.com/apache/arrow/pull/39455#discussion_r1446406542
##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -1979,13 +1979,61 @@ Result<std::shared_ptr<RecordBatch>> ImportDeviceRecordBatch(
namespace {
+template <typename T>
+static inline Status ExportStreamSchema(const std::shared_ptr<T>& src,
+ struct ArrowSchema* out_schema);
+
+template <>
+inline Status ExportStreamSchema(const std::shared_ptr<RecordBatchReader>& src,
+ struct ArrowSchema* out_schema) {
+ return ExportSchema(*src->schema(), out_schema);
+}
+
+template <>
+inline Status ExportStreamSchema(const std::shared_ptr<ChunkedArray>& src,
+ struct ArrowSchema* out_schema) {
+ return ExportType(*src->type(), out_schema);
+}
+
+template <typename T>
Review Comment:
Same here.
##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -2152,6 +2211,27 @@ class ArrayStreamBatchReader : public RecordBatchReader {
}
private:
+ template <typename BatchT>
Review Comment:
Stuffing everything in one class makes for complicated code. Why not have a base class with common helpers and then two distinct derived classes?
##########
cpp/src/arrow/c/bridge_test.cc:
##########
@@ -4506,6 +4554,29 @@ class TestArrayStreamRoundtrip : public BaseArrayStreamTest {
ASSERT_TRUE(weak_reader.expired());
}
+ void Roundtrip(std::shared_ptr<ChunkedArray> src,
+ std::function<void(const std::shared_ptr<ChunkedArray>&)> check_func) {
+ ArrowArrayStream c_stream;
+
+ // One original copy which to compare the result, one copy held by the stream
+ std::weak_ptr<ChunkedArray> weak_src(src);
+ ASSERT_EQ(weak_src.use_count(), 2);
+
+ ASSERT_OK(ExportChunkedArray(std::move(src), &c_stream));
+ ASSERT_FALSE(ArrowArrayStreamIsReleased(&c_stream));
+
+ {
+ ASSERT_OK_AND_ASSIGN(auto dst, ImportChunkedArray(&c_stream));
+ // Stream was moved, consumed, and released
+ ASSERT_TRUE(ArrowArrayStreamIsReleased(&c_stream));
+
+ // Stream was released by ImportChunkedArray but original copy remains
+ ASSERT_EQ(weak_src.use_count(), 1);
Review Comment:
I'm not sure I understand what the goal of this test is. In any case, `MemoryPool::bytes_allocated` would probably be more reliable (the data could be held alive in another way than through ChunkedArray).
##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -2047,6 +2087,15 @@ class ExportedArrayStream {
return ExportedArrayStream{stream}.GetLastError();
}
+ static Status Make(const std::shared_ptr<T> reader, struct ArrowArrayStream* out) {
Review Comment:
```suggestion
static Status Make(std::shared_ptr<T> reader, struct ArrowArrayStream* out) {
```
##########
cpp/src/arrow/c/bridge_test.cc:
##########
@@ -4506,6 +4554,29 @@ class TestArrayStreamRoundtrip : public BaseArrayStreamTest {
ASSERT_TRUE(weak_reader.expired());
}
+ void Roundtrip(std::shared_ptr<ChunkedArray> src,
+ std::function<void(const std::shared_ptr<ChunkedArray>&)> check_func) {
+ ArrowArrayStream c_stream;
+
+ // One original copy which to compare the result, one copy held by the stream
+ std::weak_ptr<ChunkedArray> weak_src(src);
+ ASSERT_EQ(weak_src.use_count(), 2);
Review Comment:
Instead of hardcoding the expected ref count, why not just remember it and then compare it later?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-38717: [C++] Add ImportChunkedArray and ExportChunkedArray to/from ArrowArrayStream [arrow]
Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on PR #39455:
URL: https://github.com/apache/arrow/pull/39455#issuecomment-1923844650
@pitrou I think I've implemented the changes you suggested!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-38717: [C++] Add ImportChunkedArray and ExportChunkedArray to/from ArrowArrayStream [arrow]
Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot merged PR #39455:
URL: https://github.com/apache/arrow/pull/39455
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org