[Bugfix] Fix mv column type is not changed when do schema change (#34598)

This commit is contained in:
Lightman
2024-05-27 14:46:33 +08:00
committed by yiguolei
parent 596fb6f327
commit d71e9d34fe
9 changed files with 582 additions and 57 deletions

View File

@ -25,6 +25,7 @@ import org.apache.doris.analysis.CancelAlterTableStmt;
import org.apache.doris.analysis.CancelStmt;
import org.apache.doris.analysis.ColumnPosition;
import org.apache.doris.analysis.CreateIndexClause;
import org.apache.doris.analysis.CreateMaterializedViewStmt;
import org.apache.doris.analysis.DropColumnClause;
import org.apache.doris.analysis.DropIndexClause;
import org.apache.doris.analysis.IndexDef;
@ -671,52 +672,50 @@ public class SchemaChangeHandler extends AlterHandler {
}
List<Column> schema = entry.getValue();
for (Column column : schema) {
if (column.getName().equalsIgnoreCase(modColumn.getName())) {
String columnName = column.getName();
if (column.isMaterializedViewColumn()) {
columnName = MaterializedIndexMeta.normalizeName(
CreateMaterializedViewStmt.mvColumnBreaker(columnName));
}
if (columnName.equalsIgnoreCase(modColumn.getName())) {
otherIndexIds.add(entry.getKey());
break;
}
}
}
if (KeysType.AGG_KEYS == olapTable.getKeysType() || KeysType.UNIQUE_KEYS == olapTable.getKeysType()) {
for (Long otherIndexId : otherIndexIds) {
List<Column> otherIndexSchema = indexSchemaMap.get(otherIndexId);
for (Long otherIndexId : otherIndexIds) {
List<Column> otherIndexSchema = indexSchemaMap.get(otherIndexId);
for (int i = 0; i < otherIndexSchema.size(); i++) {
modColIndex = -1;
for (int i = 0; i < otherIndexSchema.size(); i++) {
if (otherIndexSchema.get(i).getName().equalsIgnoreCase(modColumn.getName())) {
modColIndex = i;
break;
}
Column otherCol = null;
Column col = otherIndexSchema.get(i);
String columnName = col.getName();
if (col.isMaterializedViewColumn()) {
columnName = MaterializedIndexMeta.normalizeName(
CreateMaterializedViewStmt.mvColumnBreaker(columnName));
}
Preconditions.checkState(modColIndex != -1);
// replace the old column
otherIndexSchema.set(modColIndex, modColumn);
} // end for other indices
} else {
// DUPLICATE data model has a little
for (Long otherIndexId : otherIndexIds) {
List<Column> otherIndexSchema = indexSchemaMap.get(otherIndexId);
modColIndex = -1;
for (int i = 0; i < otherIndexSchema.size(); i++) {
if (otherIndexSchema.get(i).getName().equalsIgnoreCase(modColumn.getName())) {
modColIndex = i;
break;
}
if (!columnName.equalsIgnoreCase(modColumn.getName())) {
continue;
}
modColIndex = i;
otherCol = new Column(modColumn);
otherCol.setName(col.getName());
otherCol.setDefineExpr(col.getDefineExpr());
Preconditions.checkState(modColIndex != -1);
Preconditions.checkState(otherCol != null);
// replace the old column
Column oldCol = otherIndexSchema.get(modColIndex);
Column otherCol = new Column(modColumn);
otherCol.setIsKey(oldCol.isKey());
if (null != oldCol.getAggregationType()) {
if (KeysType.AGG_KEYS != olapTable.getKeysType()
&& KeysType.UNIQUE_KEYS != olapTable.getKeysType()) {
Column oldCol = otherIndexSchema.get(modColIndex);
otherCol.setIsKey(oldCol.isKey());
otherCol.setAggregationType(oldCol.getAggregationType(), oldCol.isAggregationTypeImplicit());
} else {
otherCol.setAggregationType(null, oldCol.isAggregationTypeImplicit());
}
if (typeChanged && !lightSchemaChange) {
otherCol.setName(SHADOW_NAME_PREFIX + otherCol.getName());
}
otherIndexSchema.set(modColIndex, otherCol);
}
}
} // end for other indices
} // end for handling other indices
if (typeChanged && !lightSchemaChange) {

View File

@ -415,30 +415,6 @@ public class SchemaChangeJobV2 extends AlterJobV2 {
}
Preconditions.checkState(tbl.getState() == OlapTableState.SCHEMA_CHANGE);
Map<String, Expr> defineExprs = Maps.newHashMap();
List<Column> fullSchema = tbl.getBaseSchema(true);
DescriptorTable descTable = new DescriptorTable();
TupleDescriptor destTupleDesc = descTable.createTupleDescriptor();
for (Column column : fullSchema) {
SlotDescriptor destSlotDesc = descTable.addSlotDescriptor(destTupleDesc);
destSlotDesc.setIsMaterialized(true);
destSlotDesc.setColumn(column);
destSlotDesc.setIsNullable(column.isAllowNull());
if (indexColumnMap.containsKey(SchemaChangeHandler.SHADOW_NAME_PREFIX + column.getName())) {
Column newColumn = indexColumnMap.get(SchemaChangeHandler.SHADOW_NAME_PREFIX + column.getName());
if (newColumn.getType() != column.getType()) {
try {
SlotRef slot = new SlotRef(destSlotDesc);
slot.setCol(column.getName());
defineExprs.put(column.getName(), slot.castTo(newColumn.getType()));
} catch (AnalysisException e) {
throw new AlterCancelException(e.getMessage());
}
}
}
}
for (long partitionId : partitionIndexMap.rowKeySet()) {
Partition partition = tbl.getPartition(partitionId);
Preconditions.checkNotNull(partition, partitionId);
@ -452,6 +428,30 @@ public class SchemaChangeJobV2 extends AlterJobV2 {
long shadowIdxId = entry.getKey();
MaterializedIndex shadowIdx = entry.getValue();
long originIdxId = indexIdMap.get(shadowIdxId);
Map<String, Expr> defineExprs = Maps.newHashMap();
List<Column> fullSchema = tbl.getSchemaByIndexId(originIdxId, true);
DescriptorTable descTable = new DescriptorTable();
TupleDescriptor destTupleDesc = descTable.createTupleDescriptor();
for (Column column : fullSchema) {
SlotDescriptor destSlotDesc = descTable.addSlotDescriptor(destTupleDesc);
destSlotDesc.setIsMaterialized(true);
destSlotDesc.setColumn(column);
destSlotDesc.setIsNullable(column.isAllowNull());
if (indexColumnMap.containsKey(SchemaChangeHandler.SHADOW_NAME_PREFIX + column.getName())) {
Column newColumn = indexColumnMap.get(
SchemaChangeHandler.SHADOW_NAME_PREFIX + column.getName());
if (newColumn.getType() != column.getType()) {
try {
SlotRef slot = new SlotRef(destSlotDesc);
slot.setCol(column.getName());
defineExprs.put(column.getName(), slot.castTo(newColumn.getType()));
} catch (AnalysisException e) {
throw new AlterCancelException(e.getMessage());
}
}
}
}
int shadowSchemaHash = indexSchemaVersionAndHashMap.get(shadowIdxId).schemaHash;
int originSchemaHash = tbl.getSchemaHashByIndexId(indexIdMap.get(shadowIdxId));
List<Column> originSchemaColumns = tbl.getSchemaByIndexId(originIdxId, true);

View File

@ -671,7 +671,7 @@ public class CreateMaterializedViewStmt extends DdlStmt {
public static String mvColumnBreaker(String name) {
if (name.startsWith(MATERIALIZED_VIEW_AGGREGATE_NAME_PREFIX)) {
// mva_SUM__k2 -> k2
// mva_SUM__`k2` -> `k2`;
return mvColumnBreaker(name.substring(name.indexOf(MATERIALIZED_VIEW_AGGREGATE_NAME_LINK)
+ MATERIALIZED_VIEW_AGGREGATE_NAME_LINK.length()));
} else if (name.startsWith(MATERIALIZED_VIEW_NAME_PREFIX)) {