Skip to content

Commit 0fe5b93

Browse files
wuwenchiwuwenchi.wwc
andauthored
[opt](iceberg)support drop tag and branch (#52453)
### What problem does this PR solve? Related Issue: #48285 Problem Summary: support drop tag and branch: ```sql alter table tb1 drop branch b1; alter table tb1 drop branch if exists b2; alter table tb1 drop tag t1; alter table tb1 drop tag if exists t2; ``` Co-authored-by: wuwenchi.wwc <[email protected]>
1 parent 12bccad commit 0fe5b93

19 files changed

Lines changed: 751 additions & 10 deletions

File tree

‎fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,8 @@ alterTableClause
719719
INTERVAL INTEGER_VALUE unit=identifier? properties=propertyClause? #alterMultiPartitionClause
720720
| createOrReplaceTagClause #createOrReplaceTagClauses
721721
| createOrReplaceBranchClause #createOrReplaceBranchClauses
722+
| dropBranchClause #dropBranchClauses
723+
| dropTagClause #dropTagClauses
722724
;
723725

724726
createOrReplaceTagClause
@@ -757,6 +759,14 @@ timeValueWithUnit
757759
: timeValue=INTEGER_VALUE timeUnit=(DAYS | HOURS | MINUTES)
758760
;
759761

762+
dropBranchClause
763+
: DROP BRANCH (IF EXISTS)? name=identifier
764+
;
765+
766+
dropTagClause
767+
: DROP TAG (IF EXISTS)? name=identifier
768+
;
769+
760770
columnPosition
761771
: FIRST
762772
| AFTER position=identifier

‎fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
import org.apache.doris.analysis.CreateMaterializedViewStmt;
2828
import org.apache.doris.analysis.CreateOrReplaceBranchClause;
2929
import org.apache.doris.analysis.CreateOrReplaceTagClause;
30+
import org.apache.doris.analysis.DropBranchClause;
3031
import org.apache.doris.analysis.DropMaterializedViewStmt;
3132
import org.apache.doris.analysis.DropPartitionClause;
3233
import org.apache.doris.analysis.DropPartitionFromIndexClause;
34+
import org.apache.doris.analysis.DropTagClause;
3335
import org.apache.doris.analysis.ModifyColumnCommentClause;
3436
import org.apache.doris.analysis.ModifyDistributionClause;
3537
import org.apache.doris.analysis.ModifyEngineClause;
@@ -394,6 +396,14 @@ private void processAlterTableForExternalTable(
394396
table.getCatalog().createOrReplaceTag(
395397
table.getDbName(), table.getName(),
396398
((CreateOrReplaceTagClause) alterClause).getTagInfo());
399+
} else if (alterClause instanceof DropBranchClause) {
400+
table.getCatalog().dropBranch(
401+
table.getDbName(), table.getName(),
402+
((DropBranchClause) alterClause).getDropBranchInfo());
403+
} else if (alterClause instanceof DropTagClause) {
404+
table.getCatalog().dropTag(
405+
table.getDbName(), table.getName(),
406+
((DropTagClause) alterClause).getDropTagInfo());
397407
} else {
398408
throw new UserException("Invalid alter operations for external table: " + alterClauses);
399409
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.analysis;
19+
20+
import org.apache.doris.alter.AlterOpType;
21+
import org.apache.doris.common.UserException;
22+
import org.apache.doris.nereids.trees.plans.commands.info.DropBranchInfo;
23+
24+
public class DropBranchClause extends AlterTableClause {
25+
private final DropBranchInfo dropBranchInfo;
26+
27+
public DropBranchClause(DropBranchInfo dropBranchInfo) {
28+
super(AlterOpType.ALTER_BRANCH);
29+
this.dropBranchInfo = dropBranchInfo;
30+
}
31+
32+
@Override
33+
public boolean allowOpMTMV() {
34+
return false;
35+
}
36+
37+
@Override
38+
public boolean needChangeMTMVState() {
39+
return false;
40+
}
41+
42+
@Override
43+
public void analyze(Analyzer analyzer) throws UserException {
44+
45+
}
46+
47+
@Override
48+
public String toSql() {
49+
return dropBranchInfo.toSql();
50+
}
51+
52+
public DropBranchInfo getDropBranchInfo() {
53+
return dropBranchInfo;
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.analysis;
19+
20+
import org.apache.doris.alter.AlterOpType;
21+
import org.apache.doris.common.UserException;
22+
import org.apache.doris.nereids.trees.plans.commands.info.DropTagInfo;
23+
24+
public class DropTagClause extends AlterTableClause {
25+
private final DropTagInfo dropTagInfo;
26+
27+
public DropTagClause(DropTagInfo dropTagInfo) {
28+
super(AlterOpType.ALTER_TAG);
29+
this.dropTagInfo = dropTagInfo;
30+
}
31+
32+
@Override
33+
public boolean allowOpMTMV() {
34+
return false;
35+
}
36+
37+
@Override
38+
public boolean needChangeMTMVState() {
39+
return false;
40+
}
41+
42+
@Override
43+
public void analyze(Analyzer analyzer) throws UserException {
44+
45+
}
46+
47+
@Override
48+
public String toSql() {
49+
return dropTagInfo.toSql();
50+
}
51+
52+
public DropTagInfo getDropTagInfo() {
53+
return dropTagInfo;
54+
}
55+
}

‎fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogIf.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.apache.doris.nereids.trees.plans.commands.TruncateTableCommand;
3636
import org.apache.doris.nereids.trees.plans.commands.info.CreateOrReplaceBranchInfo;
3737
import org.apache.doris.nereids.trees.plans.commands.info.CreateOrReplaceTagInfo;
38+
import org.apache.doris.nereids.trees.plans.commands.info.DropBranchInfo;
39+
import org.apache.doris.nereids.trees.plans.commands.info.DropTagInfo;
3840

3941
import com.google.common.base.Strings;
4042
import com.google.common.collect.Lists;
@@ -234,7 +236,15 @@ default void createOrReplaceTag(String db, String tbl, CreateOrReplaceTagInfo ta
234236
throw new UserException("Not support create or replace tag operation");
235237
}
236238

237-
default void replayCreateOrReplaceBranchOrTag(String dbName, String tblName) {
239+
default void replayOperateOnBranchOrTag(String dbName, String tblName) {
238240

239241
}
242+
243+
default void dropBranch(String db, String tbl, DropBranchInfo branchInfo) throws UserException {
244+
throw new UserException("Not support drop branch operation");
245+
}
246+
247+
default void dropTag(String db, String tbl, DropTagInfo tagInfo) throws UserException {
248+
throw new UserException("Not support drop tag operation");
249+
}
240250
}

‎fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalCatalog.java‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import org.apache.doris.nereids.trees.plans.commands.TruncateTableCommand;
6262
import org.apache.doris.nereids.trees.plans.commands.info.CreateOrReplaceBranchInfo;
6363
import org.apache.doris.nereids.trees.plans.commands.info.CreateOrReplaceTagInfo;
64+
import org.apache.doris.nereids.trees.plans.commands.info.DropBranchInfo;
65+
import org.apache.doris.nereids.trees.plans.commands.info.DropTagInfo;
6466
import org.apache.doris.persist.CreateDbInfo;
6567
import org.apache.doris.persist.CreateTableInfo;
6668
import org.apache.doris.persist.DropDbInfo;
@@ -1374,9 +1376,43 @@ public void createOrReplaceTag(String db, String tbl, CreateOrReplaceTagInfo tag
13741376
}
13751377

13761378
@Override
1377-
public void replayCreateOrReplaceBranchOrTag(String dbName, String tblName) {
1379+
public void replayOperateOnBranchOrTag(String dbName, String tblName) {
13781380
if (metadataOps != null) {
1379-
metadataOps.afterCreateOrReplaceBranchOrTag(dbName, tblName);
1381+
metadataOps.afterOperateOnBranchOrTag(dbName, tblName);
1382+
}
1383+
}
1384+
1385+
@Override
1386+
public void dropBranch(String db, String tbl, DropBranchInfo branchInfo) throws UserException {
1387+
makeSureInitialized();
1388+
if (metadataOps == null) {
1389+
throw new DdlException("DropBranch operation is not supported for catalog: " + getName());
1390+
}
1391+
try {
1392+
metadataOps.dropBranch(db, tbl, branchInfo);
1393+
TableBranchOrTagInfo info = new TableBranchOrTagInfo(getName(), db, tbl);
1394+
Env.getCurrentEnv().getEditLog().logBranchOrTag(info);
1395+
} catch (Exception e) {
1396+
LOG.warn("Failed to drop branch for table {}.{} in catalog {}",
1397+
db, tbl, getName(), e);
1398+
throw e;
1399+
}
1400+
}
1401+
1402+
@Override
1403+
public void dropTag(String db, String tbl, DropTagInfo tagInfo) throws UserException {
1404+
makeSureInitialized();
1405+
if (metadataOps == null) {
1406+
throw new DdlException("DropTag operation is not supported for catalog: " + getName());
1407+
}
1408+
try {
1409+
metadataOps.dropTag(db, tbl, tagInfo);
1410+
TableBranchOrTagInfo info = new TableBranchOrTagInfo(getName(), db, tbl);
1411+
Env.getCurrentEnv().getEditLog().logBranchOrTag(info);
1412+
} catch (Exception e) {
1413+
LOG.warn("Failed to drop tag for table {}.{} in catalog {}",
1414+
db, tbl, getName(), e);
1415+
throw e;
13801416
}
13811417
}
13821418
}

‎fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveMetadataOps.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import org.apache.doris.nereids.trees.plans.commands.CreateDatabaseCommand;
4040
import org.apache.doris.nereids.trees.plans.commands.info.CreateOrReplaceBranchInfo;
4141
import org.apache.doris.nereids.trees.plans.commands.info.CreateOrReplaceTagInfo;
42+
import org.apache.doris.nereids.trees.plans.commands.info.DropBranchInfo;
43+
import org.apache.doris.nereids.trees.plans.commands.info.DropTagInfo;
4244
import org.apache.doris.qe.ConnectContext;
4345

4446
import com.google.common.annotations.VisibleForTesting;
@@ -396,6 +398,16 @@ public void createOrReplaceTagImpl(String dbName, String tblName, CreateOrReplac
396398
throw new UserException("Not support create or replace tag in hive catalog.");
397399
}
398400

401+
@Override
402+
public void dropTagImpl(String dbName, String tblName, DropTagInfo tagInfo) throws UserException {
403+
throw new UserException("Not support drop tag in hive catalog.");
404+
}
405+
406+
@Override
407+
public void dropBranchImpl(String dbName, String tblName, DropBranchInfo branchInfo) throws UserException {
408+
throw new UserException("Not support drop branch in hive catalog.");
409+
}
410+
399411
@Override
400412
public List<String> listTableNames(String dbName) {
401413
return client.getAllTables(dbName);

‎fe/fe-core/src/main/java/org/apache/doris/datasource/iceberg/IcebergMetadataOps.java‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@
3838
import org.apache.doris.nereids.trees.plans.commands.info.BranchOptions;
3939
import org.apache.doris.nereids.trees.plans.commands.info.CreateOrReplaceBranchInfo;
4040
import org.apache.doris.nereids.trees.plans.commands.info.CreateOrReplaceTagInfo;
41+
import org.apache.doris.nereids.trees.plans.commands.info.DropBranchInfo;
42+
import org.apache.doris.nereids.trees.plans.commands.info.DropTagInfo;
4143
import org.apache.doris.nereids.trees.plans.commands.info.TagOptions;
4244

4345
import org.apache.iceberg.ManageSnapshots;
4446
import org.apache.iceberg.PartitionSpec;
4547
import org.apache.iceberg.Schema;
4648
import org.apache.iceberg.Snapshot;
49+
import org.apache.iceberg.SnapshotRef;
4750
import org.apache.iceberg.Table;
4851
import org.apache.iceberg.catalog.Catalog;
4952
import org.apache.iceberg.catalog.Namespace;
@@ -449,7 +452,7 @@ public void createOrReplaceBranchImpl(String dbName, String tblName, CreateOrRep
449452
}
450453

451454
@Override
452-
public void afterCreateOrReplaceBranchOrTag(String dbName, String tblName) {
455+
public void afterOperateOnBranchOrTag(String dbName, String tblName) {
453456
ExternalDatabase<?> db = dorisCatalog.getDbNullable(dbName);
454457
if (db != null) {
455458
ExternalTable tbl = db.getTableNullable(tblName);
@@ -503,6 +506,44 @@ public void createOrReplaceTagImpl(String dbName, String tblName, CreateOrReplac
503506
}
504507
}
505508

509+
@Override
510+
public void dropTagImpl(String dbName, String tblName, DropTagInfo tagInfo) throws UserException {
511+
String tagName = tagInfo.getTagName();
512+
boolean ifExists = tagInfo.getIfExists();
513+
Table icebergTable = IcebergUtils.getIcebergTable(dorisCatalog, dbName, tblName);
514+
SnapshotRef snapshotRef = icebergTable.refs().get(tagName);
515+
516+
if (snapshotRef != null || !ifExists) {
517+
ManageSnapshots manageSnapshots = icebergTable.manageSnapshots();
518+
try {
519+
preExecutionAuthenticator.execute(() -> manageSnapshots.removeTag(tagName).commit());
520+
} catch (Exception e) {
521+
throw new RuntimeException(
522+
"Failed to drop tag: " + tagName + " in table: " + icebergTable.name()
523+
+ ", error message is: " + e.getMessage(), e);
524+
}
525+
}
526+
}
527+
528+
@Override
529+
public void dropBranchImpl(String dbName, String tblName, DropBranchInfo branchInfo) throws UserException {
530+
String branchName = branchInfo.getBranchName();
531+
boolean ifExists = branchInfo.getIfExists();
532+
Table icebergTable = IcebergUtils.getIcebergTable(dorisCatalog, dbName, tblName);
533+
SnapshotRef snapshotRef = icebergTable.refs().get(branchName);
534+
535+
if (snapshotRef != null || !ifExists) {
536+
ManageSnapshots manageSnapshots = icebergTable.manageSnapshots();
537+
try {
538+
preExecutionAuthenticator.execute(() -> manageSnapshots.removeBranch(branchName).commit());
539+
} catch (Exception e) {
540+
throw new RuntimeException(
541+
"Failed to drop branch: " + branchName + " in table: " + icebergTable.name()
542+
+ ", error message is: " + e.getMessage(), e);
543+
}
544+
}
545+
}
546+
506547
public PreExecutionAuthenticator getPreExecutionAuthenticator() {
507548
return preExecutionAuthenticator;
508549
}

0 commit comments

Comments
 (0)