|
| 1 | +/* |
| 2 | + * Copyright 2026-present Alibaba Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "paimon/core/mergetree/compact/aggregate/field_listagg_agg.h" |
| 18 | + |
| 19 | +#include <map> |
| 20 | +#include <string> |
| 21 | +#include <string_view> |
| 22 | + |
| 23 | +#include "arrow/type_fwd.h" |
| 24 | +#include "gtest/gtest.h" |
| 25 | +#include "paimon/core/core_options.h" |
| 26 | +#include "paimon/status.h" |
| 27 | +#include "paimon/testing/utils/testharness.h" |
| 28 | + |
| 29 | +namespace paimon::test { |
| 30 | + |
| 31 | +class FieldListaggAggTest : public testing::Test { |
| 32 | + protected: |
| 33 | + static Result<std::unique_ptr<FieldListaggAgg>> MakeAgg(const std::string& delimiter = ",", |
| 34 | + bool distinct = false) { |
| 35 | + std::map<std::string, std::string> opts; |
| 36 | + opts["fields.f.list-agg-delimiter"] = delimiter; |
| 37 | + opts["fields.f.distinct"] = distinct ? "true" : "false"; |
| 38 | + PAIMON_ASSIGN_OR_RAISE(auto options, CoreOptions::FromMap(opts)); |
| 39 | + return FieldListaggAgg::Create(arrow::utf8(), std::move(options), "f"); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +TEST_F(FieldListaggAggTest, TestSimple) { |
| 44 | + ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg()); |
| 45 | + auto ret = agg->Agg(std::string_view("hello"), std::string_view(" world")); |
| 46 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "hello, world"); |
| 47 | +} |
| 48 | + |
| 49 | +TEST_F(FieldListaggAggTest, TestDelimiter) { |
| 50 | + ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg("-")); |
| 51 | + auto ret = agg->Agg(std::string_view("user1"), std::string_view("user2")); |
| 52 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "user1-user2"); |
| 53 | +} |
| 54 | + |
| 55 | +TEST_F(FieldListaggAggTest, TestNull) { |
| 56 | + ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg()); |
| 57 | + |
| 58 | + // input null -> return accumulator |
| 59 | + { |
| 60 | + auto ret = agg->Agg(std::string_view("hello"), NullType()); |
| 61 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "hello"); |
| 62 | + } |
| 63 | + // accumulator null -> return input |
| 64 | + { |
| 65 | + auto ret = agg->Agg(NullType(), std::string_view("world")); |
| 66 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "world"); |
| 67 | + } |
| 68 | + // both null -> return null |
| 69 | + { |
| 70 | + auto ret = agg->Agg(NullType(), NullType()); |
| 71 | + ASSERT_TRUE(DataDefine::IsVariantNull(ret)); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +TEST_F(FieldListaggAggTest, TestEmptyString) { |
| 76 | + ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg()); |
| 77 | + |
| 78 | + // empty input -> return accumulator |
| 79 | + { |
| 80 | + auto ret = agg->Agg(std::string_view("hello"), std::string_view("")); |
| 81 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "hello"); |
| 82 | + } |
| 83 | + // empty accumulator -> return input |
| 84 | + { |
| 85 | + auto ret = agg->Agg(std::string_view(""), std::string_view("world")); |
| 86 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "world"); |
| 87 | + } |
| 88 | + // both empty -> return input (which is empty) |
| 89 | + { |
| 90 | + auto ret = agg->Agg(std::string_view(""), std::string_view("")); |
| 91 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), ""); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +TEST_F(FieldListaggAggTest, TestMultipleAccumulation) { |
| 96 | + ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg()); |
| 97 | + |
| 98 | + // "a" + "," + "b" = "a,b", then "a,b" + "," + "c" = "a,b,c" |
| 99 | + auto ret = agg->Agg(std::string_view("a"), std::string_view("b")); |
| 100 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a,b"); |
| 101 | + ret = agg->Agg(std::move(ret), std::string_view("c")); |
| 102 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a,b,c"); |
| 103 | +} |
| 104 | + |
| 105 | +TEST_F(FieldListaggAggTest, TestDistinct) { |
| 106 | + ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg(";", true)); |
| 107 | + |
| 108 | + // "a;b" + "b;c" -> "a;b;c" (deduplicate "b") |
| 109 | + auto ret = agg->Agg(std::string_view("a;b"), std::string_view("b;c")); |
| 110 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a;b;c"); |
| 111 | +} |
| 112 | + |
| 113 | +TEST_F(FieldListaggAggTest, TestDistinctNoDuplicates) { |
| 114 | + ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg(" ", true)); |
| 115 | + |
| 116 | + // "a b" + "c d" -> "a b c d" (no dups to remove) |
| 117 | + auto ret = agg->Agg(std::string_view("a b"), std::string_view("c d")); |
| 118 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a b c d"); |
| 119 | +} |
| 120 | + |
| 121 | +TEST_F(FieldListaggAggTest, TestDistinctEmptyInput) { |
| 122 | + ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg(";", true)); |
| 123 | + |
| 124 | + // empty input -> return accumulator |
| 125 | + auto ret = agg->Agg(std::string_view("a;b"), std::string_view("")); |
| 126 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a;b"); |
| 127 | +} |
| 128 | + |
| 129 | +TEST_F(FieldListaggAggTest, TestDistinctFalse) { |
| 130 | + ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg(";", false)); |
| 131 | + |
| 132 | + // "a;b" + "b;c" -> "a;b;b;c" (no dedup) |
| 133 | + auto ret = agg->Agg(std::string_view("a;b"), std::string_view("b;c")); |
| 134 | + ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a;b;b;c"); |
| 135 | +} |
| 136 | + |
| 137 | +TEST_F(FieldListaggAggTest, TestInvalidType) { |
| 138 | + EXPECT_OK_AND_ASSIGN(auto options, CoreOptions::FromMap({})); |
| 139 | + auto result = FieldListaggAgg::Create(arrow::int32(), options, "f"); |
| 140 | + ASSERT_FALSE(result.ok()); |
| 141 | + ASSERT_TRUE(result.status().ToString().find("supposed to be string") != std::string::npos) |
| 142 | + << result.status().ToString(); |
| 143 | +} |
| 144 | + |
| 145 | +} // namespace paimon::test |
0 commit comments