Skip to content

Commit 0ea052c

Browse files
authored
[fix] remove useless const_cast and explain const_cast for vec (1) (#56587)
go through whole be/ and find all const_cast Issue Number: #55057 Problem Summary: 1. remove useless const_cast 2. explain why using const_cast does not result in undefined behavior 3. don't modify some const_cast (1) some code in DBUG_EXECUTE_IF or test file (2) underlying data structures, such as cow (3) const_cast<const T*>
1 parent 289e628 commit 0ea052c

14 files changed

Lines changed: 46 additions & 32 deletions

‎be/src/util/bitmap_expr_calculation.h‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ class BitmapExprCalculation : public BitmapIntersect<std::string> {
5757
}
5858
}
5959

60-
BitmapValue bitmap_calculate() {
60+
BitmapValue bitmap_calculate() const {
6161
std::stack<BitmapValue> values;
6262
std::string bitmap_key;
6363
for (int i = 0; i < _polish.length(); i++) {
6464
char c = _polish.at(i);
6565
if (c == ' ') {
6666
if (bitmap_key.length() > 0) {
67-
values.push(_bitmaps[bitmap_key]);
67+
values.push(_bitmaps.at(bitmap_key));
6868
bitmap_key.clear();
6969
}
7070
} else if (c != '&' && c != '|' && c != '^' && c != '-' && c != '\\') {
@@ -75,7 +75,7 @@ class BitmapExprCalculation : public BitmapIntersect<std::string> {
7575
continue;
7676
} else {
7777
if (bitmap_key.length() > 0) {
78-
values.push(_bitmaps[bitmap_key]);
78+
values.push(_bitmaps.at(bitmap_key));
7979
bitmap_key.clear();
8080
}
8181
if (values.size() >= 2) {
@@ -91,15 +91,15 @@ class BitmapExprCalculation : public BitmapIntersect<std::string> {
9191
}
9292
BitmapValue result;
9393
if (bitmap_key.length() > 0) {
94-
result |= _bitmaps[bitmap_key];
94+
result |= _bitmaps.at(bitmap_key);
9595
} else if (!values.empty()) {
9696
result |= values.top();
9797
}
9898
return result;
9999
}
100100

101101
// calculate the bitmap value by expr bitmap calculate
102-
int64_t bitmap_calculate_count() {
102+
int64_t bitmap_calculate_count() const {
103103
if (_bitmaps.empty()) {
104104
return 0;
105105
}
@@ -193,7 +193,8 @@ class BitmapExprCalculation : public BitmapIntersect<std::string> {
193193
return print_stack(polish);
194194
}
195195

196-
void bitmap_calculate(BitmapValue& op_a, BitmapValue& op_b, char op, BitmapValue& result) {
196+
void bitmap_calculate(BitmapValue& op_a, BitmapValue& op_b, char op,
197+
BitmapValue& result) const {
197198
result |= op_b;
198199
switch (op) {
199200
case '&':

‎be/src/vec/aggregate_functions/aggregate_function.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class IAggregateFunction {
185185
const IColumn& column, Arena&) const = 0;
186186

187187
/// Inserts results into a column.
188+
// todo: Consider whether this passes a ConstAggregateDataPtr
188189
virtual void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const = 0;
189190

190191
virtual void insert_result_into_vec(const std::vector<AggregateDataPtr>& places,

‎be/src/vec/aggregate_functions/aggregate_function_bitmap.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ struct AggregateFunctionBitmapData {
147147
}
148148

149149
BitmapValue& get() { return value; }
150+
151+
const BitmapValue& get() const { return value; }
150152
};
151153

152154
template <typename Data, typename Derived>
@@ -309,8 +311,7 @@ class AggregateFunctionBitmapOp final
309311

310312
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
311313
Arena&) const override {
312-
this->data(place).merge(
313-
const_cast<AggregateFunctionBitmapData<Op>&>(this->data(rhs)).get());
314+
this->data(place).merge(this->data(rhs).get());
314315
}
315316

316317
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
@@ -324,8 +325,7 @@ class AggregateFunctionBitmapOp final
324325

325326
void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
326327
auto& column = assert_cast<ColVecResult&>(to);
327-
column.get_data().push_back(
328-
const_cast<AggregateFunctionBitmapData<Op>&>(this->data(place)).get());
328+
column.get_data().push_back(this->data(place).get());
329329
}
330330

331331
void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
@@ -442,7 +442,7 @@ class AggregateFunctionBitmapCount final
442442

443443
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
444444
Arena&) const override {
445-
this->data(place).merge(const_cast<AggFunctionData&>(this->data(rhs)).get());
445+
this->data(place).merge(this->data(rhs).get());
446446
}
447447

448448
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
@@ -455,7 +455,7 @@ class AggregateFunctionBitmapCount final
455455
}
456456

457457
void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
458-
auto& value_data = const_cast<AggFunctionData&>(this->data(place)).get();
458+
auto& value_data = this->data(place).get();
459459
auto& column = assert_cast<ColVecResult&>(to);
460460
column.get_data().push_back(value_data.cardinality());
461461
}

‎be/src/vec/aggregate_functions/aggregate_function_distinct.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ struct AggregateFunctionDistinctSingleNumericData {
108108

109109
if constexpr (stable) {
110110
argument_columns[0]->resize(data.size());
111+
// argument_columns[0] is a mutable column created using create_column.
112+
// since get_raw_data returns a StringRef, const_cast is required here.
111113
auto ptr = (typename PrimitiveTypeTraits<T>::CppType*)const_cast<char*>(
112114
argument_columns[0]->get_raw_data().data);
113115
for (auto it : data) {
@@ -310,6 +312,7 @@ class AggregateFunctionDistinct
310312
}
311313

312314
void insert_result_into(ConstAggregateDataPtr targetplace, IColumn& to) const override {
315+
// place is essentially an AggregateDataPtr, passed as a ConstAggregateDataPtr.
313316
auto* place = const_cast<AggregateDataPtr>(targetplace);
314317
auto arguments = this->data(place).get_arguments(this->argument_types);
315318
ColumnRawPtrs arguments_raw(arguments.size());

‎be/src/vec/aggregate_functions/aggregate_function_group_array_intersect.h‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ struct AggregateFunctionGroupArrayIntersectData {
8080
const ColVecType* nested_column_data = nullptr;
8181

8282
if (is_column_data_nullable) {
83-
auto* const_col_data = const_cast<IColumn*>(&column_data);
84-
col_null = static_cast<ColumnNullable*>(const_col_data);
83+
const auto* const_col_data = &column_data;
84+
col_null = static_cast<const ColumnNullable*>(const_col_data);
8585
nested_column_data = &assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(
8686
col_null->get_nested_column());
8787
} else {
@@ -364,11 +364,10 @@ class AggregateFunctionGroupArrayIntersectGeneric
364364
const auto arr_size = offsets[row_num] - offset;
365365
const auto& column_data = column.get_data();
366366
const bool is_column_data_nullable = column_data.is_nullable();
367-
ColumnNullable* col_null = nullptr;
367+
const ColumnNullable* col_null = nullptr;
368368

369369
if (is_column_data_nullable) {
370-
auto const_col_data = const_cast<IColumn*>(&column_data);
371-
col_null = static_cast<ColumnNullable*>(const_col_data);
370+
col_null = static_cast<const ColumnNullable*>(&column_data);
372371
}
373372

374373
auto process_element = [&](size_t i) {

‎be/src/vec/aggregate_functions/aggregate_function_java_udaf.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ class AggregateJavaUdaf final
398398
}
399399

400400
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
401-
Status st = this->data(const_cast<AggregateDataPtr&>(_exec_place))
402-
.write(buf, reinterpret_cast<int64_t>(place));
401+
Status st = this->data(_exec_place).write(buf, reinterpret_cast<int64_t>(place));
403402
if (UNLIKELY(!st.ok())) {
404403
throw doris::Exception(ErrorCode::INTERNAL_ERROR, st.to_string());
405404
}

‎be/src/vec/aggregate_functions/aggregate_function_orthogonal_bitmap.h‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,8 @@ struct AggOrthBitMapExprCal : public AggOrthBitmapExprCalBaseData<T> {
283283

284284
void get(IColumn& to) const {
285285
auto& column = assert_cast<ColumnBitmap&>(to);
286-
column.get_data().emplace_back(!result.empty()
287-
? result
288-
: const_cast<AggOrthBitMapExprCal*>(this)
289-
->bitmap_expr_cal.bitmap_calculate());
286+
column.get_data().emplace_back(!result.empty() ? result
287+
: this->bitmap_expr_cal.bitmap_calculate());
290288
}
291289

292290
void reset() {
@@ -326,8 +324,7 @@ struct AggOrthBitMapExprCalCount : public AggOrthBitmapExprCalBaseData<T> {
326324
void get(IColumn& to) const {
327325
auto& column = assert_cast<ColumnInt64&>(to);
328326
column.get_data().emplace_back(result ? result
329-
: const_cast<AggOrthBitMapExprCalCount*>(this)
330-
->bitmap_expr_cal.bitmap_calculate_count());
327+
: this->bitmap_expr_cal.bitmap_calculate_count());
331328
}
332329

333330
void reset() {
@@ -405,6 +402,8 @@ class AggFunctionOrthBitmapFunc final
405402
}
406403

407404
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
405+
// place is essentially an AggregateDataPtr, passed as a ConstAggregateDataPtr.
406+
// todo: rethink the write method to determine whether const_cast is necessary.
408407
this->data(const_cast<AggregateDataPtr>(place)).write(buf);
409408
}
410409

‎be/src/vec/aggregate_functions/aggregate_function_percentile.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,7 @@ struct PercentileState {
412412
if (vec_quantile[i] == -1.0) {
413413
vec_quantile[i] = rhs.vec_quantile[i];
414414
}
415-
vec_counts[i].merge(
416-
const_cast<Counts<typename PrimitiveTypeTraits<T>::ColumnItemType>*>(
417-
&(rhs.vec_counts[i])));
415+
vec_counts[i].merge(&(rhs.vec_counts[i]));
418416
}
419417
}
420418

‎be/src/vec/aggregate_functions/aggregate_function_percentile_reservoir.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ struct QuantileReservoirSampler {
5858
}
5959

6060
double get() const {
61+
// The caller is a ConstAggregateDataPtr, but it itself is an AggregateDataPtr.
62+
// To call a non-const method here, a const_cast is required.
6163
return const_cast<ReservoirSampler&>(data).quantileInterpolated(this->level);
6264
}
6365

‎be/src/vec/aggregate_functions/aggregate_function_quantile_state.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ struct AggregateFunctionQuantileStateData {
8989
void reset() { is_first = true; }
9090

9191
DataType& get() { return value; }
92+
93+
const DataType& get() const { return value; }
9294
};
9395

9496
template <bool arg_is_nullable, typename Op>
@@ -131,8 +133,7 @@ class AggregateFunctionQuantileStateOp final
131133

132134
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
133135
Arena&) const override {
134-
this->data(place).merge(
135-
const_cast<AggregateFunctionQuantileStateData<Op>&>(this->data(rhs)).get());
136+
this->data(place).merge(this->data(rhs).get());
136137
}
137138

138139
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
@@ -146,8 +147,7 @@ class AggregateFunctionQuantileStateOp final
146147

147148
void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
148149
auto& column = assert_cast<ColVecResult&>(to);
149-
column.get_data().push_back(
150-
const_cast<AggregateFunctionQuantileStateData<Op>&>(this->data(place)).get());
150+
column.get_data().push_back(this->data(place).get());
151151
}
152152

153153
void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }

0 commit comments

Comments
 (0)