lidavidm commented on code in PR #16: URL: https://github.com/apache/arrow-nanoarrow/pull/16#discussion_r942753583
########## src/nanoarrow/array_inline.h: ########## @@ -0,0 +1,246 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#ifndef NANOARROW_ARRAY_INLINE_H_INCLUDED +#define NANOARROW_ARRAY_INLINE_H_INCLUDED + +#include <errno.h> +#include <stdint.h> +#include <string.h> + +#include "bitmap_inline.h" +#include "buffer_inline.h" +#include "typedefs_inline.h" + +#ifdef __cplusplus +extern "C" { +#endif + +static inline struct ArrowBitmap* ArrowArrayValidityBitmap(struct ArrowArray* array) { + struct ArrowArrayPrivateData* private_data = + (struct ArrowArrayPrivateData*)array->private_data; + return &private_data->bitmap; +} + +static inline struct ArrowBuffer* ArrowArrayBuffer(struct ArrowArray* array, int64_t i) { + struct ArrowArrayPrivateData* private_data = + (struct ArrowArrayPrivateData*)array->private_data; + switch (i) { + case 0: + return &private_data->bitmap.buffer; + default: + return private_data->buffers + i - 1; + } +} + +static inline ArrowErrorCode ArrowArrayStartBuilding(struct ArrowArray* array) { + struct ArrowArrayPrivateData* private_data = + (struct ArrowArrayPrivateData*)array->private_data; + + if (private_data->storage_type == NANOARROW_TYPE_UNINITIALIZED) { + return EINVAL; + } + + // Initialize any offset buffer with a single zero + int result; + int64_t zero = 0; + + for (int i = 0; i < 3; i++) { + if (private_data->layout.buffer_type[i] == NANOARROW_BUFFER_TYPE_OFFSET && + private_data->layout.element_size_bits[i] == 64) { + result = ArrowBufferAppend(ArrowArrayBuffer(array, i), &zero, sizeof(int64_t)); + if (result != NANOARROW_OK) { + return result; + } + } else if (private_data->layout.buffer_type[i] == NANOARROW_BUFFER_TYPE_OFFSET && + private_data->layout.element_size_bits[i] == 64) { Review Comment: typo: 64->32 ########## src/nanoarrow/array_inline.h: ########## @@ -0,0 +1,246 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#ifndef NANOARROW_ARRAY_INLINE_H_INCLUDED +#define NANOARROW_ARRAY_INLINE_H_INCLUDED + +#include <errno.h> +#include <stdint.h> +#include <string.h> + +#include "bitmap_inline.h" +#include "buffer_inline.h" +#include "typedefs_inline.h" + +#ifdef __cplusplus +extern "C" { +#endif + +static inline struct ArrowBitmap* ArrowArrayValidityBitmap(struct ArrowArray* array) { + struct ArrowArrayPrivateData* private_data = + (struct ArrowArrayPrivateData*)array->private_data; + return &private_data->bitmap; +} + +static inline struct ArrowBuffer* ArrowArrayBuffer(struct ArrowArray* array, int64_t i) { + struct ArrowArrayPrivateData* private_data = + (struct ArrowArrayPrivateData*)array->private_data; + switch (i) { + case 0: + return &private_data->bitmap.buffer; + default: + return private_data->buffers + i - 1; + } +} + +static inline ArrowErrorCode ArrowArrayStartBuilding(struct ArrowArray* array) { + struct ArrowArrayPrivateData* private_data = + (struct ArrowArrayPrivateData*)array->private_data; + + if (private_data->storage_type == NANOARROW_TYPE_UNINITIALIZED) { + return EINVAL; + } + + // Initialize any offset buffer with a single zero + int result; + int64_t zero = 0; + + for (int i = 0; i < 3; i++) { + if (private_data->layout.buffer_type[i] == NANOARROW_BUFFER_TYPE_OFFSET && + private_data->layout.element_size_bits[i] == 64) { + result = ArrowBufferAppend(ArrowArrayBuffer(array, i), &zero, sizeof(int64_t)); + if (result != NANOARROW_OK) { + return result; + } + } else if (private_data->layout.buffer_type[i] == NANOARROW_BUFFER_TYPE_OFFSET && + private_data->layout.element_size_bits[i] == 64) { + result = ArrowBufferAppend(ArrowArrayBuffer(array, i), &zero, sizeof(int32_t)); + if (result != NANOARROW_OK) { + return result; + } + } + } + + return NANOARROW_OK; +} + +static inline ArrowErrorCode ArrowArrayFinishBuilding(struct ArrowArray* array, + char shrink_to_fit) { + struct ArrowArrayPrivateData* private_data = + (struct ArrowArrayPrivateData*)array->private_data; + + if (shrink_to_fit) { + // TODO: implement + return EINVAL; + } + + // 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) + for (int64_t i = 0; i < 3; i++) { + private_data->buffer_data[i] = ArrowArrayBuffer(array, i)->data; + } + + return NANOARROW_OK; +} + +static inline ArrowErrorCode ArrowArrayAppendNull(struct ArrowArray* array, int64_t n) { + struct ArrowArrayPrivateData* private_data = + (struct ArrowArrayPrivateData*)array->private_data; + + if (n == 0) { + return NANOARROW_OK; + } + + if (private_data->storage_type == NANOARROW_TYPE_NA) { + array->null_count += n; + array->length += n; + return NANOARROW_OK; + } + + int result; + + // Append n 0 bits to the validity bitmap. If we haven't allocated a bitmap yet, do it + // now + if (private_data->bitmap.buffer.data == NULL) { + result = ArrowBitmapReserve(&private_data->bitmap, array->length + n); + if (result != NANOARROW_OK) { + return result; + } + + ArrowBitmapAppendUnsafe(&private_data->bitmap, 1, array->length); + ArrowBitmapAppendUnsafe(&private_data->bitmap, 0, n); + } else { + result = ArrowBitmapReserve(&private_data->bitmap, n); + if (result != NANOARROW_OK) { + return result; + } + + ArrowBitmapAppendUnsafe(&private_data->bitmap, 0, n); + } + + // Add appropriate buffer fill + struct ArrowBuffer* buffer; + int64_t size_bytes; + + for (int i = 0; i < 3; i++) { + buffer = ArrowArrayBuffer(array, i); + size_bytes = private_data->layout.element_size_bits[i] / 8; + + switch (private_data->layout.buffer_type[i]) { + case NANOARROW_BUFFER_TYPE_NONE: + case NANOARROW_BUFFER_TYPE_VALIDITY: + continue; + case NANOARROW_BUFFER_TYPE_OFFSET: + // Append the current value at the end of the offset buffer + for (int64_t j = 0; j < n; n++) { + result = ArrowBufferAppend( Review Comment: nit: might be good if this could be a single reserve + memset instead of looping -- 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]
