Skip to content

Commit

Permalink
[fix](index) Fix create index/index def to sql #44392 (#44581)
Browse files Browse the repository at this point in the history
cherry pick from #44392
  • Loading branch information
w41ter authored Nov 26, 2024
1 parent fa3d6f0 commit 688ef8f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,7 @@ public void process(String rawSql, List<AlterClause> alterClauses, Database db,
// index id -> index schema
Map<Long, LinkedList<Column>> indexSchemaMap = new HashMap<>();

//for multi add colmuns clauses
//for multi add columns clauses
//index id -> index col_unique_id supplier
Map<Long, IntSupplier> colUniqueIdSupplierMap = new HashMap<>();
for (Map.Entry<Long, List<Column>> entry : olapTable.getIndexIdToSchema(true).entrySet()) {
Expand Down Expand Up @@ -2601,7 +2601,7 @@ private boolean processAddIndex(CreateIndexClause alterClause, OlapTable olapTab
// the column name in CreateIndexClause is not check case sensitivity,
// when send index description to BE, there maybe cannot find column by name,
// so here update column name in CreateIndexClause after checkColumn for indexDef,
// there will use the column name in olapTable insead of the column name in CreateIndexClause.
// there will use the column name in olapTable instead of the column name in CreateIndexClause.
alterIndex.setColumns(indexDef.getColumns());
alterIndex.setColumnUniqueIds(indexDef.getColumnUniqueIds());
newIndexes.add(alterIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public boolean needChangeMTMVState() {
@Override
public String toSql() {
if (alter) {
return indexDef.toSql();
return "ADD " + indexDef.toSql();
} else {
return "CREATE " + indexDef.toSql(tableName.toSql());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public boolean needChangeMTMVState() {
@Override
public String toSql() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("DROP INDEX ").append(indexName);
stringBuilder.append("DROP INDEX ").append("`" + indexName + "`");
if (!alter) {
stringBuilder.append(" ON ").append(tableName.toSql());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public String toSql() {

public String toSql(String tableName) {
StringBuilder sb = new StringBuilder("INDEX ");
sb.append(indexName);
sb.append("`" + indexName + "`");
if (tableName != null && !tableName.isEmpty()) {
sb.append(" ON ").append(tableName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,27 @@ public void testEnableFeature() throws UserException {
stmt.toSql());
Assert.assertEquals("testDb", stmt.getTbl().getDb());
}

@Test
public void testCreateIndex() throws UserException {
List<AlterClause> ops = Lists.newArrayList();
ops.add(new CreateIndexClause(
new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"),
new IndexDef("index1", false, Lists.newArrayList("col1"), IndexDef.IndexType.INVERTED, null, "balabala"),
true));
AlterTableStmt stmt = new AlterTableStmt(new TableName(internalCtl, "testDb", "testTbl"), ops);
stmt.analyze(analyzer);
Assert.assertEquals("ALTER TABLE `testDb`.`testTbl` ADD INDEX `index1` (`col1`) USING INVERTED COMMENT 'balabala'",
stmt.toSql());
}

@Test
public void testDropIndex() throws UserException {
List<AlterClause> ops = Lists.newArrayList();
ops.add(new DropIndexClause("index1", false,
new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"), true));
AlterTableStmt stmt = new AlterTableStmt(new TableName(internalCtl, "testDb", "testTbl"), ops);
stmt.analyze(analyzer);
Assert.assertEquals("ALTER TABLE `testDb`.`testTbl` DROP INDEX `index1`", stmt.toSql());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,21 @@ public void testNormal() throws AnalysisException {
new IndexDef("index1", false, Lists.newArrayList("col1"), IndexDef.IndexType.INVERTED, null, "balabala"),
false);
clause.analyze(analyzer);
Assert.assertEquals("CREATE INDEX index1 ON `db`.`table` (`col1`) USING INVERTED COMMENT 'balabala'",
Assert.assertEquals("CREATE INDEX `index1` ON `db`.`table` (`col1`) USING INVERTED COMMENT 'balabala'",
clause.toSql());

}

@Test
public void testAlter() throws AnalysisException {
CreateIndexClause clause = new CreateIndexClause(
new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"),
new IndexDef("index1", false, Lists.newArrayList("col1"), IndexDef.IndexType.INVERTED, null, "balabala"),
true);
clause.analyze(analyzer);
Assert.assertEquals("ADD INDEX `index1` (`col1`) USING INVERTED COMMENT 'balabala'", clause.toSql());
}

@Test(expected = AnalysisException.class)
public void testDuplIndex() throws AnalysisException {
CreateIndexClause clause = new CreateIndexClause(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ public void testNormal() throws UserException {
DropIndexClause clause = new DropIndexClause("index1", false,
new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"), false);
clause.analyze(analyzer);
Assert.assertEquals("DROP INDEX index1 ON `db`.`table`", clause.toSql());
Assert.assertEquals("DROP INDEX `index1` ON `db`.`table`", clause.toSql());
}

@Test
public void testAlter() throws UserException {
DropIndexClause clause = new DropIndexClause("index1", false,
new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"), true);
clause.analyze(analyzer);
Assert.assertEquals("DROP INDEX `index1`", clause.toSql());
}

@Test(expected = AnalysisException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public void testAnalyzeExpection() throws AnalysisException {

@Test
public void toSql() {
Assert.assertEquals("INDEX index1 (`col1`) USING INVERTED COMMENT 'balabala'", def.toSql());
Assert.assertEquals("INDEX index1 ON table1 (`col1`) USING INVERTED COMMENT 'balabala'",
Assert.assertEquals("INDEX `index1` (`col1`) USING INVERTED COMMENT 'balabala'", def.toSql());
Assert.assertEquals("INDEX `index1` ON table1 (`col1`) USING INVERTED COMMENT 'balabala'",
def.toSql("table1"));
}
}

0 comments on commit 688ef8f

Please sign in to comment.