|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | +#include <string> |
| 22 | + |
| 23 | +#include "core/column/column_string.h" |
| 24 | +#include "core/column/column_vector.h" |
| 25 | +#include "core/string_buffer.hpp" |
| 26 | +#include "exprs/aggregate/aggregate_function_topn.h" |
| 27 | + |
| 28 | +namespace doris { |
| 29 | +namespace { |
| 30 | + |
| 31 | +template <PrimitiveType T> |
| 32 | +AggregateFunctionTopNData<T> round_trip(const AggregateFunctionTopNData<T>& state) { |
| 33 | + auto column = ColumnString::create(); |
| 34 | + BufferWritable writer(*column); |
| 35 | + state.write(writer); |
| 36 | + writer.commit(); |
| 37 | + BufferReadable reader(column->get_data_at(0)); |
| 38 | + AggregateFunctionTopNData<T> result; |
| 39 | + result.read(reader); |
| 40 | + return result; |
| 41 | +} |
| 42 | + |
| 43 | +class AggregateFunctionTopNUnlimitedTest : public testing::TestWithParam<int> {}; |
| 44 | + |
| 45 | +TEST_P(AggregateFunctionTopNUnlimitedTest, SerializeAndMergeStrings) { |
| 46 | + AggregateFunctionTopNData<TYPE_STRING> lhs; |
| 47 | + AggregateFunctionTopNData<TYPE_STRING> rhs; |
| 48 | + lhs.set_paramenters(1, GetParam()); |
| 49 | + rhs.set_paramenters(1, GetParam()); |
| 50 | + // The global winner is not the most frequent value in either partial state. |
| 51 | + lhs.add(std::string("a"), 3); |
| 52 | + lhs.add(std::string("winner"), 2); |
| 53 | + rhs.add(std::string("b"), 3); |
| 54 | + rhs.add(std::string("winner"), 2); |
| 55 | + |
| 56 | + auto partial = round_trip(lhs); |
| 57 | + EXPECT_EQ(partial.counter_map, lhs.counter_map); |
| 58 | + AggregateFunctionTopNData<TYPE_STRING> merged; |
| 59 | + merged.merge(partial); |
| 60 | + merged = round_trip(merged); |
| 61 | + merged.merge(round_trip(rhs)); |
| 62 | + merged = round_trip(merged); |
| 63 | + |
| 64 | + ASSERT_EQ(merged.counter_map.size(), 3); |
| 65 | + EXPECT_EQ(merged.counter_map.at("a"), 3); |
| 66 | + EXPECT_EQ(merged.counter_map.at("b"), 3); |
| 67 | + EXPECT_EQ(merged.counter_map.at("winner"), 4); |
| 68 | + EXPECT_EQ(merged.get(), R"({"winner":4})"); |
| 69 | + |
| 70 | + AggregateFunctionTopNData<TYPE_STRING> empty; |
| 71 | + merged.merge(round_trip(empty)); |
| 72 | + EXPECT_EQ(merged.get(), R"({"winner":4})"); |
| 73 | + |
| 74 | + merged.reset(); |
| 75 | + EXPECT_EQ(round_trip(merged).get(), "{}"); |
| 76 | + merged.set_paramenters(1, GetParam()); |
| 77 | + merged.add(std::string("new"), 7); |
| 78 | + EXPECT_EQ(round_trip(merged).get(), R"({"new":7})"); |
| 79 | +} |
| 80 | + |
| 81 | +TEST_P(AggregateFunctionTopNUnlimitedTest, SerializeAndMergeWeightedIntegers) { |
| 82 | + AggregateFunctionTopNData<TYPE_INT> lhs; |
| 83 | + AggregateFunctionTopNData<TYPE_INT> rhs; |
| 84 | + lhs.set_paramenters(2, GetParam()); |
| 85 | + rhs.set_paramenters(2, GetParam()); |
| 86 | + lhs.add(1, 10); |
| 87 | + lhs.add(2, 7); |
| 88 | + lhs.add(3, 6); |
| 89 | + rhs.add(4, 11); |
| 90 | + rhs.add(5, 8); |
| 91 | + rhs.add(3, 6); |
| 92 | + |
| 93 | + AggregateFunctionTopNData<TYPE_INT> merged; |
| 94 | + merged.merge(round_trip(lhs)); |
| 95 | + merged.merge(round_trip(rhs)); |
| 96 | + merged = round_trip(merged); |
| 97 | + ASSERT_EQ(merged.counter_map.size(), 5); |
| 98 | + EXPECT_EQ(merged.counter_map.at(3), 12); |
| 99 | + auto result = ColumnInt32::create(); |
| 100 | + merged.insert_result_into(*result); |
| 101 | + ASSERT_EQ(result->size(), 2); |
| 102 | + EXPECT_EQ(result->get_element(0), 3); |
| 103 | + EXPECT_EQ(result->get_element(1), 4); |
| 104 | +} |
| 105 | + |
| 106 | +INSTANTIATE_TEST_SUITE_P(NonPositiveRates, AggregateFunctionTopNUnlimitedTest, |
| 107 | + testing::Values(0, -1, INT32_MIN)); |
| 108 | + |
| 109 | +TEST(AggregateFunctionTopNTest, PositiveRateStillLimitsSerializedCandidates) { |
| 110 | + AggregateFunctionTopNData<TYPE_INT> state; |
| 111 | + state.set_paramenters(1, 2); |
| 112 | + state.add(1, 3); |
| 113 | + state.add(2, 2); |
| 114 | + state.add(3, 1); |
| 115 | + auto partial = round_trip(state); |
| 116 | + ASSERT_EQ(partial.counter_map.size(), 2); |
| 117 | + EXPECT_EQ(partial.counter_map.at(1), 3); |
| 118 | + EXPECT_EQ(partial.counter_map.at(2), 2); |
| 119 | + |
| 120 | + state.set_paramenters(1); |
| 121 | + EXPECT_EQ(round_trip(state).counter_map, state.counter_map); |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace |
| 125 | +} // namespace doris |
0 commit comments