R-JunmingChen commented on code in PR #37418: URL: https://github.com/apache/arrow/pull/37418#discussion_r1333762592
########## cpp/src/arrow/compute/kernels/vector_dictionary.cc: ########## @@ -0,0 +1,210 @@ +// 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/array/array_base.h" +#include "arrow/array/array_dict.h" +#include "arrow/array/builder_primitive.h" +#include "arrow/array/dict_internal.h" +#include "arrow/compute/api_vector.h" +#include "arrow/compute/kernels/common_internal.h" +#include "arrow/result.h" + +namespace arrow { + +namespace compute { +namespace internal { + +namespace { + +// Dictionary compaction implementation + +const FunctionDoc dictionary_compact_doc{ + "Compact dictionary array", + ("Return a compacted version of the dictionary array input,\n" + "which removes unused values in dictionary.\n" + "The function assumes every indice is effective."), + {"dictionary_array"}}; + +class DictionaryCompactKernel : public KernelState { + public: + virtual Result<std::shared_ptr<Array>> Exec(std::shared_ptr<Array> dict_array, + ExecContext* ctx) const = 0; +}; + +template <typename IndiceArrowType> +class DictionaryCompactKernelImpl : public DictionaryCompactKernel { + using BuilderType = NumericBuilder<IndiceArrowType>; + using CType = typename IndiceArrowType::c_type; + + public: + Result<std::shared_ptr<Array>> Exec(std::shared_ptr<Array> dict_array, + ExecContext* ctx) const override { + const DictionaryArray& casted_dict_array = + checked_cast<const DictionaryArray&>(*dict_array); + const std::shared_ptr<Array>& dict = casted_dict_array.dictionary(); + if (dict->length() == 0) { + return dict_array; + } + const std::shared_ptr<Array>& indice = casted_dict_array.indices(); + if (indice->length() == 0) { + ARROW_ASSIGN_OR_RAISE(auto empty_dict, + MakeEmptyArray(dict->type(), ctx->memory_pool())); + return DictionaryArray::FromArrays(dict_array->type(), indice, empty_dict); + } + const CType* indices_data = + reinterpret_cast<const CType*>(indice->data()->buffers[1]->data()); + int64_t offset = indice->data()->offset; + + // check whether the input is compacted + std::vector<bool> dict_used(dict->length(), false); + int64_t dict_used_count = 0; + for (int64_t i = 0; i < indice->length(); i++) { + if (indice->IsNull(i)) { + continue; + } + + CType cur_indice = indices_data[i + offset]; + if (!dict_used[cur_indice]) { + dict_used[cur_indice] = true; + dict_used_count++; + + if (dict_used_count == dict->length()) { // input is already compacted + return dict_array; + } + } + } + + // dictionary compaction + if (dict_used_count == 0) { + ARROW_ASSIGN_OR_RAISE(auto empty_dict, + MakeEmptyArray(dict->type(), ctx->memory_pool())); + return DictionaryArray::FromArrays(dict_array->type(), indice, empty_dict); + } + std::vector<CType> dict_indice; + bool need_change_indice = false; + CType len = static_cast<CType>(dict->length()); + for (CType i = 0; i < len; i++) { + if (dict_used[i]) { + dict_indice.push_back(i); + } else if (i + 1 < len && dict_used[i + 1]) { + need_change_indice = true; + } + } + BuilderType dict_indice_builder; + ARROW_RETURN_NOT_OK(dict_indice_builder.AppendValues(dict_indice)); Review Comment: Sure, let me change the code -- 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]
