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 Dec 31, 2024
commit ecac63fb41bcbadc5f198264b03e15ef097f1a7b
1 change: 1 addition & 0 deletions be/src/exprs/agg/ds_agg.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include "exprs/agg/ds_hll_state.h"
#include "exprs/agg/ds_quantile_state.h"
#include "exprs/agg/ds_state.h"

namespace starrocks {
Expand Down
176 changes: 176 additions & 0 deletions be/src/exprs/agg/ds_quantile_state.h
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
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.

1 change: 1 addition & 0 deletions be/src/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_library(Types STATIC
array_type_info.cpp
bitmap_value.cpp
date_value.cpp
ds_quantile_sketch.cpp
hll.cpp
hll_sketch.cpp
large_int_value.cpp
Expand Down
97 changes: 97 additions & 0 deletions be/src/types/ds_quantile_sketch.cpp
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
94 changes: 94 additions & 0 deletions be/src/types/ds_quantile_sketch.h
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
Loading
Loading