-
Notifications
You must be signed in to change notification settings - Fork 208
Add DISTINCT_COUNT_APPROX function #3654
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
qianheng-aws
merged 20 commits into
opensearch-project:main
from
xinyual:addDistinctApprox
Jun 10, 2025
Merged
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
07a4200
add approx distinct count
xinyual 08bbb45
add IT
xinyual f17dc66
fix IT name
xinyual 7e211ab
change parser logic
xinyual 4c41cca
apply spotless
xinyual 07e29ed
add doc
xinyual 5ebdb41
add java doc
xinyual c8964f4
fix doc test
xinyual efe9c48
revert useless change
xinyual 361d4d2
add version to doc
xinyual eb3e37d
change to use opensearch core code
xinyual 56dcb3b
remove dependency
xinyual 067b322
Merge remote-tracking branch 'origin/main' into addDistinctApprox
xinyual 04d081a
refactor code to move function to opensearch package
xinyual a3e6a54
refactor code
xinyual e9c0cff
Merge remote-tracking branch 'origin/main' into addDistinctApprox
xinyual 9485929
Merge remote-tracking branch 'origin/main' into addDistinctApprox
xinyual 30228c9
merge from main
xinyual b90e7b8
add Doc and change to concurrency map
xinyual 4f1a0d7
Merge remote-tracking branch 'origin/main' into addDistinctApprox
xinyual 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
51 changes: 51 additions & 0 deletions
51
core/src/main/java/org/opensearch/sql/calcite/udf/udaf/DistinctCountApproxAggFunction.java
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,51 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.udf.udaf; | ||
|
|
||
| import com.google.zetasketch.HyperLogLogPlusPlus; | ||
| import org.opensearch.sql.calcite.udf.UserDefinedAggFunction; | ||
|
|
||
| /** The function use HyperLogLogPlusPlus to count distinct count approximate value */ | ||
| public class DistinctCountApproxAggFunction | ||
| implements UserDefinedAggFunction<DistinctCountApproxAggFunction.HLLAccumulator> { | ||
|
|
||
| @Override | ||
| public HLLAccumulator init() { | ||
| return new HLLAccumulator(); | ||
| } | ||
|
|
||
| @Override | ||
| public Object result(HLLAccumulator accumulator) { | ||
| return accumulator.value(); | ||
| } | ||
|
|
||
| @Override | ||
| public HLLAccumulator add(HLLAccumulator acc, Object... values) { | ||
| for (Object value : values) { | ||
| if (value != null) { | ||
| acc.add(value.toString()); | ||
| } | ||
| } | ||
| return acc; | ||
| } | ||
|
|
||
| public static class HLLAccumulator implements UserDefinedAggFunction.Accumulator { | ||
| private final HyperLogLogPlusPlus<String> hll; | ||
|
|
||
| public HLLAccumulator() { | ||
| this.hll = new HyperLogLogPlusPlus.Builder().buildForStrings(); | ||
| } | ||
|
|
||
| public void add(String value) { | ||
| hll.add(value); | ||
| } | ||
|
|
||
| @Override | ||
| public Object value(Object... args) { | ||
| return hll.result(); | ||
| } | ||
| } | ||
| } |
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
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.
is the HLL library Spark or OpenSearch using? Any other open source project use this library
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.
Spark use its own implementation of HLL. This dependencies is used by Scio, Scala API for Apache Beam:https://github.com/spotify/scio/blob/3cf3f89de339ef7b9421d50cc738d28c62027165/scio-extra/src/main/scala/com/spotify/scio/extra/hll/zetasketch/HllPlus.scala#L23
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.
From the OpenSearch doc https://docs.opensearch.org/docs/latest/aggregations/metric/cardinality/#controlling-precision, the cardinality (count distinct) aggregation uses the HyperLogLog++ algorithm already. So as long as the count distinct is pushed down to DSL, it always is approximate. Please check which HLL library OpenSearch Core is leveraging. we'd better not to introduce a new dependency if core has already had. @xinyual
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.
Got it. I already change the code to use Opensearch core's implementation. Please check it.