Skip to content

Commit 1169f85

Browse files
wumeibanfaxylaaaaa
authored andcommitted
[fix] remove useless const_cast and explain const_cast for vec (4) (apache#56603)
go through whole be/ and find all const_cast Issue Number: apache#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 4baa74e commit 1169f85

14 files changed

Lines changed: 57 additions & 20 deletions

‎be/src/olap/rowset/segment_v2/column_reader.cpp‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,11 +1844,10 @@ Status FileColumnIterator::next_batch(size_t* n, vectorized::MutableColumnPtr& d
18441844
DCHECK_EQ(this_run, num_rows);
18451845
} else {
18461846
*has_null = true;
1847-
const auto* null_col =
1847+
auto* null_col =
18481848
vectorized::check_and_get_column<vectorized::ColumnNullable>(dst.get());
18491849
if (null_col != nullptr) {
1850-
const_cast<vectorized::ColumnNullable*>(null_col)->insert_many_defaults(
1851-
this_run);
1850+
null_col->insert_many_defaults(this_run);
18521851
} else {
18531852
return Status::InternalError("unexpected column type in column reader");
18541853
}
@@ -1916,15 +1915,14 @@ Status FileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t co
19161915
auto origin_index = _page.data_decoder->current_index();
19171916
if (this_read_count > 0) {
19181917
if (is_null) {
1919-
const auto* null_col =
1918+
auto* null_col =
19201919
vectorized::check_and_get_column<vectorized::ColumnNullable>(
19211920
dst.get());
19221921
if (UNLIKELY(null_col == nullptr)) {
19231922
return Status::InternalError("unexpected column type in column reader");
19241923
}
19251924

1926-
const_cast<vectorized::ColumnNullable*>(null_col)->insert_many_defaults(
1927-
this_read_count);
1925+
null_col->insert_many_defaults(this_read_count);
19281926
} else {
19291927
size_t read_count = this_read_count;
19301928

‎be/src/vec/columns/column.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ std::string IColumn::dump_structure() const {
3636
res << ", " << subcolumn->dump_structure();
3737
};
3838

39+
// simply read using for_each_subcolumn without modification; const_cast can be used.
3940
const_cast<IColumn*>(this)->for_each_subcolumn(callback);
4041

4142
res << ")";

‎be/src/vec/columns/column.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ class IColumn : public COW<IColumn> {
321321
/// Returns pointer to the position after the read data.
322322
virtual const char* deserialize_and_insert_from_arena(const char* pos) = 0;
323323

324+
// todo: Consider replacing stringref with slice.
324325
virtual void serialize_vec(StringRef* keys, size_t num_rows) const {
325326
throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
326327
"Method serialize_vec is not supported for " + get_name());
@@ -609,7 +610,7 @@ class IColumn : public COW<IColumn> {
609610
*
610611
* To avoid confusion between these cases, we don't have isContiguous method.
611612
*/
612-
613+
// todo: We should support a non-const version of get_raw_data that returns a Slice.
613614
virtual StringRef get_raw_data() const {
614615
throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
615616
"Column {} is not a contiguous block of memory", get_name());
@@ -752,6 +753,11 @@ const Type* check_and_get_column(const IColumn& column) {
752753
return typeid_cast<const Type*>(&column);
753754
}
754755

756+
template <typename Type>
757+
Type* check_and_get_column(IColumn& column) {
758+
return typeid_cast<Type*>(&column);
759+
}
760+
755761
template <typename Type>
756762
const Type* check_and_get_column(const IColumn* column) {
757763
return typeid_cast<const Type*>(column);
@@ -780,7 +786,7 @@ ColumnType::Ptr check_and_get_column_ptr(const ColumnPtr& column) {
780786
if (raw_type_ptr == nullptr) {
781787
return nullptr;
782788
}
783-
return typename ColumnType::Ptr(const_cast<ColumnType*>(raw_type_ptr));
789+
return typename ColumnType::Ptr(raw_type_ptr);
784790
}
785791

786792
/// True if column's an ColumnConst instance. It's just a syntax sugar for type check.

‎be/src/vec/columns/column_array.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ size_t ColumnArray::get_max_row_byte_size() const {
292292

293293
void ColumnArray::serialize_vec(StringRef* keys, size_t num_rows) const {
294294
for (size_t i = 0; i < num_rows; ++i) {
295+
// Used in hash_map_context.h, this address is allocated via Arena,
296+
// but passed through StringRef, so using const_cast is acceptable.
295297
keys[i].size += serialize_impl(const_cast<char*>(keys[i].data + keys[i].size), i);
296298
}
297299
}

‎be/src/vec/columns/column_const.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ class ColumnConst final : public COWHelper<IColumn, ColumnConst> {
207207
void serialize_vec(StringRef* keys, size_t num_rows) const override {
208208
DCHECK_EQ(data->size(), 1);
209209
for (size_t i = 0; i < num_rows; i++) {
210+
// Used in hash_map_context.h, this address is allocated via Arena,
211+
// but passed through StringRef, so using const_cast is acceptable.
210212
serialize_impl(const_cast<char*>(keys[i].data + keys[i].size), i);
211213
}
212214
}

‎be/src/vec/columns/column_decimal.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ size_t ColumnDecimal<T>::get_max_row_byte_size() const {
8888
template <PrimitiveType T>
8989
void ColumnDecimal<T>::serialize_vec(StringRef* keys, size_t num_rows) const {
9090
for (size_t i = 0; i < num_rows; ++i) {
91+
// Used in hash_map_context.h, this address is allocated via Arena,
92+
// but passed through StringRef, so using const_cast is acceptable.
9193
keys[i].size += serialize_impl(const_cast<char*>(keys[i].data + keys[i].size), i);
9294
}
9395
}

‎be/src/vec/columns/column_map.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ Status ColumnMap::deduplicate_keys(bool recursive) {
563563
values_column_ = (assert_cast<ColumnNullable&>(*values_column)).get_nested_column_ptr();
564564
}
565565

566-
if (auto* values_map = check_and_get_column<ColumnMap>(values_column_.get())) {
567-
RETURN_IF_ERROR(const_cast<ColumnMap*>(values_map)->deduplicate_keys(recursive));
566+
if (ColumnMap* values_map = check_and_get_column<ColumnMap>(values_column_.get())) {
567+
RETURN_IF_ERROR(values_map->deduplicate_keys(recursive));
568568
}
569569
}
570570

@@ -764,6 +764,8 @@ void ColumnMap::sort_column(const ColumnSorter* sorter, EqualFlags& flags,
764764

765765
void ColumnMap::serialize_vec(StringRef* keys, size_t num_rows) const {
766766
for (size_t i = 0; i < num_rows; ++i) {
767+
// Used in hash_map_context.h, this address is allocated via Arena,
768+
// but passed through StringRef, so using const_cast is acceptable.
767769
keys[i].size += serialize_impl(const_cast<char*>(keys[i].data + keys[i].size), i);
768770
}
769771
}

‎be/src/vec/columns/column_nullable.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,15 @@ void ColumnNullable::serialize_vec(StringRef* keys, size_t num_rows) const {
278278
const bool has_null = simd::contain_one(get_null_map_data().data(), num_rows);
279279
if (has_null) {
280280
for (size_t i = 0; i < num_rows; ++i) {
281+
// Used in hash_map_context.h, this address is allocated via Arena,
282+
// but passed through StringRef, so using const_cast is acceptable.
281283
keys[i].size += serialize_impl(const_cast<char*>(keys[i].data + keys[i].size), i);
282284
}
283285
} else {
284286
const auto& arr = get_null_map_data();
285287
for (size_t i = 0; i < num_rows; ++i) {
288+
// Used in hash_map_context.h, this address is allocated via Arena,
289+
// but passed through StringRef, so using const_cast is acceptable.
286290
memcpy_fixed<NullMap::value_type>(const_cast<char*>(keys[i].data + keys[i].size),
287291
(char*)&arr[i]);
288292
keys[i].size += sizeof(NullMap::value_type);
@@ -397,13 +401,12 @@ size_t ColumnNullable::filter(const Filter& filter) {
397401

398402
Status ColumnNullable::filter_by_selector(const uint16_t* sel, size_t sel_size, IColumn* col_ptr) {
399403
auto* nullable_col_ptr = assert_cast<ColumnNullable*>(col_ptr);
400-
ColumnPtr nest_col_ptr = nullable_col_ptr->_nested_column;
404+
WrappedPtr nest_col_ptr = nullable_col_ptr->_nested_column;
401405

402406
/// `get_null_map_data` will set `_need_update_has_null` to true
403407
auto& res_nullmap = nullable_col_ptr->get_null_map_data();
404408

405-
RETURN_IF_ERROR(get_nested_column().filter_by_selector(
406-
sel, sel_size, const_cast<doris::vectorized::IColumn*>(nest_col_ptr.get())));
409+
RETURN_IF_ERROR(get_nested_column().filter_by_selector(sel, sel_size, nest_col_ptr.get()));
407410
DCHECK(res_nullmap.empty());
408411
res_nullmap.resize(sel_size);
409412
auto& cur_nullmap = get_null_map_column().get_data();

‎be/src/vec/columns/column_string.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ size_t ColumnStr<T>::get_max_row_byte_size() const {
487487
template <typename T>
488488
void ColumnStr<T>::serialize_vec(StringRef* keys, size_t num_rows) const {
489489
for (size_t i = 0; i < num_rows; ++i) {
490+
// Used in hash_map_context.h, this address is allocated via Arena,
491+
// but passed through StringRef, so using const_cast is acceptable.
490492
keys[i].size += serialize_impl(const_cast<char*>(keys[i].data + keys[i].size), i);
491493
}
492494
}

‎be/src/vec/columns/column_struct.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ void ColumnStruct::sort_column(const ColumnSorter* sorter, EqualFlags& flags,
451451

452452
void ColumnStruct::serialize_vec(StringRef* keys, size_t num_rows) const {
453453
for (size_t i = 0; i < num_rows; ++i) {
454+
// Used in hash_map_context.h, this address is allocated via Arena,
455+
// but passed through StringRef, so using const_cast is acceptable.
454456
keys[i].size += serialize_impl(const_cast<char*>(keys[i].data + keys[i].size), i);
455457
}
456458
}

0 commit comments

Comments
 (0)