-
Notifications
You must be signed in to change notification settings - Fork 44
feat(compact): add FieldListaggAgg aggregate function #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zouxxyy
wants to merge
9
commits into
alibaba:main
Choose a base branch
from
Zouxxyy:dev/listagg-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3fd43ac
feat(compact): add FieldListaggAgg aggregate functions
Zouxxyy c0b5ed5
fix(compact): address code review comments on FieldListaggAgg
Zouxxyy 3e34085
Merge branch 'main' into dev/listagg-fix
lxy-9602 80eb55f
Merge branch 'main' into dev/listagg-fix
lxy-9602 06c875a
fix(compact): address reviewer comments on FieldListaggAgg PR
Zouxxyy 358cb3f
1
Zouxxyy 7ab3d7a
1
Zouxxyy 0b7b5d2
Merge branch 'main' into dev/listagg-fix
lxy-9602 fbf5094
update
Zouxxyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/paimon/core/mergetree/compact/aggregate/field_listagg_agg.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * Copyright 2026-present Alibaba Inc. | ||
| * | ||
Zouxxyy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * Licensed 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. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <unordered_set> | ||
|
|
||
| #include "paimon/common/data/data_define.h" | ||
| #include "paimon/core/core_options.h" | ||
| #include "paimon/core/mergetree/compact/aggregate/field_aggregator.h" | ||
|
|
||
| namespace arrow { | ||
| class DataType; | ||
| } // namespace arrow | ||
|
|
||
| namespace paimon { | ||
| /// listagg aggregate a field of a row. | ||
| /// Concatenates string values with a delimiter. | ||
| class FieldListaggAgg : public FieldAggregator { | ||
| public: | ||
| static constexpr char NAME[] = "listagg"; | ||
|
|
||
| static Result<std::unique_ptr<FieldListaggAgg>> Create( | ||
| const std::shared_ptr<arrow::DataType>& field_type, const CoreOptions& options, | ||
| const std::string& field_name) { | ||
| if (field_type->id() != arrow::Type::type::STRING) { | ||
Zouxxyy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return Status::Invalid( | ||
| fmt::format("invalid field type {} for field '{}' of {}, supposed to be string", | ||
| field_type->ToString(), field_name, NAME)); | ||
| } | ||
| PAIMON_ASSIGN_OR_RAISE(std::string delimiter, options.FieldListAggDelimiter(field_name)); | ||
| PAIMON_ASSIGN_OR_RAISE(bool distinct, options.FieldCollectAggDistinct(field_name)); | ||
Zouxxyy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // When delimiter is empty and distinct is true, fall back to whitespace split. | ||
| if (distinct && delimiter.empty()) { | ||
| delimiter = " "; | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the Java logic is: if |
||
| return std::unique_ptr<FieldListaggAgg>( | ||
| new FieldListaggAgg(field_type, std::move(delimiter), distinct)); | ||
| } | ||
|
|
||
| VariantType Agg(const VariantType& accumulator, const VariantType& input_field) override { | ||
| bool accumulator_null = DataDefine::IsVariantNull(accumulator); | ||
| bool input_null = DataDefine::IsVariantNull(input_field); | ||
| if (accumulator_null || input_null) { | ||
| return accumulator_null ? input_field : accumulator; | ||
| } | ||
| std::string_view acc_str = DataDefine::GetStringView(accumulator); | ||
| std::string_view in_str = DataDefine::GetStringView(input_field); | ||
| if (in_str.empty()) { | ||
| return accumulator; | ||
| } | ||
| if (acc_str.empty()) { | ||
| return input_field; | ||
| } | ||
|
|
||
| if (distinct_) { | ||
| result_ = AggDistinctImpl(acc_str, in_str); | ||
| } else { | ||
| // Build into a local string to avoid aliasing when acc_str points into result_ | ||
| std::string new_result; | ||
| new_result.reserve(acc_str.size() + delimiter_.size() + in_str.size()); | ||
| new_result.append(acc_str); | ||
| new_result.append(delimiter_); | ||
| new_result.append(in_str); | ||
| result_ = std::move(new_result); | ||
| } | ||
| return std::string_view(result_); | ||
| } | ||
Zouxxyy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private: | ||
| std::string AggDistinctImpl(std::string_view acc_str, std::string_view in_str) const { | ||
| // Split accumulator tokens into a set for dedup | ||
| std::unordered_set<std::string_view> seen; | ||
| std::string_view remaining = acc_str; | ||
| while (true) { | ||
| size_t pos = remaining.find(delimiter_); | ||
| std::string_view token = | ||
| (pos == std::string_view::npos) ? remaining : remaining.substr(0, pos); | ||
| if (!token.empty()) { | ||
| seen.insert(token); | ||
| } | ||
| if (pos == std::string_view::npos) { | ||
| break; | ||
| } | ||
| remaining = remaining.substr(pos + delimiter_.size()); | ||
| } | ||
|
|
||
| // Start with the full accumulator, then append delimiter + new distinct tokens from input | ||
| std::string result; | ||
| result.reserve(acc_str.size() + in_str.size()); | ||
| result.append(acc_str); | ||
| remaining = in_str; | ||
| while (true) { | ||
| size_t pos = remaining.find(delimiter_); | ||
| std::string_view token = | ||
| (pos == std::string_view::npos) ? remaining : remaining.substr(0, pos); | ||
| if (!token.empty() && seen.insert(token).second) { | ||
| result.append(delimiter_); | ||
| result.append(token); | ||
| } | ||
| if (pos == std::string_view::npos) { | ||
| break; | ||
| } | ||
| remaining = remaining.substr(pos + delimiter_.size()); | ||
| } | ||
| return result; | ||
Zouxxyy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| explicit FieldListaggAgg(const std::shared_ptr<arrow::DataType>& field_type, | ||
| std::string delimiter, bool distinct) | ||
| : FieldAggregator(std::string(NAME), field_type), | ||
| delimiter_(std::move(delimiter)), | ||
| distinct_(distinct) {} | ||
|
|
||
| std::string delimiter_; | ||
| bool distinct_; | ||
| std::string result_; | ||
| }; | ||
| } // namespace paimon | ||
145 changes: 145 additions & 0 deletions
145
src/paimon/core/mergetree/compact/aggregate/field_listagg_agg_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * Copyright 2026-present Alibaba Inc. | ||
| * | ||
| * Licensed 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 "paimon/core/mergetree/compact/aggregate/field_listagg_agg.h" | ||
|
|
||
| #include <map> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include "arrow/type_fwd.h" | ||
| #include "gtest/gtest.h" | ||
| #include "paimon/core/core_options.h" | ||
| #include "paimon/status.h" | ||
| #include "paimon/testing/utils/testharness.h" | ||
|
|
||
| namespace paimon::test { | ||
|
|
||
| class FieldListaggAggTest : public testing::Test { | ||
| protected: | ||
| static Result<std::unique_ptr<FieldListaggAgg>> MakeAgg(const std::string& delimiter = ",", | ||
| bool distinct = false) { | ||
| std::map<std::string, std::string> opts; | ||
| opts["fields.f.list-agg-delimiter"] = delimiter; | ||
| opts["fields.f.distinct"] = distinct ? "true" : "false"; | ||
| PAIMON_ASSIGN_OR_RAISE(auto options, CoreOptions::FromMap(opts)); | ||
| return FieldListaggAgg::Create(arrow::utf8(), std::move(options), "f"); | ||
| } | ||
| }; | ||
|
|
||
Zouxxyy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TEST_F(FieldListaggAggTest, TestSimple) { | ||
| ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg()); | ||
| auto ret = agg->Agg(std::string_view("hello"), std::string_view(" world")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "hello, world"); | ||
| } | ||
|
|
||
| TEST_F(FieldListaggAggTest, TestDelimiter) { | ||
| ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg("-")); | ||
| auto ret = agg->Agg(std::string_view("user1"), std::string_view("user2")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "user1-user2"); | ||
| } | ||
|
|
||
| TEST_F(FieldListaggAggTest, TestNull) { | ||
| ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg()); | ||
|
|
||
| // input null -> return accumulator | ||
| { | ||
| auto ret = agg->Agg(std::string_view("hello"), NullType()); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "hello"); | ||
| } | ||
| // accumulator null -> return input | ||
| { | ||
| auto ret = agg->Agg(NullType(), std::string_view("world")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "world"); | ||
| } | ||
| // both null -> return null | ||
| { | ||
| auto ret = agg->Agg(NullType(), NullType()); | ||
| ASSERT_TRUE(DataDefine::IsVariantNull(ret)); | ||
| } | ||
| } | ||
|
|
||
| TEST_F(FieldListaggAggTest, TestEmptyString) { | ||
| ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg()); | ||
|
|
||
| // empty input -> return accumulator | ||
| { | ||
| auto ret = agg->Agg(std::string_view("hello"), std::string_view("")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "hello"); | ||
| } | ||
| // empty accumulator -> return input | ||
| { | ||
| auto ret = agg->Agg(std::string_view(""), std::string_view("world")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "world"); | ||
| } | ||
| // both empty -> return input (which is empty) | ||
| { | ||
| auto ret = agg->Agg(std::string_view(""), std::string_view("")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), ""); | ||
| } | ||
| } | ||
|
|
||
| TEST_F(FieldListaggAggTest, TestMultipleAccumulation) { | ||
| ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg()); | ||
|
|
||
| // "a" + "," + "b" = "a,b", then "a,b" + "," + "c" = "a,b,c" | ||
| auto ret = agg->Agg(std::string_view("a"), std::string_view("b")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a,b"); | ||
| ret = agg->Agg(std::move(ret), std::string_view("c")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a,b,c"); | ||
| } | ||
|
|
||
| TEST_F(FieldListaggAggTest, TestDistinct) { | ||
| ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg(";", true)); | ||
|
|
||
| // "a;b" + "b;c" -> "a;b;c" (deduplicate "b") | ||
| auto ret = agg->Agg(std::string_view("a;b"), std::string_view("b;c")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a;b;c"); | ||
| } | ||
|
|
||
| TEST_F(FieldListaggAggTest, TestDistinctNoDuplicates) { | ||
| ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg(" ", true)); | ||
|
|
||
| // "a b" + "c d" -> "a b c d" (no dups to remove) | ||
| auto ret = agg->Agg(std::string_view("a b"), std::string_view("c d")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a b c d"); | ||
| } | ||
|
|
||
| TEST_F(FieldListaggAggTest, TestDistinctEmptyInput) { | ||
| ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg(";", true)); | ||
|
|
||
| // empty input -> return accumulator | ||
| auto ret = agg->Agg(std::string_view("a;b"), std::string_view("")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a;b"); | ||
| } | ||
|
|
||
| TEST_F(FieldListaggAggTest, TestDistinctFalse) { | ||
| ASSERT_OK_AND_ASSIGN(auto agg, MakeAgg(";", false)); | ||
|
|
||
| // "a;b" + "b;c" -> "a;b;b;c" (no dedup) | ||
| auto ret = agg->Agg(std::string_view("a;b"), std::string_view("b;c")); | ||
| ASSERT_EQ(DataDefine::GetVariantValue<std::string_view>(ret), "a;b;b;c"); | ||
| } | ||
|
|
||
| TEST_F(FieldListaggAggTest, TestInvalidType) { | ||
| EXPECT_OK_AND_ASSIGN(auto options, CoreOptions::FromMap({})); | ||
| auto result = FieldListaggAgg::Create(arrow::int32(), options, "f"); | ||
| ASSERT_FALSE(result.ok()); | ||
| ASSERT_TRUE(result.status().ToString().find("supposed to be string") != std::string::npos) | ||
| << result.status().ToString(); | ||
| } | ||
|
|
||
| } // namespace paimon::test | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.