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

[Feature] support Quantile Sketche #54569

Open
wants to merge 53 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
d4fa445
support dataSketches include Quantile Sketches、Theta Sketch、Frequency…
chenminghua8 Oct 22, 2024
cf082a9
fix format exception.
chenminghua8 Oct 23, 2024
2540ae9
fix format exception.
chenminghua8 Oct 24, 2024
02dc2b0
fix format exception.
chenminghua8 Oct 24, 2024
358b652
fix format exception.
chenminghua8 Oct 24, 2024
60f073e
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 Oct 24, 2024
01a85d0
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 Nov 24, 2024
6aff6a7
fix AggStateCombinatorTest fault
chenminghua8 Nov 24, 2024
c573071
fix AggStateCombinatorTest fault
chenminghua8 Nov 25, 2024
51e8596
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 Nov 25, 2024
accf967
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 Nov 26, 2024
e0257e9
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 Dec 2, 2024
38b153e
add tests
chenminghua8 Dec 2, 2024
19d9389
add tests
chenminghua8 Dec 4, 2024
793cc6c
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 Dec 4, 2024
8d337aa
Add test cases to improve code coverage
chenminghua8 Dec 4, 2024
643e868
Add test cases to improve code coverage
chenminghua8 Dec 4, 2024
6e0516b
Add test cases to improve code coverage
chenminghua8 Dec 4, 2024
707b4f1
support dataSketches include Quantile Sketches、Theta Sketch、Frequency…
chenminghua8 Oct 22, 2024
f8276df
fix format exception.
chenminghua8 Oct 23, 2024
7bde2bc
fix format exception.
chenminghua8 Oct 24, 2024
573ee8c
fix format exception.
chenminghua8 Oct 24, 2024
fab9f3f
fix format exception.
chenminghua8 Oct 24, 2024
cd3cba0
fix AggStateCombinatorTest fault
chenminghua8 Nov 24, 2024
b00773a
fix AggStateCombinatorTest fault
chenminghua8 Nov 25, 2024
aaae8aa
add tests
chenminghua8 Dec 2, 2024
2f092b1
add tests
chenminghua8 Dec 4, 2024
e4db576
Add test cases to improve code coverage
chenminghua8 Dec 4, 2024
cc875af
Add test cases to improve code coverage
chenminghua8 Dec 4, 2024
139ac86
Add test cases to improve code coverage
chenminghua8 Dec 4, 2024
ebaca8d
Fix aggregate_test test error due to using unused AnyValUtils
chenminghua8 Dec 5, 2024
ca4e313
Fix aggregate_test test error due to using unused AnyValUtils
chenminghua8 Dec 5, 2024
260d88b
Fix aggregate_test test error due to using unused AnyValUtils
chenminghua8 Dec 5, 2024
6d2dcf2
Fix other sketch nullptr and add ds_theta_count_distinct syntax
chenminghua8 Dec 8, 2024
499533f
Fix Clang format failling
chenminghua8 Dec 8, 2024
bbe0ec7
Fix Clang format failling
chenminghua8 Dec 8, 2024
aaa6acc
Fix Clang format failling
chenminghua8 Dec 8, 2024
cd04f84
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 Dec 30, 2024
026988f
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 Dec 31, 2024
6495748
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 Dec 31, 2024
78a11af
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 Dec 31, 2024
40589e6
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 Dec 31, 2024
f95f6d4
Merge branch 'support_dataSketches' of github.com:chenminghua8/starro…
chenminghua8 Dec 31, 2024
df3b57b
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 Dec 31, 2024
35b7ef4
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 Dec 31, 2024
8cc2d01
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 Dec 31, 2024
ecac63f
support DataSketches quantile sketche
chenminghua8 Dec 31, 2024
8b1b598
support DataSketches quantile sketche
chenminghua8 Jan 2, 2025
8d08860
Merge branch 'StarRocks:main' into support_quantile_sketche
chenminghua8 Jan 6, 2025
5adb7aa
support DataSketches quantile sketche
chenminghua8 Jan 6, 2025
d3f49cf
Merge branch 'StarRocks:main' into support_quantile_sketche
chenminghua8 Jan 7, 2025
983ea74
support DataSketches quantile sketche
chenminghua8 Jan 7, 2025
ed11a11
support DataSketches quantile sketche
chenminghua8 Jan 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
support DataSketches quantile sketche
Signed-off-by: chenminghua8 <cmptmn@126.com>
  • Loading branch information
chenminghua8 committed Jan 6, 2025
commit 5adb7aa0935269db9a8d55552e1b50838313ab19
5 changes: 5 additions & 0 deletions be/src/exprs/agg/factory/aggregate_resolver_approx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ struct DataSketchesBuilder {
"ds_hll_count_distinct", false,
AggregateFactory::MakeDataSketchesAggregateFunction<lt, SketchType::HLL>());
}
if constexpr (lt_is_integer<lt> || lt_is_float<lt>) {
resolver->add_aggregate_mapping<lt, TYPE_ARRAY, DSSketchState<lt, SketchType::QUANTILE>>(
"ds_quantile", false,
AggregateFactory::MakeDataSketchesAggregateFunction<lt, SketchType::QUANTILE>());
}
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# DS_QUANTILE



Returns the approximation of the pth percentile, where the value of p is between 0 and 1.

## Syntax

```Haskell
DS_QUANTILE(expr, DOUBLE p[, DOUBLE compression])
```

## Examples

