[fix](schema change) fix bug of query failure after rename column (#26300)

This commit is contained in:
Luwei
2023-11-20 16:54:40 +08:00
committed by GitHub
parent bd2e8239f8
commit 80c75b6da4
3 changed files with 75 additions and 6 deletions

View File

@ -4360,7 +4360,8 @@ public class Env {
}
private void renameColumn(Database db, OlapTable table, String colName,
String newColName, boolean isReplay) throws DdlException {
String newColName, Map<Long, Integer> indexIdToSchemaVersion,
boolean isReplay) throws DdlException {
table.checkNormalStateForAlter();
if (colName.equalsIgnoreCase(newColName)) {
throw new DdlException("Same column name");
@ -4406,6 +4407,12 @@ public class Env {
Env.getCurrentEnv().getQueryStats()
.rename(Env.getCurrentEnv().getCurrentCatalog().getId(), db.getId(),
table.getId(), entry.getKey(), colName, newColName);
if (!isReplay) {
indexIdToSchemaVersion.put(entry.getKey(), entry.getValue().getSchemaVersion() + 1);
}
if (indexIdToSchemaVersion != null) {
entry.getValue().setSchemaVersion(indexIdToSchemaVersion.get(entry.getKey()));
}
}
}
if (!hasColumn) {
@ -4466,7 +4473,8 @@ public class Env {
if (!isReplay) {
// log
TableRenameColumnInfo info = new TableRenameColumnInfo(db.getId(), table.getId(), colName, newColName);
TableRenameColumnInfo info = new TableRenameColumnInfo(db.getId(), table.getId(), colName, newColName,
indexIdToSchemaVersion);
editLog.logColumnRename(info);
LOG.info("rename coloumn[{}] to {}", colName, newColName);
}
@ -4477,7 +4485,8 @@ public class Env {
try {
String colName = renameClause.getColName();
String newColName = renameClause.getNewColName();
renameColumn(db, table, colName, newColName, false);
Map<Long, Integer> indexIdToSchemaVersion = new HashMap<Long, Integer>();
renameColumn(db, table, colName, newColName, indexIdToSchemaVersion, false);
} finally {
table.writeUnlock();
}
@ -4489,12 +4498,13 @@ public class Env {
long tableId = info.getTableId();
String colName = info.getColName();
String newColName = info.getNewColName();
Map<Long, Integer> indexIdToSchemaVersion = info.getIndexIdToSchemaVersion();
Database db = getCurrentEnv().getInternalCatalog().getDbOrMetaException(dbId);
OlapTable table = (OlapTable) db.getTableOrMetaException(tableId, TableType.OLAP);
table.writeLock();
try {
renameColumn(db, table, colName, newColName, true);
renameColumn(db, table, colName, newColName, indexIdToSchemaVersion, true);
} catch (DdlException e) {
// should not happen
LOG.warn("failed to replay rename column", e);

View File

@ -26,6 +26,7 @@ import com.google.gson.annotations.SerializedName;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Map;
/**
* PersistInfo for Table rename column info
@ -39,13 +40,16 @@ public class TableRenameColumnInfo implements Writable {
private String colName;
@SerializedName(value = "newColName")
private String newColName;
@SerializedName(value = "indexIdToSchemaVersion")
private Map<Long, Integer> indexIdToSchemaVersion;
public TableRenameColumnInfo(long dbId, long tableId,
String colName, String newColName) {
String colName, String newColName, Map<Long, Integer> indexIdToSchemaVersion) {
this.dbId = dbId;
this.tableId = tableId;
this.colName = colName;
this.newColName = newColName;
this.indexIdToSchemaVersion = indexIdToSchemaVersion;
}
public long getDbId() {
@ -64,6 +68,10 @@ public class TableRenameColumnInfo implements Writable {
return newColName;
}
public Map<Long, Integer> getIndexIdToSchemaVersion() {
return indexIdToSchemaVersion;
}
@Override
public void write(DataOutput out) throws IOException {
Text.writeString(out, GsonUtils.GSON.toJson(this));
@ -86,7 +94,8 @@ public class TableRenameColumnInfo implements Writable {
TableRenameColumnInfo info = (TableRenameColumnInfo) obj;
return (dbId == info.dbId && tableId == info.tableId
&& colName.equals(info.colName) && newColName.equals(info.newColName));
&& colName.equals(info.colName) && newColName.equals(info.newColName)
&& indexIdToSchemaVersion.equals(info.indexIdToSchemaVersion));
}
@Override
@ -96,6 +105,7 @@ public class TableRenameColumnInfo implements Writable {
sb.append(" tableId: ").append(tableId);
sb.append(" colName: ").append(colName);
sb.append(" newColName: ").append(newColName);
sb.append(" indexIdToSchemaVersion: ").append(indexIdToSchemaVersion.toString());
return sb.toString();
}
}