Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]Support add/drop field for struct column(part3) #47217

Merged
merged 8 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 243 additions & 11 deletions fe/fe-core/src/main/java/com/starrocks/alter/SchemaChangeHandler.java

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ public Column(Column column) {
Preconditions.checkArgument(this.type.isComplexType() ||
this.type.getPrimitiveType() != PrimitiveType.INVALID_TYPE);
this.uniqueId = column.getUniqueId();
this.generatedColumnExpr = column.generatedColumnExpr();
}

public Column deepCopy() {
sevev marked this conversation as resolved.
Show resolved Hide resolved
Column col = new Column(this);
col.setIsAutoIncrement(this.isAutoIncrement);
Type newType = type.clone();
col.setType(newType);
return col;
}

public ColumnDef toColumnDef() {
Expand Down
25 changes: 23 additions & 2 deletions fe/fe-core/src/main/java/com/starrocks/catalog/StructType.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public class StructType extends Type {

private static final Logger LOG = LogManager.getLogger(StructType.class);

private final HashMap<String, StructField> fieldMap = Maps.newHashMap();
private HashMap<String, StructField> fieldMap = Maps.newHashMap();
sevev marked this conversation as resolved.
Show resolved Hide resolved
@SerializedName(value = "fields")
private final ArrayList<StructField> fields;
private ArrayList<StructField> fields;

@SerializedName(value = "named")
private final boolean isNamed;
Expand Down Expand Up @@ -180,6 +180,27 @@ public StructField getField(int pos) {
return fields.get(pos);
}

public void updateFields(List<StructField> structFields) {
Preconditions.checkNotNull(structFields);
Preconditions.checkArgument(structFields.size() > 0);
fields.clear();
fieldMap.clear();
for (StructField field : structFields) {
LOG.info("field name " + field.getName());
sevev marked this conversation as resolved.
Show resolved Hide resolved
String lowerFieldName = field.getName().toLowerCase();
if (fieldMap.containsKey(lowerFieldName)) {
throw new SemanticException("struct contains duplicate subfield name: " + lowerFieldName);
} else {
field.setPosition(fields.size());
fields.add(field);
// Store lowercase field name in fieldMap
fieldMap.put(lowerFieldName, field);
}
}
selectedFields = new Boolean[fields.size()];
Arrays.fill(selectedFields, false);
}

@Override
public void setSelectedField(ComplexTypeAccessPath accessPath, boolean needSetChildren) {
if (accessPath.getAccessPathType() == ComplexTypeAccessPathType.ALL_SUBFIELDS) {
sevev marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
4 changes: 2 additions & 2 deletions fe/fe-core/src/main/java/com/starrocks/persist/EditLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ public void loadJournal(GlobalStateMgr globalStateMgr, JournalEntity journal)
}
case OperationType.OP_MODIFY_TABLE_ADD_OR_DROP_COLUMNS: {
final TableAddOrDropColumnsInfo info = (TableAddOrDropColumnsInfo) journal.getData();
globalStateMgr.getSchemaChangeHandler().replayModifyTableAddOrDropColumns(info);
globalStateMgr.getSchemaChangeHandler().replayModifyTableAddOrDrop(info);
break;
}
case OperationType.OP_SET_DEFAULT_STORAGE_VOLUME: {
Expand Down Expand Up @@ -1968,7 +1968,7 @@ private void logJsonObject(short op, Object obj) {
logEdit(op, out -> Text.writeString(out, GsonUtils.GSON.toJson(obj)));
}

public void logModifyTableAddOrDropColumns(TableAddOrDropColumnsInfo info) {
public void logModifyTableAddOrDrop(TableAddOrDropColumnsInfo info) {
logEdit(OperationType.OP_MODIFY_TABLE_ADD_OR_DROP_COLUMNS, info);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ public Void visitDropFieldClause(DropFieldClause clause, ConnectContext context)
}

Column baseColumn = ((OlapTable) table).getBaseColumn(columnName);
StructFieldDesc fieldDesc = new StructFieldDesc(clause.getFieldName(), clause.getNestedFieldName(), null, null);
StructFieldDesc fieldDesc = new StructFieldDesc(clause.getFieldName(), clause.getNestedParentFieldNames(), null, null);
try {
fieldDesc.analyze(baseColumn, true);
} catch (AnalysisException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package com.starrocks.sql.ast;

import com.starrocks.alter.AlterOpType;
import com.starrocks.analysis.ColumnPosition;
import com.starrocks.catalog.Type;
import com.starrocks.sql.ast.StructFieldDesc;
import com.starrocks.sql.parser.NodePosition;

import java.util.List;
import java.util.Map;

// clause which is used to add one field to
Expand All @@ -33,6 +36,22 @@ public StructFieldDesc getFieldDesc() {
return fieldDesc;
}

public List<String> getNestedParentFieldNames() {
return fieldDesc.getNestedParentFieldNames();
}

public String getFieldName() {
return fieldDesc.getFieldName();
}

public Type getType() {
return fieldDesc.getType();
}

public ColumnPosition getFieldPos() {
return fieldDesc.getFieldPos();
}

public AddFieldClause(String colName, StructFieldDesc fieldDesc, Map<String, String> properties) {
super(AlterOpType.SCHEMA_CHANGE, null, properties, NodePosition.ZERO);
this.colName = colName;
sevev marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class DropFieldClause extends AlterTableColumnClause {
private final String colName;
private final String fieldName;
private final List<String> nestedFieldName;
private final List<String> nestedParentFieldNames;

public String getColName() {
return colName;
Expand All @@ -34,16 +34,16 @@ public String getFieldName() {
return fieldName;
}

public List<String> getNestedFieldName() {
return nestedFieldName;
public List<String> getNestedParentFieldNames() {
return nestedParentFieldNames;
}

public DropFieldClause(String colName, String fieldName, List<String> nestedFieldName,
public DropFieldClause(String colName, String fieldName, List<String> nestedParentFieldNames,
Map<String, String> properties) {
super(AlterOpType.SCHEMA_CHANGE, null, properties, NodePosition.ZERO);
this.colName = colName;
this.fieldName = fieldName;
this.nestedFieldName = nestedFieldName;
this.nestedParentFieldNames = nestedParentFieldNames;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ private Type getFieldType(Type type, String fieldName) {
return null;
}

public List<String> getNestedParentFieldNames() {
return nestedParentFieldNames;
}

public String getFieldName() {
return fieldName;
}

public Type getType() {
return typeDef.getType();
}

public ColumnPosition getFieldPos() {
return fieldPos;
}

public void analyze(Column baseCol, boolean dropField) throws AnalysisException {
if (baseCol == null) {
throw new AnalysisException(String.format("Analyze add/drop field failed, modify column is not exist"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,14 @@ public void testModifyTableAddOrDropColumns() {

Assertions.assertDoesNotThrow(
() -> ((SchemaChangeHandler) GlobalStateMgr.getCurrentState().getAlterJobMgr().getSchemaChangeHandler())
.modifyTableAddOrDropColumns(db, tbl, indexSchemaMap, newIndexes, 100, 100,
.modifyTableAddOrDrop(db, tbl, indexSchemaMap, newIndexes, 100, 100,
indexToNewSchemaId, false));
jobSize++;
Assertions.assertEquals(jobSize, alterJobs.size());

Assertions.assertDoesNotThrow(
() -> ((SchemaChangeHandler) GlobalStateMgr.getCurrentState().getAlterJobMgr().getSchemaChangeHandler())
.modifyTableAddOrDropColumns(db, tbl, indexSchemaMap, newIndexes, 101, 101,
.modifyTableAddOrDrop(db, tbl, indexSchemaMap, newIndexes, 101, 101,
indexToNewSchemaId, true));
jobSize++;
Assertions.assertEquals(jobSize, alterJobs.size());
Expand All @@ -391,7 +391,7 @@ public void testModifyTableAddOrDropColumns() {
tbl.setState(OlapTableState.ROLLUP);
Assertions.assertThrows(DdlException.class,
() -> ((SchemaChangeHandler) GlobalStateMgr.getCurrentState().getAlterJobMgr().getSchemaChangeHandler())
.modifyTableAddOrDropColumns(db, tbl, indexSchemaMap, newIndexes, 102, 102, indexToNewSchemaId,
.modifyTableAddOrDrop(db, tbl, indexSchemaMap, newIndexes, 102, 102, indexToNewSchemaId,
false));
tbl.setState(beforeState);
}
Expand Down
Loading
Loading