Skip to content

Commit 713108d

Browse files
authored
[fix] remove useless const_cast and explain const_cast for vec (6) (#56610)
### What problem does this PR solve? 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 bbd7d55 commit 713108d

14 files changed

Lines changed: 41 additions & 36 deletions

‎be/src/vec/common/hash_table/hash_map_context.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ struct MethodKeysFixed : public MethodBase<TData> {
568568
if (is_column_nullable(*key_columns[i])) {
569569
auto& nullable_col = assert_cast<ColumnNullable&>(*key_columns[i]);
570570

571+
// nullable_col is obtained via key_columns and is itself a mutable element. However, when accessed
572+
// through get_raw_data().data, it yields a const char*, necessitating the use of const_cast.
571573
data = const_cast<char*>(nullable_col.get_nested_column().get_raw_data().data);
572574
UInt8* nullmap = assert_cast<ColumnUInt8*>(&nullable_col.get_null_map_column())
573575
->get_data()
@@ -582,6 +584,8 @@ struct MethodKeysFixed : public MethodBase<TData> {
582584
(reinterpret_cast<const UInt8*>(&input_keys[j])[bucket] >> offset) & 1;
583585
}
584586
} else {
587+
// key_columns is a mutable element. However, when accessed through get_raw_data().data,
588+
// it yields a const char*, necessitating the use of const_cast.
585589
data = const_cast<char*>(key_columns[i]->get_raw_data().data);
586590
}
587591

‎be/src/vec/common/hash_table/hash_table.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ class HashTable : private boost::noncopyable,
921921
}
922922

923923
ConstLookupResult ALWAYS_INLINE find(Key x) const {
924+
// to call a non-const function without making any modifications, using const_cast is acceptable.
924925
return const_cast<std::decay_t<decltype(*this)>*>(this)->find(x);
925926
}
926927

‎be/src/vec/common/hash_table/string_hash_table.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,27 +418,27 @@ class StringHashTable : private boost::noncopyable {
418418
auto& operator*() const {
419419
switch (sub_table_index) {
420420
case 0: {
421-
const_cast<iterator_base*>(this)->cell = *(container->m0.zero_value());
421+
this->cell = *(container->m0.zero_value());
422422
break;
423423
}
424424
case 1: {
425-
const_cast<iterator_base*>(this)->cell = *iterator1;
425+
this->cell = *iterator1;
426426
break;
427427
}
428428
case 2: {
429-
const_cast<iterator_base*>(this)->cell = *iterator2;
429+
this->cell = *iterator2;
430430
break;
431431
}
432432
case 3: {
433-
const_cast<iterator_base*>(this)->cell = *iterator3;
433+
this->cell = *iterator3;
434434
break;
435435
}
436436
case 4: {
437-
const_cast<iterator_base*>(this)->cell = *iterator4;
437+
this->cell = *iterator4;
438438
break;
439439
}
440440
case 5: {
441-
const_cast<iterator_base*>(this)->cell = *iterator5;
441+
this->cell = *iterator5;
442442
break;
443443
}
444444
}

‎be/src/vec/common/pod_array.h‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -748,25 +748,22 @@ class PODArray : public PODArrayBase<sizeof(T), initial_bytes, TAllocator, pad_r
748748

749749
void assign(const PODArray& from) { assign(from.begin(), from.end()); }
750750

751-
void erase(const_iterator first, const_iterator last) {
752-
auto first_no_const = const_cast<iterator>(first);
753-
auto last_no_const = const_cast<iterator>(last);
754-
751+
void erase(iterator first, iterator last) {
755752
size_t items_to_move = end() - last;
756753

757754
while (items_to_move != 0) {
758-
*first_no_const = *last_no_const;
755+
*first = *last;
759756

760-
++first_no_const;
761-
++last_no_const;
757+
++first;
758+
++last;
762759

763760
--items_to_move;
764761
}
765762

766-
this->c_end = reinterpret_cast<char*>(first_no_const);
763+
this->c_end = reinterpret_cast<char*>(first);
767764
}
768765

769-
void erase(const_iterator pos) { this->erase(pos, pos + 1); }
766+
void erase(iterator pos) { this->erase(pos, pos + 1); }
770767

771768
bool operator==(const PODArray& rhs) const {
772769
if (this->size() != rhs.size()) {

‎be/src/vec/common/space_saving.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct SpaceSavingArena<StringRef> {
4848

4949
void free(StringRef key) {
5050
if (key.data) {
51+
// const_cast used for arena memory reclamation, no further modifications will occur
5152
arena.free(const_cast<char*>(key.data), key.size);
5253
}
5354
}

‎be/src/vec/common/string_buffer.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class BufferReadable {
242242
read((char*)&len, 1);
243243
auto ref = read(len);
244244
// read data and set it to x per byte.
245-
char* bytes = const_cast<char*>(ref.data);
245+
const char* bytes = ref.data;
246246
for (size_t i = 0; i < 9; ++i) {
247247
UInt64 byte = bytes[i];
248248
x |= (byte & 0x7F) << (7 * i);

‎be/src/vec/core/block.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ Block::Block(const std::vector<SlotDescriptor>& slots, size_t block_size,
107107
bool ignore_trivial_slot) {
108108
std::vector<SlotDescriptor*> slot_ptrs(slots.size());
109109
for (size_t i = 0; i < slots.size(); ++i) {
110+
// Slots remain unmodified and are used to read column information; const_cast can be employed.
111+
// used in src/exec/rowid_fetcher.cpp
110112
slot_ptrs[i] = const_cast<SlotDescriptor*>(&slots[i]);
111113
}
112114
*this = Block(slot_ptrs, block_size, ignore_trivial_slot);

‎be/src/vec/exprs/vdirect_in_predicate.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class VDirectInPredicate final : public VExpr {
9595
auto iter = get_set_func()->begin();
9696
while (iter->has_next()) {
9797
DCHECK(iter->get_value() != nullptr);
98-
auto* value = const_cast<void*>(iter->get_value());
98+
const void* value = iter->get_value();
9999

100100
TExprNode node = create_texpr_node_from(value, slot_data_type->get_primitive_type(),
101101
slot_data_type->get_precision(),

‎be/src/vec/exprs/vruntimefilter_wrapper.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ Status VRuntimeFilterWrapper::execute(VExprContext* context, Block* block, int*
114114
// bloom filter will handle null aware inside itself
115115
if (_null_aware && TExprNodeType::BLOOM_PRED != node_type()) {
116116
DCHECK_GE(args.size(), 1);
117-
change_null_to_true(result_column.column, block->get_by_position(args[0]).column);
117+
change_null_to_true(result_column.column->assume_mutable(),
118+
block->get_by_position(args[0]).column);
118119
}
119120

120121
return Status::OK();

‎be/src/vec/exprs/vtopn_pred.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ class VTopNPred : public VExpr {
109109

110110
if (is_nullable() && _predicate->nulls_first()) {
111111
// null values ​​are always not filtered
112-
change_null_to_true(block->get_by_position(num_columns_without_result).column);
112+
change_null_to_true(
113+
block->get_by_position(num_columns_without_result).column->assume_mutable());
113114
}
114115
return Status::OK();
115116
}

0 commit comments

Comments
 (0)