lidavidm commented on code in PR #143:
URL: https://github.com/apache/arrow-nanoarrow/pull/143#discussion_r1129405480
##########
extensions/nanoarrow_ipc/src/nanoarrow/nanoarrow_ipc.h:
##########
@@ -80,8 +129,33 @@ ArrowErrorCode ArrowIpcReaderDecode(struct ArrowIpcReader*
reader,
struct ArrowBufferView data,
struct ArrowError* error);
-#endif
+ArrowErrorCode ArrowIpcReaderGetSchema(struct ArrowIpcReader* reader,
+ struct ArrowSchema* out, struct
ArrowError* error);
+
+ArrowErrorCode ArrowIpcReaderGetArray(struct ArrowIpcReader* reader,
+ struct ArrowBufferView body, int64_t i,
+ struct ArrowArray* out, struct
ArrowError* error);
+
+ArrowErrorCode ArrowIpcReaderSetSchema(struct ArrowIpcReader* reader,
+ struct ArrowSchema* schema,
+ struct ArrowError* error);
+
+struct ArrowIpcField {
+ struct ArrowArrayView* array_view;
+ int64_t buffer_offset;
+};
+
+struct ArrowIpcReaderPrivate {
Review Comment:
nit: maybe indicate that it's only here for testing?
(alternatively, you could keep it in the .c file and copy-paste it to the
.cc file in an 'extern C' block)
##########
extensions/nanoarrow_ipc/src/nanoarrow/nanoarrow_ipc.c:
##########
@@ -785,5 +876,261 @@ ArrowErrorCode ArrowIpcReaderDecode(struct
ArrowIpcReader* reader,
return EINVAL;
}
+ private_data->last_message = message_header;
+ return NANOARROW_OK;
+}
+
+ArrowErrorCode ArrowIpcReaderGetSchema(struct ArrowIpcReader* reader,
+ struct ArrowSchema* out,
+ struct ArrowError* error) {
+ struct ArrowIpcReaderPrivate* private_data =
+ (struct ArrowIpcReaderPrivate*)reader->private_data;
+
+ if (private_data->schema.release == NULL) {
+ ArrowErrorSet(error, "reader does not contain a valid schema");
+ return EINVAL;
+ }
+
+ ArrowSchemaMove(&private_data->schema, out);
Review Comment:
Do we really want Move here, or a copy?
##########
extensions/nanoarrow_ipc/src/nanoarrow/nanoarrow_ipc_test.cc:
##########
@@ -205,6 +386,74 @@ TEST_P(ArrowTypeParameterizedTestFixture,
NanoarrowIpcArrowTypeRoundtrip) {
ArrowIpcReaderReset(&reader);
}
+TEST_P(ArrowTypeParameterizedTestFixture, NanoarrowIpcArrowArrayRoundtrip) {
+ const std::shared_ptr<arrow::DataType>& data_type = GetParam();
+ std::shared_ptr<arrow::Schema> dummy_schema =
+ arrow::schema({arrow::field("dummy_name", data_type)});
+
+ auto maybe_empty = arrow::RecordBatch::MakeEmpty(dummy_schema);
+ ASSERT_TRUE(maybe_empty.ok());
+ auto empty = maybe_empty.ValueUnsafe();
+
+ auto maybe_nulls_array = arrow::MakeArrayOfNull(data_type, 3);
+ ASSERT_TRUE(maybe_nulls_array.ok());
+ auto nulls =
+ arrow::RecordBatch::Make(dummy_schema, 3,
{maybe_nulls_array.ValueUnsafe()});
+
+ auto options = arrow::ipc::IpcWriteOptions::Defaults();
+
+ struct ArrowSchema schema;
+ struct ArrowIpcReader reader;
+ struct ArrowBufferView buffer_view;
+ struct ArrowArray array;
+
+ // Initialize the reader
+ ASSERT_TRUE(arrow::ExportSchema(*dummy_schema, &schema).ok());
+ ArrowIpcReaderInit(&reader);
+ ASSERT_EQ(ArrowIpcReaderSetSchema(&reader, &schema, nullptr), NANOARROW_OK);
+
+ // Check the empty array
+ auto maybe_serialized = arrow::ipc::SerializeRecordBatch(*empty, options);
+ ASSERT_TRUE(maybe_serialized.ok());
+ buffer_view.data.data = maybe_serialized.ValueUnsafe()->data();
+ buffer_view.size_bytes = maybe_serialized.ValueOrDie()->size();
+
+ ASSERT_EQ(ArrowIpcReaderDecode(&reader, buffer_view, nullptr), NANOARROW_OK);
+ buffer_view.data.as_uint8 += reader.header_size_bytes;
+ buffer_view.size_bytes -= reader.header_size_bytes;
+ ASSERT_EQ(ArrowIpcReaderGetArray(&reader, buffer_view, -1, &array, nullptr),
+ NANOARROW_OK);
+
+ auto maybe_batch = arrow::ImportRecordBatch(&array, dummy_schema);
+ ASSERT_TRUE(maybe_batch.ok());
+ EXPECT_EQ(maybe_batch.ValueUnsafe()->ToString(), empty->ToString());
+ EXPECT_TRUE(maybe_batch.ValueUnsafe()->Equals(*empty));
+
+ // Check the array with 3 null values
+ maybe_serialized = arrow::ipc::SerializeRecordBatch(*nulls, options);
+ ASSERT_TRUE(maybe_serialized.ok());
+ buffer_view.data.data = maybe_serialized.ValueUnsafe()->data();
+ buffer_view.size_bytes = maybe_serialized.ValueOrDie()->size();
+
+ ASSERT_EQ(ArrowIpcReaderDecode(&reader, buffer_view, nullptr), NANOARROW_OK);
+ buffer_view.data.as_uint8 += reader.header_size_bytes;
+ buffer_view.size_bytes -= reader.header_size_bytes;
+ ASSERT_EQ(ArrowIpcReaderGetArray(&reader, buffer_view, -1, &array, nullptr),
+ NANOARROW_OK);
+
+ maybe_batch = arrow::ImportRecordBatch(&array, dummy_schema);
+ ASSERT_TRUE(maybe_batch.ok());
+ EXPECT_EQ(maybe_batch.ValueUnsafe()->ToString(), nulls->ToString());
+ EXPECT_TRUE(maybe_batch.ValueUnsafe()->Equals(*nulls));
+
+ if (!maybe_batch.ValueUnsafe()->Equals(*nulls)) {
+ std::cout << "something";
Review Comment:
ping
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]