Skip to content

Commit 321107c

Browse files
[refactor](schema change) Using tablet schema shared ptr instead of raw ptr (#11475)
* Using tabletschema shared ptr instead of raw ptrs Co-authored-by: yiguolei <yiguolei@gmail.com>
1 parent 74340da commit 321107c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+478
-480
lines changed

be/src/exec/olap_scanner.cpp

+12-11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ OlapScanner::OlapScanner(RuntimeState* runtime_state, OlapScanNode* parent, bool
5353
_need_agg_finalize(need_agg_finalize),
5454
_version(-1) {
5555
_mem_tracker = tracker;
56+
_tablet_schema = std::make_shared<TabletSchema>();
5657
}
5758

5859
Status OlapScanner::prepare(
@@ -79,12 +80,12 @@ Status OlapScanner::prepare(
7980
LOG(WARNING) << ss.str();
8081
return Status::InternalError(ss.str());
8182
}
82-
_tablet_schema.copy_from(*_tablet->tablet_schema());
83+
_tablet_schema->copy_from(*_tablet->tablet_schema());
8384
if (!_parent->_olap_scan_node.columns_desc.empty() &&
8485
_parent->_olap_scan_node.columns_desc[0].col_unique_id >= 0) {
85-
_tablet_schema.clear_columns();
86+
_tablet_schema->clear_columns();
8687
for (const auto& column_desc : _parent->_olap_scan_node.columns_desc) {
87-
_tablet_schema.append_column(TabletColumn(column_desc));
88+
_tablet_schema->append_column(TabletColumn(column_desc));
8889
}
8990
}
9091
{
@@ -189,7 +190,7 @@ Status OlapScanner::_init_tablet_reader_params(
189190
RETURN_IF_ERROR(_init_return_columns(!_tablet_reader_params.direct_mode));
190191

191192
_tablet_reader_params.tablet = _tablet;
192-
_tablet_reader_params.tablet_schema = &_tablet_schema;
193+
_tablet_reader_params.tablet_schema = _tablet_schema;
193194
_tablet_reader_params.reader_type = READER_QUERY;
194195
_tablet_reader_params.aggregation = _aggregation;
195196
_tablet_reader_params.version = Version(0, _version);
@@ -234,7 +235,7 @@ Status OlapScanner::_init_tablet_reader_params(
234235
_tablet_reader_params.return_columns.push_back(i);
235236
}
236237
for (auto index : _return_columns) {
237-
if (_tablet_schema.column(index).is_key()) {
238+
if (_tablet_schema->column(index).is_key()) {
238239
continue;
239240
} else {
240241
_tablet_reader_params.return_columns.push_back(index);
@@ -270,31 +271,31 @@ Status OlapScanner::_init_return_columns(bool need_seq_col) {
270271
continue;
271272
}
272273
int32_t index = slot->col_unique_id() >= 0
273-
? _tablet_schema.field_index(slot->col_unique_id())
274-
: _tablet_schema.field_index(slot->col_name());
274+
? _tablet_schema->field_index(slot->col_unique_id())
275+
: _tablet_schema->field_index(slot->col_name());
275276
if (index < 0) {
276277
std::stringstream ss;
277278
ss << "field name is invalid. field=" << slot->col_name();
278279
LOG(WARNING) << ss.str();
279280
return Status::InternalError(ss.str());
280281
}
281282
_return_columns.push_back(index);
282-
if (slot->is_nullable() && !_tablet_schema.column(index).is_nullable())
283+
if (slot->is_nullable() && !_tablet_schema->column(index).is_nullable())
283284
_tablet_columns_convert_to_null_set.emplace(index);
284285
_query_slots.push_back(slot);
285286
}
286287

287288
// expand the sequence column
288-
if (_tablet_schema.has_sequence_col() && need_seq_col) {
289+
if (_tablet_schema->has_sequence_col() && need_seq_col) {
289290
bool has_replace_col = false;
290291
for (auto col : _return_columns) {
291-
if (_tablet_schema.column(col).aggregation() ==
292+
if (_tablet_schema->column(col).aggregation() ==
292293
FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE) {
293294
has_replace_col = true;
294295
break;
295296
}
296297
}
297-
if (auto sequence_col_idx = _tablet_schema.sequence_col_idx();
298+
if (auto sequence_col_idx = _tablet_schema->sequence_col_idx();
298299
has_replace_col && std::find(_return_columns.begin(), _return_columns.end(),
299300
sequence_col_idx) == _return_columns.end()) {
300301
_return_columns.push_back(sequence_col_idx);

be/src/exec/olap_scanner.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class OlapScanner {
153153

154154
MemTracker* _mem_tracker;
155155

156-
TabletSchema _tablet_schema;
156+
TabletSchemaSPtr _tablet_schema;
157157
};
158158

159159
} // namespace doris

be/src/olap/collect_iterator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ CollectIterator::Level0Iterator::Level0Iterator(RowsetReaderSharedPtr rs_reader,
202202
CollectIterator::Level0Iterator::~Level0Iterator() = default;
203203

204204
Status CollectIterator::Level0Iterator::init() {
205-
RETURN_NOT_OK_LOG(_row_cursor.init(*_reader->_tablet_schema, _reader->_seek_columns),
205+
RETURN_NOT_OK_LOG(_row_cursor.init(_reader->_tablet_schema, _reader->_seek_columns),
206206
"failed to init row cursor");
207207
return (this->*_refresh_current_row)();
208208
}

be/src/olap/compaction.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ Status Compaction::do_compaction_impl(int64_t permits) {
171171
}
172172

173173
if (use_vectorized_compaction) {
174-
res = Merger::vmerge_rowsets(_tablet, compaction_type(), cur_tablet_schema.get(),
174+
res = Merger::vmerge_rowsets(_tablet, compaction_type(), cur_tablet_schema,
175175
_input_rs_readers, _output_rs_writer.get(), &stats);
176176
} else {
177-
res = Merger::merge_rowsets(_tablet, compaction_type(), cur_tablet_schema.get(),
177+
res = Merger::merge_rowsets(_tablet, compaction_type(), cur_tablet_schema,
178178
_input_rs_readers, _output_rs_writer.get(), &stats);
179179
}
180180

be/src/olap/delete_handler.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ bool DeleteHandler::_parse_condition(const std::string& condition_str, TConditio
237237
return true;
238238
}
239239

240-
Status DeleteHandler::init(const TabletSchema& schema,
240+
Status DeleteHandler::init(TabletSchemaSPtr schema,
241241
const std::vector<DeletePredicatePB>& delete_conditions, int64_t version,
242242
const TabletReader* reader) {
243243
DCHECK(!_is_inited) << "reinitialize delete handler.";
@@ -258,7 +258,7 @@ Status DeleteHandler::init(const TabletSchema& schema,
258258
return Status::OLAPInternalError(OLAP_ERR_MALLOC_ERROR);
259259
}
260260

261-
temp.del_cond->set_tablet_schema(&schema);
261+
temp.del_cond->set_tablet_schema(schema);
262262
for (const auto& sub_predicate : delete_condition.sub_predicates()) {
263263
TCondition condition;
264264
if (!_parse_condition(sub_predicate, &condition)) {

be/src/olap/delete_handler.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "olap/block_column_predicate.h"
2626
#include "olap/column_predicate.h"
2727
#include "olap/olap_define.h"
28+
#include "olap/tablet_schema.h"
2829

2930
namespace doris {
3031

@@ -89,7 +90,7 @@ class DeleteHandler {
8990
// return:
9091
// * Status::OLAPInternalError(OLAP_ERR_DELETE_INVALID_PARAMETERS): input parameters are not valid
9192
// * Status::OLAPInternalError(OLAP_ERR_MALLOC_ERROR): alloc memory failed
92-
Status init(const TabletSchema& schema, const std::vector<DeletePredicatePB>& delete_conditions,
93+
Status init(TabletSchemaSPtr schema, const std::vector<DeletePredicatePB>& delete_conditions,
9394
int64_t version, const doris::TabletReader* = nullptr);
9495

9596
// Return the delete conditions' size.

be/src/olap/delta_writer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Status DeltaWriter::init() {
131131

132132
RETURN_NOT_OK(_tablet->create_rowset_writer(_req.txn_id, _req.load_id, PREPARED, OVERLAPPING,
133133
_tablet_schema, &_rowset_writer));
134-
_schema.reset(new Schema(*_tablet_schema));
134+
_schema.reset(new Schema(_tablet_schema));
135135
_reset_mem_table();
136136

137137
// create flush handler

be/src/olap/iterators.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class StorageReadOptions {
9393
bool use_page_cache = false;
9494
int block_row_max = 4096;
9595

96-
const TabletSchema* tablet_schema = nullptr;
96+
TabletSchemaSPtr tablet_schema = nullptr;
9797
bool record_rowids = false;
9898
};
9999

be/src/olap/merger.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
namespace doris {
3131

3232
Status Merger::merge_rowsets(TabletSharedPtr tablet, ReaderType reader_type,
33-
const TabletSchema* cur_tablet_schema,
33+
TabletSchemaSPtr cur_tablet_schema,
3434
const std::vector<RowsetReaderSharedPtr>& src_rowset_readers,
3535
RowsetWriter* dst_rowset_writer, Merger::Statistics* stats_output) {
3636
TRACE_COUNTER_SCOPE_LATENCY_US("merge_rowsets_latency_us");
@@ -47,9 +47,9 @@ Status Merger::merge_rowsets(TabletSharedPtr tablet, ReaderType reader_type,
4747

4848
RowCursor row_cursor;
4949
RETURN_NOT_OK_LOG(
50-
row_cursor.init(*cur_tablet_schema),
50+
row_cursor.init(cur_tablet_schema),
5151
"failed to init row cursor when merging rowsets of tablet " + tablet->full_name());
52-
row_cursor.allocate_memory_for_string_type(*cur_tablet_schema);
52+
row_cursor.allocate_memory_for_string_type(cur_tablet_schema);
5353

5454
std::unique_ptr<MemPool> mem_pool(new MemPool());
5555

@@ -91,7 +91,7 @@ Status Merger::merge_rowsets(TabletSharedPtr tablet, ReaderType reader_type,
9191
}
9292

9393
Status Merger::vmerge_rowsets(TabletSharedPtr tablet, ReaderType reader_type,
94-
const TabletSchema* cur_tablet_schema,
94+
TabletSchemaSPtr cur_tablet_schema,
9595
const std::vector<RowsetReaderSharedPtr>& src_rowset_readers,
9696
RowsetWriter* dst_rowset_writer, Statistics* stats_output) {
9797
TRACE_COUNTER_SCOPE_LATENCY_US("merge_rowsets_latency_us");
@@ -108,8 +108,7 @@ Status Merger::vmerge_rowsets(TabletSharedPtr tablet, ReaderType reader_type,
108108
reader_params.record_rowids = true;
109109
}
110110

111-
const auto& schema = *cur_tablet_schema;
112-
reader_params.return_columns.resize(schema.num_columns());
111+
reader_params.return_columns.resize(cur_tablet_schema->num_columns());
113112
std::iota(reader_params.return_columns.begin(), reader_params.return_columns.end(), 0);
114113
reader_params.origin_return_columns = &reader_params.return_columns;
115114
RETURN_NOT_OK(reader.init(reader_params));
@@ -124,7 +123,7 @@ Status Merger::vmerge_rowsets(TabletSharedPtr tablet, ReaderType reader_type,
124123
}
125124
}
126125

127-
vectorized::Block block = schema.create_block(reader_params.return_columns);
126+
vectorized::Block block = cur_tablet_schema->create_block(reader_params.return_columns);
128127
size_t output_rows = 0;
129128
bool eof = false;
130129
while (!eof) {

be/src/olap/merger.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ class Merger {
3838
// return OLAP_SUCCESS and set statistics into `*stats_output`.
3939
// return others on error
4040
static Status merge_rowsets(TabletSharedPtr tablet, ReaderType reader_type,
41-
const TabletSchema* cur_tablet_schema,
41+
TabletSchemaSPtr cur_tablet_schema,
4242
const std::vector<RowsetReaderSharedPtr>& src_rowset_readers,
4343
RowsetWriter* dst_rowset_writer, Statistics* stats_output);
4444

4545
static Status vmerge_rowsets(TabletSharedPtr tablet, ReaderType reader_type,
46-
const TabletSchema* cur_tablet_schema,
46+
TabletSchemaSPtr cur_tablet_schema,
4747
const std::vector<RowsetReaderSharedPtr>& src_rowset_readers,
4848
RowsetWriter* dst_rowset_writer, Statistics* stats_output);
4949
};

be/src/olap/olap_cond.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class Conditions {
158158
bool empty() const { return _columns.empty(); }
159159

160160
// TODO(yingchun): should do it in constructor
161-
void set_tablet_schema(const TabletSchema* schema) { _schema = schema; }
161+
void set_tablet_schema(TabletSchemaSPtr schema) { _schema = schema; }
162162

163163
// 如果成功,则_columns中增加一项,如果失败则无视此condition,同时输出日志
164164
// 对于下列情况,将不会被处理
@@ -176,7 +176,7 @@ class Conditions {
176176
}
177177

178178
private:
179-
const TabletSchema* _schema = nullptr;
179+
TabletSchemaSPtr _schema = nullptr;
180180
// CondColumns in _index_conds are in 'AND' relationship
181181
CondColumns _columns; // list of condition column
182182

be/src/olap/push_handler.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Status PushHandler::_convert_v2(TabletSharedPtr cur_tablet, RowsetSharedPtr* cur
220220
}
221221

222222
// init schema
223-
std::unique_ptr<Schema> schema(new (std::nothrow) Schema(*tablet_schema));
223+
std::unique_ptr<Schema> schema(new (std::nothrow) Schema(tablet_schema));
224224
if (schema == nullptr) {
225225
LOG(WARNING) << "fail to create schema. tablet=" << cur_tablet->full_name();
226226
res = Status::OLAPInternalError(OLAP_ERR_MALLOC_ERROR);
@@ -363,7 +363,7 @@ Status PushHandler::_convert(TabletSharedPtr cur_tablet, RowsetSharedPtr* cur_ro
363363
<< ", block_row_size=" << cur_tablet->num_rows_per_row_block();
364364

365365
// 4. Init RowCursor
366-
if (!(res = row.init(*tablet_schema))) {
366+
if (!(res = row.init(tablet_schema))) {
367367
LOG(WARNING) << "fail to init rowcursor. res=" << res;
368368
break;
369369
}

be/src/olap/reader.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ Status TabletReader::_init_keys_param(const ReaderParams& read_params) {
402402
}
403403

404404
Status res = _keys_param.start_keys[i].init_scan_key(
405-
*_tablet_schema, read_params.start_key[i].values(), schema);
405+
_tablet_schema, read_params.start_key[i].values(), schema);
406406
if (!res.ok()) {
407407
LOG(WARNING) << "fail to init row cursor. res = " << res;
408408
return res;
@@ -424,7 +424,7 @@ Status TabletReader::_init_keys_param(const ReaderParams& read_params) {
424424
return Status::OLAPInternalError(OLAP_ERR_INPUT_PARAMETER_ERROR);
425425
}
426426

427-
Status res = _keys_param.end_keys[i].init_scan_key(*_tablet_schema,
427+
Status res = _keys_param.end_keys[i].init_scan_key(_tablet_schema,
428428
read_params.end_key[i].values(), schema);
429429
if (!res.ok()) {
430430
LOG(WARNING) << "fail to init row cursor. res = " << res;
@@ -613,7 +613,7 @@ Status TabletReader::_init_delete_condition(const ReaderParams& read_params) {
613613
}
614614

615615
auto delete_init = [&]() -> Status {
616-
return _delete_handler.init(*_tablet_schema, _tablet->delete_predicates(),
616+
return _delete_handler.init(_tablet_schema, _tablet->delete_predicates(),
617617
read_params.version.second, this);
618618
};
619619

be/src/olap/reader.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class TabletReader {
5858
// mainly include tablet, data version and fetch range.
5959
struct ReaderParams {
6060
TabletSharedPtr tablet;
61-
const TabletSchema* tablet_schema;
61+
TabletSchemaSPtr tablet_schema;
6262
ReaderType reader_type = READER_QUERY;
6363
bool direct_mode = false;
6464
bool aggregation = false;
@@ -186,7 +186,7 @@ class TabletReader {
186186

187187
TabletSharedPtr _tablet;
188188
RowsetReaderContext _reader_context;
189-
const TabletSchema* _tablet_schema;
189+
TabletSchemaSPtr _tablet_schema;
190190
KeysParam _keys_param;
191191
std::vector<bool> _is_lower_keys_included;
192192
std::vector<bool> _is_upper_keys_included;

be/src/olap/row_block.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ using std::vector;
3737

3838
namespace doris {
3939

40-
RowBlock::RowBlock(const TabletSchema* schema) : _capacity(0), _schema(schema) {
40+
RowBlock::RowBlock(TabletSchemaSPtr schema) : _capacity(0), _schema(schema) {
4141
_mem_pool.reset(new MemPool());
4242
}
4343

be/src/olap/row_block.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class RowBlock {
5454
friend class RowBlockChanger;
5555

5656
public:
57-
RowBlock(const TabletSchema* schema);
57+
RowBlock(TabletSchemaSPtr schema);
5858

5959
// 注意回收内部buffer
6060
~RowBlock();
@@ -80,7 +80,7 @@ class RowBlock {
8080

8181
const uint32_t row_num() const { return _info.row_num; }
8282
const RowBlockInfo& row_block_info() const { return _info; }
83-
const TabletSchema& tablet_schema() const { return *_schema; }
83+
const TabletSchemaSPtr tablet_schema() const { return _schema; }
8484
size_t capacity() const { return _capacity; }
8585

8686
// Return field pointer, this pointer point to the nullbyte before the field
@@ -112,7 +112,7 @@ class RowBlock {
112112

113113
uint32_t _capacity;
114114
RowBlockInfo _info;
115-
const TabletSchema* _schema; // 内部保存的schema句柄
115+
TabletSchemaSPtr _schema; // 内部保存的schema句柄
116116

117117
bool _null_supported;
118118

0 commit comments

Comments
 (0)