-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add datasketches HLL sketch aggregate functions #63143
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
nooneuse
wants to merge
24
commits into
apache:master
Choose a base branch
from
nooneuse:add_datasketches_union_aggregate_functions
base: master
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
24 commits
Select commit
Hold shift + click to select a range
a70254f
add datasketches HLL sketches union aggregate functions for doris
8a277f9
fix typo & compile
nooneuse 89dae1b
add be unit test
nooneuse 216d172
fix corner case & add regression test
nooneuse 2fd6967
revert vcs.xml. This file modification is unnecessary
nooneuse cdf2f9d
change regression test to groovy-out style
nooneuse 894be1a
Merge branch 'master' into add_datasketches_union_aggregate_functions
nooneuse 201ae9c
Merge branch 'master' into add_datasketches_union_aggregate_functions
nooneuse 8d36961
reformat be codes
nooneuse fd97b44
reformat be codes (part2)
nooneuse 03e739c
reformat imports lines
nooneuse 99f59fe
fix submodule build command
nooneuse 09b8b25
fix be ut build script
nooneuse 82b7fc2
fix be ut build script (Part2)
nooneuse b90dc9d
fix be ut build script (Part3)
nooneuse 4d2e209
fix be ut build script (Part4)
nooneuse ace372e
make CMakeList compatible both be & ut
nooneuse d2a11a2
fix build & be ut build script
nooneuse 1e9c55f
improve ut coverage
nooneuse 1b85ce6
reformat new ut functions
nooneuse 1873aec
Merge branch 'master' into add_datasketches_union_aggregate_functions
nooneuse acb64ee
fix review comments 1
nooneuse 42bab33
fix review comments 2
nooneuse 661002c
remove new aggregation type (which is no use)
nooneuse 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
44 changes: 44 additions & 0 deletions
44
be/src/exprs/aggregate/aggregate_function_datasketches_hll_union_agg.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,44 @@ | ||
| // 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/aggregate/aggregate_function_datasketches_hll_union_agg.h" | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "core/data_type/data_type.h" | ||
| #include "core/data_type/define_primitive_type.h" | ||
| #include "exec/common/hash_table/hash.h" // IWYU pragma: keep | ||
| #include "exprs/aggregate/aggregate_function_simple_factory.h" | ||
| #include "exprs/aggregate/helpers.h" | ||
| namespace doris { | ||
| template <template <PrimitiveType> class Data> | ||
| AggregateFunctionPtr create_aggregate_function_datasketches_hll_union_agg( | ||
| const std::string& name, const DataTypes& argument_types, const DataTypePtr& result_type, | ||
| const bool result_is_nullable, const AggregateFunctionAttr& attr) { | ||
| return creator_with_type_list<TYPE_STRING, TYPE_VARCHAR, TYPE_BINARY, TYPE_VARBINARY>::create< | ||
| AggregateFunctionDataSketchesHllUnionAgg, Data>(argument_types, result_is_nullable, | ||
| attr); | ||
| } | ||
| void register_aggregate_function_datasketches_HLL_union_agg( | ||
| AggregateFunctionSimpleFactory& factory) { | ||
| AggregateFunctionCreator creator = | ||
| create_aggregate_function_datasketches_hll_union_agg<AggregateFunctionHllSketchData>; | ||
| factory.register_function_both("datasketches_hll_union_agg", creator); | ||
| factory.register_alias("datasketches_hll_union_agg", "ds_hll_union_count"); | ||
| factory.register_alias("datasketches_hll_union_agg", "ds_cardinality"); | ||
| } | ||
| } // namespace doris |
181 changes: 181 additions & 0 deletions
181
be/src/exprs/aggregate/aggregate_function_datasketches_hll_union_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,181 @@ | ||
| // 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. | ||
|
|
||
| #pragma once | ||
| #include <stddef.h> | ||
|
|
||
| #include <DataSketches/hll.hpp> | ||
| #include <boost/iterator/iterator_facade.hpp> | ||
| #include <memory> | ||
| #include <type_traits> | ||
| #include <vector> | ||
|
|
||
| #include "common/compiler_util.h" // IWYU pragma: keep | ||
| #include "core/assert_cast.h" | ||
| #include "core/column/column.h" | ||
| #include "core/column/column_vector.h" | ||
| #include "core/data_type/data_type_number.h" | ||
| #include "core/data_type/define_primitive_type.h" | ||
| #include "core/field.h" | ||
| #include "core/string_ref.h" | ||
| #include "core/types.h" | ||
| #include "core/uint128.h" | ||
| #include "exec/common/hash_table/hash.h" | ||
| #include "exec/common/hash_table/phmap_fwd_decl.h" | ||
| #include "exprs/aggregate/aggregate_function.h" | ||
| #include "util/var_int.h" | ||
| template <typename T> | ||
| struct HashCRC32; | ||
| namespace doris { | ||
| class Arena; | ||
| class BufferReadable; | ||
| class BufferWritable; | ||
| template <PrimitiveType T> | ||
| class ColumnDecimal; | ||
| /// datasketches_hll_union_agg | ||
| template <PrimitiveType T> | ||
| struct AggregateFunctionHllSketchData { | ||
| /** We set the default LgK to 12, | ||
| * as this value is used as a performance baseline in the relevant documentation. | ||
| * (https://datasketches.apache.org/docs/HLL/HllPerformance.html) | ||
| */ | ||
| static const uint8_t DEFAULT_LOG_K = 12; | ||
| std::unique_ptr<datasketches::hll_union> hll_union_data; | ||
| static String get_name() { return "datasketches_hll_union_agg"; } | ||
| void merge(const datasketches::hll_sketch& sketch_data) { | ||
| if (hll_union_data == nullptr) { | ||
| hll_union_data = | ||
| std::make_unique<datasketches::hll_union>(sketch_data.get_lg_config_k()); | ||
| } | ||
| try { | ||
| hll_union_data->update(sketch_data); | ||
| } catch (...) { | ||
| throw Exception(ErrorCode::INTERNAL_ERROR, | ||
| "Internal error happened when update HLL sketch."); | ||
| } | ||
| } | ||
| void reset() { | ||
| if (hll_union_data != nullptr) { | ||
| hll_union_data->reset(); | ||
| } | ||
| hll_union_data = nullptr; | ||
| } | ||
| void write_sketch(BufferWritable& buf, const datasketches::hll_sketch& sk) const { | ||
| auto serialized_bytes = sk.serialize_compact(); | ||
| StringRef d(serialized_bytes.data(), serialized_bytes.size()); | ||
| buf.write_binary(d); | ||
| } | ||
| void write(BufferWritable& buf) const { | ||
| if (hll_union_data == nullptr) { | ||
| /** Using DEFAULT_LOG_K(12) here is surely sufficient, | ||
| * because in this case the union that actually needs to be serialized should contain no data. | ||
| */ | ||
| datasketches::hll_union u(DEFAULT_LOG_K); | ||
| write_sketch(buf, u.get_result()); | ||
| return; | ||
| } | ||
| try { | ||
| auto cache = hll_union_data->get_result(); | ||
| write_sketch(buf, cache); | ||
| } catch (...) { | ||
| throw Exception(ErrorCode::INTERNAL_ERROR, | ||
| "Internal error happened when serialize HLL sketch."); | ||
| } | ||
| } | ||
| void read(BufferReadable& buf) { | ||
| StringRef d; | ||
| buf.read_binary(d); | ||
| try { | ||
| auto cache = datasketches::hll_sketch::deserialize(d.data, d.size); | ||
| merge(cache); | ||
| } catch (...) { | ||
| throw Exception(ErrorCode::CORRUPTION, "HLL sketch data corrupted when read."); | ||
| } | ||
| } | ||
| int64_t get_result() const { | ||
| if (hll_union_data != nullptr) { | ||
| try { | ||
| return static_cast<int64_t>(hll_union_data->get_estimate()); | ||
| } catch (...) { | ||
| throw Exception(ErrorCode::INTERNAL_ERROR, | ||
| "Internal error happened when get HLL sketch estimate."); | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
| }; | ||
| namespace detail { | ||
| /** The structure for the delegation work to add one element to the `datasketches_hll_union_agg` aggregate functions. | ||
| * Used for partial specialization to add strings. | ||
| */ | ||
| template <PrimitiveType T, typename Data> | ||
| struct OneAdder { | ||
| static void ALWAYS_INLINE add(Data& data, const IColumn& column, size_t row_num) { | ||
| if constexpr (is_string_type(T) || is_varbinary(T)) { | ||
| StringRef value = column.get_data_at(row_num); | ||
| if (value.empty()) { | ||
| return; | ||
| } | ||
| try { | ||
| datasketches::hll_sketch sketch_data = | ||
| datasketches::hll_sketch::deserialize(value.begin(), value.size); | ||
| data.merge(sketch_data); | ||
| } catch (...) { | ||
| throw Exception(ErrorCode::CORRUPTION, "HLL sketch data corrupted when add."); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } // namespace detail | ||
| /// Calculates the number of different values approximately using hll sketch. | ||
| template <PrimitiveType T, typename Data> | ||
| class AggregateFunctionDataSketchesHllUnionAgg final | ||
| : public IAggregateFunctionDataHelper<Data, | ||
| AggregateFunctionDataSketchesHllUnionAgg<T, Data>>, | ||
| VarargsExpression, | ||
| NotNullableAggregateFunction { | ||
| public: | ||
| AggregateFunctionDataSketchesHllUnionAgg(const DataTypes& argument_types_) | ||
| : IAggregateFunctionDataHelper<Data, AggregateFunctionDataSketchesHllUnionAgg<T, Data>>( | ||
| argument_types_) {} | ||
| String get_name() const override { return Data::get_name(); } | ||
| DataTypePtr get_return_type() const override { return std::make_shared<DataTypeInt64>(); } | ||
| void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); } | ||
| void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num, | ||
| Arena&) const override { | ||
| detail::OneAdder<T, Data>::add(this->data(place), *columns[0], row_num); | ||
| } | ||
| void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, | ||
| Arena&) const override { | ||
| const auto& rhs_data = this->data(rhs); | ||
| if (rhs_data.hll_union_data == nullptr) { | ||
| return; | ||
| } | ||
| this->data(place).merge(rhs_data.hll_union_data->get_result(datasketches::HLL_8)); | ||
| } | ||
| void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { | ||
| this->data(place).write(buf); | ||
| } | ||
| void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, | ||
| Arena&) const override { | ||
| this->data(place).read(buf); | ||
| } | ||
| void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { | ||
| assert_cast<ColumnInt64&>(to).get_data().push_back(this->data(place).get_result()); | ||
| } | ||
| }; | ||
| } // namespace doris |
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
Oops, something went wrong.
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.
I directly pointed the submodule to the Apache DataSketches GitHub repository. Later, if needed, we can consider adding DataSketches to the doris-thirdparty repository.