Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#1279] feat(jdbc): MySQL supports auto_increment (#1280)

Clearvivee82e2beb3a30
diff --git a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcCatalogOperations.java b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcCatalogOperations.java
index 4fa1425934..05b1226feb 100644
--- a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcCatalogOperations.java
+++ b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcCatalogOperations.java
@@ -385,6 +385,7 @@ public class JdbcCatalogOperations implements CatalogOperations, SupportsSchemas
.withType(column.dataType())
.withComment(column.comment())
.withNullable(column.nullable())
+ .withAutoIncrement(column.autoIncrement())
.build())
.toArray(JdbcColumn[]::new);
String databaseName = NameIdentifier.of(tableIdent.namespace().levels()).name();
diff --git a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcColumn.java b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcColumn.java
index e197deb093..49739b4c62 100644
--- a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcColumn.java
+++ b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcColumn.java
@@ -5,29 +5,15 @@
package com.datastrato.gravitino.catalog.jdbc;
import com.datastrato.gravitino.catalog.rel.BaseColumn;
-import java.util.List;
/** Represents a column in the Jdbc column. */
public class JdbcColumn extends BaseColumn {
- private List<String> properties;
private JdbcColumn() {}
- public List<String> getProperties() {
- return properties;
- }
-
/** A builder class for constructing JdbcColumn instances. */
public static class Builder extends BaseColumnBuilder<Builder, JdbcColumn> {
- /** Attribute value of the field, such as AUTO_INCREMENT, PRIMARY KEY, etc. */
- private List<String> properties;
-
- public Builder withProperties(List<String> properties) {
- this.properties = properties;
- return this;
- }
-
/**
* Internal method to build a JdbcColumn instance using the provided values.
*
@@ -41,7 +27,6 @@ public class JdbcColumn extends BaseColumn {
jdbcColumn.dataType = dataType;
jdbcColumn.nullable = nullable;
jdbcColumn.defaultValue = defaultValue;
- jdbcColumn.properties = properties;
jdbcColumn.autoIncrement = autoIncrement;
return jdbcColumn;
}
diff --git a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcTableOperations.java b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcTableOperations.java
index b346e6c8fe..697c28004f 100644
--- a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcTableOperations.java
+++ b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcTableOperations.java
@@ -282,7 +282,6 @@ public abstract class JdbcTableOperations implements TableOperation {
.withName(column.getString("COLUMN_NAME"))
.withType(typeConverter.toGravitinoType(typeBean))
.withComment(StringUtils.isEmpty(comment) ? null : comment)
- .withNullable(column.getBoolean("NULLABLE"))
- .withProperties(Collections.emptyList());
+ .withNullable(column.getBoolean("NULLABLE"));
}
}
diff --git a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.java b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.java
index ad8082be91..88d77a0d0f 100644
--- a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.java
+++ b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.java
@@ -19,6 +19,7 @@ import com.datastrato.gravitino.rel.TableChange;
import com.datastrato.gravitino.rel.expressions.transforms.Transform;
import com.datastrato.gravitino.rel.indexes.Index;
import com.datastrato.gravitino.rel.indexes.Indexes;
+import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;
import java.sql.Connection;
@@ -33,6 +34,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
@@ -45,6 +47,7 @@ import org.apache.commons.lang3.StringUtils;
public class MysqlTableOperations extends JdbcTableOperations {
public static final String BACK_QUOTE = "`";
+ public static final String MYSQL_AUTO_INCREMENT = "AUTO_INCREMENT";
@Override
public List<String> listTables(String databaseName) throws NoSuchSchemaException {
@@ -88,6 +91,7 @@ public class MysqlTableOperations extends JdbcTableOperations {
if (ArrayUtils.isNotEmpty(partitioning)) {
throw new UnsupportedOperationException("Currently we do not support Partitioning in mysql");
}
+ validateIncrementCol(columns, indexes);
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("CREATE TABLE ").append(tableName).append(" (\n");
@@ -132,6 +136,44 @@ public class MysqlTableOperations extends JdbcTableOperations {
return result;
}
+ /**
+ * The auto-increment column will be verified. There can only be one auto-increment column and it
+ * must be the primary key or unique index.
+ *
+ * @param columns jdbc column
+ * @param indexes table indexes
+ */
+ private static void validateIncrementCol(JdbcColumn[] columns, Index[] indexes) {
+ // Check auto increment column
+ List<JdbcColumn> autoIncrementCols =
+ Arrays.stream(columns).filter(Column::autoIncrement).collect(Collectors.toList());
+ String autoIncrementColsStr =
+ autoIncrementCols.stream().map(JdbcColumn::name).collect(Collectors.joining(",", "[", "]"));
+ Preconditions.checkArgument(
+ autoIncrementCols.size() <= 1,
+ "Only one column can be auto-incremented. There are multiple auto-increment columns in your table: "
+ + autoIncrementColsStr);
+ if (!autoIncrementCols.isEmpty()) {
+ Optional<Index> existAutoIncrementColIndexOptional =
+ Arrays.stream(indexes)
+ .filter(
+ index ->
+ Arrays.stream(index.fieldNames())
+ .flatMap(Arrays::stream)
+ .anyMatch(
+ s ->
+ StringUtils.equalsIgnoreCase(autoIncrementCols.get(0).name(), s)))
+ .filter(
+ index ->
+ index.type() == Index.IndexType.PRIMARY_KEY
+ || index.type() == Index.IndexType.UNIQUE_KEY)
+ .findAny();
+ Preconditions.checkArgument(
+ existAutoIncrementColIndexOptional.isPresent(),
+ "Incorrect table definition; there can be only one auto column and it must be defined as a key");
+ }
+ }
+
public static void appendIndexesSql(Index[] indexes, StringBuilder sqlBuilder) {
for (Index index : indexes) {
String fieldStr =
@@ -366,9 +408,9 @@ public class MysqlTableOperations extends JdbcTableOperations {
// TODO #1531 will add default value.
.withDefaultValue(null)
.withNullable(change.nullable())
- .withProperties(column.getProperties())
.withType(column.dataType())
.withComment(column.comment())
+ .withAutoIncrement(column.autoIncrement())
.build();
return "MODIFY COLUMN " + col + appendColumnDefinition(updateColumn, new StringBuilder());
}
@@ -400,9 +442,9 @@ public class MysqlTableOperations extends JdbcTableOperations {
// TODO #1531 will add default value.
.withDefaultValue(null)
.withNullable(column.nullable())
- .withProperties(column.getProperties())
.withType(column.dataType())
.withComment(newComment)
+ .withAutoIncrement(column.autoIncrement())
.build();
return "MODIFY COLUMN " + col + appendColumnDefinition(updateColumn, new StringBuilder());
}
@@ -455,10 +497,10 @@ public class MysqlTableOperations extends JdbcTableOperations {
.withName(newColumnName)
.withType(column.dataType())
.withComment(column.comment())
- .withProperties(column.getProperties())
// TODO #1531 will add default value.
.withDefaultValue(null)
.withNullable(column.nullable())
+ .withAutoIncrement(column.autoIncrement())
.build();
return appendColumnDefinition(newColumn, sqlBuilder).toString();
}
@@ -522,12 +564,9 @@ public class MysqlTableOperations extends JdbcTableOperations {
.withName(col)
.withType(updateColumnType.getNewDataType())
.withComment(column.comment())
- // Modifying a field type does not require adding its attributes. If
- // additional attributes are required, they must be modified separately.
- // TODO #839
- .withProperties(null)
.withDefaultValue(null)
.withNullable(column.nullable())
+ .withAutoIncrement(column.autoIncrement())
.build();
return appendColumnDefinition(newColumn, sqlBuilder).toString();
}
@@ -547,12 +586,11 @@ public class MysqlTableOperations extends JdbcTableOperations {
}
// TODO #1531 will add default value.
- // Add column properties if specified
- if (CollectionUtils.isNotEmpty(column.getProperties())) {
- for (String property : column.getProperties()) {
- sqlBuilder.append(property).append(SPACE);
- }
+ // Add column auto_increment if specified
+ if (column.autoIncrement()) {
+ sqlBuilder.append(MYSQL_AUTO_INCREMENT).append(" ");
}
+
// Add column comment if specified
if (StringUtils.isNotEmpty(column.comment())) {
sqlBuilder.append("COMMENT '").append(column.comment()).append("' ");
diff --git a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java
index 03a0b7767f..3e9183589b 100644
--- a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java
+++ b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java
@@ -26,7 +26,6 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
-import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.BooleanUtils;
@@ -146,11 +145,6 @@ public class PostgreSqlTableOperations extends JdbcTableOperations {
// sqlBuilder.append("DEFAULT '").append(column.getDefaultValue()).append("'").append(SPACE);
// }
- // Add column properties if specified
- if (CollectionUtils.isNotEmpty(column.getProperties())) {
- // TODO #804 will add properties
- throw new IllegalArgumentException("Properties are not supported yet");
- }
return sqlBuilder;
}
diff --git a/common/src/main/java/com/datastrato/gravitino/dto/rel/ColumnDTO.java b/common/src/main/java/com/datastrato/gravitino/dto/rel/ColumnDTO.java
index ffa0e3534b..848d543009 100644
--- a/common/src/main/java/com/datastrato/gravitino/dto/rel/ColumnDTO.java
+++ b/common/src/main/java/com/datastrato/gravitino/dto/rel/ColumnDTO.java
@@ -204,10 +204,6 @@ public class ColumnDTO implements Column {
}
public void validate() throws IllegalArgumentException {
- if (autoIncrement()) {
- // TODO This part of the code will be deleted after underlying support.
- throw new UnsupportedOperationException("Auto-increment column is not supported yet.");
- }
if (name() == null || name().isEmpty()) {
throw new IllegalArgumentException("Column name cannot be null or empty.");
}
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/TestJdbcAbstractIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/TestJdbcAbstractIT.java
index ff7938bee0..09d44a55f0 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/TestJdbcAbstractIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/TestJdbcAbstractIT.java
@@ -94,10 +94,6 @@ public abstract class TestJdbcAbstractIT {
// TODO: uncomment this after default value is supported.
// Assertions.assertEquals(
// columns.get(i).getDefaultValue(), ((JdbcColumn) table.columns()[i]).getDefaultValue());
- if (null != columns.get(i).getProperties()) {
- Assertions.assertEquals(
- columns.get(i).getProperties(), ((JdbcColumn) table.columns()[i]).getProperties());
- }
}
for (Map.Entry<String, String> entry : properties.entrySet()) {
Assertions.assertEquals(entry.getValue(), table.properties().get(entry.getKey()));
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java
index c1289eecc6..20bf89208c 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java
@@ -671,4 +671,138 @@ public class CatalogMysqlIT extends AbstractIT {
Assertions.assertNotNull(table.index()[0].name());
Assertions.assertNotNull(table.index()[1].name());
}
+
+ @Test
+ public void testAutoIncrement() {
+ Column col1 = Column.of("col_1", Types.LongType.get(), "id", false, true, null);
+ Column col2 = Column.of("col_2", Types.ByteType.get(), "yes", false, false, null);
+ Column col3 = Column.of("col_3", Types.DateType.get(), "comment", false, false, null);
+ Column col4 = Column.of("col_4", Types.VarCharType.of(255), "code", false, false, null);
+ Column col5 = Column.of("col_5", Types.VarCharType.of(255), "config", false, false, null);
+ Column[] newColumns = new Column[] {col1, col2, col3, col4, col5};
+
+ Index[] indexes =
+ new Index[] {
+ Indexes.createMysqlPrimaryKey(new String[][] {{"col_1"}, {"col_2"}}),
+ Indexes.unique("u1_key", new String[][] {{"col_2"}, {"col_3"}})
+ };
+
+ NameIdentifier tableIdentifier =
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);
+
+ Map<String, String> properties = createProperties();
+ TableCatalog tableCatalog = catalog.asTableCatalog();
+ Table createdTable =
+ tableCatalog.createTable(
+ tableIdentifier,
+ newColumns,
+ table_comment,
+ properties,
+ Transforms.EMPTY_TRANSFORM,
+ Distributions.NONE,
+ new SortOrder[0],
+ indexes);
+ // Test create auto increment key success.
+ assertionsTableInfo(
+ tableName, table_comment, Arrays.asList(newColumns), properties, indexes, createdTable);
+ Table table = tableCatalog.loadTable(tableIdentifier);
+ assertionsTableInfo(
+ tableName, table_comment, Arrays.asList(newColumns), properties, indexes, table);
+
+ // Test alter table. auto increment exist.
+ // UpdateColumnType
+ tableCatalog.alterTable(
+ tableIdentifier,
+ TableChange.updateColumnType(new String[] {"col_1"}, Types.IntegerType.get()));
+ table = tableCatalog.loadTable(tableIdentifier);
+ Column[] alterColumns =
+ new Column[] {
+ Column.of("col_1", Types.IntegerType.get(), "id", false, true, null),
+ col2,
+ col3,
+ col4,
+ col5
+ };
+ assertionsTableInfo(
+ tableName, table_comment, Arrays.asList(alterColumns), properties, indexes, table);
+
+ // UpdateColumnComment
+ tableCatalog.alterTable(
+ tableIdentifier, TableChange.updateColumnComment(new String[] {"col_1"}, "new_id_comment"));
+ table = tableCatalog.loadTable(tableIdentifier);
+ alterColumns =
+ new Column[] {
+ Column.of("col_1", Types.IntegerType.get(), "new_id_comment", false, true, null),
+ col2,
+ col3,
+ col4,
+ col5
+ };
+ assertionsTableInfo(
+ tableName, table_comment, Arrays.asList(alterColumns), properties, indexes, table);
+
+ // RenameColumn
+ tableCatalog.alterTable(
+ tableIdentifier, TableChange.renameColumn(new String[] {"col_1"}, "col_1_1"));
+ table = tableCatalog.loadTable(tableIdentifier);
+ alterColumns =
+ new Column[] {
+ Column.of("col_1_1", Types.IntegerType.get(), "new_id_comment", false, true, null),
+ col2,
+ col3,
+ col4,
+ col5
+ };
+ assertionsTableInfo(
+ tableName, table_comment, Arrays.asList(alterColumns), properties, indexes, table);
+
+ tableCatalog.dropTable(tableIdentifier);
+
+ // Test create auto increment fail(No index)
+ RuntimeException runtimeException =
+ assertThrows(
+ RuntimeException.class,
+ () ->
+ tableCatalog.createTable(
+ tableIdentifier,
+ newColumns,
+ table_comment,
+ properties,
+ Transforms.EMPTY_TRANSFORM,
+ Distributions.NONE,
+ new SortOrder[0],
+ Indexes.EMPTY_INDEXES));
+ Assertions.assertTrue(
+ StringUtils.contains(
+ runtimeException.getMessage(),
+ "Incorrect table definition; there can be only one auto column and it must be defined as a key"));
+
+ // Test create auto increment fail(Many index col)
+ runtimeException =
+ assertThrows(
+ RuntimeException.class,
+ () ->
+ tableCatalog.createTable(
+ tableIdentifier,
+ new Column[] {
+ col1,
+ col2,
+ col3,
+ col4,
+ col5,
+ Column.of("col_6", Types.LongType.get(), "id2", false, true, null)
+ },
+ table_comment,
+ properties,
+ Transforms.EMPTY_TRANSFORM,
+ Distributions.NONE,
+ new SortOrder[0],
+ new Index[] {
+ Indexes.createMysqlPrimaryKey(new String[][] {{"col_1"}, {"col_6"}})
+ }));
+ Assertions.assertTrue(
+ StringUtils.contains(
+ runtimeException.getMessage(),
+ "Only one column can be auto-incremented. There are multiple auto-increment columns in your table: [col_1,col_6]"));
+ }
}
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/TestMysqlTableOperations.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/TestMysqlTableOperations.java
index 3a84acbe83..af611001d0 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/TestMysqlTableOperations.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/TestMysqlTableOperations.java
@@ -24,6 +24,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions;
@@ -243,7 +244,7 @@ public class TestMysqlTableOperations extends TestMysqlAbstractIT {
.withName(col_1.name())
.withType(INT)
.withComment(col_1.comment())
- .withProperties(col_1.getProperties())
+ .withAutoIncrement(col_1.autoIncrement())
.withNullable(col_1.nullable())
// TODO: uncomment this after supporting default values
// .withDefaultValue(col_1.getDefaultValue())
@@ -253,7 +254,7 @@ public class TestMysqlTableOperations extends TestMysqlAbstractIT {
.withName(col_2.name())
.withType(col_2.dataType())
.withComment(newComment)
- .withProperties(col_2.getProperties())
+ .withAutoIncrement(col_2.autoIncrement())
.withNullable(col_2.nullable())
// TODO: uncomment this after supporting default values
// .withDefaultValue(col_2.getDefaultValue())
@@ -279,7 +280,7 @@ public class TestMysqlTableOperations extends TestMysqlAbstractIT {
.withName(newColName_1)
.withType(col_1.dataType())
.withComment(col_1.comment())
- .withProperties(col_1.getProperties())
+ .withAutoIncrement(col_1.autoIncrement())
.withNullable(col_1.nullable())
// TODO: uncomment this after supporting default values
// .withDefaultValue(col_1.getDefaultValue())
@@ -289,7 +290,7 @@ public class TestMysqlTableOperations extends TestMysqlAbstractIT {
.withName(newColName_2)
.withType(col_2.dataType())
.withComment(col_2.comment())
- .withProperties(col_2.getProperties())
+ .withAutoIncrement(col_2.autoIncrement())
.withNullable(col_2.nullable())
// TODO: uncomment this after supporting default values
// .withDefaultValue(col_2.getDefaultValue())
@@ -318,7 +319,7 @@ public class TestMysqlTableOperations extends TestMysqlAbstractIT {
.withName(col_2.name())
.withType(col_2.dataType())
.withComment(newCol2Comment)
- .withProperties(col_2.getProperties())
+ .withAutoIncrement(col_2.autoIncrement())
// TODO: uncomment this after supporting default values
// .withDefaultValue(col_2.getDefaultValue())
.withNullable(col_2.nullable())
@@ -668,4 +669,137 @@ public class TestMysqlTableOperations extends TestMysqlAbstractIT {
+ "CONSTRAINT `uk_3` UNIQUE (`col_4`, `col_5`, `col_6`, `col_7`)";
Assertions.assertEquals(expectedStr, sql.toString());
}
+
+ @Test
+ public void testAutoIncrement() {
+ String tableName = "test_increment_table_1";
+ String comment = "test_comment";
+ Map<String, String> properties =
+ new HashMap<String, String>() {
+ {
+ put(MYSQL_AUTO_INCREMENT_OFFSET_KEY, "10");
+ }
+ };
+ JdbcColumn[] columns = {
+ new JdbcColumn.Builder()
+ .withName("col_1")
+ .withType(Types.LongType.get())
+ .withComment("id")
+ .withAutoIncrement(true)
+ .withNullable(false)
+ .build(),
+ new JdbcColumn.Builder()
+ .withName("col_2")
+ .withType(Types.VarCharType.of(255))
+ .withComment("city")
+ .withNullable(false)
+ .build(),
+ new JdbcColumn.Builder()
+ .withName("col_3")
+ .withType(Types.VarCharType.of(255))
+ .withComment("name")
+ .withNullable(false)
+ .build()
+ };
+ // Test create increment key for unique index.
+ Index[] indexes =
+ new Index[] {
+ Indexes.createMysqlPrimaryKey(new String[][] {{"col_2"}}),
+ Indexes.unique("uk_1", new String[][] {{"col_1"}})
+ };
+ TABLE_OPERATIONS.create(TEST_DB_NAME, tableName, columns, comment, properties, null, indexes);
+
+ JdbcTable table = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ assertionsTableInfo(
+ tableName,
+ comment,
+ Arrays.stream(columns).collect(Collectors.toList()),
+ properties,
+ indexes,
+ table);
+ TABLE_OPERATIONS.drop(TEST_DB_NAME, tableName);
+
+ // Test create increment key for primary index.
+ indexes =
+ new Index[] {
+ Indexes.createMysqlPrimaryKey(new String[][] {{"col_1"}}),
+ Indexes.unique("uk_2", new String[][] {{"col_2"}})
+ };
+ TABLE_OPERATIONS.create(TEST_DB_NAME, tableName, columns, comment, properties, null, indexes);
+
+ table = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ assertionsTableInfo(
+ tableName,
+ comment,
+ Arrays.stream(columns).collect(Collectors.toList()),
+ properties,
+ indexes,
+ table);
+ TABLE_OPERATIONS.drop(TEST_DB_NAME, tableName);
+
+ // Test create increment key for col_1 + col_3 uk.
+ indexes = new Index[] {Indexes.unique("uk_2_3", new String[][] {{"col_1"}, {"col_3"}})};
+ TABLE_OPERATIONS.create(TEST_DB_NAME, tableName, columns, comment, properties, null, indexes);
+
+ table = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ assertionsTableInfo(
+ tableName,
+ comment,
+ Arrays.stream(columns).collect(Collectors.toList()),
+ properties,
+ indexes,
+ table);
+ TABLE_OPERATIONS.drop(TEST_DB_NAME, tableName);
+
+ // Test create auto increment fail
+ IllegalArgumentException exception =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME,
+ tableName,
+ columns,
+ comment,
+ properties,
+ null,
+ Indexes.EMPTY_INDEXES));
+ Assertions.assertTrue(
+ StringUtils.contains(
+ exception.getMessage(),
+ "Incorrect table definition; there can be only one auto column and it must be defined as a key"));
+
+ // Test create many auto increment col
+ JdbcColumn[] newColumns = {
+ columns[0],
+ columns[1],
+ columns[2],
+ new JdbcColumn.Builder()
+ .withName("col_4")
+ .withType(Types.IntegerType.get())
+ .withComment("test_id")
+ .withAutoIncrement(true)
+ .withNullable(false)
+ .build()
+ };
+
+ exception =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME,
+ tableName,
+ newColumns,
+ comment,
+ properties,
+ null,
+ new Index[] {
+ Indexes.createMysqlPrimaryKey(new String[][] {{"col_1"}, {"col_4"}})
+ }));
+ Assertions.assertTrue(
+ StringUtils.contains(
+ exception.getMessage(),
+ "Only one column can be auto-incremented. There are multiple auto-increment columns in your table: [col_1,col_4]"));
+ }
}