Copilot commented on code in PR #60833: URL: https://github.com/apache/doris/pull/60833#discussion_r2853375419
########## be/src/vec/aggregate_functions/aggregate_function_entropy.h: ########## @@ -0,0 +1,191 @@ +// 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. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/src/AggregateFunctions/AggregateFunctionEntropy.cpp +// and modified by Doris + +#pragma once + +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/columns/column.h" +#include "vec/columns/column_decimal.h" +#include "vec/common/assert_cast.h" +#include "vec/common/hash_table/hash.h" +#include "vec/common/hash_table/phmap_fwd_decl.h" +#include "vec/common/string_ref.h" +#include "vec/common/uint128.h" +#include "vec/core/types.h" +#include "vec/data_types/data_type.h" +#include "vec/data_types/data_type_number.h" + +namespace doris::vectorized { +#include "common/compile_check_begin.h" + +class Arena; +class BufferReadable; +class BufferWritable; +template <PrimitiveType> +class ColumnVector; + +/** Calculates Shannon Entropy, using HashMap and computing empirical distribution function. + * Entropy is measured in bits (base-2 logarithm is used). + */ +template <typename Value, typename Hash = HashCRC32<Value>> +struct AggregateFunctionEntropyData { + using Container = flat_hash_map<Value, uint64_t, Hash>; + using Self = AggregateFunctionEntropyData<Value, Hash>; + Container frequency_map; + + void clear() { frequency_map.clear(); } + + void add(const Value& elem) { ++frequency_map[elem]; } + + void merge(const Self& rhs) { + frequency_map.reserve(frequency_map.size() + rhs.frequency_map.size()); + for (const auto& [elem, count] : rhs.frequency_map) { + frequency_map[elem] += count; + } + } + + void write(BufferWritable& buf) const { + buf.write_var_uint(frequency_map.size()); + for (const auto& [elem, count] : frequency_map) { + buf.write_binary(elem); + buf.write_binary(count); + } + } + + void read(BufferReadable& buf) { + uint64_t new_size = 0; + buf.read_var_uint(new_size); + frequency_map.reserve(frequency_map.size() + new_size); + + Value elem; + uint64_t count; + for (size_t i = 0; i < new_size; ++i) { + buf.read_binary(elem); + buf.read_binary(count); + frequency_map[elem] += count; + } + } + + Float64 get_result() const { + Float64 entropy = 0; + uint64_t total_count = 0; + for (const auto& [_, count] : frequency_map) { + total_count += count; + } + for (const auto& [_, count] : frequency_map) { + Float64 p = static_cast<Float64>(count) / static_cast<Float64>(total_count); + entropy -= p * std::log2(p); + } + return entropy; + } Review Comment: The get_result() method has a division by zero bug when the frequency_map is empty (no data has been added). When total_count is 0, line 93 performs a division by zero. This needs to be handled by checking if total_count is 0 and returning 0.0 in that case before attempting the division. ########## be/test/vec/aggregate_functions/agg_entropy_test.cpp: ########## @@ -0,0 +1,115 @@ +// 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 "agg_function_test.h" + +namespace doris::vectorized { + +struct AggregateFunctionEntropyTest : public AggregateFunctiontest {}; + +// ------------------------------------------------------------ +// 1. Numeric entropy test +// ------------------------------------------------------------ +TEST_F(AggregateFunctionEntropyTest, test_numeric_entropy) { + create_agg("entropy", false, {std::make_shared<DataTypeInt32>()}, + std::make_shared<DataTypeFloat64>()); + + // values: 1,1,2,2,3 + Block block = ColumnHelper::create_block<DataTypeInt32>({1, 1, 2, 2, 3}); + + double p1 = 2.0 / 5; + double p2 = 2.0 / 5; + double p3 = 1.0 / 5; + double expected = -(p1 * log2(p1) + p2 * log2(p2) + p3 * log2(p3)); + + execute(block, ColumnHelper::create_column_with_name<DataTypeFloat64>({expected})); +} + +// ------------------------------------------------------------ +// 2. String entropy test +// ------------------------------------------------------------ +TEST_F(AggregateFunctionEntropyTest, test_string_entropy) { + create_agg("entropy", false, {std::make_shared<DataTypeString>()}, + std::make_shared<DataTypeFloat64>()); + + Block block = ColumnHelper::create_block<DataTypeString>({"a", "a", "b"}); + + double p1 = 2.0 / 3; + double p2 = 1.0 / 3; + double expected = -(p1 * log2(p1) + p2 * log2(p2)); + + execute(block, ColumnHelper::create_column_with_name<DataTypeFloat64>({expected})); +} + +// ------------------------------------------------------------ +// 3. Generic entropy test +// ------------------------------------------------------------ +TEST_F(AggregateFunctionEntropyTest, test_generic_entropy) { + create_agg("entropy", false, + {std::make_shared<DataTypeInt32>(), std::make_shared<DataTypeString>()}, + std::make_shared<DataTypeFloat64>()); + + // rows: + // (1, "a") + // (1, "a") + // (2, "b") + Block block = + ColumnHelper::create_block<DataTypeInt32, DataTypeString>({1, 1, 2}, {"a", "a", "b"}); + + double p1 = 2.0 / 3; + double p2 = 1.0 / 3; + double expected = -(p1 * log2(p1) + p2 * log2(p2)); + + execute(block, ColumnHelper::create_column_with_name<DataTypeFloat64>({expected})); +} + +// ------------------------------------------------------------ +// 4. NULL entropy test +// ------------------------------------------------------------ +TEST_F(AggregateFunctionEntropyTest, test_nullable_entropy) { + create_agg("entropy", false, + {std::make_shared<DataTypeNullable>(std::make_shared<DataTypeInt32>())}, + std::make_shared<DataTypeFloat64>()); + + // values: 1,1,NULL,2,NULL + Block block = + ColumnHelper::create_nullable_block<DataTypeInt32>({1, 1, 0, 2, 0}, {0, 0, 1, 0, 1}); + + // only non-null values: 1,1,2 + double p1 = 2.0 / 3; + double p2 = 1.0 / 3; + double expected = -(p1 * log2(p1) + p2 * log2(p2)); + + execute(block, ColumnHelper::create_column_with_name<DataTypeFloat64>({expected})); +} + +// ------------------------------------------------------------ +// 5. Empty input test +// ------------------------------------------------------------ +TEST_F(AggregateFunctionEntropyTest, test_empty) { + create_agg("entropy", false, {std::make_shared<DataTypeInt32>()}, + std::make_shared<DataTypeFloat64>()); + + Block block = ColumnHelper::create_block<DataTypeInt32>({}); + + // entropy of empty set = 0 + execute(block, ColumnHelper::create_column_with_name<DataTypeFloat64>({0.0})); Review Comment: There is a discrepancy in the expected behavior for empty input. The unit test expects entropy of an empty set to be 0.0 (line 112), but the regression test expects NULL (line 114 of the .out file). This inconsistency needs to be resolved. Based on the regression test and the fact that the function inherits from NullableAggregateFunction, the correct behavior should likely be NULL for empty input, which means the unit test expectation at line 112 is incorrect. ```suggestion // entropy of empty set = NULL execute(block, ColumnHelper::create_nullable_column_with_name<DataTypeFloat64>({0.0}, {1})); ``` -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
