benibus commented on code in PR #37418: URL: https://github.com/apache/arrow/pull/37418#discussion_r1320374964
########## cpp/src/arrow/compute/kernels/vector_dictionary_test.cc: ########## @@ -0,0 +1,139 @@ +// 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 <gtest/gtest.h> + +#include "arrow/array.h" +#include "arrow/chunked_array.h" +#include "arrow/compute/api.h" +#include "arrow/compute/kernels/test_util.h" +#include "arrow/status.h" +#include "arrow/testing/util.h" +#include "arrow/type.h" +#include "arrow/util/decimal.h" + +namespace arrow { + +namespace compute { + +class TestDictionaryCompactionKernel : public ::testing::Test {}; + +void CheckDictionaryCompaction(const std::shared_ptr<DataType>& dict_type, + const std::string& input_dictionary_json, + const std::string& input_index_json, + const std::string& expected_dictionary_json, + const std::string& expected_index_json) { + auto input = DictArrayFromJSON(dict_type, input_index_json, input_dictionary_json); + auto expected = + DictArrayFromJSON(dict_type, expected_index_json, expected_dictionary_json); + + ASSERT_OK_AND_ASSIGN(Datum actual_datum, DictionaryCompaction(input)); + ValidateOutput(actual_datum); + std::shared_ptr<Array> actual = actual_datum.make_array(); + AssertArraysEqual(*expected, *actual, /*verbose=*/true); +} + +void CheckDictionaryCompactionOnChunks(const std::shared_ptr<DataType>& dict_type, + const ArrayVector& input, + const ArrayVector& expected) { + auto input_chunked_array = std::make_shared<ChunkedArray>(input, dict_type); + + ASSERT_OK_AND_ASSIGN(Datum actual_datum, DictionaryCompaction(input_chunked_array)); + ValidateOutput(actual_datum); + auto actual = actual_datum.chunked_array(); + AssertChunkedEqual(*actual, expected); +} + +TEST_F(TestDictionaryCompactionKernel, DictionaryArray) { Review Comment: We should probably add a test case where the index array has a nonzero offset - e.g. it was sliced from a larger array. ########## cpp/src/arrow/compute/api_vector.cc: ########## @@ -327,6 +327,10 @@ Result<Datum> DictionaryEncode(const Datum& value, const DictionaryEncodeOptions return CallFunction("dictionary_encode", {value}, &options, ctx); } +Result<Datum> DictionaryCompaction(const Datum& value, ExecContext* ctx) { + return CallFunction("dictionary_compaction", {value}, ctx); Review Comment: Minor, but `dictionary_compact` sounds like a more natural function name to me. ########## cpp/src/arrow/compute/kernels/vector_dictionary_test.cc: ########## @@ -0,0 +1,139 @@ +// 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 <gtest/gtest.h> + +#include "arrow/array.h" +#include "arrow/chunked_array.h" +#include "arrow/compute/api.h" +#include "arrow/compute/kernels/test_util.h" +#include "arrow/status.h" +#include "arrow/testing/util.h" +#include "arrow/type.h" +#include "arrow/util/decimal.h" + +namespace arrow { + +namespace compute { + +class TestDictionaryCompactionKernel : public ::testing::Test {}; + +void CheckDictionaryCompaction(const std::shared_ptr<DataType>& dict_type, + const std::string& input_dictionary_json, + const std::string& input_index_json, + const std::string& expected_dictionary_json, + const std::string& expected_index_json) { + auto input = DictArrayFromJSON(dict_type, input_index_json, input_dictionary_json); + auto expected = + DictArrayFromJSON(dict_type, expected_index_json, expected_dictionary_json); + + ASSERT_OK_AND_ASSIGN(Datum actual_datum, DictionaryCompaction(input)); + ValidateOutput(actual_datum); + std::shared_ptr<Array> actual = actual_datum.make_array(); + AssertArraysEqual(*expected, *actual, /*verbose=*/true); +} + +void CheckDictionaryCompactionOnChunks(const std::shared_ptr<DataType>& dict_type, + const ArrayVector& input, + const ArrayVector& expected) { + auto input_chunked_array = std::make_shared<ChunkedArray>(input, dict_type); + + ASSERT_OK_AND_ASSIGN(Datum actual_datum, DictionaryCompaction(input_chunked_array)); + ValidateOutput(actual_datum); + auto actual = actual_datum.chunked_array(); + AssertChunkedEqual(*actual, expected); +} + +TEST_F(TestDictionaryCompactionKernel, DictionaryArray) { + std::shared_ptr<arrow::DataType> type; + std::shared_ptr<arrow::DataType> dict_type; + + for (const auto& index_type : all_dictionary_index_types()) { + ARROW_SCOPED_TRACE("index_type = ", index_type->ToString()); + + type = boolean(); + dict_type = dictionary(index_type, type); + // input is compacted + CheckDictionaryCompaction(dict_type, "[]", "[]", "[]", "[]"); + CheckDictionaryCompaction(dict_type, "[true, false]", "[0, 1, 0]", "[true, false]", + "[0, 1, 0]"); + CheckDictionaryCompaction(dict_type, "[true, null, false]", "[2, 1, 0]", + "[true, null, false]", "[2, 1, 0]"); + CheckDictionaryCompaction(dict_type, "[true, false]", "[0, null, 1, 0]", + "[true, false]", "[0, null, 1, 0]"); + CheckDictionaryCompaction(dict_type, "[true, null, false]", "[2, null, 1, 0]", + "[true, null, false]", "[2, null, 1, 0]"); + // input isn't compacted + CheckDictionaryCompaction(dict_type, "[null]", "[]", "[]", "[]"); + CheckDictionaryCompaction(dict_type, "[false]", "[null]", "[]", "[null]"); + CheckDictionaryCompaction(dict_type, "[true, false]", "[0]", "[true]", "[0]"); + CheckDictionaryCompaction(dict_type, "[true, null, false]", "[2, 1]", "[null, false]", + "[1, 0]"); + CheckDictionaryCompaction(dict_type, "[true, false]", "[0, null]", "[true]", + "[0, null]"); + CheckDictionaryCompaction(dict_type, "[true, null, false]", "[2, null, 1]", + "[null, false]", "[1, null, 0]"); + } Review Comment: It'd be nice to also add a test case with a larger input array, where several contiguous chunks of the dictionary array are unused. It'd stress the logic for index re-adjustment more. ########## cpp/src/arrow/compute/kernels/vector_dictionary.cc: ########## @@ -0,0 +1,211 @@ +// 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_compaction_doc{ + "Compact dictionary array", + ("Return a compacted version of the dictionary array input,\n" + "which would remove unused values in dictionary.\n" + "The function assume every indice is effective."), + {"dictionary_array"}}; + +class DictionaryCompactionKernel : public KernelState { + public: + virtual Result<std::shared_ptr<Array>> Exec(std::shared_ptr<Array> dict_array, + ExecContext* ctx) const = 0; +}; + +template <typename IndiceArrowType> +class DictionaryCompactionKernelImpl : public DictionaryCompactionKernel { + 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 = (CType)dict->length(); Review Comment: Nit ```suggestion CType len = static_cast<CType>(dict->length()); ``` -- 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]
