Skip to content

Commit

Permalink
[BugFix] Fix optimize table fail when specify partition name (#34896)
Browse files Browse the repository at this point in the history
Signed-off-by: meegoo <meegoo.sr@gmail.com>
  • Loading branch information
meegoo authored Nov 14, 2023
1 parent 4333085 commit 04a0be7
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ protected void runPendingJob() throws AlterCancelException {
try {
PartitionUtils.createAndAddTempPartitionsForTable(db, targetTable, postfix,
optimizeClause.getSourcePartitionIds(), getTmpPartitionIds(), optimizeClause.getDistributionDesc());
LOG.debug("create temp partitions {} success. job: {}", getTmpPartitionIds(), jobId);
} catch (Exception e) {
LOG.warn("create temp partitions failed", e);
throw new AlterCancelException("create temp partitions failed " + e);
Expand Down Expand Up @@ -215,7 +216,7 @@ protected void runWaitingTxnJob() throws AlterCancelException {
throw new AlterCancelException(e.getMessage());
}

LOG.info("previous transactions are all finished, begin to rewrite data. job: {}", jobId);
LOG.info("previous transactions are all finished, begin to optimize table. job: {}", jobId);

List<String> tmpPartitionNames;
List<String> partitionNames = Lists.newArrayList();
Expand Down Expand Up @@ -261,6 +262,7 @@ protected void runWaitingTxnJob() throws AlterCancelException {
try {
taskManager.createTask(rewriteTask, false);
taskManager.executeTask(rewriteTask.getName());
LOG.debug("create rewrite task {}", rewriteTask.toString());
} catch (DdlException e) {
rewriteTask.setOptimizeTaskState(Constants.TaskRunState.FAILED);
LOG.warn("create rewrite task failed", e);
Expand Down Expand Up @@ -343,6 +345,8 @@ protected void runRunningJob() throws AlterCancelException {

this.progress = 99;

LOG.debug("all insert overwrite tasks finished, optimize job: {}", jobId);

// replace partition
db.writeLock();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public void write(DataOutput out) throws IOException {

@Override
public String toString() {
return super.toString();
return super.toString() +
" OptimizeTask{" +
"partitionName='" + partitionName + '\'' +
", tempPartitionName='" + tempPartitionName + '\'' +
", optimizeTaskState=" + optimizeTaskState +
", lastVersion=" + lastVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ public Void visitOptimizeClause(OptimizeClause clause, ConnectContext context) {
}
partitionIds.add(partition.getId());
}
clause.setSourcePartitionIds(partitionIds);
} else {
clause.setSourcePartitionIds(olapTable.getPartitions().stream().map(Partition::getId).collect(Collectors.toList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -952,11 +952,11 @@ modifyCommentClause
;

optimizeClause
: keyDesc?
: partitionNames?
keyDesc?
partitionDesc?
orderByDesc?
distributionDesc?
partitionNames?
;

addColumnClause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void testOptimizeParser() throws Exception {
stmt = "alter table testTable1 order by (v1)";
alterStmt = (AlterTableStmt) UtFrameUtils.parseStmtWithNewParser(stmt, starRocksAssert.getCtx());

stmt = "alter table testTable1 duplicate key(v1) partition (t1)";
stmt = "alter table testTable1 partition (t1) duplicate key(v1)";
try {
alterStmt = (AlterTableStmt) UtFrameUtils.parseStmtWithNewParser(stmt, starRocksAssert.getCtx());
Assert.fail();
Expand All @@ -101,7 +101,7 @@ public void testOptimizeParser() throws Exception {
Assert.assertTrue(e.getMessage().contains("not support optimize"));
}

stmt = "alter table testTable1 distributed by hash(v1) partition (t1)";
stmt = "alter table testTable1 partition (t1) distributed by hash(v1)";
try {
alterStmt = (AlterTableStmt) UtFrameUtils.parseStmtWithNewParser(stmt, starRocksAssert.getCtx());
Assert.fail();
Expand All @@ -110,7 +110,7 @@ public void testOptimizeParser() throws Exception {
Assert.assertTrue(e.getMessage().contains("does not exist"));
}

stmt = "alter table testTable1 distributed by hash(v1) temporary partition (t1)";
stmt = "alter table testTable1 temporary partition (t1) distributed by hash(v1)";
try {
alterStmt = (AlterTableStmt) UtFrameUtils.parseStmtWithNewParser(stmt, starRocksAssert.getCtx());
Assert.fail();
Expand Down
43 changes: 41 additions & 2 deletions test/sql/test_optimize_table/R/test_optimize_table
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ create table t(k int) distributed by hash(k) buckets 10;
-- !result
show partitions from t;
-- result:
[REGEX].*k 10 3 HDD 9999-12-31 23:59:59 None 0B false 0
[REGEX].*k 10 3 HDD 9999-12-31 23:59:59 None.*
-- !result
insert into t values(1),(2),(3);
-- result:
Expand All @@ -30,5 +30,44 @@ select * from t;
-- !result
show partitions from t;
-- result:
[REGEX].*k 1 3 HDD 9999-12-31 23:59:59 None 0B false 0
[REGEX].*k 1 3 HDD 9999-12-31 23:59:59 None.*
-- !result

-- name: test_optimize_table_by_partition_name
CREATE TABLE `duplicate_table_with_null_partition` (
`k1` date,
`k2` datetime,
`k3` char(20),
`k4` varchar(20),
`k5` boolean,
`k6` tinyint,
`k7` smallint,
`k8` int,
`k9` bigint,
`k10` largeint,
`k11` float,
`k12` double,
`k13` decimal(27,9)
)
DUPLICATE KEY(`k1`, `k2`, `k3`, `k4`, `k5`)
COMMENT "OLAP"
PARTITION BY RANGE(`k1`)
(
PARTITION `p202006` VALUES LESS THAN ("2020-07-01"),
PARTITION `p202007` VALUES LESS THAN ("2020-08-01"),
PARTITION `p202008` VALUES LESS THAN ("2020-09-01")
)
DISTRIBUTED BY HASH(`k1`, `k2`, `k3`, `k4`, `k5`) BUCKETS 3
PROPERTIES (
"replication_num" = "3",
"storage_format" = "v2"
);
-- result:
-- !result
alter table duplicate_table_with_null_partition PARTITIONS(p202006,p202007,p202008) DISTRIBUTED BY HASH(`k1`, `k2`, `k3`, `k4`, `k5`) BUCKETS 4;
-- result:
-- !result
function: wait_optimize_table_finish()
-- result:
None
-- !result
32 changes: 32 additions & 0 deletions test/sql/test_optimize_table/T/test_optimize_table
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,35 @@ alter table t distributed by hash(k);
function: wait_optimize_table_finish()
select * from t;
show partitions from t;

-- name: test_optimize_table_by_partition_name
CREATE TABLE `duplicate_table_with_null_partition` (
`k1` date,
`k2` datetime,
`k3` char(20),
`k4` varchar(20),
`k5` boolean,
`k6` tinyint,
`k7` smallint,
`k8` int,
`k9` bigint,
`k10` largeint,
`k11` float,
`k12` double,
`k13` decimal(27,9)
)
DUPLICATE KEY(`k1`, `k2`, `k3`, `k4`, `k5`)
COMMENT "OLAP"
PARTITION BY RANGE(`k1`)
(
PARTITION `p202006` VALUES LESS THAN ("2020-07-01"),
PARTITION `p202007` VALUES LESS THAN ("2020-08-01"),
PARTITION `p202008` VALUES LESS THAN ("2020-09-01")
)
DISTRIBUTED BY HASH(`k1`, `k2`, `k3`, `k4`, `k5`) BUCKETS 3
PROPERTIES (
"replication_num" = "3",
"storage_format" = "v2"
);
alter table duplicate_table_with_null_partition PARTITIONS(p202006,p202007,p202008) DISTRIBUTED BY HASH(`k1`, `k2`, `k3`, `k4`, `k5`) BUCKETS 4;
function: wait_optimize_table_finish()
130 changes: 0 additions & 130 deletions test/sql/tmp/R/test_sync_materialized_view_with_colocate

This file was deleted.

0 comments on commit 04a0be7

Please sign in to comment.