paleolimbot commented on issue #482: URL: https://github.com/apache/arrow-nanoarrow/issues/482#issuecomment-2127295647
I think what you might be looking for is `ArrowArrayFinishElement()` ( https://arrow.apache.org/nanoarrow/latest/reference/c.html#_CPPv423ArrowArrayFinishElementP10ArrowArray ). ```c struct ArrowSchema schema{}; struct ArrowArray array{}; struct ArrowError error{}; // Make the schema NANOARROW_RETURN_NOT_OK(ArrowSchemaInit(&schema)); NANOARROW_RETURN_NOT_OK(ArrowSchemaSetType(&schema, NANOARROW_TYPE_LIST)); NANOARROW_RETURN_NOT_OK(ArrowSchemaSetType(schema.children[0], NANOARROW_TYPE_INT32); // Build the array NANOARROW_RETURN_NOT_OK(ArrowArrayInitFromSchema(&array, &schema, &error)); NANOARROW_RETURN_NOT_OK(ArrowArrayStartAppending(&array)); // First element NANOARROW_RETURN_NOT_OK(ArrowArrayAppendInt(array.children[0], 1)); NANOARROW_RETURN_NOT_OK(ArrowArrayAppendInt(array.children[0], 2)); NANOARROW_RETURN_NOT_OK(ArrowArrayAppendInt(array.children[0], 3)); NANOARROW_RETURN_NOT_OK(ArrowArrayFinishElement(&array)); // Second element NANOARROW_RETURN_NOT_OK(ArrowArrayAppendInt(array.children[0], 4)); NANOARROW_RETURN_NOT_OK(ArrowArrayAppendInt(array.children[0], 5)); NANOARROW_RETURN_NOT_OK(ArrowArrayAppendInt(array.children[0], 6)); NANOARROW_RETURN_NOT_OK(ArrowArrayFinishElement(&array)); // Finish the outer array NANOARROW_RETURN_NOT_OK(ArrowArrayFinishBuildingDefault(int32_values, arrow_error)); ``` Using the `ArrowArrayAppendXX()` functions can be nice because they protect you from various types of errors (e.g., they will error if you try to append something outside the integer range of the type you're working with). Another option (that requires a little more knowledge of the specification) is to build by buffer: ```c struct ArrowSchema schema{}; struct ArrowArray array{}; struct ArrowError error{}; // Make the schema NANOARROW_RETURN_NOT_OK(ArrowSchemaInit(&schema)); NANOARROW_RETURN_NOT_OK(ArrowSchemaSetType(&schema, NANOARROW_TYPE_LIST)); NANOARROW_RETURN_NOT_OK(ArrowSchemaSetType(schema.children[0], NANOARROW_TYPE_INT32); struct ArrowBuffer values; for (int i = 1; i <= 6; i++) { NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(&values, i); } struct ArrowBuffer offsets; NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(&offsets, 0); NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(&offsets, 3); NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(&offsets, 6); // Build the array by buffer NANOARROW_RETURN_NOT_OK(ArrowArrayInitFromSchema(&array, &schema, &error)); NANOARROW_RETURN_NOT_OK(ArrowArraySetBuffer(&array, 1, &offsets)); NANOARROW_RETURN_NOT_OK(ArrowArraySetBuffer(array.children[0], 1, &values)); array.length = 2; array.children[0]->length = 6; array.null_count = 0; array.children[0]->null_count = 0; NANOARROW_RETURN_NOT_OK(ArrowArrayFinishBuildingDefault(int32_values, arrow_error)); ``` The nice part about the "build by buffer" method is that you can build the buffers independently (e.g., you can wrap an `std::vector` as a `ArrowBuffer`). If you're in C++ or something else with templates, this can often take care of the same types of things that the `ArrowArrayAppendXXX()` methods do. I hope all of that helps! -- 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]
