sanjibansg commented on code in PR #14758: URL: https://github.com/apache/arrow/pull/14758#discussion_r1144950111
########## cpp/src/arrow/util/align_util.cc: ########## @@ -0,0 +1,215 @@ +// 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 "arrow/util/align_util.h" + +#include "arrow/array.h" +#include "arrow/chunked_array.h" +#include "arrow/record_batch.h" +#include "arrow/table.h" + +namespace arrow { + +namespace util { + +bool CheckAlignment(const Buffer& buffer, const int64_t& alignment) { + return buffer.address() % alignment == 0; +} + +bool CheckAlignment(const ArrayData& array, const int64_t& alignment) { + for (const auto& buffer : array.buffers) { + if (buffer) { + if (!CheckAlignment(*buffer, alignment)) return false; + } + } + return true; +} + +bool CheckAlignment(const Array& array, const int64_t& alignment) { + bool align_dictionary = true, align_children = true; + + if (array.type()->id() == Type::DICTIONARY) { + align_dictionary = CheckAlignment(*array.data()->dictionary, alignment); + } + + for (const auto& child : array.data()->child_data) { + if (child) { + if (!CheckAlignment(*child, alignment)) align_children = false; + } + } + return CheckAlignment(*array.data(), alignment) && align_dictionary && align_children; +} + +bool CheckAlignment(const ChunkedArray& array, const int64_t& alignment, + std::vector<bool>& needs_alignment, const int& offset) { + needs_alignment.resize(needs_alignment.size() + array.num_chunks(), false); Review Comment: The resize is basically to add space for the extra elements that should be added for the check. For example, if we have a table made up of 2 chunked arrays, which then are made of 2 arrays each. Then, initially `needs_alignment` will be of size 2 (because of 2 chunked arrays). When the `CheckAlignment()` is called for the first chunked array, `needs_alignment` will be resized to 4 `(/*needs_alignment.size()/* 2 + /*array.num_chunks()*/ 2), thus now in needs_alignment, first two bits indicate status of the 2 arrays of the first chunk array, the third one for the ChunkArray as a whole, and the last one for the other chunk array which yet to be checked. The similar operation gets repeated for the other chunked array. Instead if we use offset, then for the first iteration here, offset will be 0. So, the resize will enforce its size to 2, which will not be enough for storing the alignment check bits of the first two arrays of the first chunk array, and the second chunk array. -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org