This is an automated email from the ASF dual-hosted git repository.
paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git
The following commit(s) were added to refs/heads/main by this push:
new f218a6c [C] Make sure the data buffer is non-null (#45)
f218a6c is described below
commit f218a6c2b0fdb92fc5ba26d811ce9f6b3d8d9e3e
Author: Dewey Dunnington <[email protected]>
AuthorDate: Tue Sep 13 12:01:35 2022 -0300
[C] Make sure the data buffer is non-null (#45)
* make sure data buffer is non-null
* free array in test
---
src/nanoarrow/array.c | 29 +++++++++++++++++++++++++++++
src/nanoarrow/array_test.cc | 10 ++++++++++
2 files changed, 39 insertions(+)
diff --git a/src/nanoarrow/array.c b/src/nanoarrow/array.c
index a84d6cc..05b50ed 100644
--- a/src/nanoarrow/array.c
+++ b/src/nanoarrow/array.c
@@ -347,6 +347,32 @@ ArrowErrorCode ArrowArrayReserve(struct ArrowArray* array,
return NANOARROW_OK;
}
+static ArrowErrorCode ArrowArrayFinalizeBuffers(struct ArrowArray* array) {
+ struct ArrowArrayPrivateData* private_data =
+ (struct ArrowArrayPrivateData*)array->private_data;
+
+ // The only buffer finalizing this currently does is make sure the data
+ // buffer for (Large)String|Binary is never NULL
+ switch (private_data->storage_type) {
+ case NANOARROW_TYPE_BINARY:
+ case NANOARROW_TYPE_STRING:
+ case NANOARROW_TYPE_LARGE_BINARY:
+ case NANOARROW_TYPE_LARGE_STRING:
+ if (ArrowArrayBuffer(array, 2)->data == NULL) {
+ ArrowBufferAppendUInt8(ArrowArrayBuffer(array, 2), 0);
+ }
+ break;
+ default:
+ break;
+ }
+
+ for (int64_t i = 0; i < array->n_children; i++) {
+ NANOARROW_RETURN_NOT_OK(ArrowArrayFinalizeBuffers(array->children[i]));
+ }
+
+ return NANOARROW_OK;
+}
+
static void ArrowArrayFlushInternalPointers(struct ArrowArray* array) {
struct ArrowArrayPrivateData* private_data =
(struct ArrowArrayPrivateData*)array->private_data;
@@ -395,6 +421,9 @@ static ArrowErrorCode ArrowArrayCheckInternalBufferSizes(
ArrowErrorCode ArrowArrayFinishBuilding(struct ArrowArray* array,
struct ArrowError* error) {
+ // Even if the data buffer is size zero, the value needs to be non-null
+ NANOARROW_RETURN_NOT_OK(ArrowArrayFinalizeBuffers(array));
+
// Make sure the value we get with array->buffers[i] is set to the actual
// pointer (which may have changed from the original due to reallocation)
ArrowArrayFlushInternalPointers(array);
diff --git a/src/nanoarrow/array_test.cc b/src/nanoarrow/array_test.cc
index e66fb31..1383401 100644
--- a/src/nanoarrow/array_test.cc
+++ b/src/nanoarrow/array_test.cc
@@ -363,6 +363,16 @@ TEST(ArrayTest, ArrayTestAppendToStringArray) {
ArrayFromJSON(utf8(), "[\"1234\", null, null, \"56789\"]")));
}
+TEST(ArrayTest, ArrayTestAppendEmptyToString) {
+ struct ArrowArray array;
+ ASSERT_EQ(ArrowArrayInit(&array, NANOARROW_TYPE_STRING), NANOARROW_OK);
+ ASSERT_EQ(ArrowArrayStartAppending(&array), NANOARROW_OK);
+ ASSERT_EQ(ArrowArrayAppendString(&array, ArrowCharView("")), NANOARROW_OK);
+ EXPECT_EQ(ArrowArrayFinishBuilding(&array, nullptr), NANOARROW_OK);
+ EXPECT_NE(array.buffers[2], nullptr);
+ array.release(&array);
+}
+
TEST(ArrayTest, ArrayTestAppendToUInt64Array) {
struct ArrowArray array;