Skip to content

Commit 5933286

Browse files
wumeibanfaYour Name
authored andcommitted
[fix] remove useless const_cast and explain const_cast for vec (3) (#56600)
### 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*> ### Release note None ### Check List (For Author) - Test <!-- At least one of them must be included. --> - [ ] Regression test - [ ] Unit Test - [ ] Manual test (add detailed scripts or steps below) - [x] No need to test or manual test. Explain why: - [ ] This is a refactor/code format and no logic has been changed. - [x] Previous test can cover this change. - [ ] No code files have been changed. - [ ] Other reason <!-- Add your reason? --> - Behavior changed: - [x] No. - [ ] Yes. <!-- Explain the behavior change --> - Does this need documentation? - [x] No. - [ ] Yes. <!-- Add document PR link here. eg: apache/doris-website#1214 --> ### Check List (For Reviewer who merge this PR) - [ ] Confirm the release note - [ ] Confirm test cases - [ ] Confirm document - [ ] Add branch pick label <!-- Add branch pick label that this PR should merge into -->
1 parent 522414c commit 5933286

16 files changed

Lines changed: 139 additions & 132 deletions

‎be/src/exprs/math_functions.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ StringRef MathFunctions::decimal_to_base(FunctionContext* ctx, int64_t src_num,
125125
buf[buf_index] = '-';
126126
++result_len;
127127
}
128+
129+
// Modify a string passed via stringref
128130
StringRef result = ctx->create_temp_string_val(result_len);
129131
memcpy(const_cast<char*>(result.data), buf + max_digits - result_len, result_len);
130132
return result;

‎be/src/olap/like_column_predicate.cpp‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ uint16_t LikeColumnPredicate<T>::_evaluate_inner(const vectorized::IColumn& colu
9191
sel[new_size] = idx;
9292
unsigned char flag = 0;
9393
THROW_IF_ERROR((_state->scalar_function)(
94-
const_cast<vectorized::LikeSearchState*>(&_like_state),
95-
str_col->get_data_at(idx), pattern, &flag));
94+
&_like_state, str_col->get_data_at(idx), pattern, &flag));
9695
new_size += _opposite ^ flag;
9796
}
9897
} else {
@@ -107,8 +106,8 @@ uint16_t LikeColumnPredicate<T>::_evaluate_inner(const vectorized::IColumn& colu
107106
StringRef cell_value = str_col->get_data_at(idx);
108107
unsigned char flag = 0;
109108
THROW_IF_ERROR((_state->scalar_function)(
110-
const_cast<vectorized::LikeSearchState*>(&_like_state),
111-
StringRef(cell_value.data, cell_value.size), pattern, &flag));
109+
&_like_state, StringRef(cell_value.data, cell_value.size), pattern,
110+
&flag));
112111
new_size += _opposite ^ flag;
113112
}
114113
}
@@ -134,9 +133,8 @@ uint16_t LikeColumnPredicate<T>::_evaluate_inner(const vectorized::IColumn& colu
134133
uint16_t idx = sel[i];
135134
sel[new_size] = idx;
136135
unsigned char flag = 0;
137-
THROW_IF_ERROR((_state->scalar_function)(
138-
const_cast<vectorized::LikeSearchState*>(&_like_state),
139-
str_col->get_data_at(idx), pattern, &flag));
136+
THROW_IF_ERROR((_state->scalar_function)(&_like_state, str_col->get_data_at(idx),
137+
pattern, &flag));
140138
new_size += _opposite ^ flag;
141139
}
142140
}

‎be/src/olap/like_column_predicate.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ class LikeColumnPredicate : public ColumnPredicate {
154154
StringRef cell_value = column.get_shrink_value(i);
155155
unsigned char flag = 0;
156156
THROW_IF_ERROR((_state->scalar_function)(
157-
const_cast<vectorized::LikeSearchState*>(&_like_state),
158-
StringRef(cell_value.data, cell_value.size), pattern, &flag));
157+
&_like_state, StringRef(cell_value.data, cell_value.size), pattern, &flag));
159158
tmp_res[i] = flag;
160159
}
161160
// Sometimes the dict is not initialized when run comparison predicate here, for example,

‎be/src/util/bitmap_value.h‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ class BitmapValue {
889889
DCHECK(res);
890890
}
891891

