-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
chenminghua8
wants to merge
53
commits into
StarRocks:main
Choose a base branch
from
chenminghua8:support_quantile_sketche
base: main
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.
+1,205
−269
Open
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 cf082a9
fix format exception.
chenminghua8 2540ae9
fix format exception.
chenminghua8 02dc2b0
fix format exception.
chenminghua8 358b652
fix format exception.
chenminghua8 60f073e
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 01a85d0
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 6aff6a7
fix AggStateCombinatorTest fault
chenminghua8 c573071
fix AggStateCombinatorTest fault
chenminghua8 51e8596
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 accf967
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 e0257e9
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 38b153e
add tests
chenminghua8 19d9389
add tests
chenminghua8 793cc6c
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 8d337aa
Add test cases to improve code coverage
chenminghua8 643e868
Add test cases to improve code coverage
chenminghua8 6e0516b
Add test cases to improve code coverage
chenminghua8 707b4f1
support dataSketches include Quantile Sketches、Theta Sketch、Frequency…
chenminghua8 f8276df
fix format exception.
chenminghua8 7bde2bc
fix format exception.
chenminghua8 573ee8c
fix format exception.
chenminghua8 fab9f3f
fix format exception.
chenminghua8 cd3cba0
fix AggStateCombinatorTest fault
chenminghua8 b00773a
fix AggStateCombinatorTest fault
chenminghua8 aaae8aa
add tests
chenminghua8 2f092b1
add tests
chenminghua8 e4db576
Add test cases to improve code coverage
chenminghua8 cc875af
Add test cases to improve code coverage
chenminghua8 139ac86
Add test cases to improve code coverage
chenminghua8 ebaca8d
Fix aggregate_test test error due to using unused AnyValUtils
chenminghua8 ca4e313
Fix aggregate_test test error due to using unused AnyValUtils
chenminghua8 260d88b
Fix aggregate_test test error due to using unused AnyValUtils
chenminghua8 6d2dcf2
Fix other sketch nullptr and add ds_theta_count_distinct syntax
chenminghua8 499533f
Fix Clang format failling
chenminghua8 bbe0ec7
Fix Clang format failling
chenminghua8 aaa6acc
Fix Clang format failling
chenminghua8 cd04f84
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 026988f
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 6495748
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 78a11af
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 40589e6
Merge branch 'StarRocks:main' into support_dataSketches
chenminghua8 f95f6d4
Merge branch 'support_dataSketches' of github.com:chenminghua8/starro…
chenminghua8 df3b57b
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 35b7ef4
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 8cc2d01
Refactor to support DataSketches Quantile、Theta、Frequency Sketches
chenminghua8 ecac63f
support DataSketches quantile sketche
chenminghua8 8b1b598
support DataSketches quantile sketche
chenminghua8 8d08860
Merge branch 'StarRocks:main' into support_quantile_sketche
chenminghua8 5adb7aa
support DataSketches quantile sketche
chenminghua8 d3f49cf
Merge branch 'StarRocks:main' into support_quantile_sketche
chenminghua8 983ea74
support DataSketches quantile sketche
chenminghua8 ed11a11
support DataSketches quantile sketche
chenminghua8 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
support DataSketches quantile sketche
Signed-off-by: chenminghua8 <cmptmn@126.com>
- Loading branch information
commit ecac63fb41bcbadc5f198264b03e15ef097f1a7b
There are no files selected for viewing
This file contains 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 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,176 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
#pragma once | ||
|
||
#include "column/array_column.h" | ||
#include "column/column_helper.h" | ||
#include "exprs/agg/ds_state.h" | ||
#include "types/ds_quantile_sketch.cpp" | ||
|
||
namespace starrocks { | ||
template<LogicalType LT> | ||
struct DSSketchState<LT, QUANTILE> { | ||
using CppType = RunTimeCppType<LT>; | ||
using ColumnType = RunTimeColumnType<LT>; | ||
using SketchWarapperType = DataSketchesQuantile<CppType>; | ||
uint32_t ranks_size; | ||
std::unique_ptr<double[]> ranks = nullptr; | ||
std::unique_ptr <SketchWarapperType> ds_sketch_wrapper = nullptr; | ||
int64_t memory_usage = 0; | ||
|
||
void init(FunctionContext *ctx) { | ||
DatumArray datum_array; | ||
uint16_t k; | ||
std::tie(k, datum_array) = _parse_sketch_args(ctx); | ||
if (datum_array.size() < 1) { | ||
ranks_size = 1; | ||
ranks = std::make_unique<double[]>(ranks_size); | ||
*ranks.get() = 0.5; | ||
} else { | ||
ranks_size = datum_array.size(); | ||
ranks = std::make_unique<double[]>(ranks_size); | ||
double *ranks_prt = ranks.get(); | ||
for (Datum rank: datum_array) { | ||
*ranks_prt = rank.get_double(); | ||
ranks_prt++; | ||
} | ||
} | ||
if (ranks_size == 0) { | ||
ranks_size = 0; | ||
} | ||
ds_sketch_wrapper = std::make_unique<SketchWarapperType>(k, &memory_usage); | ||
} | ||
|
||
bool is_inited() const { return ds_sketch_wrapper != nullptr; } | ||
|
||
void update(const Column *data_column, size_t row_num) const { | ||
const ColumnType *column = down_cast<const ColumnType *>(data_column); | ||
const auto &values = column->get_data(); | ||
ds_sketch_wrapper->update(values[row_num]); | ||
} | ||
|
||
void | ||
update_batch_single_state_with_frame(const Column *data_column, int64_t frame_start, int64_t frame_end) const { | ||
const ColumnType *column = down_cast<const ColumnType *>(data_column); | ||
const auto &values = column->get_data(); | ||
for (size_t i = frame_start; i < frame_end; ++i) { | ||
ds_sketch_wrapper->update(values[i]); | ||
} | ||
} | ||
|
||
void merge(const BinaryColumn *sketch_data_column, size_t row_num) { | ||
DSSketchState<LT, QUANTILE> other_state; | ||
other_state.deserialize(sketch_data_column->get(row_num).get_slice(), &memory_usage); | ||
if (UNLIKELY(!is_inited())) { | ||
ranks_size = other_state.ranks_size; | ||
ranks = std::make_unique<double[]>(ranks_size); | ||
double *ranks_prt = ranks.get(); | ||
for (int i = 0; i < ranks_size; i++) { | ||
*ranks_prt = other_state.ranks.get()[i]; | ||
ranks_prt++; | ||
} | ||
ds_sketch_wrapper = | ||
std::make_unique<SketchWarapperType>(other_state.ds_sketch_wrapper->get_k(), &memory_usage); | ||
} | ||
ds_sketch_wrapper->merge(*other_state.ds_sketch_wrapper); | ||
} | ||
|
||
size_t serialize(uint8_t *dst) const { | ||
size_t offset = 0; | ||
memcpy(dst + offset, &ranks_size, sizeof(ranks_size)); | ||
offset = offset + sizeof(uint32_t); | ||
memcpy(dst + offset, ranks.get(), ranks_size * sizeof(double)); | ||
offset = offset + ranks_size * sizeof(double); | ||
size_t ser_sketch_size = ds_sketch_wrapper->serialize(dst + offset); | ||
return offset + ser_sketch_size; | ||
} | ||
|
||
size_t serialize_size() const { | ||
return sizeof(uint32_t) + ranks_size * sizeof(double) + ds_sketch_wrapper->serialize_size(); | ||
} | ||
|
||
void deserialize(const Slice &slice, int64_t *memory_usage) { | ||
uint8_t *ptr = (uint8_t *) slice.get_data(); | ||
size_t offset = 0; | ||
memcpy(&ranks_size, ptr + offset, sizeof(uint32_t)); | ||
if (ranks_size == 0) { | ||
ranks_size = 0; | ||
} | ||
offset = offset + sizeof(uint32_t); | ||
ranks = std::make_unique<double[]>(ranks_size); | ||
memcpy(ranks.get(), ptr + offset, ranks_size * sizeof(double)); | ||
offset = offset + ranks_size * sizeof(double); | ||
const Slice sketch_data_slice = Slice(slice.get_data() + offset, slice.size - offset); | ||
ds_sketch_wrapper = std::make_unique<SketchWarapperType>(sketch_data_slice, memory_usage); | ||
} | ||
|
||
void get_values(Column *dst, size_t start, size_t end) const { | ||
auto *array_column = down_cast<ArrayColumn *>(dst); | ||
auto &offset_column = array_column->offsets_column(); | ||
auto &elements_column = array_column->elements_column(); | ||
auto *nullable_column = down_cast<NullableColumn *>(elements_column.get()); | ||
auto *result_column = down_cast<ColumnType *>(nullable_column->data_column().get()); | ||
|
||
std::vector <CppType> result; | ||
if (LIKELY(ds_sketch_wrapper != nullptr)) { | ||
result = ds_sketch_wrapper->get_quantiles(ranks.get(), ranks_size); | ||
} | ||
|
||
uint32_t index = 0; | ||
for (size_t row = start; row < end; row++) { | ||
for (CppType result_data: result) { | ||
result_column->append(result_data); | ||
nullable_column->null_column()->append(0); | ||
index++; | ||
} | ||
offset_column->append(index); | ||
} | ||
} | ||
|
||
static std::string getFunName() { return "ds_quantile"; } | ||
|
||
private: | ||
// parse k and rank_arr from args | ||
static std::tuple <uint16_t, DatumArray> _parse_sketch_args(FunctionContext *ctx) { | ||
uint16_t k = DEFAULT_QUANTILE_K; | ||
if (ctx->get_num_constant_columns() > 1) { | ||
if (ctx->get_num_constant_columns() > 2) { | ||
k = ColumnHelper::get_const_value<TYPE_INT>(ctx->get_constant_column(2)); | ||
if (k <= 1) { | ||
k = DEFAULT_QUANTILE_K; | ||
} | ||
int i = 1; | ||
while ((1 << i) < k) { | ||
i += 1; | ||
} | ||
k = 1 << i; | ||
} | ||
Column *ranks_column = ColumnHelper::get_data_column(ctx->get_constant_column(1).get()); | ||
if (ranks_column->is_array()) { | ||
DatumArray rank_arr = ranks_column->get(0).get_array(); | ||
return {k, rank_arr}; | ||
} else { | ||
DatumArray rank_arr; | ||
double rank_value = ranks_column->get(0).get_double(); | ||
rank_arr.push_back(rank_value); | ||
return {k, rank_arr}; | ||
} | ||
} | ||
DatumArray rank_arr; | ||
return {k, rank_arr}; | ||
} | ||
}; | ||
|
||
} // namespace starrocks | ||
This file contains 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 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,97 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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 "types/ds_quantile_sketch.h" | ||
|
||
namespace starrocks { | ||
|
||
template <typename T> | ||
void DataSketchesQuantile<T>::update(T value) { _sketch->update(value); } | ||
|
||
template <typename T> | ||
void DataSketchesQuantile<T>::merge(const DataSketchesQuantile<T>& other) { | ||
if (other._sketch == nullptr) { | ||
return; | ||
} | ||
if (UNLIKELY(_sketch == nullptr)) { | ||
_sketch = std::make_unique<quantile_sketch_type>(other._sketch->get_k(), std::less<T>(), | ||
alloc_type(_memory_usage)); | ||
} | ||
_sketch.get()->merge(*other._sketch); | ||
} | ||
|
||
template <typename T> | ||
uint64_t DataSketchesQuantile<T>::serialize_size() const { | ||
if (_sketch == nullptr) { | ||
return 0; | ||
} | ||
return _sketch->get_serialized_size_bytes(); | ||
} | ||
|
||
template <typename T> | ||
bool DataSketchesQuantile<T>::deserialize(const Slice& slice) { | ||
DCHECK(_sketch == nullptr); | ||
|
||
if (!is_valid(slice)) { | ||
return false; | ||
} | ||
try { | ||
_sketch = std::make_unique<quantile_sketch_type>( | ||
quantile_sketch_type::deserialize((uint8_t*)slice.data, slice.size, datasketches::serde<T>(), | ||
std::less<T>(), alloc_type(_memory_usage))); | ||
} catch (std::logic_error& e) { | ||
LOG(WARNING) << "DataSketchesQuantile deserialize error: " << e.what(); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
template <typename T> | ||
std::vector<T> DataSketchesQuantile<T>::get_quantiles(const double* ranks, uint32_t size) const { | ||
std::vector<T> result; | ||
if (_sketch == nullptr) { | ||
return result; | ||
} | ||
try { | ||
std::vector<T, alloc_type> quantiles = _sketch->get_quantiles(ranks, size); | ||
for (T quantile : quantiles) { | ||
result.push_back(quantile); | ||
} | ||
} catch (std::logic_error& e) { | ||
LOG(WARNING) << "DataSketchesQuantile get_quantiles error: " << e.what(); | ||
result.clear(); | ||
} | ||
return result; | ||
} | ||
|
||
template <typename T> | ||
void DataSketchesQuantile<T>::clear() { | ||
*_memory_usage = 0; | ||
this->_sketch = | ||
std::make_unique<quantile_sketch_type>(_sketch->get_k(), std::less<T>(), alloc_type(_memory_usage)); | ||
} | ||
|
||
template <typename T> | ||
std::string DataSketchesQuantile<T>::to_string() const { | ||
if (_sketch == nullptr) { | ||
return ""; | ||
} | ||
datasketches::string<alloc_type> str = _sketch->to_string(); | ||
return std::string(str.begin(), str.end()); | ||
} | ||
|
||
} // namespace starrocks |
This file contains 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,94 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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. | ||
|
||
#pragma once | ||
|
||
#include <datasketches/quantiles_sketch.hpp> | ||
#include "runtime/memory/counting_allocator.h" | ||
#include "util/slice.h" | ||
|
||
namespace starrocks { | ||
template <typename T> | ||
class DataSketchesQuantile { | ||
public: | ||
using alloc_type = STLCountingAllocator<T>; | ||
using quantile_sketch_type = datasketches::quantiles_sketch<T, std::less<T>, alloc_type>; | ||
|
||
explicit DataSketchesQuantile(uint16_t k, int64_t* memory_usage) : _memory_usage(memory_usage) { | ||
this->_sketch = std::make_unique<quantile_sketch_type>(k, std::less<T>(), alloc_type(_memory_usage)); | ||
} | ||
|
||
DataSketchesQuantile(const DataSketchesQuantile& other) = delete; | ||
DataSketchesQuantile& operator=(const DataSketchesQuantile& other) = delete; | ||
|
||
DataSketchesQuantile(DataSketchesQuantile&& other) noexcept | ||
: _memory_usage(std::move(other._memory_usage)), _sketch(std::move(other._sketch)) {} | ||
DataSketchesQuantile& operator=(DataSketchesQuantile&& other) noexcept { | ||
if (this != &other) { | ||
this->_memory_usage = std::move(other._memory_usage); | ||
this->_sketch = std::move(other._sketch); | ||
} | ||
return *this; | ||
} | ||
|
||
explicit DataSketchesQuantile(const Slice& src, int64_t* memory_usage) : _memory_usage(memory_usage) { | ||
if (!deserialize(src)) { | ||
LOG(WARNING) << "Failed to init DataSketchesQuantile from slice, will be reset to 0."; | ||
} | ||
} | ||
|
||
~DataSketchesQuantile() = default; | ||
|
||
uint16_t get_k() const { return _sketch->get_k(); } | ||
|
||
void update(T value); | ||
|
||
void merge(const DataSketchesQuantile& other); | ||
|
||
int64_t mem_usage() const { return _memory_usage == nullptr ? 0L : *_memory_usage; } | ||
|
||
size_t serialize(uint8_t* dst) const { | ||
if (_sketch == nullptr) { | ||
return 0; | ||
} | ||
auto serialize_compact = _sketch->serialize(); | ||
std::copy(serialize_compact.begin(), serialize_compact.end(), dst); | ||
return _sketch->get_serialized_size_bytes(); | ||
} | ||
|
||
uint64_t serialize_size() const; | ||
|
||
bool deserialize(const Slice& slice); | ||
|
||
std::vector<T> get_quantiles(const double* ranks, uint32_t size) const; | ||
|
||
static bool is_valid(const Slice& slice) { | ||
if (slice.size < 1) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
void clear(); | ||
|
||
std::string to_string() const; | ||
|
||
private: | ||
int64_t* _memory_usage; | ||
mutable std::unique_ptr<quantile_sketch_type> _sketch = nullptr; | ||
}; | ||
} // namespace starrocks |
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.
The most risky bug in this code is:
Typographical error in type name
SketchWarapperType
.You can modify the code like this:
This modification corrects the misspelled type name from
SketchWarapperType
toSketchWrapperType
, ensuring consistency and preventing potential compilation errors.