Skip to content

Commit d9c48c4

Browse files
wumeibanfaYour Name
authored andcommitted
[fix] remove useless const_cast and explain const_cast(3) (#56441)
### 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 6514527 commit d9c48c4

11 files changed

Lines changed: 27 additions & 28 deletions

‎be/src/olap/rowset/segment_v2/bloom_filter.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class BloomFilter {
194194
/// Bloom filters must have equal size and seed.
195195
virtual bool contains(const BloomFilter& bf_) const { return true; };
196196

197-
virtual char* data() const { return _data; }
197+
virtual const char* data() const { return _data; }
198198

199199
size_t num_bytes() const { return _num_bytes; }
200200

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -738,12 +738,12 @@ bool DorisRAMFSDirectory::list(std::vector<std::string>* names) const {
738738

739739
bool DorisRAMFSDirectory::fileExists(const char* name) const {
740740
std::lock_guard<std::mutex> wlock(_this_lock);
741-
return filesMap->exists((char*)name);
741+
return filesMap->exists(name);
742742
}
743743

744744
int64_t DorisRAMFSDirectory::fileModified(const char* name) const {
745745
std::lock_guard<std::mutex> wlock(_this_lock);
746-
auto* f = filesMap->get((char*)name);
746+
auto* f = filesMap->get(name);
747747
DBUG_EXECUTE_IF("DorisRAMFSDirectory::fileModified_file_not_found", { f = nullptr; })
748748
if (f == nullptr) {
749749
_CLTHROWA(CL_ERR_IO, fmt::format("NOT FOUND File {}.", name).c_str());
@@ -755,7 +755,7 @@ void DorisRAMFSDirectory::touchFile(const char* name) {
755755
lucene::store::RAMFile* file = nullptr;
756756
{
757757
std::lock_guard<std::mutex> wlock(_this_lock);
758-
file = filesMap->get((char*)name);
758+
file = filesMap->get(name);
759759
DBUG_EXECUTE_IF("DorisRAMFSDirectory::touchFile_file_not_found", { file = nullptr; })
760760
if (file == nullptr) {
761761
_CLTHROWA(CL_ERR_IO, fmt::format("NOT FOUND File {}.", name).c_str());
@@ -775,7 +775,7 @@ void DorisRAMFSDirectory::touchFile(const char* name) {
775775

776776
int64_t DorisRAMFSDirectory::fileLength(const char* name) const {
777777
std::lock_guard<std::mutex> wlock(_this_lock);
778-
auto* f = filesMap->get((char*)name);
778+
auto* f = filesMap->get(name);
779779
DBUG_EXECUTE_IF("DorisRAMFSDirectory::fileLength_file_not_found", { f = nullptr; })
780780
if (f == nullptr) {
781781
_CLTHROWA(CL_ERR_IO, fmt::format("NOT FOUND File {}.", name).c_str());
@@ -786,7 +786,7 @@ int64_t DorisRAMFSDirectory::fileLength(const char* name) const {
786786
bool DorisRAMFSDirectory::openInput(const char* name, lucene::store::IndexInput*& ret,
787787
CLuceneError& error, int32_t bufferSize) {
788788
std::lock_guard<std::mutex> wlock(_this_lock);
789-
auto* file = filesMap->get((char*)name);
789+
auto* file = filesMap->get(name);
790790
DBUG_EXECUTE_IF("DorisRAMFSDirectory::openInput_file_not_found", { file = nullptr; })
791791
if (file == nullptr) {
792792
error.set(CL_ERR_IO,
@@ -805,7 +805,7 @@ void DorisRAMFSDirectory::close() {
805805

806806
bool DorisRAMFSDirectory::doDeleteFile(const char* name) {
807807
std::lock_guard<std::mutex> wlock(_this_lock);
808-
auto itr = filesMap->find((char*)name);
808+
auto itr = filesMap->find(name);
809809
if (itr != filesMap->end()) {
810810
SCOPED_LOCK_MUTEX(this->THIS_LOCK);
811811
sizeInBytes -= itr->second->sizeInBytes;
@@ -821,15 +821,15 @@ bool DorisRAMFSDirectory::deleteDirectory() {
821821

822822
void DorisRAMFSDirectory::renameFile(const char* from, const char* to) {
823823
std::lock_guard<std::mutex> wlock(_this_lock);
824-
auto itr = filesMap->find((char*)from);
824+
auto itr = filesMap->find(from);
825825

826826
/* DSR:CL_BUG_LEAK:
827827
** If a file named $to already existed, its old value was leaked.
828828
** My inclination would be to prevent this implicit deletion with an
829829
** exception, but it happens routinely in CLucene's internals (e.g., during
830830
** IndexWriter.addIndexes with the file named 'segments'). */
831-
if (filesMap->exists((char*)to)) {
832-
auto itr1 = filesMap->find((char*)to);
831+
if (filesMap->exists(to)) {
832+
auto itr1 = filesMap->find(to);
833833
SCOPED_LOCK_MUTEX(this->THIS_LOCK);
834834
sizeInBytes -= itr1->second->sizeInBytes;
835835
filesMap->removeitr(itr1);
@@ -855,8 +855,8 @@ lucene::store::IndexOutput* DorisRAMFSDirectory::createOutput(const char* name)
855855
std::lock_guard<std::mutex> wlock(_this_lock);
856856

857857
// get the actual pointer to the output name
858-
char* n = nullptr;
859-
auto itr = filesMap->find(const_cast<char*>(name));
858+
const char* n = nullptr;
859+
auto itr = filesMap->find(name);
860860
DBUG_EXECUTE_IF("DorisRAMFSDirectory::createOutput_itr_filesMap_end",
861861
{ itr = filesMap->end(); })
862862
if (itr != filesMap->end()) {

‎be/src/olap/rowset/segment_v2/inverted_index_fs_directory.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ class CLUCENE_EXPORT DorisFSDirectory : public lucene::store::Directory {
102102

103103
class CLUCENE_EXPORT DorisRAMFSDirectory : public DorisFSDirectory {
104104
protected:
105-
using FileMap =
106-
lucene::util::CLHashMap<char*, lucene::store::RAMFile*, lucene::util::Compare::Char,
107-
lucene::util::Equals::Char, lucene::util::Deletor::acArray,
108-
lucene::util::Deletor::Object<lucene::store::RAMFile>>;
105+
using FileMap = lucene::util::CLHashMap<const char*, lucene::store::RAMFile*,
106+
lucene::util::Compare::Char, lucene::util::Equals::Char,
107+
lucene::util::Deletor::cacArray,
108+
lucene::util::Deletor::Object<lucene::store::RAMFile>>;
109109

110110
// unlike the java Hashtable, FileMap is not synchronized, and all access must be protected by a lock
111111
FileMap* filesMap;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ Status InvertedIndexColumnWriter<field_type>::add_array_values(size_t field_size
542542
return Status::InternalError("field or index writer is null in inverted index writer");
543543
}
544544
for (int i = 0; i < count; ++i) {
545-
auto* item_data_ptr = const_cast<CollectionValue*>(values)->mutable_data();
545+
const auto* item_data_ptr = values->data();
546546
std::vector<std::string> strings;
547547

548548
for (size_t j = 0; j < values->length(); ++j) {
@@ -561,7 +561,7 @@ Status InvertedIndexColumnWriter<field_type>::add_array_values(size_t field_size
561561
}
562562
} else if constexpr (field_is_numeric_type(field_type)) {
563563
for (int i = 0; i < count; ++i) {
564-
auto* item_data_ptr = const_cast<CollectionValue*>(values)->mutable_data();
564+
const auto* item_data_ptr = values->data();
565565

566566
for (size_t j = 0; j < values->length(); ++j) {
567567
const auto* p = reinterpret_cast<const CppType*>(item_data_ptr);

‎be/src/olap/rowset/segment_v2/ngram_bloom_filter.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ class NGramBloomFilter : public BloomFilter {
4141
void add_bytes(const char* data, size_t len) override;
4242
bool contains(const BloomFilter& bf_) const override;
4343
Status init(const char* buf, size_t size, HashStrategyPB strategy) override;
44-
char* data() const override {
45-
return reinterpret_cast<char*>(const_cast<uint64_t*>(filter.data()));
46-
}
44+
const char* data() const override { return reinterpret_cast<const char*>(filter.data()); }
4745
size_t size() const override { return _size; }
4846
void add_hash(uint64_t) override {}
4947
bool test_hash(uint64_t hash) const override { return true; }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ Status PageIO::read_and_decompress_page_(const PageReadOptions& opts, PageHandle
176176
if (opts.verify_checksum) {
177177
uint32_t expect = decode_fixed32_le((uint8_t*)page_slice.data + page_slice.size - 4);
178178
uint32_t actual = crc32c::Value(page_slice.data, page_slice.size - 4);
179+
// here const_cast is used for testing.
179180
InjectionContext ctx = {&actual, const_cast<PageReadOptions*>(&opts)};
180181
(void)ctx;
181182
TEST_INJECTION_POINT_CALLBACK("PageIO::read_and_decompress_page:crc_failure_inj", &ctx);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ void SegmentWriter::_maybe_invalid_row_cache(const std::string& key) {
375375
}
376376
}
377377

378-
void SegmentWriter::_serialize_block_to_row_column(vectorized::Block& block) {
378+
void SegmentWriter::_serialize_block_to_row_column(const vectorized::Block& block) {
379379
if (block.rows() == 0) {
380380
return;
381381
}
@@ -697,7 +697,7 @@ Status SegmentWriter::append_block(const vectorized::Block* block, size_t row_po
697697
// or it's schema change write(since column data type maybe changed, so we should reubild)
698698
if (_opts.write_type == DataWriteType::TYPE_DIRECT ||
699699
_opts.write_type == DataWriteType::TYPE_SCHEMA_CHANGE) {
700-
_serialize_block_to_row_column(*const_cast<vectorized::Block*>(block));
700+
_serialize_block_to_row_column(*block);
701701
}
702702

703703
_olap_data_convertor->set_source_content(block, row_pos, num_rows);

‎be/src/olap/rowset/segment_v2/segment_writer.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class SegmentWriter {
192192
void set_min_max_key(const Slice& key);
193193
void set_min_key(const Slice& key);
194194
void set_max_key(const Slice& key);
195-
void _serialize_block_to_row_column(vectorized::Block& block);
195+
void _serialize_block_to_row_column(const vectorized::Block& block);
196196
Status _generate_primary_key_index(
197197
const std::vector<const KeyCoder*>& primary_key_coders,
198198
const std::vector<vectorized::IOlapColumnDataAccessor*>& primary_key_columns,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ void VerticalSegmentWriter::_maybe_invalid_row_cache(const std::string& key) con
355355
}
356356
}
357357

358-
void VerticalSegmentWriter::_serialize_block_to_row_column(vectorized::Block& block) {
358+
void VerticalSegmentWriter::_serialize_block_to_row_column(const vectorized::Block& block) {
359359
if (block.rows() == 0) {
360360
return;
361361
}
@@ -941,7 +941,7 @@ Status VerticalSegmentWriter::write_batch() {
941941
_opts.write_type == DataWriteType::TYPE_SCHEMA_CHANGE) {
942942
for (auto& data : _batched_blocks) {
943943
// TODO: maybe we should pass range to this method
944-
_serialize_block_to_row_column(*const_cast<vectorized::Block*>(data.block));
944+
_serialize_block_to_row_column(*data.block);
945945
}
946946
}
947947

‎be/src/olap/rowset/segment_v2/vertical_segment_writer.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class VerticalSegmentWriter {
162162
void _set_min_max_key(const Slice& key);
163163
void _set_min_key(const Slice& key);
164164
void _set_max_key(const Slice& key);
165-
void _serialize_block_to_row_column(vectorized::Block& block);
165+
void _serialize_block_to_row_column(const vectorized::Block& block);
166166
Status _probe_key_for_mow(std::string key, std::size_t segment_pos, bool have_input_seq_column,
167167
bool have_delete_sign,
168168
const std::vector<RowsetSharedPtr>& specified_rowsets,

0 commit comments

Comments
 (0)