-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Feature] Add Topn udaf #4803
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
Merged
Merged
[Feature] Add Topn udaf #4803
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| // 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 "exprs/topn_function.h" | ||
| #include "util/topn_counter.h" | ||
| #include "util/slice.h" | ||
|
|
||
| namespace doris { | ||
|
|
||
| using doris_udf::AnyVal; | ||
|
|
||
| void TopNFunctions::init() { | ||
| } | ||
|
|
||
| void TopNFunctions::topn_init(FunctionContext* ctx, StringVal *dst) { | ||
| dst->is_null = false; | ||
| dst->len = sizeof(TopNCounter); | ||
| const AnyVal* space_expand_rate_val = ctx->get_constant_arg(2); | ||
| if (space_expand_rate_val != nullptr) { | ||
| int32_t space_expand_rate = reinterpret_cast<const IntVal*>(space_expand_rate_val)->val; | ||
| dst->ptr = (uint8_t *) new TopNCounter(space_expand_rate); | ||
| return; | ||
| } | ||
| dst->ptr = (uint8_t *) new TopNCounter(); | ||
| } | ||
|
|
||
| template <typename T> | ||
| void TopNFunctions::topn_update(FunctionContext*, const T& src, const IntVal& topn, StringVal* dst) { | ||
| if (src.is_null) { | ||
| return; | ||
| } | ||
| auto* dst_topn = reinterpret_cast<TopNCounter*>(dst->ptr); | ||
| dst_topn->set_top_num(topn.val); | ||
| dst_topn->add_item(src); | ||
| } | ||
|
|
||
| template <typename T> | ||
| void TopNFunctions::topn_update(FunctionContext *, const T &src, const IntVal &topn, const IntVal &space_expand_rate, | ||
| StringVal *dst) { | ||
| if (src.is_null) { | ||
| return; | ||
| } | ||
| auto* dst_topn = reinterpret_cast<TopNCounter*>(dst->ptr); | ||
| dst_topn->set_top_num(topn.val); | ||
| dst_topn->add_item(src); | ||
| } | ||
|
|
||
| void TopNFunctions::topn_merge(FunctionContext* ctx, const StringVal &src, StringVal *dst) { | ||
| if (src.is_null) { | ||
| return; | ||
| } | ||
| auto* dst_topn = reinterpret_cast<TopNCounter*>(dst->ptr); | ||
| dst_topn->merge(TopNCounter(Slice(src.ptr, src.len))); | ||
| } | ||
|
|
||
| StringVal TopNFunctions::topn_serialize(FunctionContext *ctx, const StringVal &src) { | ||
| auto* src_topn = reinterpret_cast<TopNCounter*>(src.ptr); | ||
|
|
||
| std::string buffer; | ||
| src_topn->serialize(&buffer); | ||
| StringVal result(ctx, buffer.size()); | ||
| memcpy(result.ptr, buffer.data(), buffer.size()); | ||
| delete src_topn; | ||
| return result; | ||
| } | ||
|
|
||
| StringVal TopNFunctions::topn_finalize(FunctionContext* ctx, const StringVal &src) { | ||
| auto* src_topn = reinterpret_cast<TopNCounter*>(src.ptr); | ||
| std::string result_str; | ||
| src_topn->finalize(result_str); | ||
|
|
||
| StringVal result(ctx, result_str.size()); | ||
| memcpy(result.ptr, result_str.data(), result_str.size()); | ||
|
|
||
| delete src_topn; | ||
| return result; | ||
| } | ||
|
|
||
| template void TopNFunctions::topn_update(FunctionContext*, const BooleanVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const TinyIntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const SmallIntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const BigIntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const FloatVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const DoubleVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const StringVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext*, const DateTimeVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext*, const LargeIntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext*, const DecimalVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext*, const DecimalV2Val&, const IntVal&, StringVal*); | ||
|
|
||
| template void TopNFunctions::topn_update(FunctionContext*, const BooleanVal&, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const TinyIntVal&, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const SmallIntVal&, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const IntVal&, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const BigIntVal&, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const FloatVal&, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const DoubleVal&, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext *, const StringVal&, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext*, const DateTimeVal&, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext*, const LargeIntVal&, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext*, const DecimalVal&, const IntVal&, const IntVal&, StringVal*); | ||
| template void TopNFunctions::topn_update(FunctionContext*, const DecimalV2Val&, const IntVal&, const IntVal&, StringVal*); | ||
|
|
||
| } |
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,47 @@ | ||
| // 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. | ||
|
|
||
| #ifndef DORIS_BE_SRC_EXPRS_TOPN_FUNCTION_H | ||
| #define DORIS_BE_SRC_EXPRS_TOPN_FUNCTION_H | ||
|
|
||
| #include "udf/udf.h" | ||
|
|
||
| namespace doris { | ||
|
|
||
| class TopNFunctions { | ||
| public: | ||
| static void init(); | ||
|
|
||
| static void topn_init(FunctionContext*, StringVal* dst); | ||
|
|
||
| template <typename T> | ||
| static void topn_update(FunctionContext*, const T& src, const IntVal& topn, StringVal* dst); | ||
|
|
||
| template <typename T> | ||
| static void topn_update(FunctionContext*, const T& src, const IntVal& topn, const IntVal& space_expand_rate, | ||
| StringVal* dst); | ||
|
|
||
| static void topn_merge(FunctionContext*,const StringVal& src, StringVal* dst); | ||
|
|
||
| static StringVal topn_serialize(FunctionContext* ctx, const StringVal& src); | ||
|
|
||
| static StringVal topn_finalize(FunctionContext*, const StringVal& src); | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| #endif //DORIS_BE_SRC_EXPRS_TOPN_FUNCTION_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 |
|---|---|---|
|
|
@@ -102,6 +102,7 @@ set(UTIL_FILES | |
| brpc_stub_cache.cpp | ||
| zlib.cpp | ||
| pprof_utils.cpp | ||
| topn_counter.cpp | ||
| ) | ||
|
|
||
| if (WITH_MYSQL) | ||
|
|
||
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,142 @@ | ||
| // 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 <algorithm> | ||
| #include <rapidjson/writer.h> | ||
| #include <rapidjson/stringbuffer.h> | ||
|
|
||
| #include "gen_cpp/olap_common.pb.h" | ||
| #include "topn_counter.h" | ||
| #include "slice.h" | ||
|
|
||
| namespace doris { | ||
|
|
||
| void TopNCounter::add_item(const std::string& item, uint64_t incrementCount) { | ||
| auto iter = _counter_map->find(item); | ||
| if (iter != _counter_map->end()) { | ||
| iter->second.add_count(incrementCount); | ||
| } else { | ||
| _counter_map->insert(std::make_pair(item, Counter(item, incrementCount))); | ||
| } | ||
| _ordered = false; | ||
| } | ||
|
|
||
| void TopNCounter::serialize(std::string* buffer) { | ||
| sort_retain(_capacity); | ||
| PTopNCounter topn_counter; | ||
| topn_counter.set_top_num(_top_num); | ||
| topn_counter.set_space_expand_rate(_space_expand_rate); | ||
| for(std::vector<Counter>::const_iterator it = _counter_vec->begin(); it != _counter_vec->end(); ++it) | ||
| { | ||
| PCounter* counter = topn_counter.add_counter(); | ||
| counter->set_item(it->get_item()); | ||
| counter->set_count(it->get_count()); | ||
| } | ||
| topn_counter.SerializeToString(buffer); | ||
| } | ||
|
|
||
| bool TopNCounter::deserialize(const doris::Slice &src) { | ||
| PTopNCounter topn_counter; | ||
| if (!topn_counter.ParseFromArray(src.data, src.size)) { | ||
| LOG(WARNING) << "topn counter deserialize failed"; | ||
| return false; | ||
| } | ||
|
|
||
| _space_expand_rate = topn_counter.space_expand_rate(); | ||
| set_top_num(topn_counter.top_num()); | ||
| for (int i = 0; i < topn_counter.counter_size(); ++i) { | ||
| const PCounter& counter = topn_counter.counter(i); | ||
| _counter_map->insert(std::make_pair(counter.item(), Counter(counter.item(), counter.count()))); | ||
| _counter_vec->emplace_back(counter.item(), counter.count()); | ||
| } | ||
| _ordered = true; | ||
| return true; | ||
| } | ||
|
|
||
| void TopNCounter::sort_retain(uint32_t capacity) { | ||
| _counter_vec->clear(); | ||
| sort_retain(capacity, _counter_vec); | ||
| _ordered = true; | ||
| } | ||
|
|
||
| void TopNCounter::sort_retain(uint32_t capacity, std::vector<Counter>* sort_vec) { | ||
| for(std::unordered_map<std::string, Counter>::const_iterator it = _counter_map->begin(); it != _counter_map->end(); ++it) { | ||
| sort_vec->emplace_back(it->second.get_item(), it->second.get_count()); | ||
| } | ||
|
|
||
| std::sort(sort_vec->begin(), sort_vec->end(), TopNComparator()); | ||
| if (sort_vec->size() > capacity) { | ||
| for (uint32_t i = 0, n = sort_vec->size() - capacity; i < n; ++i) { | ||
| auto &counter = sort_vec->back(); | ||
| _counter_map->erase(counter.get_item()); | ||
| sort_vec->pop_back(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Based on the parallel version of the Space Saving algorithm as described in: | ||
| // A parallel space saving algorithm for frequent items and the Hurwitz zeta distribution by Massimo Cafaro, et al. | ||
| void TopNCounter::merge(doris::TopNCounter &&other) { | ||
| if (other._counter_map->size() == 0) { | ||
| return; | ||
| } | ||
|
|
||
| _space_expand_rate = other._space_expand_rate; | ||
| set_top_num(other._top_num); | ||
| bool this_full = _counter_map->size() >= _capacity; | ||
| bool another_full = other._counter_map->size() >= other._capacity; | ||
|
|
||
| uint64_t m1 = this_full ? _counter_vec->back().get_count() : 0; | ||
| uint64_t m2 = another_full ? other._counter_vec->back().get_count() : 0; | ||
|
|
||
| if (another_full == true) { | ||
| for (auto &entry : *(this->_counter_map)) { | ||
| entry.second.add_count(m2); | ||
| } | ||
| } | ||
|
|
||
| for (auto &other_entry : *(other._counter_map)) { | ||
| auto itr = this->_counter_map->find(other_entry.first); | ||
| if (itr != _counter_map->end()) { | ||
| itr->second.add_count(other_entry.second.get_count() - m2); | ||
| } else { | ||
| this->_counter_map->insert(std::make_pair(other_entry.first, | ||
| Counter(other_entry.first,other_entry.second.get_count() + m1))); | ||
| } | ||
| } | ||
| _ordered = false; | ||
| sort_retain(_capacity); | ||
| } | ||
|
|
||
| void TopNCounter::finalize(std::string& finalize_str) { | ||
| if (!_ordered) { | ||
| sort_retain(_top_num); | ||
| } | ||
| // use json format print | ||
| rapidjson::StringBuffer buffer; | ||
| rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); | ||
| uint32_t k = 0; | ||
| writer.StartObject(); | ||
| for (std::vector<Counter>::const_iterator it = _counter_vec->begin(); it != _counter_vec->end() && k < _top_num; ++it, ++k) { | ||
| writer.Key(it->get_item().data()); | ||
| writer.Uint64(it->get_count()); | ||
| } | ||
| writer.EndObject(); | ||
| finalize_str = buffer.GetString(); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using JSON format output?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok,done