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

Conversation

chenminghua8
Copy link
Contributor

Why I'm doing:

What I'm doing:

[Feature] support Quantile Theta Sketche
Implement the aggregation function that supports DataSketches Quantile:

  1. Add ds_quantile_state.h to implement the AggregateState that supports DataSketches Quantile;
  2. Add quantile_sketch.h and quantile_sketch.cpp to implement the encapsulation of DataSketches Quantile Sketche;
  3. Register the ds_quantile function through SketchType::QUANTILET generic to implement the function that supports DataSketches Quantile.

Fixes #50671

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.4
    • 3.3
    • 3.2
    • 3.1
    • 3.0

chenminghua8 and others added 30 commits October 22, 2024 19:10
… Sketches.

Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
… Sketches.

Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
Signed-off-by: chenminghua8 <cmptmn@126.com>
@chenminghua8 chenminghua8 requested review from a team as code owners December 31, 2024 10:31
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Dec 31, 2024
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.

}
};

} // namespace starrocks
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:

Typographical error in type name SketchWarapperType.

You can modify the code like this:

+    using SketchWrapperType = DataSketchesQuantile<CppType>;
...
+    std::unique_ptr <SketchWrapperType> ds_sketch_wrapper = nullptr;
...
+        ds_sketch_wrapper = std::make_unique<SketchWrapperType>(k, &memory_usage);
...
+                *ranks_prt = other_state.ranks.get()[i];
+                ranks_prt++;
+            }
+            ds_sketch_wrapper =
+                    std::make_unique<SketchWrapperType>(other_state.ds_sketch_wrapper->get_k(), &memory_usage);
...
+        ds_sketch_wrapper = std::make_unique<SketchWrapperType>(sketch_data_slice, memory_usage);
...

This modification corrects the misspelled type name from SketchWarapperType to SketchWrapperType, ensuring consistency and preventing potential compilation errors.

}
};

} // namespace starrocks No newline at end of file
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:
Possible null pointer dereference because ds_sketch_wrapper may not be initialized before calling update.

You can modify the code like this:

void update(const Column* data_column, size_t row_num) const {
    if (!is_inited()) {
        // Handle uninitialized ds_sketch_wrapper scenario, e.g., log an error or throw an exception.
        return;
    }

    uint64_t value = 0;
    const ColumnType* column = down_cast<const ColumnType*>(data_column);

    if constexpr (lt_is_string<LT>) {
        Slice s = column->get_slice(row_num);
        value = HashUtil::murmur_hash64A(s.data, s.size, HashUtil::MURMUR_SEED);
    } else {
        const auto& v = column->get_data();
        value = HashUtil::murmur_hash64A(&v[row_num], sizeof(v[row_num]), HashUtil::MURMUR_SEED);
    }
    ds_sketch_wrapper->update(value);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no need to check whether it has been initialized, because the method calling it has already done the corresponding processing.

Signed-off-by: chenminghua8 <cmptmn@126.com>
@chenminghua8 chenminghua8 changed the title [Feature] support Quantile Theta Sketche [Feature] support Quantile Sketche Jan 3, 2025
Copy link

sonarqubecloud bot commented Jan 7, 2025

Copy link

github-actions bot commented Jan 7, 2025

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

github-actions bot commented Jan 7, 2025

[FE Incremental Coverage Report]

pass : 91 / 95 (95.79%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/analyzer/FunctionAnalyzer.java 27 31 87.10% [539, 553, 556, 576]
🔵 com/starrocks/sql/optimizer/rule/tree/PreAggregateTurnOnRule.java 3 3 100.00% []
🔵 com/starrocks/catalog/FunctionSet.java 61 61 100.00% []

Copy link

github-actions bot commented Jan 7, 2025

[BE Incremental Coverage Report]

pass : 266 / 297 (89.56%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 be/src/types/ds_quantile_sketch.cpp 22 33 66.67% [33, 34, 57, 58, 77, 78, 81, 84, 85, 86, 87]
🔵 be/src/exprs/agg/ds_hll_state.h 57 68 83.82% [53, 54, 75, 76, 77, 78, 80, 81, 114, 122, 125]
🔵 be/src/exprs/agg/ds_quantile_state.h 100 106 94.34% [51, 108, 150, 160, 161, 162]
🔵 be/src/types/ds_quantile_sketch.h 16 17 94.12% [51]
🔵 be/src/exprs/agg/ds_agg.h 64 66 96.97% [31, 32]
🔵 be/src/exprs/agg/factory/aggregate_factory.hpp 2 2 100.00% []
🔵 be/src/exprs/agg/factory/aggregate_resolver_approx.cpp 5 5 100.00% []

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Aggregate Function] Support DataSketches in StarRocks
1 participant