Skip to content

Commit

Permalink
[fix](index) Fix create index/index def to sql (#44392)
Browse files Browse the repository at this point in the history
Fix toSql for CreateIndexClause with alter
  • Loading branch information
w41ter authored and Your Name committed Nov 22, 2024
1 parent 5f1f5ef commit 76025f8
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,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 @@ -2708,7 +2708,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 @@ -118,7 +118,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 @@ -54,7 +54,7 @@ public class Index implements Writable {
public static final int INDEX_ID_INIT_VALUE = -1;

@SerializedName(value = "i", alternate = {"indexId"})
private long indexId = -1; // -1 for compatibale
private long indexId = -1; // -1 for compatiable
@SerializedName(value = "in", alternate = {"indexName"})
private String indexName;
@SerializedName(value = "c", alternate = {"columns"})
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 @@ -77,8 +77,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 76025f8

Please sign in to comment.