892+
// !FIXME: We should rethink the logic here
892893
BitmapValue(const BitmapValue& other) {
893894
_type = other._type;
894895
switch (other._type) {
@@ -909,10 +910,11 @@ class BitmapValue {
909910
_is_shared = true;
910911
// should also set other's state to shared, so that other bitmap value will
911912
// create a new bitmap when it wants to modify it.
912-
const_cast<BitmapValue&>(other)._is_shared = true;
913+
other._is_shared = true;
913914
}
914915
}
915916

917+
// !FIXME: We should rethink the logic here
916918
BitmapValue(BitmapValue&& other) noexcept {
917919
_type = other._type;
918920
switch (other._type) {
@@ -934,6 +936,7 @@ class BitmapValue {
934936
other._is_shared = false;
935937
}
936938

939+
// !FIXME: We should rethink the logic here
937940
BitmapValue& operator=(const BitmapValue& other) {
938941
if (this == &other) {
939942
return *this;
@@ -958,7 +961,7 @@ class BitmapValue {
958961
_is_shared = true;
959962
// should also set other's state to shared, so that other bitmap value will
960963
// create a new bitmap when it wants to modify it.
961-
const_cast<BitmapValue&>(other)._is_shared = true;
964+
other._is_shared = true;
962965
}
963966
return *this;
964967
}
@@ -1219,6 +1222,7 @@ class BitmapValue {
12191222
// EMPTY -> SINGLE
12201223
// EMPTY -> BITMAP
12211224
// SINGLE -> BITMAP
1225+
// !FIXME: We should rethink the logic here
12221226
BitmapValue& operator|=(const BitmapValue& rhs) {
12231227
switch (rhs._type) {
12241228
case EMPTY:
@@ -1230,13 +1234,13 @@ class BitmapValue {
12301234
switch (_type) {
12311235
case EMPTY:
12321236
_bitmap = rhs._bitmap;
1233-
const_cast<BitmapValue&>(rhs)._is_shared = true;
1237+
rhs._is_shared = true;
12341238
_is_shared = true;
12351239
_type = BITMAP;
12361240
break;
12371241
case SINGLE:
12381242
_bitmap = rhs._bitmap;
1239-
const_cast<BitmapValue&>(rhs)._is_shared = true;
1243+
rhs._is_shared = true;
12401244
_is_shared = true;
12411245
_prepare_bitmap_for_write();
12421246
_bitmap->add(_sv);
@@ -1529,6 +1533,7 @@ class BitmapValue {
15291533
// SINGLE -> EMPTY
15301534
// BITMAP -> EMPTY
15311535
// BITMAP -> SINGLE
1536+
// !FIXME: We should rethink the logic here
15321537
BitmapValue& operator^=(const BitmapValue& rhs) {
15331538
switch (rhs._type) {
15341539
case EMPTY:
@@ -1566,13 +1571,13 @@ class BitmapValue {
15661571
switch (_type) {
15671572
case EMPTY:
15681573
_bitmap = rhs._bitmap;
1569-
const_cast<BitmapValue&>(rhs)._is_shared = true;
1574+
rhs._is_shared = true;
15701575
_is_shared = true;
15711576
_type = BITMAP;
15721577
break;
15731578
case SINGLE:
15741579
_bitmap = rhs._bitmap;
1575-
const_cast<BitmapValue&>(rhs)._is_shared = true;
1580+
rhs._is_shared = true;
15761581
_is_shared = true;
15771582
_type = BITMAP;
15781583
_prepare_bitmap_for_write();
@@ -1870,7 +1875,8 @@ class BitmapValue {
18701875

18711876
// Return how many bytes are required to serialize this bitmap.
18721877
// See BitmapTypeCode for the serialized format.
1873-
size_t getSizeInBytes() {
1878+
// !FIXME: We should rethink the logic here
1879+
size_t getSizeInBytes() const {
18741880
size_t res = 0;
18751881
switch (_type) {
18761882
case EMPTY:
@@ -2129,7 +2135,7 @@ class BitmapValue {
21292135
* Return new set with specified range (not include the range_end)
21302136
*/
21312137
int64_t sub_range(const int64_t& range_start, const int64_t& range_end,
2132-
BitmapValue* ret_bitmap) {
2138+
BitmapValue* ret_bitmap) const {
21332139
switch (_type) {
21342140
case EMPTY:
21352141
return 0;
@@ -2181,7 +2187,7 @@ class BitmapValue {
21812187
* @return the real count for subset, maybe less than cardinality_limit
21822188
*/
21832189
int64_t sub_limit(const int64_t& range_start, const int64_t& cardinality_limit,
2184-
BitmapValue* ret_bitmap) {
2190+
BitmapValue* ret_bitmap) const {
21852191
switch (_type) {
21862192
case EMPTY:
21872193
return 0;
@@ -2236,7 +2242,8 @@ class BitmapValue {
22362242
* The number of returned elements is limited by the cardinality_limit parameter.
22372243
* Analog of the substring string function, but for bitmap.
22382244
*/
2239-
int64_t offset_limit(const int64_t& offset, const int64_t& limit, BitmapValue* ret_bitmap) {
2245+
int64_t offset_limit(const int64_t& offset, const int64_t& limit,
2246+
BitmapValue* ret_bitmap) const {
22402247
switch (_type) {
22412248
case EMPTY:
22422249
return 0;
@@ -2393,7 +2400,7 @@ class BitmapValue {
23932400
return result;
23942401
}
23952402

2396-
void _prepare_bitmap_for_write() {
2403+
void _prepare_bitmap_for_write() const {
23972404
if (!_bitmap) {
23982405
_bitmap = std::make_shared<detail::Roaring64Map>();
23992406
_is_shared = false;
@@ -2431,12 +2438,13 @@ class BitmapValue {
24312438
BITMAP = 2, // more than one elements
24322439
SET = 3 // elements count less or equal than 32
24332440
};
2434-
uint64_t _sv = 0; // store the single value when _type == SINGLE
2435-
std::shared_ptr<detail::Roaring64Map> _bitmap; // used when _type == BITMAP
2441+
uint64_t _sv = 0; // store the single value when _type == SINGLE
2442+
// !FIXME: We should rethink the logic about _bitmap and _is_shared
2443+
mutable std::shared_ptr<detail::Roaring64Map> _bitmap; // used when _type == BITMAP
24362444
SetContainer<uint64_t> _set;
24372445
BitmapDataType _type {EMPTY};
24382446
// Indicate whether the state is shared among multi BitmapValue object
2439-
bool _is_shared = true;
2447+
mutable bool _is_shared = true;
24402448
static constexpr uint64_t SET_TYPE_THRESHOLD = 32;
24412449
};
24422450

‎be/src/util/simd/vstring_function.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,20 @@ class VStringFunctions {
211211
return validate_ascii_fast(str.data, str.size);
212212
}
213213

214-
static void reverse(const StringRef& str, StringRef dst) {
214+
static void reverse(const StringRef& str, std::string* dst) {
215215
if (is_ascii(str)) {
216216
int64_t begin = 0;
217217
int64_t end = str.size;
218-
int64_t result_end = dst.size - 1;
218+
int64_t result_end = dst->size() - 1;
219219

220220
// auto SIMD here
221-
auto* __restrict l = const_cast<char*>(dst.data);
221+
auto* __restrict l = dst->data();
222222
auto* __restrict r = str.data;
223223
for (; begin < end; ++begin, --result_end) {
224224
l[result_end] = r[begin];
225225
}
226226
} else {
227-
char* dst_data = const_cast<char*>(dst.data);
227+
char* dst_data = dst->data();
228228
for (size_t i = 0, char_size = 0; i < str.size; i += char_size) {
229229
char_size = UTF8_BYTE_LENGTH[(unsigned char)(str.data)[i]];
230230
// there exists occasion where the last character is an illegal UTF-8 one which returns

‎be/src/vec/data_types/data_type_bitmap.cpp‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int64_t DataTypeBitMap::get_uncompressed_serialized_bytes(const IColumn& column,
5050
auto allocate_len_size = sizeof(size_t) * real_need_copy_num;
5151
size_t allocate_content_size = 0;
5252
for (size_t i = 0; i < real_need_copy_num; ++i) {
53-
auto& bitmap = const_cast<BitmapValue&>(data_column.get_element(i));
53+
auto& bitmap = data_column.get_element(i);
5454
allocate_content_size += bitmap.getSizeInBytes();
5555
}
5656
return size + allocate_len_size + allocate_content_size;
@@ -61,7 +61,7 @@ int64_t DataTypeBitMap::get_uncompressed_serialized_bytes(const IColumn& column,
6161
auto allocate_len_size = sizeof(size_t) * (column.size() + 1);
6262
size_t allocate_content_size = 0;
6363
for (size_t i = 0; i < column.size(); ++i) {
64-
auto& bitmap = const_cast<BitmapValue&>(data_column.get_element(i));
64+
auto& bitmap = data_column.get_element(i);
6565
allocate_content_size += bitmap.getSizeInBytes();
6666
}
6767

@@ -79,14 +79,14 @@ char* DataTypeBitMap::serialize(const IColumn& column, char* buf, int be_exec_ve
7979
// serialize the bitmap size array
8080
auto* meta_ptr = reinterpret_cast<size_t*>(buf);
8181
for (size_t i = 0; i < real_need_copy_num; ++i) {
82-
auto& bitmap = const_cast<BitmapValue&>(data_column.get_element(i));
82+
auto& bitmap = data_column.get_element(i);
8383
unaligned_store<size_t>(&meta_ptr[i], bitmap.getSizeInBytes());
8484
}
8585

8686
// serialize each bitmap
8787
char* data_ptr = buf + sizeof(size_t) * real_need_copy_num;
8888
for (size_t i = 0; i < real_need_copy_num; ++i) {
89-
auto& bitmap = const_cast<BitmapValue&>(data_column.get_element(i));
89+
auto& bitmap = data_column.get_element(i);
9090
bitmap.write_to(data_ptr);
9191
data_ptr += unaligned_load<size_t>(&meta_ptr[i]);
9292
}
@@ -99,14 +99,14 @@ char* DataTypeBitMap::serialize(const IColumn& column, char* buf, int be_exec_ve
9999
auto* meta_ptr = reinterpret_cast<size_t*>(buf);
100100
meta_ptr[0] = column.size();
101101
for (size_t i = 0; i < meta_ptr[0]; ++i) {
102-
auto& bitmap = const_cast<BitmapValue&>(data_column.get_element(i));
102+
auto& bitmap = data_column.get_element(i);
103103
unaligned_store<size_t>(&meta_ptr[i + 1], bitmap.getSizeInBytes());
104104
}
105105

106106
// serialize each bitmap
107107
char* data_ptr = buf + sizeof(size_t) * (meta_ptr[0] + 1);
108108
for (size_t i = 0; i < meta_ptr[0]; ++i) {
109-
auto& bitmap = const_cast<BitmapValue&>(data_column.get_element(i));
109+
auto& bitmap = data_column.get_element(i);
110110
bitmap.write_to(data_ptr);
111111
data_ptr += unaligned_load<size_t>(&meta_ptr[i + 1]);
112112
}
@@ -161,11 +161,10 @@ Status DataTypeBitMap::check_column(const IColumn& column) const {
161161
}
162162

163163
void DataTypeBitMap::serialize_as_stream(const BitmapValue& cvalue, BufferWritable& buf) {
164-
auto& value = const_cast<BitmapValue&>(cvalue);
165164
std::string memory_buffer;
166-
size_t bytesize = value.getSizeInBytes();
165+
size_t bytesize = cvalue.getSizeInBytes();
167166
memory_buffer.resize(bytesize);
168-
value.write_to(const_cast<char*>(memory_buffer.data()));
167+
cvalue.write_to(memory_buffer.data());
169168
buf.write_binary(memory_buffer);
170169
}
171170

‎be/src/vec/data_types/serde/data_type_bitmap_serde.cpp‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ Status DataTypeBitMapSerDe::write_column_to_pb(const IColumn& column, PValues& r
8787
auto row_count = cast_set<int>(end - start);
8888
result.mutable_bytes_value()->Reserve(row_count);
8989
for (auto row = start; row < end; ++row) {
90-
auto& value = const_cast<BitmapValue&>(data_column.get_element(row));
90+
auto& value = data_column.get_element(row);
9191
std::string memory_buffer;
9292
auto bytesize = value.getSizeInBytes();
9393
memory_buffer.resize(bytesize);
94-
value.write_to(const_cast<char*>(memory_buffer.data()));
94+
value.write_to(memory_buffer.data());
9595
result.add_bytes_value(memory_buffer);
9696
}
9797
return Status::OK();
@@ -111,12 +111,12 @@ void DataTypeBitMapSerDe::write_one_cell_to_jsonb(const IColumn& column, JsonbWr
111111
int64_t row_num) const {
112112
const auto& data_column = assert_cast<const ColumnBitmap&>(column);
113113
result.writeKey(cast_set<JsonbKeyValue::keyid_type>(col_id));
114-
auto bitmap_value = const_cast<BitmapValue&>(data_column.get_element(row_num));
114+
auto bitmap_value = data_column.get_element(row_num);
115115
// serialize the content of string
116116
auto size = bitmap_value.getSizeInBytes();
117117
// serialize the content of string
118-
auto* ptr = arena.alloc(size);
119-
bitmap_value.write_to(const_cast<char*>(ptr));
118+
char* ptr = arena.alloc(size);
119+
bitmap_value.write_to(ptr);
120120
result.writeStartBinary();
121121
result.writeBinary(reinterpret_cast<const char*>(ptr), size);
122122
result.writeEndBinary();
@@ -132,7 +132,7 @@ Status DataTypeBitMapSerDe::write_column_to_arrow(const IColumn& column, const N
132132
RETURN_IF_ERROR(checkArrowStatus(builder.AppendNull(), column.get_name(),
133133
array_builder->type()->name()));
134134
} else {
135-
auto& bitmap_value = const_cast<BitmapValue&>(col.get_element(string_i));
135+
auto& bitmap_value = col.get_element(string_i);
136136
std::string memory_buffer(bitmap_value.getSizeInBytes(), '0');
137137
bitmap_value.write_to(memory_buffer.data());
138138
RETURN_IF_ERROR(checkArrowStatus(
@@ -198,7 +198,7 @@ Status DataTypeBitMapSerDe::write_column_to_orc(const std::string& timezone, con
198198
size_t total_size = 0;
199199
for (size_t row_id = start; row_id < end; row_id++) {
200200
if (cur_batch->notNull[row_id] == 1) {
201-
auto bitmap_value = const_cast<BitmapValue&>(col_data.get_element(row_id));
201+
auto bitmap_value = col_data.get_element(row_id);
202202
size_t len = bitmap_value.getSizeInBytes();
203203
total_size += len;
204204
}
@@ -213,7 +213,7 @@ Status DataTypeBitMapSerDe::write_column_to_orc(const std::string& timezone, con
213213
size_t offset = 0;
214214
for (size_t row_id = start; row_id < end; row_id++) {
215215
if (cur_batch->notNull[row_id] == 1) {
216-
auto bitmap_value = const_cast<BitmapValue&>(col_data.get_element(row_id));
216+
auto bitmap_value = col_data.get_element(row_id);
217217
size_t len = bitmap_value.getSizeInBytes();
218218
if (offset + len > total_size) {
219219
return Status::InternalError(

‎be/src/vec/functions/ai/ai_functions.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ class AIFunction : public IFunction {
7777

7878
TAIResource config;
7979
std::shared_ptr<AIAdapter> adapter;
80-
if (Status status =
81-
const_cast<Derived*>(assert_cast<const Derived*>(this))
82-
->_init_from_resource(context, block, arguments, config, adapter);
80+
if (Status status = assert_cast<const Derived*>(this)->_init_from_resource(
81+
context, block, arguments, config, adapter);
8382
!status.ok()) {
8483
return status;
8584
}
@@ -151,7 +150,7 @@ class AIFunction : public IFunction {
151150
// The ai resource must be literal
152151
Status _init_from_resource(FunctionContext* context, const Block& block,
153152
const ColumnNumbers& arguments, TAIResource& config,
154-
std::shared_ptr<AIAdapter>& adapter) {
153+
std::shared_ptr<AIAdapter>& adapter) const {
155154
// 1. Initialize config
156155
const ColumnWithTypeAndName& resource_column = block.get_by_position(arguments[0]);
157156
StringRef resource_name_ref = resource_column.column->get_data_at(0);

0 commit comments

Comments
 (0)