This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.0 by this push:
new 77fedd8e331 branch-4.0: [fix] remove useless const_cast and explain
const_cast for vec (1) #56587 (#56664)
77fedd8e331 is described below
commit 77fedd8e3310dafec66fe096fdeb9f126122daf7
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Sep 30 13:57:06 2025 +0800
branch-4.0: [fix] remove useless const_cast and explain const_cast for vec
(1) #56587 (#56664)
Cherry-picked from #56587
Co-authored-by: admiring_xm <[email protected]>
---
be/src/util/bitmap_expr_calculation.h | 13 +++++++------
be/src/vec/aggregate_functions/aggregate_function.h | 1 +
be/src/vec/aggregate_functions/aggregate_function_bitmap.h | 12 ++++++------
.../vec/aggregate_functions/aggregate_function_distinct.h | 3 +++
.../aggregate_function_group_array_intersect.h | 9 ++++-----
.../vec/aggregate_functions/aggregate_function_java_udaf.h | 3 +--
.../aggregate_function_orthogonal_bitmap.h | 11 +++++------
.../vec/aggregate_functions/aggregate_function_percentile.h | 4 +---
.../aggregate_function_percentile_reservoir.h | 2 ++
.../aggregate_functions/aggregate_function_quantile_state.h | 8 ++++----
be/src/vec/aggregate_functions/aggregate_function_rpc.h | 6 ++++++
.../aggregate_functions/aggregate_function_sequence_match.h | 3 +++
be/src/vec/aggregate_functions/aggregate_function_sort.h | 1 +
.../aggregate_functions/aggregate_function_window_funnel.h | 2 ++
14 files changed, 46 insertions(+), 32 deletions(-)
diff --git a/be/src/util/bitmap_expr_calculation.h
b/be/src/util/bitmap_expr_calculation.h
index 24784cc957c..ae5adf00b31 100644
--- a/be/src/util/bitmap_expr_calculation.h
+++ b/be/src/util/bitmap_expr_calculation.h
@@ -57,14 +57,14 @@ public:
}
}
- BitmapValue bitmap_calculate() {
+ BitmapValue bitmap_calculate() const {
std::stack<BitmapValue> values;
std::string bitmap_key;
for (int i = 0; i < _polish.length(); i++) {
char c = _polish.at(i);
if (c == ' ') {
if (bitmap_key.length() > 0) {
- values.push(_bitmaps[bitmap_key]);
+ values.push(_bitmaps.at(bitmap_key));
bitmap_key.clear();
}
} else if (c != '&' && c != '|' && c != '^' && c != '-' && c !=
'\\') {
@@ -75,7 +75,7 @@ public:
continue;
} else {
if (bitmap_key.length() > 0) {
- values.push(_bitmaps[bitmap_key]);
+ values.push(_bitmaps.at(bitmap_key));
bitmap_key.clear();
}
if (values.size() >= 2) {
@@ -91,7 +91,7 @@ public:
}
BitmapValue result;
if (bitmap_key.length() > 0) {
- result |= _bitmaps[bitmap_key];
+ result |= _bitmaps.at(bitmap_key);
} else if (!values.empty()) {
result |= values.top();
}
@@ -99,7 +99,7 @@ public:
}
// calculate the bitmap value by expr bitmap calculate
- int64_t bitmap_calculate_count() {
+ int64_t bitmap_calculate_count() const {
if (_bitmaps.empty()) {
return 0;
}
@@ -193,7 +193,8 @@ private:
return print_stack(polish);
}
- void bitmap_calculate(BitmapValue& op_a, BitmapValue& op_b, char op,
BitmapValue& result) {
+ void bitmap_calculate(BitmapValue& op_a, BitmapValue& op_b, char op,
+ BitmapValue& result) const {
result |= op_b;
switch (op) {
case '&':
diff --git a/be/src/vec/aggregate_functions/aggregate_function.h
b/be/src/vec/aggregate_functions/aggregate_function.h
index ca1ad190101..95c96c8a038 100644
--- a/be/src/vec/aggregate_functions/aggregate_function.h
+++ b/be/src/vec/aggregate_functions/aggregate_function.h
@@ -185,6 +185,7 @@ public:
const IColumn& column,
Arena&) const = 0;
/// Inserts results into a column.
+ // todo: Consider whether this passes a ConstAggregateDataPtr
virtual void insert_result_into(ConstAggregateDataPtr __restrict place,
IColumn& to) const = 0;
virtual void insert_result_into_vec(const std::vector<AggregateDataPtr>&
places,
diff --git a/be/src/vec/aggregate_functions/aggregate_function_bitmap.h
b/be/src/vec/aggregate_functions/aggregate_function_bitmap.h
index 5c9238a723c..a17f1a42ffb 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_bitmap.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_bitmap.h
@@ -147,6 +147,8 @@ struct AggregateFunctionBitmapData {
}
BitmapValue& get() { return value; }
+
+ const BitmapValue& get() const { return value; }
};
template <typename Data, typename Derived>
@@ -309,8 +311,7 @@ public:
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
Arena&) const override {
- this->data(place).merge(
-
const_cast<AggregateFunctionBitmapData<Op>&>(this->data(rhs)).get());
+ this->data(place).merge(this->data(rhs).get());
}
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable&
buf) const override {
@@ -324,8 +325,7 @@ public:
void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn&
to) const override {
auto& column = assert_cast<ColVecResult&>(to);
- column.get_data().push_back(
-
const_cast<AggregateFunctionBitmapData<Op>&>(this->data(place)).get());
+ column.get_data().push_back(this->data(place).get());
}
void reset(AggregateDataPtr __restrict place) const override {
this->data(place).reset(); }
@@ -442,7 +442,7 @@ public:
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
Arena&) const override {
-
this->data(place).merge(const_cast<AggFunctionData&>(this->data(rhs)).get());
+ this->data(place).merge(this->data(rhs).get());
}
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable&
buf) const override {
@@ -455,7 +455,7 @@ public:
}
void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn&
to) const override {
- auto& value_data =
const_cast<AggFunctionData&>(this->data(place)).get();
+ auto& value_data = this->data(place).get();
auto& column = assert_cast<ColVecResult&>(to);
column.get_data().push_back(value_data.cardinality());
}
diff --git a/be/src/vec/aggregate_functions/aggregate_function_distinct.h
b/be/src/vec/aggregate_functions/aggregate_function_distinct.h
index c458cf06af4..27ce89e62c2 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_distinct.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_distinct.h
@@ -108,6 +108,8 @@ struct AggregateFunctionDistinctSingleNumericData {
if constexpr (stable) {
argument_columns[0]->resize(data.size());
+ // argument_columns[0] is a mutable column created using
create_column.
+ // since get_raw_data returns a StringRef, const_cast is required
here.
auto ptr = (typename
PrimitiveTypeTraits<T>::CppType*)const_cast<char*>(
argument_columns[0]->get_raw_data().data);
for (auto it : data) {
@@ -310,6 +312,7 @@ public:
}
void insert_result_into(ConstAggregateDataPtr targetplace, IColumn& to)
const override {
+ // place is essentially an AggregateDataPtr, passed as a
ConstAggregateDataPtr.
auto* place = const_cast<AggregateDataPtr>(targetplace);
auto arguments = this->data(place).get_arguments(this->argument_types);
ColumnRawPtrs arguments_raw(arguments.size());
diff --git
a/be/src/vec/aggregate_functions/aggregate_function_group_array_intersect.h
b/be/src/vec/aggregate_functions/aggregate_function_group_array_intersect.h
index 48e4567af79..dd0982902ea 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_group_array_intersect.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_group_array_intersect.h
@@ -80,8 +80,8 @@ struct AggregateFunctionGroupArrayIntersectData {
const ColVecType* nested_column_data = nullptr;
if (is_column_data_nullable) {
- auto* const_col_data = const_cast<IColumn*>(&column_data);
- col_null = static_cast<ColumnNullable*>(const_col_data);
+ const auto* const_col_data = &column_data;
+ col_null = static_cast<const ColumnNullable*>(const_col_data);
nested_column_data = &assert_cast<const ColVecType&,
TypeCheckOnRelease::DISABLE>(
col_null->get_nested_column());
} else {
@@ -364,11 +364,10 @@ public:
const auto arr_size = offsets[row_num] - offset;
const auto& column_data = column.get_data();
const bool is_column_data_nullable = column_data.is_nullable();
- ColumnNullable* col_null = nullptr;
+ const ColumnNullable* col_null = nullptr;
if (is_column_data_nullable) {
- auto const_col_data = const_cast<IColumn*>(&column_data);
- col_null = static_cast<ColumnNullable*>(const_col_data);
+ col_null = static_cast<const ColumnNullable*>(&column_data);
}
auto process_element = [&](size_t i) {
diff --git a/be/src/vec/aggregate_functions/aggregate_function_java_udaf.h
b/be/src/vec/aggregate_functions/aggregate_function_java_udaf.h
index ee44fd68466..2e63275cdad 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_java_udaf.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_java_udaf.h
@@ -398,8 +398,7 @@ public:
}
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable&
buf) const override {
- Status st = this->data(const_cast<AggregateDataPtr&>(_exec_place))
- .write(buf, reinterpret_cast<int64_t>(place));
+ Status st = this->data(_exec_place).write(buf,
reinterpret_cast<int64_t>(place));
if (UNLIKELY(!st.ok())) {
throw doris::Exception(ErrorCode::INTERNAL_ERROR, st.to_string());
}
diff --git
a/be/src/vec/aggregate_functions/aggregate_function_orthogonal_bitmap.h
b/be/src/vec/aggregate_functions/aggregate_function_orthogonal_bitmap.h
index 7ecb95c8665..81023182caf 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_orthogonal_bitmap.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_orthogonal_bitmap.h
@@ -283,10 +283,8 @@ public:
void get(IColumn& to) const {
auto& column = assert_cast<ColumnBitmap&>(to);
- column.get_data().emplace_back(!result.empty()
- ? result
- :
const_cast<AggOrthBitMapExprCal*>(this)
-
->bitmap_expr_cal.bitmap_calculate());
+ column.get_data().emplace_back(!result.empty() ? result
+ :
this->bitmap_expr_cal.bitmap_calculate());
}
void reset() {
@@ -326,8 +324,7 @@ public:
void get(IColumn& to) const {
auto& column = assert_cast<ColumnInt64&>(to);
column.get_data().emplace_back(result ? result
- :
const_cast<AggOrthBitMapExprCalCount*>(this)
-
->bitmap_expr_cal.bitmap_calculate_count());
+ :
this->bitmap_expr_cal.bitmap_calculate_count());
}
void reset() {
@@ -405,6 +402,8 @@ public:
}
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable&
buf) const override {
+ // place is essentially an AggregateDataPtr, passed as a
ConstAggregateDataPtr.
+ // todo: rethink the write method to determine whether const_cast is
necessary.
this->data(const_cast<AggregateDataPtr>(place)).write(buf);
}
diff --git a/be/src/vec/aggregate_functions/aggregate_function_percentile.h
b/be/src/vec/aggregate_functions/aggregate_function_percentile.h
index f398dbebdc3..5a711c39272 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_percentile.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_percentile.h
@@ -412,9 +412,7 @@ struct PercentileState {
if (vec_quantile[i] == -1.0) {
vec_quantile[i] = rhs.vec_quantile[i];
}
- vec_counts[i].merge(
- const_cast<Counts<typename
PrimitiveTypeTraits<T>::ColumnItemType>*>(
- &(rhs.vec_counts[i])));
+ vec_counts[i].merge(&(rhs.vec_counts[i]));
}
}
diff --git
a/be/src/vec/aggregate_functions/aggregate_function_percentile_reservoir.h
b/be/src/vec/aggregate_functions/aggregate_function_percentile_reservoir.h
index 19c3476b0f7..598d5d97409 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_percentile_reservoir.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_percentile_reservoir.h
@@ -58,6 +58,8 @@ struct QuantileReservoirSampler {
}
double get() const {
+ // The caller is a ConstAggregateDataPtr, but it itself is an
AggregateDataPtr.
+ // To call a non-const method here, a const_cast is required.
return
const_cast<ReservoirSampler&>(data).quantileInterpolated(this->level);
}
diff --git a/be/src/vec/aggregate_functions/aggregate_function_quantile_state.h
b/be/src/vec/aggregate_functions/aggregate_function_quantile_state.h
index bb00a232976..5894c509681 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_quantile_state.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_quantile_state.h
@@ -89,6 +89,8 @@ struct AggregateFunctionQuantileStateData {
void reset() { is_first = true; }
DataType& get() { return value; }
+
+ const DataType& get() const { return value; }
};
template <bool arg_is_nullable, typename Op>
@@ -131,8 +133,7 @@ public:
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
Arena&) const override {
- this->data(place).merge(
-
const_cast<AggregateFunctionQuantileStateData<Op>&>(this->data(rhs)).get());
+ this->data(place).merge(this->data(rhs).get());
}
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable&
buf) const override {
@@ -146,8 +147,7 @@ public:
void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn&
to) const override {
auto& column = assert_cast<ColVecResult&>(to);
- column.get_data().push_back(
-
const_cast<AggregateFunctionQuantileStateData<Op>&>(this->data(place)).get());
+ column.get_data().push_back(this->data(place).get());
}
void reset(AggregateDataPtr __restrict place) const override {
this->data(place).reset(); }
diff --git a/be/src/vec/aggregate_functions/aggregate_function_rpc.h
b/be/src/vec/aggregate_functions/aggregate_function_rpc.h
index b19fd25e10e..1d5dc5799d8 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_rpc.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_rpc.h
@@ -378,10 +378,14 @@ public:
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
Arena&) const override {
+ // place is essentially an AggregateDataPtr, passed as a
ConstAggregateDataPtr.
+ // todo: rethink the merge method to determine whether const_cast is
necessary.
static_cast<void>(this->data(place).merge(this->data(const_cast<AggregateDataPtr>(rhs))));
}
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable&
buf) const override {
+ // place is essentially an AggregateDataPtr, passed as a
ConstAggregateDataPtr.
+ // todo: rethink the serialize method to determine whether const_cast
is necessary.
this->data(const_cast<AggregateDataPtr&>(place)).serialize(buf);
}
@@ -391,6 +395,8 @@ public:
}
void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn&
to) const override {
+ // place is essentially an AggregateDataPtr, passed as a
ConstAggregateDataPtr.
+ // todo: rethink the get method to determine whether const_cast is
necessary.
static_cast<void>(this->data(const_cast<AggregateDataPtr>(place)).get(to,
_return_type));
}
diff --git a/be/src/vec/aggregate_functions/aggregate_function_sequence_match.h
b/be/src/vec/aggregate_functions/aggregate_function_sequence_match.h
index 96846e8a457..6bfe8558750 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_sequence_match.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_sequence_match.h
@@ -128,6 +128,7 @@ public:
conditions_met |= other.conditions_met;
}
+ // todo: rethink the sort method.
void sort() {
if (sorted) return;
@@ -676,6 +677,7 @@ public:
output.push_back(false);
return;
}
+ // place is essentially an AggregateDataPtr, passed as a
ConstAggregateDataPtr.
this->data(const_cast<AggregateDataPtr>(place)).sort();
const auto& data_ref = this->data(place);
@@ -722,6 +724,7 @@ public:
output.push_back(0);
return;
}
+ // place is essentially an AggregateDataPtr, passed as a
ConstAggregateDataPtr.
this->data(const_cast<AggregateDataPtr>(place)).sort();
output.push_back(count(place));
}
diff --git a/be/src/vec/aggregate_functions/aggregate_function_sort.h
b/be/src/vec/aggregate_functions/aggregate_function_sort.h
index f787b0430ee..79fdf4979b1 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_sort.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_sort.h
@@ -164,6 +164,7 @@ public:
}
void insert_result_into(ConstAggregateDataPtr targetplace, IColumn& to)
const override {
+ // place is essentially an AggregateDataPtr, passed as a
ConstAggregateDataPtr.
auto* place = const_cast<AggregateDataPtr>(targetplace);
Arena arena;
if (!this->data(place).block.is_empty_column()) {
diff --git a/be/src/vec/aggregate_functions/aggregate_function_window_funnel.h
b/be/src/vec/aggregate_functions/aggregate_function_window_funnel.h
index afad283f7f6..7dbef1a17fb 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_window_funnel.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_window_funnel.h
@@ -134,6 +134,7 @@ struct WindowFunnelState {
}
}
+ // todo: rethink thid sort method.
void sort() {
auto num = events_list.size();
std::vector<size_t> indices(num);
@@ -394,6 +395,7 @@ public:
}
void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn&
to) const override {
+ // place is essentially an AggregateDataPtr, passed as a
ConstAggregateDataPtr.
this->data(const_cast<AggregateDataPtr>(place)).sort();
assert_cast<ColumnInt32&>(to).get_data().push_back(
IAggregateFunctionDataHelper<WindowFunnelState,
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]