Skip to content

Commit

Permalink
[apache#1707] feat(mysql): Support mysql index.
Browse files Browse the repository at this point in the history
  • Loading branch information
Clearvive authored and Clearvive committed Jan 29, 2024
1 parent b4b04f3 commit 66bf56a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,31 @@ protected String generateCreateTableSql(
}
}

appendIndexesSql(indexes, sqlBuilder);

sqlBuilder.append("\n)");

// Add table comment if specified
if (StringUtils.isNotEmpty(comment)) {
sqlBuilder.append(" COMMENT='").append(comment).append("'");
}

// Add table properties if any
if (MapUtils.isNotEmpty(properties)) {
sqlBuilder.append(
properties.entrySet().stream()
.map(entry -> String.format("%s = %s", entry.getKey(), entry.getValue()))
.collect(Collectors.joining(",\n", "\n", "")));
}

// Return the generated SQL statement
String result = sqlBuilder.append(";").toString();

LOG.info("Generated create table:{} sql: {}", tableName, result);
return result;
}

public static void appendIndexesSql(Index[] indexes, StringBuilder sqlBuilder) {
for (Index index : indexes) {
String fieldStr =
Arrays.stream(index.fieldNames())
Expand Down Expand Up @@ -301,27 +326,6 @@ protected String generateCreateTableSql(
throw new IllegalArgumentException("MySQL doesn't support index : " + index.type());
}
}

sqlBuilder.append("\n)");

// Add table comment if specified
if (StringUtils.isNotEmpty(comment)) {
sqlBuilder.append(" COMMENT='").append(comment).append("'");
}

// Add table properties if any
if (MapUtils.isNotEmpty(properties)) {
sqlBuilder.append(
properties.entrySet().stream()
.map(entry -> String.format("%s = %s", entry.getKey(), entry.getValue()))
.collect(Collectors.joining(",\n", "\n", "")));
}

// Return the generated SQL statement
String result = sqlBuilder.append(";").toString();

LOG.info("Generated create table:{} sql: {}", tableName, result);
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.datastrato.gravitino.catalog.jdbc.JdbcColumn;
import com.datastrato.gravitino.catalog.jdbc.JdbcTable;
import com.datastrato.gravitino.catalog.mysql.operation.MysqlTableOperations;
import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
import com.datastrato.gravitino.exceptions.NoSuchTableException;
import com.datastrato.gravitino.integration.test.util.GravitinoITUtils;
Expand Down Expand Up @@ -630,4 +631,45 @@ public void testLoadTableDefaultProperties() {
JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, test_table_1);
Assertions.assertEquals("InnoDB", load.properties().get(MYSQL_ENGINE_KEY));
}

@Test
public void testAppendIndexesBuilder() {
Index[] indexes =
new Index[] {
Indexes.createMysqlPrimaryKey(new String[][] {{"col_2"}, {"col_1"}}),
Indexes.unique("uk_col_4", new String[][] {{"col_4"}}),
Indexes.unique("uk_col_5", new String[][] {{"col_4"}, {"col_5"}}),
Indexes.unique("uk_col_6", new String[][] {{"col_4"}, {"col_5"}, {"col_6"}})
};
StringBuilder sql = new StringBuilder();
MysqlTableOperations.appendIndexesSql(indexes, sql);
String expectedStr =
",\n"
+ "CONSTRAINT PRIMARY KEY (`col_2`, `col_1`),\n"
+ "CONSTRAINT `uk_col_4` UNIQUE (`col_4`),\n"
+ "CONSTRAINT `uk_col_5` UNIQUE (`col_4`, `col_5`),\n"
+ "CONSTRAINT `uk_col_6` UNIQUE (`col_4`, `col_5`, `col_6`)";
Assertions.assertEquals(expectedStr, sql.toString());

indexes =
new Index[] {
Indexes.unique("uk_1", new String[][] {{"col_4"}}),
Indexes.unique("uk_2", new String[][] {{"col_4"}, {"col_3"}}),
Indexes.createMysqlPrimaryKey(new String[][] {{"col_2"}, {"col_1"}, {"col_3"}}),
Indexes.unique("uk_3", new String[][] {{"col_4"}, {"col_5"}, {"col_6"}, {"col_7"}})
};
sql = new StringBuilder();
MysqlTableOperations.appendIndexesSql(indexes, sql);
expectedStr =
",\n"
+ "CONSTRAINT PRIMARY KEY (`col_2`, `col_1`),\n"
+ "CONSTRAINT `uk_col_4` UNIQUE (`col_4`),\n"
+ "CONSTRAINT `uk_col_5` UNIQUE (`col_4`, `col_5`),\n"
+ "CONSTRAINT `uk_col_6` UNIQUE (`col_4`, `col_5`, `col_6`)> but was: <,\n"
+ "CONSTRAINT `uk_1` UNIQUE (`col_4`),\n"
+ "CONSTRAINT `uk_2` UNIQUE (`col_4`, `col_3`),\n"
+ "CONSTRAINT PRIMARY KEY (`col_2`, `col_1`, `col_3`),\n"
+ "CONSTRAINT `uk_3` UNIQUE (`col_4`, `col_5`, `col_6`, `col_7`)";
Assertions.assertEquals(expectedStr, sql.toString());
}
}

0 comments on commit 66bf56a

Please sign in to comment.