Skip to content

Commit

Permalink
[fix](index)Add duplicated indexes check in add index (#46155)
Browse files Browse the repository at this point in the history
Issue Number: close #46153

Problem Summary:

```
create table t(a int, b int, c tinyint);
alter table t add index idx1(a), add index idx2(a);
alter table t add index idx1(a), add index idx1(a);
```
Both create index sql should fail.
  • Loading branch information
qidaye authored Dec 31, 2024
1 parent 909353f commit 9bcb801
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2704,7 +2704,7 @@ private void cancelIndexJob(CancelAlterTableStmt cancelAlterTableStmt) throws Dd

/**
* Returns true if the index already exists, there is no need to create the job to add the index.
* Otherwise return false, there is need to create a job to add the index.
* Otherwise, return false, there is need to create a job to add the index.
*/
private boolean processAddIndex(CreateIndexClause alterClause, OlapTable olapTable, List<Index> newIndexes)
throws UserException {
Expand All @@ -2718,23 +2718,11 @@ private boolean processAddIndex(CreateIndexClause alterClause, OlapTable olapTab
Set<String> newColset = Sets.newTreeSet(String.CASE_INSENSITIVE_ORDER);
newColset.addAll(indexDef.getColumns());
Set<Long> existedIndexIdSet = Sets.newHashSet();
for (Index existedIdx : existedIndexes) {
if (existedIdx.getIndexName().equalsIgnoreCase(indexDef.getIndexName())) {
if (indexDef.isSetIfNotExists()) {
LOG.info("create index[{}] which already exists on table[{}]", indexDef.getIndexName(),
olapTable.getName());
return true;
}
throw new DdlException("index `" + indexDef.getIndexName() + "` already exist.");
}
Set<String> existedIdxColSet = Sets.newTreeSet(String.CASE_INSENSITIVE_ORDER);
existedIdxColSet.addAll(existedIdx.getColumns());
if (existedIdx.getIndexType() == indexDef.getIndexType() && newColset.equals(existedIdxColSet)) {
throw new DdlException(
indexDef.getIndexType() + " index for columns (" + String.join(",", indexDef.getColumns())
+ " ) already exist.");
}
existedIndexIdSet.add(existedIdx.getIndexId());
if (checkDuplicateIndexes(existedIndexes, indexDef, newColset, existedIndexIdSet, olapTable)) {
return true;
}
if (checkDuplicateIndexes(newIndexes, indexDef, newColset, existedIndexIdSet, olapTable)) {
return true;
}

// The restored olap table may not reset the index id, which comes from the upstream,
Expand Down Expand Up @@ -2770,6 +2758,29 @@ private boolean processAddIndex(CreateIndexClause alterClause, OlapTable olapTab
return false;
}

private boolean checkDuplicateIndexes(List<Index> indexes, IndexDef indexDef, Set<String> newColset,
Set<Long> existedIndexIdSet, OlapTable olapTable) throws DdlException {
for (Index index : indexes) {
if (index.getIndexName().equalsIgnoreCase(indexDef.getIndexName())) {
if (indexDef.isSetIfNotExists()) {
LOG.info("create index[{}] which already exists on table[{}]", indexDef.getIndexName(),
olapTable.getName());
return true;
}
throw new DdlException("index `" + indexDef.getIndexName() + "` already exist.");
}
Set<String> existedIdxColSet = Sets.newTreeSet(String.CASE_INSENSITIVE_ORDER);
existedIdxColSet.addAll(index.getColumns());
if (index.getIndexType() == indexDef.getIndexType() && newColset.equals(existedIdxColSet)) {
throw new DdlException(
indexDef.getIndexType() + " index for columns (" + String.join(",", indexDef.getColumns())
+ ") already exist.");
}
existedIndexIdSet.add(index.getIndexId());
}
return false;
}

/**
* Returns true if the index does not exist, there is no need to create the job to drop the index.
* Otherwise return false, there is need to create a job to drop the index.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,40 @@ public void testDupAddOrDropInvertedIndex() throws Exception {
tbl.readUnlock();
}
}

@Test
public void testAddDuplicateInvertedIndexException() throws Exception {

LOG.info("dbName: {}", Env.getCurrentInternalCatalog().getDbNames());

Database db = Env.getCurrentInternalCatalog().getDbOrMetaException("test");
OlapTable tbl = (OlapTable) db.getTableOrMetaException("sc_dup", Table.TableType.OLAP);
tbl.readLock();
try {
Assertions.assertNotNull(tbl);
Assertions.assertEquals("Doris", tbl.getEngine());
Assertions.assertEquals(0, tbl.getIndexes().size());
} finally {
tbl.readUnlock();
}

String addInvertedIndexStmtStr = "alter table test.sc_dup add index idx_error_msg(error_msg), "
+ "add index idx_error_msg1(error_msg)";
AlterTableStmt addInvertedIndexStmt = (AlterTableStmt) parseAndAnalyzeStmt(addInvertedIndexStmtStr);
try {
Env.getCurrentEnv().getAlterInstance().processAlterTable(addInvertedIndexStmt);
} catch (Exception e) {
// Verify the error message contains relevant info
Assertions.assertTrue(e.getMessage().contains("INVERTED index for columns (error_msg) already exist"));
}
addInvertedIndexStmtStr = "alter table test.sc_dup add index idx_error_msg(error_msg), "
+ "add index idx_error_msg(error_msg)";
addInvertedIndexStmt = (AlterTableStmt) parseAndAnalyzeStmt(addInvertedIndexStmtStr);
try {
Env.getCurrentEnv().getAlterInstance().processAlterTable(addInvertedIndexStmt);
} catch (Exception e) {
// Verify the error message contains relevant info
Assertions.assertTrue(e.getMessage().contains("index `idx_error_msg` already exist."));
}
}
}

0 comments on commit 9bcb801

Please sign in to comment.