```plain text
MySQL > CREATE TABLE t1 (
id BIGINT NOT NULL,
province VARCHAR(64),
age SMALLINT,
dt VARCHAR(10) NOT NULL
)
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 4
PROPERTIES (
"replication_num" = "1"
);
MySQL > insert into t1 select generate_series, generate_series, generate_series % 10, "2024-07-24" from table(generate_series(1, 100));
MySQL > select ds_quantile(id), ds_quantile(age) from t2 order by 1, 2;
+-------------------------------------------------------------+
| ds_quantile(id) ds_quantile(age) |
+------------------------------------------------------------ +
| [50] [4] |
+------------------------------------------------------------+
MySQL > select ds_quantile(id, [0.1, 0.5, 0.9]), ds_quantile(age, [0.1, 0.5, 0.9]) from t2 order by 1, 2;
+-------------------------------------------------------------+
| ds_quantile(id) ds_quantile(age) |
+------------------------------------------------------------ +
| [10,50,90] [0,4,8] |
+------------------------------------------------------------+
MySQL > select ds_quantile(id, [0.1, 0.5, 0.9], 10000), ds_quantile(age, [0.1, 0.5, 0.9], 10000) from t2 order by 1, 2;
+-------------------------------------------------------------+
| ds_quantile(id) ds_quantile(age) |
+------------------------------------------------------------ +
| [10,50,90] [0,4,8] |
+------------------------------------------------------------+
```
10 changes: 0 additions & 10 deletions fe/fe-core/src/main/java/com/starrocks/catalog/FunctionSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1692,16 +1692,6 @@ private void registerBuiltinDsFunction() {
false, false, true));
}

public static final java.util.function.Function<Type, ArrayType> DS_FREQUENT_RET_TYPE_BUILDER =
(Type itemType) -> {
List<StructField> fields = Lists.newArrayList();
fields.add(new StructField("value", itemType));
fields.add(new StructField("count", Type.BIGINT));
fields.add(new StructField("lower_bound", Type.BIGINT));
fields.add(new StructField("upper_bound", Type.BIGINT));
return new ArrayType(new StructType(fields, true));
};

public List<Function> getBuiltinFunctions() {
List<Function> builtinFunctions = Lists.newArrayList();
for (Map.Entry<String, List<Function>> entry : vectorizedFunctions.entrySet()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most risky bug in this code is:
Duplicate registration of DS_HLL_COUNT_DISTINCT functions, which can lead to errors or unexpected behavior during function lookup.

You can modify the code like this:

private void registerBuiltinDsFunction() {
    for (Type t : Type.getSupportedTypes()) {
        if (t.isFunctionType() || t.isNull() || t.isChar() || t.isPseudoType()) {
            continue;
        }

        // Avoid re-registering ds_hll_count_distinct functions.
        if (!vectorizedFunctions.containsKey(DS_HLL_COUNT_DISTINCT)) {
            addBuiltin(AggregateFunction.createBuiltin(DS_HLL_COUNT_DISTINCT,
                    Lists.newArrayList(t), Type.BIGINT, Type.VARBINARY,
                    true, false, true));
            addBuiltin(AggregateFunction.createBuiltin(DS_HLL_COUNT_DISTINCT,
                    Lists.newArrayList(t, Type.INT), Type.BIGINT, Type.VARBINARY,
                    true, false, true));
            addBuiltin(AggregateFunction.createBuiltin(DS_HLL_COUNT_DISTINCT,
                    Lists.newArrayList(t, Type.INT, Type.VARCHAR), Type.BIGINT, Type.VARBINARY,
                    true, false, true));
        }
    }
    
    // Rest of the function registrations...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function parameters are different and need to be registered multiple times.

Expand Down
29 changes: 29 additions & 0 deletions test/sql/test_agg_function/R/test_quantile_sketch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- name: test_datasketchs
CREATE TABLE t2 (
id BIGINT NOT NULL,
province VARCHAR(64),
age SMALLINT,
dt VARCHAR(10) NOT NULL
)
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 4
PROPERTIES (
"replication_num" = "1"
);
-- result:
-- !result
insert into t2 select generate_series, generate_series, generate_series % 10, "2024-07-24" from table(generate_series(1, 100));
-- result:
-- !result
select ds_quantile(id), ds_quantile(age) from t2 order by 1, 2;
-- result:
[50] [4]
-- !result
select ds_quantile(id, [0.1, 0.5, 0.9]), ds_quantile(age, [0.1, 0.5, 0.9]) from t2 order by 1, 2;
-- result:
[10,50,90] [0,4,8]
-- !result
select ds_quantile(id, [0.1, 0.5, 0.9], 10000), ds_quantile(age, [0.1, 0.5, 0.9], 10000) from t2 order by 1, 2;
-- result:
[10,50,90] [0,4,8]
-- !result
20 changes: 20 additions & 0 deletions test/sql/test_agg_function/T/test_quantile_sketch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- name: test_quantile_sketch
CREATE TABLE t2 (
id BIGINT NOT NULL,
province VARCHAR(64),
age SMALLINT,
dt VARCHAR(10) NOT NULL
)
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 4
PROPERTIES (
"replication_num" = "1"
);

insert into t2 select generate_series, generate_series, generate_series % 10, "2024-07-24" from table(generate_series(1, 100));

select ds_quantile(id), ds_quantile(age) from t2 order by 1, 2;

select ds_quantile(id, [0.1, 0.5, 0.9]), ds_quantile(age, [0.1, 0.5, 0.9]) from t2 order by 1, 2;

select ds_quantile(id, [0.1, 0.5, 0.9], 10000), ds_quantile(age, [0.1, 0.5, 0.9], 10000) from t2 order by 1, 2;
Loading