Skip to content

Commit b1421b5

Browse files
trueeyuleiyang0324
authored andcommitted
[Refactor] Extract some common code logic in TabletSchema (StarRocks#44193)
Signed-off-by: trueeyu <[email protected]>
1 parent b464f09 commit b1421b5

File tree

11 files changed

+42
-48
lines changed

11 files changed

+42
-48
lines changed

be/src/exec/pipeline/scan/olap_chunk_source.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,18 +412,13 @@ Status OlapChunkSource::_init_olap_reader(RuntimeState* runtime_state) {
412412

413413
auto scope = IOProfiler::scope(IOProfiler::TAG_QUERY, _scan_range->tablet_id);
414414

415-
auto tablet_schema_ptr = _tablet->tablet_schema();
416-
_tablet_schema = TabletSchema::copy(tablet_schema_ptr);
417-
418415
// if column_desc come from fe, reset tablet schema
419416
if (_scan_node->thrift_olap_scan_node().__isset.columns_desc &&
420417
!_scan_node->thrift_olap_scan_node().columns_desc.empty() &&
421418
_scan_node->thrift_olap_scan_node().columns_desc[0].col_unique_id >= 0) {
422-
_tablet_schema->clear_columns();
423-
for (const auto& column_desc : _scan_node->thrift_olap_scan_node().columns_desc) {
424-
_tablet_schema->append_column(TabletColumn(column_desc));
425-
}
426-
_tablet_schema->generate_sort_key_idxes();
419+
_tablet_schema = TabletSchema::copy(_tablet->tablet_schema(), _scan_node->thrift_olap_scan_node().columns_desc);
420+
} else {
421+
_tablet_schema = _tablet->tablet_schema();
427422
}
428423

429424
RETURN_IF_ERROR(_init_global_dicts(&_params));

be/src/exec/pipeline/scan/olap_chunk_source.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class OlapChunkSource final : public ChunkSource {
8585

8686
ObjectPool _obj_pool;
8787
TabletSharedPtr _tablet;
88-
std::shared_ptr<TabletSchema> _tablet_schema;
88+
TabletSchemaCSPtr _tablet_schema;
8989
int64_t _version = 0;
9090

9191
RuntimeState* _runtime_state = nullptr;

be/src/exec/tablet_scanner.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,12 @@ Status TabletScanner::init(RuntimeState* runtime_state, const TabletScannerParam
4949
RETURN_IF_ERROR(Expr::clone_if_not_exists(runtime_state, &_pool, *params.conjunct_ctxs, &_conjunct_ctxs));
5050
RETURN_IF_ERROR(_get_tablet(params.scan_range));
5151

52-
auto tablet_schema_ptr = _tablet->tablet_schema();
53-
_tablet_schema = TabletSchema::copy(tablet_schema_ptr);
54-
5552
// if column_desc come from fe, reset tablet schema
5653
if (_parent->_olap_scan_node.__isset.columns_desc && !_parent->_olap_scan_node.columns_desc.empty() &&
5754
_parent->_olap_scan_node.columns_desc[0].col_unique_id >= 0) {
58-
_tablet_schema->clear_columns();
59-
for (const auto& column_desc : _parent->_olap_scan_node.columns_desc) {
60-
_tablet_schema->append_column(TabletColumn(column_desc));
61-
}
62-
_tablet_schema->generate_sort_key_idxes();
55+
_tablet_schema = TabletSchema::copy(_tablet->tablet_schema(), _parent->_olap_scan_node.columns_desc);
56+
} else {
57+
_tablet_schema = _tablet->tablet_schema();
6358
}
6459

6560
RETURN_IF_ERROR(_init_unused_output_columns(*params.unused_output_columns));

be/src/exec/tablet_scanner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class TabletScanner {
105105
std::shared_ptr<TabletReader> _reader;
106106

107107
TabletSharedPtr _tablet;
108-
TabletSchemaSPtr _tablet_schema;
108+
TabletSchemaCSPtr _tablet_schema;
109109
int64_t _version = 0;
110110

111111
// output columns of `this` TabletScanner, i.e, the final output columns of `get_chunk`.

be/src/storage/compaction.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ Status Compaction::_merge_rowsets_horizontally(size_t segment_iterator_num, Stat
158158
const TabletSchemaCSPtr& tablet_schema) {
159159
TRACE_COUNTER_SCOPE_LATENCY_US("merge_rowsets_latency_us");
160160
Schema schema = ChunkHelper::convert_schema(tablet_schema);
161-
auto merge_tablet_schema = std::shared_ptr<TabletSchema>(TabletSchema::copy(tablet_schema));
162-
TabletReader reader(_tablet, _output_rs_writer->version(), merge_tablet_schema, schema);
161+
TabletReader reader(_tablet, _output_rs_writer->version(), tablet_schema, schema);
163162
TabletReaderParams reader_params;
164163
reader_params.reader_type = compaction_type();
165164
reader_params.profile = _runtime_profile.create_child("merge_rowsets");

be/src/storage/push_handler.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,12 @@ Status PushHandler::_do_streaming_ingestion(TabletSharedPtr tablet, const TPushR
102102
DeletePredicatePB del_pred;
103103
DeleteConditionHandler del_cond_handler;
104104
tablet_var.tablet->obtain_header_rdlock();
105-
auto tablet_schema = TabletSchema::copy(tablet_var.tablet->tablet_schema());
105+
TabletSchemaCSPtr tablet_schema;
106106
if (request.__isset.columns_desc && !request.columns_desc.empty() &&
107107
request.columns_desc[0].col_unique_id >= 0) {
108-
tablet_schema->clear_columns();
109-
for (const auto& column_desc : request.columns_desc) {
110-
tablet_schema->append_column(TabletColumn(column_desc));
111-
}
112-
tablet_schema->generate_sort_key_idxes();
108+
tablet_schema = TabletSchema::copy(tablet_var.tablet->tablet_schema(), request.columns_desc);
109+
} else {
110+
tablet_schema = tablet_var.tablet->tablet_schema();
113111
}
114112
res = del_cond_handler.generate_delete_predicate(*tablet_schema, request.delete_conditions, &del_pred);
115113
del_preds.push(del_pred);
@@ -122,13 +120,11 @@ Status PushHandler::_do_streaming_ingestion(TabletSharedPtr tablet, const TPushR
122120
}
123121
}
124122

125-
auto tablet_schema = std::shared_ptr<TabletSchema>(TabletSchema::copy(tablet_vars->at(0).tablet->tablet_schema()));
123+
TabletSchemaCSPtr tablet_schema;
126124
if (request.__isset.columns_desc && !request.columns_desc.empty() && request.columns_desc[0].col_unique_id >= 0) {
127-
tablet_schema->clear_columns();
128-
for (const auto& column_desc : request.columns_desc) {
129-
tablet_schema->append_column(TabletColumn(column_desc));
130-
}
131-
tablet_schema->generate_sort_key_idxes();
125+
tablet_schema = TabletSchema::copy(tablet_vars->at(0).tablet->tablet_schema(), request.columns_desc);
126+
} else {
127+
tablet_schema = tablet_vars->at(0).tablet->tablet_schema();
132128
}
133129

134130
Status st = Status::OK();

be/src/storage/rowset/rowset_meta.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,15 @@ class RowsetMeta {
245245
}
246246
}
247247

248-
void get_tablet_schema_pb(TabletSchemaPB* tablet_schema_pb) {
249-
DCHECK(_schema != nullptr);
250-
_schema->to_schema_pb(tablet_schema_pb);
251-
}
252-
253248
void set_tablet_schema(const TabletSchemaCSPtr& tablet_schema_ptr) {
254249
_rowset_meta_pb->clear_tablet_schema();
255250
TabletSchemaPB ts_pb;
256251
tablet_schema_ptr->to_schema_pb(&ts_pb);
257252
if (ts_pb.has_id() && ts_pb.id() != TabletSchema::invalid_id()) {
258253
_schema = GlobalTabletSchemaMap::Instance()->emplace(ts_pb).first;
259254
} else {
255+
// Only for compatible, in very old versions, there is no schema id.
256+
// If you fill with the default value, you cannot judge whether it is the same schema through the schema id.
260257
_schema = TabletSchemaCSPtr(TabletSchema::copy(tablet_schema_ptr));
261258
}
262259
_has_tablet_schema_pb = true;

be/src/storage/tablet_reader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ TabletReader::TabletReader(TabletSharedPtr tablet, const Version& version, Schem
5151
}
5252

5353
TabletReader::TabletReader(TabletSharedPtr tablet, const Version& version, Schema schema,
54-
std::vector<RowsetSharedPtr> captured_rowsets, const TabletSchemaSPtr* tablet_schema)
54+
std::vector<RowsetSharedPtr> captured_rowsets, const TabletSchemaCSPtr* tablet_schema)
5555
: ChunkIterator(std::move(schema)),
5656
_tablet(std::move(tablet)),
5757
_version(version),
@@ -73,7 +73,7 @@ TabletReader::TabletReader(TabletSharedPtr tablet, const Version& version, Schem
7373
_tablet_schema = !tablet_schema ? _tablet->tablet_schema() : tablet_schema;
7474
}
7575

76-
TabletReader::TabletReader(TabletSharedPtr tablet, const Version& version, const TabletSchemaSPtr& tablet_schema,
76+
TabletReader::TabletReader(TabletSharedPtr tablet, const Version& version, const TabletSchemaCSPtr& tablet_schema,
7777
Schema schema)
7878
: ChunkIterator(std::move(schema)), _tablet(std::move(tablet)), _version(version) {
7979
_tablet_schema = tablet_schema;

be/src/storage/tablet_reader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class TabletReader final : public ChunkIterator {
3838
const TabletSchemaCSPtr& tablet_schema = nullptr);
3939
// *captured_rowsets* is captured forward before creating TabletReader.
4040
TabletReader(TabletSharedPtr tablet, const Version& version, Schema schema,
41-
std::vector<RowsetSharedPtr> captured_rowsets, const TabletSchemaSPtr* tablet_schema = nullptr);
41+
std::vector<RowsetSharedPtr> captured_rowsets, const TabletSchemaCSPtr* tablet_schema = nullptr);
4242
TabletReader(TabletSharedPtr tablet, const Version& version, Schema schema, bool is_key,
4343
RowSourceMaskBuffer* mask_buffer, const TabletSchemaCSPtr& tablet_schema = nullptr);
44-
TabletReader(TabletSharedPtr tablet, const Version& version, const TabletSchemaSPtr& tablet_schema, Schema schema);
44+
TabletReader(TabletSharedPtr tablet, const Version& version, const TabletSchemaCSPtr& tablet_schema, Schema schema);
4545
~TabletReader() override { close(); }
4646

4747
void set_is_asc_hint(bool is_asc) { _is_asc_hint = is_asc; }

be/src/storage/tablet_schema.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,17 @@ std::unique_ptr<TabletSchema> TabletSchema::copy(const std::shared_ptr<const Tab
375375
return t_ptr;
376376
}
377377

378+
TabletSchemaCSPtr TabletSchema::copy(const TabletSchemaCSPtr& src_schema, const std::vector<TColumn>& cols) {
379+
auto dst_schema = std::make_unique<TabletSchema>();
380+
dst_schema->copy_from(src_schema);
381+
dst_schema->clear_columns();
382+
for (const auto& col : cols) {
383+
dst_schema->append_column(TabletColumn(col));
384+
}
385+
dst_schema->generate_sort_key_idxes();
386+
return dst_schema;
387+
}
388+
378389
void TabletSchema::_fill_index_map(const TabletIndex& index) {
379390
const auto idx_type = index.index_type();
380391
if (_index_map_col_unique_id.count(idx_type) <= 0) {

0 commit comments

Comments
 (0)