-
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 DataSketches Theta Sketche #54568
Open
chenminghua8
wants to merge
51
commits into
StarRocks:main
Choose a base branch
from
chenminghua8:support_theta_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.
Open
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 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 33693d0
support DataSketches theta sketche
chenminghua8 d02447e
support DataSketches theta sketche
chenminghua8 1ba8808
support DataSketches theta sketche
chenminghua8 240e813
support DataSketches theta sketche
chenminghua8 079a46a
support DataSketches theta 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
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
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 | ||
This file was deleted.
Oops, something went wrong.
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:
Potential undefined behavior due to uninitialized memory access when serializing the data.
You can modify the code like this:
This change uses a
std::vector<uint8_t>
instead of a stack-allocated array with variable length, ensuring memory is properly initialized and managed.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.
fixed