Skip to content

Commit

Permalink
[Feature]Support add/drop field for struct column(part2) (backport #4…
Browse files Browse the repository at this point in the history
…6619) (#47206)

Signed-off-by: sevev <[email protected]>
Co-authored-by: zhangqiang <[email protected]>
  • Loading branch information
mergify[bot] and sevev authored Jun 26, 2024
1 parent d9f1792 commit 4546416
Show file tree
Hide file tree
Showing 14 changed files with 882 additions and 2 deletions.
14 changes: 14 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/alter/AlterJobExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.AddColumnClause;
import com.starrocks.sql.ast.AddColumnsClause;
import com.starrocks.sql.ast.AddFieldClause;
import com.starrocks.sql.ast.AddPartitionClause;
import com.starrocks.sql.ast.AddRollupClause;
import com.starrocks.sql.ast.AlterClause;
Expand All @@ -47,6 +48,7 @@
import com.starrocks.sql.ast.CompactionClause;
import com.starrocks.sql.ast.CreateIndexClause;
import com.starrocks.sql.ast.DropColumnClause;
import com.starrocks.sql.ast.DropFieldClause;
import com.starrocks.sql.ast.DropIndexClause;
import com.starrocks.sql.ast.DropPartitionClause;
import com.starrocks.sql.ast.DropRollupClause;
Expand Down Expand Up @@ -231,6 +233,18 @@ public Void visitCompactionClause(CompactionClause clause, ConnectContext contex
return null;
}

@Override
public Void visitAddFieldClause(AddFieldClause clause, ConnectContext context) {
unsupportedException("Not support");
return null;
}

@Override
public Void visitDropFieldClause(DropFieldClause clause, ConnectContext context) {
unsupportedException("Not support");
return null;
}

//Alter partition clause

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.AddColumnClause;
import com.starrocks.sql.ast.AddColumnsClause;
import com.starrocks.sql.ast.AddFieldClause;
import com.starrocks.sql.ast.AlterClause;
import com.starrocks.sql.ast.CancelAlterTableStmt;
import com.starrocks.sql.ast.CancelStmt;
import com.starrocks.sql.ast.CreateIndexClause;
import com.starrocks.sql.ast.DropColumnClause;
import com.starrocks.sql.ast.DropFieldClause;
import com.starrocks.sql.ast.DropIndexClause;
import com.starrocks.sql.ast.ModifyColumnClause;
import com.starrocks.sql.ast.ModifyTablePropertiesClause;
Expand Down Expand Up @@ -1648,6 +1650,10 @@ public int getAsInt() {

// modify column
fastSchemaEvolution &= processModifyColumn(modifyColumnClause, olapTable, indexSchemaMap);
} else if (alterClause instanceof AddFieldClause) {
throw new DdlException("Add field is not support so far");
} else if (alterClause instanceof DropFieldClause) {
throw new DdlException("Drop field is not support so far");
} else if (alterClause instanceof ReorderColumnsClause) {
// reorder column
fastSchemaEvolution = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import com.starrocks.qe.ConnectContext;
import com.starrocks.sql.ast.AddColumnClause;
import com.starrocks.sql.ast.AddColumnsClause;
import com.starrocks.sql.ast.AddFieldClause;
import com.starrocks.sql.ast.AlterTableCommentClause;
import com.starrocks.sql.ast.AlterTableStmt;
import com.starrocks.sql.ast.ColumnDef;
import com.starrocks.sql.ast.ColumnRenameClause;
import com.starrocks.sql.ast.DropColumnClause;
import com.starrocks.sql.ast.DropFieldClause;
import com.starrocks.sql.ast.ModifyColumnClause;
import com.starrocks.sql.ast.ModifyTablePropertiesClause;
import com.starrocks.sql.ast.TableRenameClause;
Expand All @@ -48,6 +50,7 @@
import static com.starrocks.connector.iceberg.IcebergMetadata.COMPRESSION_CODEC;
import static com.starrocks.connector.iceberg.IcebergMetadata.FILE_FORMAT;
import static com.starrocks.connector.iceberg.IcebergMetadata.LOCATION_PROPERTY;
import static com.starrocks.sql.common.UnsupportedException.unsupportedException;

public class IcebergAlterTableExecutor extends ConnectorAlterTableExecutor {
private org.apache.iceberg.Table table;
Expand Down Expand Up @@ -191,6 +194,18 @@ public Void visitModifyColumnClause(ModifyColumnClause clause, ConnectContext co
return null;
}

@Override
public Void visitAddFieldClause(AddFieldClause clause, ConnectContext context) {
unsupportedException("Not support");
return null;
}

@Override
public Void visitDropFieldClause(DropFieldClause clause, ConnectContext context) {
unsupportedException("Not support");
return null;
}

@Override
public Void visitModifyTablePropertiesClause(ModifyTablePropertiesClause clause, ConnectContext context) {
actions.add(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.starrocks.server.RunMode;
import com.starrocks.sql.ast.AddColumnClause;
import com.starrocks.sql.ast.AddColumnsClause;
import com.starrocks.sql.ast.AddFieldClause;
import com.starrocks.sql.ast.AddPartitionClause;
import com.starrocks.sql.ast.AddRollupClause;
import com.starrocks.sql.ast.AlterClause;
Expand All @@ -70,6 +71,7 @@
import com.starrocks.sql.ast.CreateIndexClause;
import com.starrocks.sql.ast.DistributionDesc;
import com.starrocks.sql.ast.DropColumnClause;
import com.starrocks.sql.ast.DropFieldClause;
import com.starrocks.sql.ast.DropPartitionClause;
import com.starrocks.sql.ast.DropRollupClause;
import com.starrocks.sql.ast.HashDistributionDesc;
Expand All @@ -93,6 +95,7 @@
import com.starrocks.sql.ast.RollupRenameClause;
import com.starrocks.sql.ast.SingleItemListPartitionDesc;
import com.starrocks.sql.ast.SingleRangePartitionDesc;
import com.starrocks.sql.ast.StructFieldDesc;
import com.starrocks.sql.ast.TableRenameClause;

import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -709,6 +712,48 @@ public Void visitDropColumnClause(DropColumnClause clause, ConnectContext contex
return null;
}

@Override
public Void visitAddFieldClause(AddFieldClause clause, ConnectContext context) {
String columnName = clause.getColName();
if (Strings.isNullOrEmpty(columnName)) {
throw new SemanticException(PARSER_ERROR_MSG.invalidColFormat(columnName));
}

if (!table.isOlapTable()) {
throw new SemanticException("Add field only support olap table");
}

Column baseColumn = ((OlapTable) table).getBaseColumn(columnName);
StructFieldDesc fieldDesc = clause.getFieldDesc();
try {
fieldDesc.analyze(baseColumn, false);
} catch (AnalysisException e) {
throw new SemanticException("Analyze add field definition failed: %s", e.getMessage());
}
return null;
}

@Override
public Void visitDropFieldClause(DropFieldClause clause, ConnectContext context) {
String columnName = clause.getColName();
if (Strings.isNullOrEmpty(columnName)) {
throw new SemanticException(PARSER_ERROR_MSG.invalidColFormat(columnName));
}

if (!table.isOlapTable()) {
throw new SemanticException("Drop field only support olap table");
}

Column baseColumn = ((OlapTable) table).getBaseColumn(columnName);
StructFieldDesc fieldDesc = new StructFieldDesc(clause.getFieldName(), clause.getNestedFieldName(), null, null);
try {
fieldDesc.analyze(baseColumn, true);
} catch (AnalysisException e) {
throw new SemanticException("Analyze drop field definition failed: %s", e.getMessage());
}
return null;
}

@Override
public Void visitModifyColumnClause(ModifyColumnClause clause, ConnectContext context) {
ColumnDef columnDef = clause.getColumnDef();
Expand Down
46 changes: 46 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/sql/ast/AddFieldClause.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.starrocks.sql.ast;

import com.starrocks.alter.AlterOpType;
import com.starrocks.sql.ast.StructFieldDesc;
import com.starrocks.sql.parser.NodePosition;

import java.util.Map;

// clause which is used to add one field to
public class AddFieldClause extends AlterTableColumnClause {
private final String colName;
private final StructFieldDesc fieldDesc;

public String getColName() {
return colName;
}

public StructFieldDesc getFieldDesc() {
return fieldDesc;
}

public AddFieldClause(String colName, StructFieldDesc fieldDesc, Map<String, String> properties) {
super(AlterOpType.SCHEMA_CHANGE, null, properties, NodePosition.ZERO);
this.colName = colName;
this.fieldDesc = fieldDesc;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitAddFieldClause(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,14 @@ default R visitCompactionClause(CompactionClause clause, C context) {
return visitNode(clause, context);
}

default R visitAddFieldClause(AddFieldClause clause, C context) {
return visitNode(clause, context);
}

default R visitDropFieldClause(DropFieldClause clause, C context) {
return visitNode(clause, context);
}

//Alter partition clause

default R visitModifyPartitionClause(ModifyPartitionClause clause, C context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


package com.starrocks.sql.ast;

import com.starrocks.alter.AlterOpType;
import com.starrocks.sql.parser.NodePosition;

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

public class DropFieldClause extends AlterTableColumnClause {
private final String colName;
private final String fieldName;
private final List<String> nestedFieldName;

public String getColName() {
return colName;
}

public String getFieldName() {
return fieldName;
}

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

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

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitDropFieldClause(this, context);
}
}
Loading

0 comments on commit 4546416

Please sign in to comment.