Skip to content

Commit d154f4f

Browse files
wumeibanfaYour Name
authored andcommitted
[fix] remove useless const_cast and explain const_cast(1) (#55943)
### 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 0799962 commit d154f4f

16 files changed

Lines changed: 57 additions & 62 deletions

‎be/src/agent/task_worker_pool.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,8 @@ Status _submit_task(const TAgentTaskRequest& task,
419419
// TODO(plat1ko): check task request member
420420

421421
// Set the receiving time of task so that we can determine whether it is timed out later
422+
// exist a path task_worker_pool <- agent_server <- backend_service <- BackendService
423+
// use the arg BackendService_submit_tasks_args.tasks is not const, so modify is ok
422424
(const_cast<TAgentTaskRequest&>(task)).__set_recv_time(time(nullptr));
423425
auto st = submit_op(task);
424426
if (!st.ok()) [[unlikely]] {
@@ -614,7 +616,7 @@ Status PriorTaskWorkerPool::submit_task(const TAgentTaskRequest& task) {
614616
});
615617
}
616618

617-
Status PriorTaskWorkerPool::submit_high_prior_and_cancel_low(const TAgentTaskRequest& task) {
619+
Status PriorTaskWorkerPool::submit_high_prior_and_cancel_low(TAgentTaskRequest& task) {
618620
const TTaskType::type task_type = task.task_type;
619621
int64_t signature = task.signature;
620622
std::string type_str;
@@ -654,7 +656,7 @@ Status PriorTaskWorkerPool::submit_high_prior_and_cancel_low(const TAgentTaskReq
654656
} while (false);
655657

656658
// Set the receiving time of task so that we can determine whether it is timed out later
657-
(const_cast<TAgentTaskRequest&>(task)).__set_recv_time(time(nullptr));
659+
task.__set_recv_time(time(nullptr));
658660

659661
LOG_INFO("successfully submit task").tag("type", type_str).tag("signature", signature);
660662
return Status::OK();
@@ -1923,6 +1925,9 @@ void push_callback(StorageEngine& engine, const TAgentTaskRequest& req) {
19231925
<< " push_type=" << push_req.push_type;
19241926
std::vector<TTabletInfo> tablet_infos;
19251927

1928+
// exist a path task_worker_pool <- agent_server <- backend_service <- BackendService
1929+
// use the arg BackendService_submit_tasks_args.tasks is not const
1930+
// and push_req will be modify, so modify is ok
19261931
EngineBatchLoadTask engine_task(engine, const_cast<TPushReq&>(push_req), &tablet_infos);
19271932
SCOPED_ATTACH_TASK(engine_task.mem_tracker());
19281933
auto status = engine_task.execute();

‎be/src/agent/task_worker_pool.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class PriorTaskWorkerPool final : public TaskWorkerPoolIf {
8888

8989
Status submit_task(const TAgentTaskRequest& task) override;
9090

91-
Status submit_high_prior_and_cancel_low(const TAgentTaskRequest& task);
91+
Status submit_high_prior_and_cancel_low(TAgentTaskRequest& task);
9292

9393
private:
9494
void normal_loop();

‎be/src/cloud/cloud_meta_mgr.cpp‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ Status retry_rpc(std::string_view op_name, const Request& req, Response* res,
388388
static_assert(std::is_base_of_v<::google::protobuf::Message, Request>);
389389
static_assert(std::is_base_of_v<::google::protobuf::Message, Response>);
390390

391+
// Applies only to the current file, and all req are non-const, but passed as const types.
391392
const_cast<Request&>(req).set_request_ip(BackendOptions::get_be_endpoint());
392393

393394
int retry_times = 0;
@@ -1915,12 +1916,12 @@ void CloudMetaMgr::remove_delete_bitmap_update_lock(int64_t table_id, int64_t lo
19151916
}
19161917
}
19171918

1918-
void CloudMetaMgr::check_table_size_correctness(const RowsetMeta& rs_meta) {
1919+
void CloudMetaMgr::check_table_size_correctness(RowsetMeta& rs_meta) {
19191920
if (!config::enable_table_size_correctness_check) {
19201921
return;
19211922
}
19221923
int64_t total_segment_size = get_segment_file_size(rs_meta);
1923-
int64_t total_inverted_index_size = get_inverted_index_file_szie(rs_meta);
1924+
int64_t total_inverted_index_size = get_inverted_index_file_size(rs_meta);
19241925
if (rs_meta.data_disk_size() != total_segment_size ||
19251926
rs_meta.index_disk_size() != total_inverted_index_size ||
19261927
rs_meta.data_disk_size() + rs_meta.index_disk_size() != rs_meta.total_disk_size()) {
@@ -1939,9 +1940,9 @@ void CloudMetaMgr::check_table_size_correctness(const RowsetMeta& rs_meta) {
19391940
}
19401941
}
19411942

1942-
int64_t CloudMetaMgr::get_segment_file_size(const RowsetMeta& rs_meta) {
1943+
int64_t CloudMetaMgr::get_segment_file_size(RowsetMeta& rs_meta) {
19431944
int64_t total_segment_size = 0;
1944-
const auto fs = const_cast<RowsetMeta&>(rs_meta).fs();
1945+
const auto fs = rs_meta.fs();
19451946
if (!fs) {
19461947
LOG(WARNING) << "get fs failed, resource_id={}" << rs_meta.resource_id();
19471948
}
@@ -1966,9 +1967,9 @@ int64_t CloudMetaMgr::get_segment_file_size(const RowsetMeta& rs_meta) {
19661967
return total_segment_size;
19671968
}
19681969

1969-
int64_t CloudMetaMgr::get_inverted_index_file_szie(const RowsetMeta& rs_meta) {
1970+
int64_t CloudMetaMgr::get_inverted_index_file_size(RowsetMeta& rs_meta) {
19701971
int64_t total_inverted_index_size = 0;
1971-
const auto fs = const_cast<RowsetMeta&>(rs_meta).fs();
1972+
const auto fs = rs_meta.fs();
19721973
if (!fs) {
19731974
LOG(WARNING) << "get fs failed, resource_id={}" << rs_meta.resource_id();
19741975
}

‎be/src/cloud/cloud_meta_mgr.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ class CloudMetaMgr {
191191
GetDeleteBitmapResponse& res,
192192
int64_t bytes_threadhold);
193193

194-
void check_table_size_correctness(const RowsetMeta& rs_meta);
195-
int64_t get_segment_file_size(const RowsetMeta& rs_meta);
196-
int64_t get_inverted_index_file_szie(const RowsetMeta& rs_meta);
194+
void check_table_size_correctness(RowsetMeta& rs_meta);
195+
int64_t get_segment_file_size(RowsetMeta& rs_meta);
196+
int64_t get_inverted_index_file_size(RowsetMeta& rs_meta);
197197
};
198198

199199
} // namespace cloud

‎be/src/exec/decompressor.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ Status Bzip2Decompressor::decompress(uint8_t* input, uint32_t input_len, size_t*
273273
size_t* decompressed_len, bool* stream_end,
274274
size_t* more_input_bytes, size_t* more_output_bytes) {
275275
// 1. set input and output
276-
_bz_strm.next_in = const_cast<char*>(reinterpret_cast<const char*>(input));
276+
_bz_strm.next_in = reinterpret_cast<char*>(input);
277277
_bz_strm.avail_in = input_len;
278278
_bz_strm.next_out = reinterpret_cast<char*>(output);
279279
_bz_strm.avail_out = output_max_len;

‎be/src/exec/table_connector.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ std::u16string TableConnector::utf8_to_u16string(const char* first, const char*
7272
};
7373
std::unique_ptr<std::remove_pointer_t<iconv_t>, decltype(deleter)> convertor(
7474
iconv_open("UTF-16LE", "UTF-8"), deleter);
75-
75+
// The `first` variable remains unmodified; `const_cast` is used to adapt to third-party functions.
7676
char* in = const_cast<char*>(first);
7777
size_t inbytesleft = last - first;
7878

‎be/src/exprs/hybrid_set.h‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ class StringSet : public HybridSetBase {
498498

499499
bool find(const void* data) const override {
500500
const auto* value = reinterpret_cast<const StringRef*>(data);
501-
std::string str_value(const_cast<const char*>(value->data), value->size);
501+
std::string str_value(value->data, value->size);
502502
return _set.find(str_value);
503503
}
504504

@@ -565,7 +565,7 @@ class StringSet : public HybridSetBase {
565565
~Iterator() override = default;
566566
bool has_next() const override { return !(_begin == _end); }
567567
const void* get_value() override {
568-
_value.data = const_cast<char*>(_begin->data());
568+
_value.data = _begin->data();
569569
_value.size = _begin->length();
570570
return &_value;
571571
}
@@ -715,7 +715,6 @@ class StringValueSet : public HybridSetBase {
715715
const auto& col = assert_cast<const doris::vectorized::ColumnString&>(column);
716716
const auto& offset = col.get_offsets();
717717
const uint8_t* __restrict data = col.get_chars().data();
718-
auto* __restrict cursor = const_cast<uint8_t*>(data);
719718
const uint8_t* __restrict null_map_data;
720719
if constexpr (is_nullable) {
721720
null_map_data = null_map->data();
@@ -729,15 +728,15 @@ class StringValueSet : public HybridSetBase {
729728
for (size_t i = 0; i < rows; ++i) {
730729
uint32_t len = offset[i] - offset[i - 1];
731730
if constexpr (!is_nullable && !is_negative) {
732-
result_data[i] = _set.find(StringRef(cursor, len));
731+
result_data[i] = _set.find(StringRef(data, len));
733732
} else if constexpr (!is_nullable && is_negative) {
734-
result_data[i] = !_set.find(StringRef(cursor, len));
733+
result_data[i] = !_set.find(StringRef(data, len));
735734
} else if constexpr (is_nullable && !is_negative) {
736-
result_data[i] = (!null_map_data[i]) & _set.find(StringRef(cursor, len));
735+
result_data[i] = (!null_map_data[i]) & _set.find(StringRef(data, len));
737736
} else { // (is_nullable && is_negative)
738-
result_data[i] = !((!null_map_data[i]) & _set.find(StringRef(cursor, len)));
737+
result_data[i] = !((!null_map_data[i]) & _set.find(StringRef(data, len)));
739738
}
740-
cursor += len;
739+
data += len;
741740
}
742741
}
743742

@@ -748,7 +747,7 @@ class StringValueSet : public HybridSetBase {
748747
~Iterator() override = default;
749748
bool has_next() const override { return !(_begin == _end); }
750749
const void* get_value() override {
751-
_value.data = const_cast<char*>(_begin->data);
750+
_value.data = _begin->data;
752751
_value.size = _begin->size;
753752
return &_value;
754753
}

‎be/src/geo/geo_types.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,8 @@ int GeoLine::numPoint() const {
779779
return _polyline->num_vertices();
780780
}
781781

782-
S2Point* GeoLine::getPoint(int i) const {
783-
return const_cast<S2Point*>(&(_polyline->vertex(i)));
782+
const S2Point* GeoLine::getPoint(int i) const {
783+
return &(_polyline->vertex(i));
784784
}
785785

786786
GeoParseStatus GeoPolygon::from_coords(const GeoCoordinateListList& list) {
@@ -1102,7 +1102,7 @@ int GeoPolygon::numLoops() const {
11021102
}
11031103

11041104
S2Loop* GeoPolygon::getLoop(int i) const {
1105-
return const_cast<S2Loop*>(_polygon->loop(i));
1105+
return _polygon->loop(i);
11061106
}
11071107

11081108
GeoParseStatus GeoMultiPolygon::from_coords(const std::vector<GeoCoordinateListList>& list) {

‎be/src/geo/geo_types.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class GeoLine : public GeoShape {
141141
std::string as_wkt() const override;
142142

143143
int numPoint() const;
144-
S2Point* getPoint(int i) const;
144+
const S2Point* getPoint(int i) const;
145145

146146
protected:
147147
void encode(std::string* buf) override;

‎be/src/olap/comparison_predicate.h‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,24 +220,20 @@ class ComparisonPredicateBase : public ColumnPredicate {
220220
// DecimalV2 using decimal12_t in bloom filter, should convert value to decimal12_t
221221
if constexpr (Type == PrimitiveType::TYPE_DECIMALV2) {
222222
decimal12_t decimal12_t_val(_value.int_value(), _value.frac_value());
223-
return bf->test_bytes(
224-
const_cast<char*>(reinterpret_cast<const char*>(&decimal12_t_val)),
225-
sizeof(decimal12_t));
223+
return bf->test_bytes(reinterpret_cast<const char*>(&decimal12_t_val),
224+
sizeof(decimal12_t));
226225
// Datev1 using uint24_t in bloom filter
227226
} else if constexpr (Type == PrimitiveType::TYPE_DATE) {
228227
uint24_t date_value(uint32_t(_value.to_olap_date()));
229-
return bf->test_bytes(
230-
const_cast<char*>(reinterpret_cast<const char*>(&date_value)),
231-
sizeof(uint24_t));
228+
return bf->test_bytes(reinterpret_cast<const char*>(&date_value),
229+
sizeof(uint24_t));
232230
// DatetimeV1 using int64_t in bloom filter
233231
} else if constexpr (Type == PrimitiveType::TYPE_DATETIME) {
234232
int64_t datetime_value(_value.to_olap_datetime());
235-
return bf->test_bytes(
236-
const_cast<char*>(reinterpret_cast<const char*>(&datetime_value)),
237-
sizeof(int64_t));
233+
return bf->test_bytes(reinterpret_cast<const char*>(&datetime_value),
234+
sizeof(int64_t));
238235
} else {
239-
return bf->test_bytes(const_cast<char*>(reinterpret_cast<const char*>(&_value)),
240-
sizeof(T));
236+
return bf->test_bytes(reinterpret_cast<const char*>(&_value), sizeof(T));
241237
}
242238
}
243239
} else {

0 commit comments

Comments
 (0)