paleolimbot commented on code in PR #16: URL: https://github.com/apache/arrow-nanoarrow/pull/16#discussion_r942446356
########## src/nanoarrow/array_inline.h: ########## @@ -0,0 +1,146 @@ +// 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 ArrowArrayReserve(struct ArrowArray* array, + int64_t additional_size_elements) { + struct ArrowArrayPrivateData* private_data = + (struct ArrowArrayPrivateData*)array->private_data; + return EINVAL; +} + +static inline ArrowErrorCode ArrowArrayFinish(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; + } + + 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); + } + + // TODO Depending on the type, we need to append some elements to some buffers Review Comment: Yes, plus preserving the "append by value" semantics (`ArrowArrayAppendInt(array, 0)` will append a literal non-null zero; `ArrowArrayAppendNull(array, 1)` will append a null and nobody has to think about what the fill value is or whether it's required. -- 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]
