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 DataSketches Theta Sketche #54568

Open
wants to merge 51 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 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
33693d0
support DataSketches theta sketche
chenminghua8 Dec 31, 2024
d02447e
support DataSketches theta sketche
chenminghua8 Jan 2, 2025
1ba8808
support DataSketches theta sketche
chenminghua8 Jan 2, 2025
240e813
support DataSketches theta sketche
chenminghua8 Jan 3, 2025
079a46a
support DataSketches theta sketche
chenminghua8 Jan 5, 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
125 changes: 125 additions & 0 deletions be/src/exprs/agg/ds_agg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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 "exprs/agg/ds_hll_state.h"
#include "exprs/agg/ds_state.h"
#include "exprs/agg/ds_theta_state.h"

namespace starrocks {

template <LogicalType LT, SketchType ST, typename StateType = DSSketchState<LT, ST>, typename T = RunTimeCppType<LT>>
class DataSketchesAggregateFunction final
: public AggregateFunctionBatchHelper<StateType, DataSketchesAggregateFunction<LT, ST, StateType, T>> {
public:
using ColumnType = RunTimeColumnType<LT>;

void reset(FunctionContext* ctx, const Columns& args, AggDataPtr state) const override {
if (this->data(state).is_inited()) {
ctx->add_mem_usage(-this->data(state).memory_usage);
this->data(state).ds_sketch_wrapper->clear();
}
}

void update(FunctionContext* ctx, const Column** columns, AggDataPtr __restrict state,
size_t row_num) const override {
// init state if needed
_init_if_needed(ctx, state);
int64_t prev_memory = this->data(state).memory_usage;
const Column* data_column = ColumnHelper::get_data_column(columns[0]);
this->data(state).update(data_column, row_num);
ctx->add_mem_usage(this->data(state).memory_usage - prev_memory);
}

void update_batch_single_state_with_frame(FunctionContext* ctx, AggDataPtr __restrict state, const Column** columns,
int64_t peer_group_start, int64_t peer_group_end, int64_t frame_start,
int64_t frame_end) const override {
// init state if needed
_init_if_needed(ctx, state);
int64_t prev_memory = this->data(state).memory_usage;
const Column* data_column = ColumnHelper::get_data_column(columns[0]);
this->data(state).update_batch_single_state_with_frame(data_column, frame_start, frame_end);
ctx->add_mem_usage(this->data(state).memory_usage - prev_memory);
}

void merge(FunctionContext* ctx, const Column* column, AggDataPtr __restrict state, size_t row_num) const override {
DCHECK(column->is_binary());
const BinaryColumn* sketch_data_column = down_cast<const BinaryColumn*>(column);
int64_t prev_memory = this->data(state).memory_usage;
this->data(state).merge(sketch_data_column, row_num);
ctx->add_mem_usage(this->data(state).memory_usage - prev_memory);
}

void get_values(FunctionContext* ctx, ConstAggDataPtr __restrict state, Column* dst, size_t start,
size_t end) const override {
DCHECK_GT(end, start);
this->data(state).get_values(dst, start, end);
}

void serialize_to_column([[maybe_unused]] FunctionContext* ctx, ConstAggDataPtr __restrict state,
Column* to) const override {
DCHECK(to->is_binary());
auto* column = down_cast<BinaryColumn*>(to);
if (UNLIKELY(!this->data(state).is_inited())) {
column->append_default();
} else {
size_t serialized_size = this->data(state).serialize_size();
std::vector<uint8_t> result(serialized_size);
size_t actual_size = this->data(state).serialize(result.data());
column->append(Slice(result.data(), actual_size));
}
}

void convert_to_serialize_format([[maybe_unused]] FunctionContext* ctx, const Columns& src, size_t chunk_size,
ColumnPtr* dst) const override {
auto* result = down_cast<BinaryColumn*>((*dst).get());

Bytes& bytes = result->get_bytes();
bytes.reserve(chunk_size * 10);
result->get_offset().resize(chunk_size + 1);

size_t old_size = bytes.size();
// convert to const Column*
const auto* data_column = ColumnHelper::get_data_column(src[0].get());
for (size_t i = 0; i < chunk_size; ++i) {
StateType state;
state.init(ctx);
state.update(data_column, i);
size_t new_size = old_size + state.serialize_size();
bytes.resize(new_size);
state.serialize(bytes.data() + old_size);
result->get_offset()[i + 1] = new_size;
old_size = new_size;
}
}

void finalize_to_column(FunctionContext* ctx __attribute__((unused)), ConstAggDataPtr __restrict state,
Column* to) const override {
// this->data(state).finalize_to_column(to);
this->data(state).get_values(to, 0, 1);
}

std::string get_name() const override { return StateType::getFunName(); }

private:
// init hll sketch if needed
void _init_if_needed(FunctionContext* ctx, AggDataPtr __restrict state) const {
if (UNLIKELY(!this->data(state).is_inited())) {
this->data(state).init(ctx);
}
}
};

} // 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:
Potential undefined behavior due to uninitialized memory access when serializing the data.

You can modify the code like this:

void serialize_to_column([[maybe_unused]] FunctionContext* ctx, ConstAggDataPtr __restrict state,
                         Column* to) const override {
    DCHECK(to->is_binary());
    auto* column = down_cast<BinaryColumn*>(to);
    if (UNLIKELY(!this->data(state).is_inited())) {
        column->append_default();
    } else {
        size_t serialized_size = this->data(state).serialize_size();
        std::vector<uint8_t> result(serialized_size); // Use vector to manage memory safely
        size_t actual_size = this->data(state).serialize(result.data());
        column->append(Slice(result.data(), actual_size));
    }
}

This change uses a std::vector<uint8_t> instead of a stack-allocated array with variable length, ensuring memory is properly initialized and managed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

240 changes: 0 additions & 240 deletions be/src/exprs/agg/ds_hll_count_distinct.h

This file was deleted.

Loading
Loading