bkietz commented on code in PR #37418: URL: https://github.com/apache/arrow/pull/37418#discussion_r1333337606
########## 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."), Review Comment: Here too ########## cpp/src/arrow/compute/api_vector.h: ########## @@ -586,6 +586,20 @@ Result<Datum> DictionaryEncode( const DictionaryEncodeOptions& options = DictionaryEncodeOptions::Defaults(), ExecContext* ctx = NULLPTR); +/// \brief Compact a dictionary array +/// +/// The output removes unused values in dictionary from the input. +/// The function assumes every indice is effective. Review Comment: Could you reword this to be more explicit about what is assumed about each index? I'm not sure what you mean by "effective" ########## 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(); Review Comment: ```suggestion const std::shared_ptr<Array>& indices = casted_dict_array.indices(); ``` ########## 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]; Review Comment: ```suggestion CType current_index = indices_data[i + offset]; ``` ########## 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)); + ARROW_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Array> compacted_dict_indices, + dict_indice_builder.Finish()); + ARROW_ASSIGN_OR_RAISE( + auto compacted_dict_res, + Take(dict, compacted_dict_indices, TakeOptions::NoBoundsCheck(), ctx)); + std::shared_ptr<arrow::Array> compacted_dict = compacted_dict_res.make_array(); + + // indice changes + if (!need_change_indice) { Review Comment: Please check this case before building the vector of indices; that way we might be able to skip the allocation ########## 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)); + ARROW_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Array> compacted_dict_indices, + dict_indice_builder.Finish()); + ARROW_ASSIGN_OR_RAISE( + auto compacted_dict_res, + Take(dict, compacted_dict_indices, TakeOptions::NoBoundsCheck(), ctx)); + std::shared_ptr<arrow::Array> compacted_dict = compacted_dict_res.make_array(); + + // indice changes + if (!need_change_indice) { + return DictionaryArray::FromArrays(dict_array->type(), indice, compacted_dict); + } + std::vector<CType> indice_minus_number(dict->length(), 0); + if (!dict_used[0]) { + indice_minus_number[0] = 1; + } + for (int64_t i = 1; i < dict->length(); i++) { + indice_minus_number[i] = indice_minus_number[i - 1]; + if (!dict_used[i]) { + indice_minus_number[i] = indice_minus_number[i] + 1; + } + } + + std::vector<CType> raw_changed_indice(indice->length(), 0); + std::vector<bool> is_valid(indice->length(), true); + for (int64_t i = 0; i < indice->length(); i++) { + if (indice->IsNull(i)) { + is_valid[i] = false; + } else { + CType cur_indice = indices_data[i + offset]; + raw_changed_indice[i] = cur_indice - indice_minus_number[cur_indice]; + } + } + BuilderType indice_builder; Review Comment: Please use the context's memory pool; it may have overridden the default ```suggestion BuilderType indices_builder(ctx->memory_pool()); ``` ########## 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; Review Comment: The GetValues helper function can reduce the boilerplate here. It also adds in the offset ```suggestion const auto* indices_data = indices->data()->GetValues<CType>(1); ``` ########## 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)); + ARROW_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Array> compacted_dict_indices, + dict_indice_builder.Finish()); + ARROW_ASSIGN_OR_RAISE( + auto compacted_dict_res, + Take(dict, compacted_dict_indices, TakeOptions::NoBoundsCheck(), ctx)); + std::shared_ptr<arrow::Array> compacted_dict = compacted_dict_res.make_array(); + + // indice changes + if (!need_change_indice) { + return DictionaryArray::FromArrays(dict_array->type(), indice, compacted_dict); + } + std::vector<CType> indice_minus_number(dict->length(), 0); + if (!dict_used[0]) { + indice_minus_number[0] = 1; + } + for (int64_t i = 1; i < dict->length(); i++) { + indice_minus_number[i] = indice_minus_number[i - 1]; + if (!dict_used[i]) { + indice_minus_number[i] = indice_minus_number[i] + 1; + } + } + + std::vector<CType> raw_changed_indice(indice->length(), 0); Review Comment: here too please use the builder directly rather than building vectors ########## 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: Wouldn't it be more straightforward to append to the builder in the loop instead of building a vector first? -- 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]
