Skip to content
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

[Enhancement] Support configurable hll_sketch and optimize hll_sketch performance (backport #48939) #50008

Closed
wants to merge 1 commit into from

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Aug 20, 2024

Why I'm doing:

HLL_SKETCH is very useful for high precision ndv compute. The PR(#20836) has supported hll_sketch aggregation, but there are some limitions:

  1. HLLSketch's precision and hll_type cannot be configurable for different scences.
  2. There is a performance throttle for high cardinality compute in merge:
+   88.79%     0.00%  pip_wg_executor  [.] starrocks::Thread::supervise_thread
+   88.79%     0.00%  pip_wg_executor  [.] starrocks::ThreadPool::dispatch_thread
+   88.79%     0.00%  pip_wg_executor  [.] starrocks::pipeline::GlobalDriverExecutor::_worker_thread
+   88.79%     0.00%  pip_wg_executor  [.] starrocks::pipeline::PipelineDriver::process                                                                                                                                                                                                                                                                           ▒
+   88.79%     0.00%  pip_wg_executor  [.] starrocks::pipeline::AggregateBlockingSinkOperator::push_chunk
-   88.79%     0.00%  pip_wg_executor  [.] starrocks::Aggregator::compute_batch_agg_states
     starrocks::Aggregator::compute_batch_agg_states                                                                                                                                                                                                                                                                                                              ▒
   - starrocks::NullableAggregateFunctionUnary<std::shared_ptr<starrocks::AggregateFunction>, starrocks::NullableAggregateFunctionState<starrocks::DataSketchesHll, false>, false, true, starrocks::AggNonNullPred<starrocks::DataSketchesHll> >::merge_batch                                                                                                     ▒
      - 88.77% starrocks::HllSketchAggregateFunction<(starrocks::LogicalType)17, starrocks::Slice>::merge                                                                                                                                                                                                                                                         ▒
         + 88.76% starrocks::DataSketchesHll::merge                                                                                                                                                                                                                                                                                                               ▒
-   88.79%     0.00%  pip_wg_executor  [.] starrocks::NullableAggregateFunctionUnary<std::shared_ptr<starrocks::AggregateFunction>, starrocks::NullableAggregateFunctionState<starrocks::DataSketchesHll, false>, false, true, starrocks::AggNonNullPred<starrocks::DataSketchesHll> >::merge_batch                                                               ▒
   - starrocks::NullableAggregateFunctionUnary<std::shared_ptr<starrocks::AggregateFunction>, starrocks::NullableAggregateFunctionState<starrocks::DataSketchesHll, false>, false, true, starrocks::AggNonNullPred<starrocks::DataSketchesHll> >::merge_batch                                                                                                     ▒
      - 88.77% starrocks::HllSketchAggregateFunction<(starrocks::LogicalType)17, starrocks::Slice>::merge                                                                                                                                                                                                                                                         ▒
         - 88.76% starrocks::DataSketchesHll::merge                                                                                                                                                                                                                                                                                                               ▒
            - 44.77% datasketches::hll_union_alloc<std::allocator<unsigned char> >::union_impl                                                                                                                                                                                                                                                                    ▒
               - datasketches::HllArray<std::allocator<unsigned char> >::copyAs                                                                                                                                                                                                                                                                                   ▒
                    31.51% datasketches::Hll8Array<std::allocator<unsigned char> >::mergeHll                                                                                                                                                                                                                                                                      ▒
                    12.78% datasketches::HllArray<std::allocator<unsigned char> >::hipAndKxQIncrementalUpdate                                                                                                                                                                                                                                                     ▒
            - 41.25% datasketches::HllSketchImplFactory<std::allocator<unsigned char> >::convertToHll6                                                                                                                                                                                                                                                            ▒
                 10.63% datasketches::Hll6Array<std::allocator<unsigned char> >::internalCouponUpdate                                                                                                                                                                                                                                                             ▒
                 9.18% datasketches::HllArray<std::allocator<unsigned char> >::hipAndKxQIncrementalUpdate                                                                                                                                                                                                                                                         ▒
              1.77% datasketches::Hll6Array<std::allocator<unsigned char> >::internalCouponUpdate                                                                                                                                                                                                                                                                 ▒
              0.93% __memmove_evex_unaligned_erms                                                                                                                                                                                                                                                                                                                 ▒
+   88.77%     0.00%  pip_wg_executor  [.] starrocks::HllSketchAggregateFunction<(starrocks::LogicalType)17, starrocks::Slice>::merge                                                                                                                                                                                                                             ▒
+   88.77%     0.00%  pip_wg_executor  [.] starrocks::DataSketchesHll::merge                                                                                                                                                                                                                                                                                      ◆
+   41.56%    20.98%  pip_wg_executor  [.] datasketches::HllSketchImplFactory<std::allocator<unsigned char> >::convertToHll6                                                                                                                                                                                                                                      ▒
+   21.96%    20.54%  pip_wg_executor  [.] datasketches::HllArray<std::allocator<unsigned char> >::hipAndKxQIncrementalUpdate                                                                                                                                                                                                                                     ▒
+   12.71%    12.39%  pip_wg_executor  [.] datasketches::Hll6Array<std::allocator<unsigned char> >::internalCouponUpdate

merge function is heavy since it will copy hllsketch into hll_union each call:

void DataSketchesHll::merge(const DataSketchesHll& other) {
    datasketches::hll_union u(MAX_HLL_LOG_K);
    u.update(_sketch);
    u.update(other._sketch);
    _sketch = u.get_result(HLL_TGT_TYPE);
}

What I'm doing:

  • Add an alias name ds_hll_count_distinct to be better syntax meanings(original function name is also kept either)
  • Support configurable hll_sketch with different precision and hll_type:
    image
  • Optimize merge performance by using hll_union as state rather than hllsketch to avoid copying in merge call.
aggregate mode Tables After(ms) Before(ms)
force_streaming t1 7377 8930
force_streaming t2 7660 9526
force_streaming t3 13425 timeout(5min)
force_streaming t4 7830 16568
auto t1 4821 4626
auto t2 7880 8834
auto t3 1366 1546
auto t4 784 797
Fixes [#issue](https://github.com//issues/49000)

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

This is an automatic backport of pull request #48939 done by [Mergify](https://mergify.com). ## Why I'm doing: `HLL_SKETCH` is very useful for high precision ndv compute. The PR(https://github.com//pull/20836) has supported hll_sketch aggregation, but there are some limitions: 1. HLLSketch's `precision` and `hll_type` cannot be configurable for different scences. 2. There is a performance throttle for high cardinality compute in `merge`:
+   88.79%     0.00%  pip_wg_executor  [.] starrocks::Thread::supervise_thread
+   88.79%     0.00%  pip_wg_executor  [.] starrocks::ThreadPool::dispatch_thread
+   88.79%     0.00%  pip_wg_executor  [.] starrocks::pipeline::GlobalDriverExecutor::_worker_thread
+   88.79%     0.00%  pip_wg_executor  [.] starrocks::pipeline::PipelineDriver::process                                                                                                                                                                                                                                                                           ▒
+   88.79%     0.00%  pip_wg_executor  [.] starrocks::pipeline::AggregateBlockingSinkOperator::push_chunk
-   88.79%     0.00%  pip_wg_executor  [.] starrocks::Aggregator::compute_batch_agg_states
     starrocks::Aggregator::compute_batch_agg_states                                                                                                                                                                                                                                                                                                              ▒
   - starrocks::NullableAggregateFunctionUnary<std::shared_ptr<starrocks::AggregateFunction>, starrocks::NullableAggregateFunctionState<starrocks::DataSketchesHll, false>, false, true, starrocks::AggNonNullPred<starrocks::DataSketchesHll> >::merge_batch                                                                                                     ▒
      - 88.77% starrocks::HllSketchAggregateFunction<(starrocks::LogicalType)17, starrocks::Slice>::merge                                                                                                                                                                                                                                                         ▒
         + 88.76% starrocks::DataSketchesHll::merge                                                                                                                                                                                                                                                                                                               ▒
-   88.79%     0.00%  pip_wg_executor  [.] starrocks::NullableAggregateFunctionUnary<std::shared_ptr<starrocks::AggregateFunction>, starrocks::NullableAggregateFunctionState<starrocks::DataSketchesHll, false>, false, true, starrocks::AggNonNullPred<starrocks::DataSketchesHll> >::merge_batch                                                               ▒
   - starrocks::NullableAggregateFunctionUnary<std::shared_ptr<starrocks::AggregateFunction>, starrocks::NullableAggregateFunctionState<starrocks::DataSketchesHll, false>, false, true, starrocks::AggNonNullPred<starrocks::DataSketchesHll> >::merge_batch                                                                                                     ▒
      - 88.77% starrocks::HllSketchAggregateFunction<(starrocks::LogicalType)17, starrocks::Slice>::merge                                                                                                                                                                                                                                                         ▒
         - 88.76% starrocks::DataSketchesHll::merge                                                                                                                                                                                                                                                                                                               ▒
            - 44.77% datasketches::hll_union_alloc<std::allocator<unsigned char> >::union_impl                                                                                                                                                                                                                                                                    ▒
               - datasketches::HllArray<std::allocator<unsigned char> >::copyAs                                                                                                                                                                                                                                                                                   ▒
                    31.51% datasketches::Hll8Array<std::allocator<unsigned char> >::mergeHll                                                                                                                                                                                                                                                                      ▒
                    12.78% datasketches::HllArray<std::allocator<unsigned char> >::hipAndKxQIncrementalUpdate                                                                                                                                                                                                                                                     ▒
            - 41.25% datasketches::HllSketchImplFactory<std::allocator<unsigned char> >::convertToHll6                                                                                                                                                                                                                                                            ▒
                 10.63% datasketches::Hll6Array<std::allocator<unsigned char> >::internalCouponUpdate                                                                                                                                                                                                                                                             ▒
                 9.18% datasketches::HllArray<std::allocator<unsigned char> >::hipAndKxQIncrementalUpdate                                                                                                                                                                                                                                                         ▒
              1.77% datasketches::Hll6Array<std::allocator<unsigned char> >::internalCouponUpdate                                                                                                                                                                                                                                                                 ▒
              0.93% __memmove_evex_unaligned_erms                                                                                                                                                                                                                                                                                                                 ▒
+   88.77%     0.00%  pip_wg_executor  [.] starrocks::HllSketchAggregateFunction<(starrocks::LogicalType)17, starrocks::Slice>::merge                                                                                                                                                                                                                             ▒
+   88.77%     0.00%  pip_wg_executor  [.] starrocks::DataSketchesHll::merge                                                                                                                                                                                                                                                                                      ◆
+   41.56%    20.98%  pip_wg_executor  [.] datasketches::HllSketchImplFactory<std::allocator<unsigned char> >::convertToHll6                                                                                                                                                                                                                                      ▒
+   21.96%    20.54%  pip_wg_executor  [.] datasketches::HllArray<std::allocator<unsigned char> >::hipAndKxQIncrementalUpdate                                                                                                                                                                                                                                     ▒
+   12.71%    12.39%  pip_wg_executor  [.] datasketches::Hll6Array<std::allocator<unsigned char> >::internalCouponUpdate

merge function is heavy since it will copy hllsketch into hll_union each call:

void DataSketchesHll::merge(const DataSketchesHll& other) {
    datasketches::hll_union u(MAX_HLL_LOG_K);
    u.update(_sketch);
    u.update(other._sketch);
    _sketch = u.get_result(HLL_TGT_TYPE);
}

What I'm doing:

  • Add an alias name ds_hll_count_distinct to be better syntax meanings(original function name is also kept either)
  • Support configurable hll_sketch with different precision and hll_type:
    image
  • Optimize merge performance by using hll_union as state rather than hllsketch to avoid copying in merge call.
aggregate mode Tables After(ms) Before(ms)
force_streaming t1 7377 8930
force_streaming t2 7660 9526
force_streaming t3 13425 timeout(5min)
force_streaming t4 7830 16568
auto t1 4821 4626
auto t2 7880 8834
auto t3 1366 1546
auto t4 784 797


Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

… performance (#48939)

Signed-off-by: shuming.li <[email protected]>
(cherry picked from commit 89a6b77)

# Conflicts:
#	be/src/exprs/agg/factory/aggregate_resolver_approx.cpp
#	be/src/exprs/agg/hll_sketch.h
#	be/src/types/constexpr.h
#	be/src/types/hll.cpp
#	be/src/types/hll.h
#	fe/fe-core/src/main/java/com/starrocks/catalog/FunctionSet.java
#	fe/fe-core/src/main/java/com/starrocks/sql/analyzer/FunctionAnalyzer.java
#	fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rule/tree/PreAggregateTurnOnRule.java
@mergify mergify bot added the conflicts label Aug 20, 2024
Copy link
Contributor Author

mergify bot commented Aug 20, 2024

Cherry-pick of 89a6b77 has failed:

On branch mergify/bp/branch-3.3/pr-48939
Your branch is up to date with 'origin/branch-3.3'.

You are currently cherry-picking commit 89a6b7741a.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   fe/fe-core/src/main/java/com/starrocks/sql/parser/SyntaxSugars.java
	new file:   test/sql/test_agg_function/R/test_hll_sketch_count.sql
	new file:   test/sql/test_agg_function/T/test_hll_sketch_count.sql

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)
	both modified:   be/src/exprs/agg/factory/aggregate_resolver_approx.cpp
	deleted by us:   be/src/exprs/agg/hll_sketch.h
	both modified:   be/src/types/constexpr.h
	both modified:   be/src/types/hll.cpp
	both modified:   be/src/types/hll.h
	both modified:   fe/fe-core/src/main/java/com/starrocks/catalog/FunctionSet.java
	both modified:   fe/fe-core/src/main/java/com/starrocks/sql/analyzer/FunctionAnalyzer.java
	both modified:   fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rule/tree/PreAggregateTurnOnRule.java

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

Copy link
Contributor Author

mergify bot commented Aug 20, 2024

@mergify[bot]: Backport conflict, please reslove the conflict and resubmit the pr

@mergify mergify bot deleted the mergify/bp/branch-3.3/pr-48939 branch August 20, 2024 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant