westonpace commented on a change in pull request #11542: URL: https://github.com/apache/arrow/pull/11542#discussion_r741577703
########## File path: cpp/src/arrow/compute/kernels/vector_buffer.cc ########## @@ -0,0 +1,345 @@ +// 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. + +#include <memory> +#include <type_traits> +#include <utility> +#include <vector> + +#include "arrow/array.h" +#include "arrow/array/builder_primitive.h" +#include "arrow/array/concatenate.h" +#include "arrow/buffer.h" +#include "arrow/chunked_array.h" +#include "arrow/compute/function.h" +#include "arrow/compute/kernel.h" +#include "arrow/compute/registry.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "arrow/status.h" +#include "arrow/table.h" +#include "arrow/util/bit_util.h" +#include "arrow/util/logging.h" +#include "arrow/visitor_inline.h" + +namespace arrow { + +namespace compute { +namespace internal { + +namespace { + +struct GetByteRangesArray { + const std::shared_ptr<ArrayData>& input; + int64_t offset; + int64_t length; + UInt64Builder* range_starts; + UInt64Builder* range_offsets; + UInt64Builder* range_lengths; + + Status VisitBitmap(const std::shared_ptr<Buffer>& buffer) { + if (buffer) { + uint64_t data_start = reinterpret_cast<uint64_t>(buffer->data()); + RETURN_NOT_OK(range_starts->Append(data_start)); + RETURN_NOT_OK(range_offsets->Append(BitUtil::RoundDown(offset, 8) / 8)); + RETURN_NOT_OK(range_lengths->Append(BitUtil::CoveringBytes(offset, length))); + } + return Status::OK(); + } + + Status VisitFixedWidthArray(const Buffer& buffer, const FixedWidthType& type) { + uint64_t data_start = reinterpret_cast<uint64_t>(buffer.data()); + uint64_t offset_bits = offset * type.bit_width(); + uint64_t offset_bytes = BitUtil::RoundDown(offset_bits, 8) / 8; + uint64_t end_byte = + BitUtil::RoundUp(offset_bits + (length * type.bit_width()), 8) / 8; + uint64_t length_bytes = (end_byte - offset_bytes); + RETURN_NOT_OK(range_starts->Append(data_start)); + RETURN_NOT_OK(range_offsets->Append(offset_bytes)); + return range_lengths->Append(length_bytes); + } + + Status Visit(const FixedWidthType& type) { + static_assert(sizeof(uint8_t*) <= sizeof(uint64_t), + "Undefined behavior if pointer larger than uint64_t"); + RETURN_NOT_OK(VisitBitmap(input->buffers[0])); + RETURN_NOT_OK(VisitFixedWidthArray(*input->buffers[1], type)); + if (input->dictionary) { + // This is slightly imprecise because we always assume the entire dictionary is + // referenced. If this array has an offset it may only be referencing a portion of + // the dictionary + GetByteRangesArray dict_visitor{input->dictionary, + input->dictionary->offset, + input->dictionary->length, + range_starts, + range_offsets, + range_lengths}; + return VisitTypeInline(*input->dictionary->type, &dict_visitor); + } + return Status::OK(); + } + + Status Visit(const NullType& type) { return Status::OK(); } + + template <typename BaseBinaryType> + Status VisitBaseBinary(const BaseBinaryType& type) { + using offset_type = typename BaseBinaryType::offset_type; + RETURN_NOT_OK(VisitBitmap(input->buffers[0])); + + const Buffer& offsets_buffer = *input->buffers[1]; + RETURN_NOT_OK( + range_starts->Append(reinterpret_cast<uint64_t>(offsets_buffer.data()))); + RETURN_NOT_OK(range_offsets->Append(sizeof(offset_type) * offset)); + RETURN_NOT_OK(range_lengths->Append(sizeof(offset_type) * length)); + + const offset_type* offsets = input->GetValues<offset_type>(1, offset); + const Buffer& values = *input->buffers[2]; + offset_type start = offsets[0]; + offset_type end = offsets[length]; + RETURN_NOT_OK(range_starts->Append(reinterpret_cast<uint64_t>(values.data()))); + RETURN_NOT_OK(range_offsets->Append(static_cast<uint64_t>(start))); + return range_lengths->Append(static_cast<uint64_t>(end - start)); + } + + Status Visit(const BinaryType& type) { return VisitBaseBinary(type); } + + Status Visit(const LargeBinaryType& type) { return VisitBaseBinary(type); } + + template <typename BaseListType> + Status VisitBaseList(const BaseListType& type) { + using offset_type = typename BaseListType::offset_type; + RETURN_NOT_OK(VisitBitmap(input->buffers[0])); + + const Buffer& offsets_buffer = *input->buffers[1]; + RETURN_NOT_OK( + range_starts->Append(reinterpret_cast<uint64_t>(offsets_buffer.data()))); + RETURN_NOT_OK(range_offsets->Append(sizeof(offset_type) * offset)); + RETURN_NOT_OK(range_lengths->Append(sizeof(offset_type) * length)); + + const offset_type* offsets = input->GetValues<offset_type>(1, offset); + int64_t start = static_cast<int64_t>(offsets[0]); + int64_t end = static_cast<int64_t>(offsets[length]); + GetByteRangesArray child{input->child_data[0], start, end - start, + range_starts, range_offsets, range_lengths}; + return VisitTypeInline(*type.value_type(), &child); + } + + Status Visit(const ListType& type) { return VisitBaseList(type); } + + Status Visit(const LargeListType& type) { return VisitBaseList(type); } + + Status Visit(const FixedSizeListType& type) { + RETURN_NOT_OK(VisitBitmap(input->buffers[0])); + GetByteRangesArray child{input->child_data[0], + offset * type.list_size(), + length * type.list_size(), + range_starts, + range_offsets, + range_lengths}; + return VisitTypeInline(*type.value_type(), &child); + } + + Status Visit(const StructType& type) { + for (int i = 0; i < type.num_fields(); i++) { + GetByteRangesArray child{input->child_data[i], + offset + input->child_data[i]->offset, + length, + range_starts, + range_offsets, + range_lengths}; + RETURN_NOT_OK(VisitTypeInline(*type.field(i)->type(), &child)); + } + return Status::OK(); + } + + Status Visit(const DenseUnionType& type) { + // Skip validity map for DenseUnionType + // Types buffer is always int8 + RETURN_NOT_OK(VisitFixedWidthArray( + *input->buffers[1], *std::dynamic_pointer_cast<FixedWidthType>(int8()))); + // Offsets buffer is always int32 + RETURN_NOT_OK(VisitFixedWidthArray( + *input->buffers[2], *std::dynamic_pointer_cast<FixedWidthType>(int32()))); + + // We have to loop through the types buffer to figure out the correct + // offset / length being referenced in the child arrays + std::array<std::size_t, UnionType::kMaxTypeCode> type_code_index_lookup; Review comment: You are right. Thanks for the pointer. I hadn't realized that. -- 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]
