Skip to content

Commit

Permalink
[fix](index) Fix create index/index def to sql
Browse files Browse the repository at this point in the history
  • Loading branch information
w41ter committed Nov 21, 2024
1 parent babd6ce commit 382c08b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,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 @@ -2749,7 +2749,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 @@ -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 @@ -40,11 +40,22 @@ 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("ALTER TABLE `db`.`table` 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("ALTER TABLE `db`.`table` 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 382c08b

Please sign in to comment.