diff --git a/be/src/olap/delta_writer.cpp b/be/src/olap/delta_writer.cpp index ae732c55b818a5..083d026473c610 100644 --- a/be/src/olap/delta_writer.cpp +++ b/be/src/olap/delta_writer.cpp @@ -381,11 +381,18 @@ void DeltaWriter::_build_current_tablet_schema(int64_t index_id, const POlapTableSchemaParam& ptable_schema_param, const TabletSchema& ori_tablet_schema) { _tablet_schema->copy_from(ori_tablet_schema); - //new tablet schame if new table + + // find the right index id + int i = 0; + for (; i < ptable_schema_param.indexes_size(); i++) { + if (ptable_schema_param.indexes(i).id() == index_id) break; + } + if (ptable_schema_param.indexes_size() > 0 && - ptable_schema_param.indexes(0).columns_desc_size() != 0 && - ptable_schema_param.indexes(0).columns_desc(0).unique_id() >= 0) { - _tablet_schema->build_current_tablet_schema(index_id, ptable_schema_param, + ptable_schema_param.indexes(i).columns_desc_size() != 0 && + ptable_schema_param.indexes(i).columns_desc(0).unique_id() >= 0) { + _tablet_schema->build_current_tablet_schema(index_id, ptable_schema_param.version(), + ptable_schema_param.indexes(i), ori_tablet_schema); } } diff --git a/be/src/olap/tablet_meta.cpp b/be/src/olap/tablet_meta.cpp index 06f04a4578892c..c1bc473bbdadef 100644 --- a/be/src/olap/tablet_meta.cpp +++ b/be/src/olap/tablet_meta.cpp @@ -148,8 +148,9 @@ TabletMeta::TabletMeta(int64_t table_id, int64_t partition_id, int64_t tablet_id if (tcolumn.col_unique_id >= 0) { unique_id = tcolumn.col_unique_id; } else { - unique_id = col_ordinal_to_unique_id.at(col_ordinal++); + unique_id = col_ordinal_to_unique_id.at(col_ordinal); } + col_ordinal++; init_column_from_tcolumn(unique_id, tcolumn, column); if (column->is_key()) { diff --git a/be/src/olap/tablet_schema.cpp b/be/src/olap/tablet_schema.cpp index fae5865f876218..597c5e60f27ee1 100644 --- a/be/src/olap/tablet_schema.cpp +++ b/be/src/olap/tablet_schema.cpp @@ -543,8 +543,8 @@ std::string TabletSchema::to_key() const { return pb.SerializeAsString(); } -void TabletSchema::build_current_tablet_schema(int64_t index_id, - const POlapTableSchemaParam& ptable_schema_param, +void TabletSchema::build_current_tablet_schema(int64_t index_id, int32_t version, + const POlapTableIndexSchema& index, const TabletSchema& ori_tablet_schema) { // copy from ori_tablet_schema _keys_type = ori_tablet_schema.keys_type(); @@ -561,6 +561,7 @@ void TabletSchema::build_current_tablet_schema(int64_t index_id, _sort_col_num = ori_tablet_schema.sort_col_num(); // copy from table_schema_param + _schema_version = version; _num_columns = 0; _num_key_columns = 0; _num_null_columns = 0; @@ -569,28 +570,24 @@ void TabletSchema::build_current_tablet_schema(int64_t index_id, _field_name_to_index.clear(); _field_id_to_index.clear(); - for (const POlapTableIndexSchema& index : ptable_schema_param.indexes()) { - if (index.id() == index_id) { - for (auto& pcolumn : index.columns_desc()) { - TabletColumn column; - column.init_from_pb(pcolumn); - if (column.is_key()) { - _num_key_columns++; - } - if (column.is_nullable()) { - _num_null_columns++; - } - if (column.is_bf_column()) { - has_bf_columns = true; - } - _field_name_to_index[column.name()] = _num_columns; - _field_id_to_index[column.unique_id()] = _num_columns; - _cols.emplace_back(std::move(column)); - _num_columns++; - } - break; + for (auto& pcolumn : index.columns_desc()) { + TabletColumn column; + column.init_from_pb(pcolumn); + if (column.is_key()) { + _num_key_columns++; + } + if (column.is_nullable()) { + _num_null_columns++; } + if (column.is_bf_column()) { + has_bf_columns = true; + } + _field_name_to_index[column.name()] = _num_columns; + _field_id_to_index[column.unique_id()] = _num_columns; + _cols.emplace_back(std::move(column)); + _num_columns++; } + if (has_bf_columns) { _has_bf_fpp = true; _bf_fpp = ori_tablet_schema.bloom_filter_fpp(); @@ -598,7 +595,6 @@ void TabletSchema::build_current_tablet_schema(int64_t index_id, _has_bf_fpp = false; _bf_fpp = BLOOM_FILTER_DEFAULT_FPP; } - _schema_version = ptable_schema_param.version(); } void TabletSchema::to_schema_pb(TabletSchemaPB* tablet_schema_pb) const { diff --git a/be/src/olap/tablet_schema.h b/be/src/olap/tablet_schema.h index 5789d9f60ff8ac..aa0206cbbed755 100644 --- a/be/src/olap/tablet_schema.h +++ b/be/src/olap/tablet_schema.h @@ -31,7 +31,7 @@ namespace vectorized { class Block; } -class POlapTableSchemaParam; +class POlapTableIndexSchema; class TabletColumn { public: @@ -169,8 +169,8 @@ class TabletSchema { const std::unordered_set* tablet_columns_need_convert_null = nullptr) const; vectorized::Block create_block() const; - void build_current_tablet_schema(int64_t index_id, - const POlapTableSchemaParam& ptable_schema_param, + void build_current_tablet_schema(int64_t index_id, int32_t version, + const POlapTableIndexSchema& index, const TabletSchema& out_tablet_schema); private: diff --git a/fe/fe-core/src/main/java/org/apache/doris/alter/MaterializedViewHandler.java b/fe/fe-core/src/main/java/org/apache/doris/alter/MaterializedViewHandler.java index 35fe2834008da6..0bfd0e6ab9b607 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/alter/MaterializedViewHandler.java +++ b/fe/fe-core/src/main/java/org/apache/doris/alter/MaterializedViewHandler.java @@ -498,6 +498,11 @@ private List checkAndPrepareMaterializedView(CreateMaterializedViewStmt if (KeysType.UNIQUE_KEYS == olapTable.getKeysType() && olapTable.hasSequenceCol()) { newMVColumns.add(new Column(olapTable.getSequenceCol())); } + + // set MV column unique id to Column.COLUMN_UNIQUE_ID_INIT_VALUE support old unique id rule. + newMVColumns.stream().forEach(column -> { + column.setUniqueId(Column.COLUMN_UNIQUE_ID_INIT_VALUE); + }); return newMVColumns; }