-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(native): Implement Sketch Theta aggregate and scalar functions #25685
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
nmahadevuni
wants to merge
1
commit into
prestodb:master
Choose a base branch
from
nmahadevuni:theta_sketch_native_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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| ******************** | ||
| Presto C++ Functions | ||
| ******************** | ||
|
|
||
| .. toctree:: | ||
| :maxdepth: 1 | ||
|
|
||
| functions/sketch.rst |
38 changes: 38 additions & 0 deletions
38
presto-docs/src/main/sphinx/presto_cpp/functions/sketch.rst
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,38 @@ | ||
| ================ | ||
| Sketch Functions | ||
| ================ | ||
|
|
||
| Sketches are data structures that can approximately answer particular questions | ||
| about a dataset when full accuracy is not required. Approximate answers are | ||
| often faster and more efficient to compute than functions which result in full | ||
| accuracy. | ||
|
|
||
| Presto C++ provides support for computing some sketches available in the `Apache | ||
| DataSketches`_ library. | ||
|
|
||
| Theta Sketches | ||
| -------------- | ||
|
|
||
| Theta sketches enable distinct value counting on datasets and also provide the | ||
| ability to perform set operations. For more information on Theta sketches, | ||
| please see the Apache Datasketches `Theta sketch documentation`_. | ||
|
|
||
| .. function:: sketch_theta(x) -> varbinary | ||
|
|
||
| Computes a theta sketch from an input dataset. The output from | ||
| this function can be used as an input to any of the other ``sketch_theta_*`` | ||
| family of functions. | ||
|
|
||
| .. function:: sketch_theta_estimate(sketch) -> double | ||
|
|
||
| Returns the estimate of distinct values from the input sketch. | ||
|
|
||
| .. function:: sketch_theta_summary(sketch) -> row(estimate double, theta double, upper_bound_std double, lower_bound_std double, retained_entries int) | ||
|
|
||
| Returns a summary of the input sketch which includes the distinct values | ||
| estimate alongside other useful information such as the sketch theta | ||
| parameter, current error bounds corresponding to 1 standard deviation, and | ||
| the number of retained entries in the sketch. | ||
|
|
||
| .. _Apache DataSketches: https://datasketches.apache.org/ | ||
| .. _Theta sketch documentation: https://datasketches.apache.org/docs/Theta/ThetaSketches.html#theta-sketch-framework |
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
17 changes: 17 additions & 0 deletions
17
presto-native-execution/presto_cpp/main/functions/theta_sketch/CMakeLists.txt
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,17 @@ | ||
| # 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. | ||
|
|
||
| add_library(presto_theta_sketch_functions ThetaSketchAggregate.cpp ThetaSketchFunctions.cpp) | ||
|
|
||
| if(PRESTO_ENABLE_TESTING) | ||
| add_subdirectory(tests) | ||
| endif() |
255 changes: 255 additions & 0 deletions
255
presto-native-execution/presto_cpp/main/functions/theta_sketch/ThetaSketchAggregate.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,255 @@ | ||
| /* | ||
| * 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 "presto_cpp/main/functions/theta_sketch/ThetaSketchRegistration.h" | ||
|
|
||
| #include "DataSketches/theta_sketch.hpp" | ||
| #include "DataSketches/theta_union.hpp" | ||
|
|
||
| #include "velox/exec/Aggregate.h" | ||
| #include "velox/exec/SimpleAggregateAdapter.h" | ||
| #include "velox/functions/prestosql/aggregates/AggregateNames.h" | ||
| #include "velox/type/HugeInt.h" | ||
|
|
||
| namespace facebook::presto::functions::aggregate { | ||
|
|
||
| namespace { | ||
|
|
||
| const char* const kThetaSketch = "sketch_theta"; | ||
|
|
||
| template <typename T> | ||
| class ThetaSketchAggregate { | ||
| public: | ||
| // Type(s) of input vector(s) wrapped in Row. | ||
| using InputType = velox::Row<T>; | ||
|
|
||
| // Type of intermediate result | ||
| using IntermediateType = velox::Varbinary; | ||
|
|
||
| // Type of output vector. | ||
| using OutputType = velox::Varbinary; | ||
|
|
||
| static constexpr bool default_null_behavior_ = false; | ||
|
|
||
| static bool toIntermediate( | ||
| velox::exec::out_type<IntermediateType>& out, | ||
| velox::exec::optional_arg_type<T> in) { | ||
| if (in.has_value()) { | ||
| auto updateSketch = datasketches::update_theta_sketch::builder().build(); | ||
| if constexpr (std::is_same_v<T, velox::int128_t>) { | ||
| updateSketch.update(std::to_string(in.value())); | ||
| } else if constexpr ( | ||
| std::is_same_v<T, std::string> || | ||
| std::is_same_v<T, velox::Varbinary>) { | ||
| const auto& strView = in.value(); | ||
| updateSketch.update(std::string(strView.data(), strView.size())); | ||
| } else { | ||
| updateSketch.update(in.value()); | ||
| } | ||
| datasketches::theta_union thetaUnion = | ||
| datasketches::theta_union::builder().build(); | ||
| thetaUnion.update(updateSketch); | ||
| auto compactSketch = thetaUnion.get_result(); | ||
| out.resize(compactSketch.get_serialized_size_bytes()); | ||
| auto serializedBytes = compactSketch.serialize(); | ||
| std::memcpy(out.data(), serializedBytes.data(), out.size()); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| struct AccumulatorType { | ||
| datasketches::theta_union thetaUnion = | ||
| datasketches::theta_union::builder().build(); | ||
| datasketches::update_theta_sketch updateSketch = | ||
| datasketches::update_theta_sketch::builder().build(); | ||
|
|
||
| AccumulatorType() = delete; | ||
|
|
||
| // Constructor used in initializeNewGroups(). | ||
| explicit AccumulatorType( | ||
| velox::HashStringAllocator* /*allocator*/, | ||
| ThetaSketchAggregate* /*fn*/) {} | ||
|
|
||
| void updateUnion() { | ||
| thetaUnion.update(updateSketch); | ||
| updateSketch.reset(); | ||
| } | ||
|
|
||
| // addInput expects one parameter of exec::arg_type<T> for each child-type T | ||
| // wrapped in InputType. | ||
| bool addInput( | ||
| velox::HashStringAllocator* /*allocator*/, | ||
| velox::exec::optional_arg_type<T> data) { | ||
| if (data.has_value()) { | ||
| if constexpr (std::is_same_v<T, velox::int128_t>) { | ||
| updateSketch.update(std::to_string(data.value())); | ||
| } else if constexpr ( | ||
| std::is_same_v<T, std::string> || | ||
| std::is_same_v<T, velox::Varbinary>) { | ||
| const auto& strView = data.value(); | ||
| updateSketch.update(std::string(strView.data(), strView.size())); | ||
| } else { | ||
| updateSketch.update(data.value()); | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // combine expects one parameter of exec::arg_type<IntermediateType>. | ||
| bool combine( | ||
| velox::HashStringAllocator* /*allocator*/, | ||
| velox::exec::optional_arg_type<velox::Varbinary> other) { | ||
| if (other.has_value()) { | ||
| updateUnion(); | ||
| auto compactSketch = datasketches::wrapped_compact_theta_sketch::wrap( | ||
| other->data(), other->size()); | ||
| thetaUnion.update(compactSketch); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool getResult(velox::exec::out_type<velox::Varbinary>& out) { | ||
| updateUnion(); | ||
| auto compactSketch = thetaUnion.get_result(); | ||
| out.resize(compactSketch.get_serialized_size_bytes()); | ||
| auto serializedBytes = compactSketch.serialize(); | ||
| std::memcpy(out.data(), serializedBytes.data(), out.size()); | ||
| return true; | ||
| } | ||
|
|
||
| bool writeFinalResult( | ||
| bool nonNullGroup, | ||
| velox::exec::out_type<velox::Varbinary>& out) { | ||
| return getResult(out); | ||
| } | ||
|
|
||
| bool writeIntermediateResult( | ||
| bool nonNullGroup, | ||
| velox::exec::out_type<velox::Varbinary>& out) { | ||
| return getResult(out); | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| velox::exec::AggregateRegistrationResult registerThetaSketchAggregate( | ||
| const std::string& prefix, | ||
| bool withCompanionFunctions, | ||
| bool overwrite) { | ||
| std::vector<std::shared_ptr<velox::exec::AggregateFunctionSignature>> | ||
| signatures; | ||
| std::string returnType = "varbinary"; | ||
| std::string intermediateType = "varbinary"; | ||
|
|
||
| for (const auto& inputType : | ||
| {"tinyint", | ||
| "smallint", | ||
| "integer", | ||
| "bigint", | ||
| "hugeint", | ||
| "real", | ||
| "double", | ||
| "varchar", | ||
| "date", | ||
| "timestamp"}) { | ||
| signatures.push_back( | ||
| velox::exec::AggregateFunctionSignatureBuilder() | ||
| .returnType(returnType) | ||
| .intermediateType(intermediateType) | ||
| .argumentType(inputType) | ||
| .build()); | ||
| } | ||
| signatures.push_back( | ||
| velox::exec::AggregateFunctionSignatureBuilder() | ||
| .integerVariable("a_precision") | ||
| .integerVariable("a_scale") | ||
| .returnType(returnType) | ||
| .intermediateType(intermediateType) | ||
| .argumentType("DECIMAL(a_precision, a_scale)") | ||
| .build()); | ||
| signatures.push_back( | ||
| velox::exec::AggregateFunctionSignatureBuilder() | ||
| .integerVariable("a_precision") | ||
| .integerVariable("a_scale") | ||
| .returnType(returnType) | ||
| .intermediateType(intermediateType) | ||
| .argumentType("DECIMAL(a_precision, a_scale)") | ||
| .argumentType("double") | ||
| .build()); | ||
|
|
||
| auto name = prefix + kThetaSketch; | ||
|
|
||
| return velox::exec::registerAggregateFunction( | ||
| name, | ||
| std::move(signatures), | ||
| [name]( | ||
| velox::core::AggregationNode::Step step, | ||
| const std::vector<velox::TypePtr>& argTypes, | ||
| const velox::TypePtr& resultType, | ||
| const velox::core::QueryConfig& /*config*/) | ||
| -> std::unique_ptr<velox::exec::Aggregate> { | ||
| VELOX_CHECK_LE( | ||
| argTypes.size(), 1, "{} takes at most one argument", name); | ||
| auto inputType = argTypes[0]; | ||
| if (velox::exec::isRawInput(step)) { | ||
| switch (inputType->kind()) { | ||
|
Contributor
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. The formating of the lines seems off... PTAL. |
||
| case velox::TypeKind::TINYINT: | ||
| return std::make_unique<velox::exec::SimpleAggregateAdapter< | ||
| ThetaSketchAggregate<int8_t>>>(step, argTypes, resultType); | ||
| case velox::TypeKind::SMALLINT: | ||
| return std::make_unique<velox::exec::SimpleAggregateAdapter< | ||
| ThetaSketchAggregate<int16_t>>>(step, argTypes, resultType); | ||
| case velox::TypeKind::INTEGER: | ||
| return std::make_unique<velox::exec::SimpleAggregateAdapter< | ||
| ThetaSketchAggregate<int32_t>>>(step, argTypes, resultType); | ||
| case velox::TypeKind::BIGINT: | ||
| return std::make_unique<velox::exec::SimpleAggregateAdapter< | ||
| ThetaSketchAggregate<int64_t>>>(step, argTypes, resultType); | ||
| case velox::TypeKind::HUGEINT: | ||
| return std::make_unique<velox::exec::SimpleAggregateAdapter< | ||
| ThetaSketchAggregate<velox::int128_t>>>( | ||
| step, argTypes, resultType); | ||
| case velox::TypeKind::REAL: | ||
| return std::make_unique<velox::exec::SimpleAggregateAdapter< | ||
| ThetaSketchAggregate<float>>>(step, argTypes, resultType); | ||
| case velox::TypeKind::DOUBLE: | ||
| return std::make_unique<velox::exec::SimpleAggregateAdapter< | ||
| ThetaSketchAggregate<double>>>(step, argTypes, resultType); | ||
| case velox::TypeKind::VARCHAR: | ||
| return std::make_unique<velox::exec::SimpleAggregateAdapter< | ||
| ThetaSketchAggregate<std::string>>>( | ||
| step, argTypes, resultType); | ||
| case velox::TypeKind::TIMESTAMP: | ||
| return std::make_unique<velox::exec::SimpleAggregateAdapter< | ||
| ThetaSketchAggregate<typename velox::TypeTraits< | ||
| velox::TypeKind::TIMESTAMP>::NativeType>>>( | ||
| step, argTypes, resultType); | ||
| default: | ||
| VELOX_FAIL( | ||
| "Unknown input type for {} aggregation {}", | ||
| name, | ||
| inputType->kindName()); | ||
| } | ||
| } else { | ||
| return std::make_unique<velox::exec::SimpleAggregateAdapter< | ||
| ThetaSketchAggregate<velox::Varbinary>>>( | ||
| step, argTypes, resultType); | ||
| } | ||
| }, | ||
| withCompanionFunctions, | ||
| overwrite); | ||
| } | ||
|
|
||
| } // namespace facebook::presto::functions::aggregate | ||
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.
This code seems exactly as writeIntermediateResult. Can you abstract a common function for it ?