Skip to content

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

[MINOR] refactor(tests): Remove non-IT tests from IT package (#3633)

mchades38cd984d28b1
diff --git a/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/integration/test/TestJdbcAbstractIT.java b/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/TestJdbc.java
similarity index 95%
rename from catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/integration/test/TestJdbcAbstractIT.java
rename to catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/TestJdbc.java
index 4772dcd7c5..ca44e57e94 100644
--- a/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/integration/test/TestJdbcAbstractIT.java
+++ b/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/TestJdbc.java
@@ -2,13 +2,10 @@
* Copyright 2023 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
-package com.datastrato.gravitino.catalog.jdbc.integration.test;
+package com.datastrato.gravitino.catalog.jdbc;
import static com.datastrato.gravitino.dto.util.DTOConverters.toDTO;
-import com.datastrato.gravitino.catalog.jdbc.JdbcColumn;
-import com.datastrato.gravitino.catalog.jdbc.JdbcSchema;
-import com.datastrato.gravitino.catalog.jdbc.JdbcTable;
import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
import com.datastrato.gravitino.catalog.jdbc.converter.JdbcExceptionConverter;
import com.datastrato.gravitino.catalog.jdbc.operation.JdbcDatabaseOperations;
@@ -34,7 +31,7 @@ import org.junit.jupiter.api.Assertions;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.shaded.com.google.common.collect.Maps;
-public abstract class TestJdbcAbstractIT {
+public abstract class TestJdbc {
protected static JdbcDatabaseContainer<?> CONTAINER;
protected static JdbcDatabaseOperations DATABASE_OPERATIONS;
diff --git a/catalogs/catalog-jdbc-doris/build.gradle.kts b/catalogs/catalog-jdbc-doris/build.gradle.kts
index 204b5d5532..f884d512d4 100644
--- a/catalogs/catalog-jdbc-doris/build.gradle.kts
+++ b/catalogs/catalog-jdbc-doris/build.gradle.kts
@@ -69,6 +69,10 @@ tasks {
tasks.test {
val skipUTs = project.hasProperty("skipTests")
+ doFirst {
+ environment("GRAVITINO_CI_DORIS_DOCKER_IMAGE", "datastrato/gravitino-ci-doris:0.1.3")
+ }
+
if (skipUTs) {
// Only run integration tests
include("**/integration/**")
@@ -81,10 +85,6 @@ tasks.test {
} else {
dependsOn(tasks.jar)
- doFirst {
- environment("GRAVITINO_CI_DORIS_DOCKER_IMAGE", "datastrato/gravitino-ci-doris:0.1.3")
- }
-
val init = project.extra.get("initIntegrationTest") as (Test) -> Unit
init(this)
}
diff --git a/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/integration/test/TestDorisAbstractIT.java b/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/operation/TestDoris.java
similarity index 87%
rename from catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/integration/test/TestDorisAbstractIT.java
rename to catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/operation/TestDoris.java
index 677dae7294..96ba1bb9bf 100644
--- a/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/integration/test/TestDorisAbstractIT.java
+++ b/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/operation/TestDoris.java
@@ -2,15 +2,13 @@
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
-package com.datastrato.gravitino.catalog.doris.integration.test;
+package com.datastrato.gravitino.catalog.doris.operation;
import com.datastrato.gravitino.catalog.doris.converter.DorisColumnDefaultValueConverter;
import com.datastrato.gravitino.catalog.doris.converter.DorisExceptionConverter;
import com.datastrato.gravitino.catalog.doris.converter.DorisTypeConverter;
-import com.datastrato.gravitino.catalog.doris.operation.DorisDatabaseOperations;
-import com.datastrato.gravitino.catalog.doris.operation.DorisTableOperations;
+import com.datastrato.gravitino.catalog.jdbc.TestJdbc;
import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
-import com.datastrato.gravitino.catalog.jdbc.integration.test.TestJdbcAbstractIT;
import com.datastrato.gravitino.catalog.jdbc.utils.DataSourceUtils;
import com.datastrato.gravitino.integration.test.container.ContainerSuite;
import com.datastrato.gravitino.integration.test.container.DorisContainer;
@@ -21,7 +19,7 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
-public class TestDorisAbstractIT extends TestJdbcAbstractIT {
+public class TestDoris extends TestJdbc {
private static final ContainerSuite containerSuite = ContainerSuite.getInstance();
protected static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
diff --git a/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/integration/test/TestDorisDatabaseOperations.java b/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/operation/TestDorisDatabaseOperations.java
similarity index 89%
rename from catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/integration/test/TestDorisDatabaseOperations.java
rename to catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/operation/TestDorisDatabaseOperations.java
index f20bf5f269..58a8a396f2 100644
--- a/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/integration/test/TestDorisDatabaseOperations.java
+++ b/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/operation/TestDorisDatabaseOperations.java
@@ -2,7 +2,7 @@
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
-package com.datastrato.gravitino.catalog.doris.integration.test;
+package com.datastrato.gravitino.catalog.doris.operation;
import com.datastrato.gravitino.exceptions.SchemaAlreadyExistsException;
import com.datastrato.gravitino.utils.RandomNameUtils;
@@ -14,7 +14,7 @@ import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@Tag("gravitino-docker-it")
-public class TestDorisDatabaseOperations extends TestDorisAbstractIT {
+public class TestDorisDatabaseOperations extends TestDoris {
@Test
public void testBaseOperationDatabase() {
diff --git a/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/integration/test/DorisTableOperationsIT.java b/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/operation/TestDorisTableOperations.java
similarity index 99%
rename from catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/integration/test/DorisTableOperationsIT.java
rename to catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/operation/TestDorisTableOperations.java
index 89c04124f9..5c1c94c137 100644
--- a/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/integration/test/DorisTableOperationsIT.java
+++ b/catalogs/catalog-jdbc-doris/src/test/java/com/datastrato/gravitino/catalog/doris/operation/TestDorisTableOperations.java
@@ -2,7 +2,7 @@
* Copyright 2023 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
-package com.datastrato.gravitino.catalog.doris.integration.test;
+package com.datastrato.gravitino.catalog.doris.operation;
import com.datastrato.gravitino.catalog.jdbc.JdbcColumn;
import com.datastrato.gravitino.catalog.jdbc.JdbcTable;
@@ -33,7 +33,7 @@ import org.junit.jupiter.api.Test;
import org.testcontainers.shaded.org.awaitility.Awaitility;
@Tag("gravitino-docker-it")
-public class DorisTableOperationsIT extends TestDorisAbstractIT {
+public class TestDorisTableOperations extends TestDoris {
private static final Type VARCHAR_255 = Types.VarCharType.of(255);
private static final Type VARCHAR_1024 = Types.VarCharType.of(1024);
@@ -52,7 +52,7 @@ public class DorisTableOperationsIT extends TestDorisAbstractIT {
@BeforeAll
public static void startup() {
- TestDorisAbstractIT.startup();
+ TestDoris.startup();
createDatabase();
}
diff --git a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/MysqlTableOperationsIT.java b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/MysqlTableOperationsIT.java
deleted file mode 100644
index dd780a4c9a..0000000000
--- a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/MysqlTableOperationsIT.java
+++ /dev/null
@@ -1,954 +0,0 @@
-/*
- * Copyright 2023 Datastrato Pvt Ltd.
- * This software is licensed under the Apache License version 2.
- */
-package com.datastrato.gravitino.catalog.mysql.integration.test;
-
-import static com.datastrato.gravitino.catalog.mysql.MysqlTablePropertiesMetadata.MYSQL_AUTO_INCREMENT_OFFSET_KEY;
-import static com.datastrato.gravitino.catalog.mysql.MysqlTablePropertiesMetadata.MYSQL_ENGINE_KEY;
-
-import com.datastrato.gravitino.catalog.jdbc.JdbcColumn;
-import com.datastrato.gravitino.catalog.jdbc.JdbcTable;
-import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
-import com.datastrato.gravitino.rel.Column;
-import com.datastrato.gravitino.rel.TableChange;
-import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
-import com.datastrato.gravitino.rel.expressions.literals.Literals;
-import com.datastrato.gravitino.rel.indexes.Index;
-import com.datastrato.gravitino.rel.indexes.Indexes;
-import com.datastrato.gravitino.rel.types.Decimal;
-import com.datastrato.gravitino.rel.types.Type;
-import com.datastrato.gravitino.rel.types.Types;
-import com.datastrato.gravitino.utils.RandomNameUtils;
-import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.Arrays;
-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.lang3.RandomStringUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Tag;
-import org.junit.jupiter.api.Test;
-
-@Tag("gravitino-docker-it")
-public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
- private static Type VARCHAR = Types.VarCharType.of(255);
- private static Type INT = Types.IntegerType.get();
-
- @Test
- public void testOperationTable() {
- String tableName = RandomStringUtils.randomAlphabetic(16) + "_op_table";
- String tableComment = "test_comment";
- List<JdbcColumn> columns = new ArrayList<>();
- columns.add(
- JdbcColumn.builder()
- .withName("col_1")
- .withType(VARCHAR)
- .withComment("test_comment")
- .withNullable(true)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(INT)
- .withNullable(false)
- .withComment("set primary key")
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_3")
- .withType(INT)
- .withNullable(true)
- .withDefaultValue(Literals.NULL)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_4")
- .withType(VARCHAR)
- .withDefaultValue(Literals.of("hello world", VARCHAR))
- .withNullable(false)
- .build());
- Map<String, String> properties = new HashMap<>();
- properties.put(MYSQL_AUTO_INCREMENT_OFFSET_KEY, "10");
-
- Index[] indexes = new Index[] {Indexes.unique("test", new String[][] {{"col_1"}, {"col_2"}})};
- // create table
- TABLE_OPERATIONS.create(
- TEST_DB_NAME.toString(),
- tableName,
- columns.toArray(new JdbcColumn[0]),
- tableComment,
- properties,
- null,
- Distributions.NONE,
- indexes);
-
- // list table
- List<String> tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME.toString());
- Assertions.assertTrue(tables.contains(tableName));
-
- // load table
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
- assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
-
- // rename table
- String newName = "new_table";
- Assertions.assertDoesNotThrow(
- () -> TABLE_OPERATIONS.rename(TEST_DB_NAME.toString(), tableName, newName));
- Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), newName));
-
- // alter table
- JdbcColumn newColumn =
- JdbcColumn.builder()
- .withName("col_5")
- .withType(VARCHAR)
- .withComment("new_add")
- .withNullable(true)
- .withDefaultValue(Literals.of("hello test", VARCHAR))
- .build();
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME.toString(),
- newName,
- TableChange.addColumn(
- new String[] {newColumn.name()},
- newColumn.dataType(),
- newColumn.comment(),
- TableChange.ColumnPosition.after("col_1"),
- newColumn.defaultValue()),
- TableChange.setProperty(MYSQL_ENGINE_KEY, "InnoDB"));
- properties.put(MYSQL_ENGINE_KEY, "InnoDB");
- load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), newName);
- List<JdbcColumn> alterColumns =
- new ArrayList<JdbcColumn>() {
- {
- add(columns.get(0));
- add(newColumn);
- add(columns.get(1));
- add(columns.get(2));
- add(columns.get(3));
- }
- };
- assertionsTableInfo(newName, tableComment, alterColumns, properties, indexes, load);
-
- // Detect unsupported properties
- TableChange setProperty = TableChange.setProperty(MYSQL_ENGINE_KEY, "ABC");
- GravitinoRuntimeException gravitinoRuntimeException =
- Assertions.assertThrows(
- GravitinoRuntimeException.class,
- () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME.toString(), newName, setProperty));
- Assertions.assertTrue(
- StringUtils.contains(
- gravitinoRuntimeException.getMessage(), "Unknown storage engine 'ABC'"));
-
- // delete column
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME.toString(),
- newName,
- TableChange.deleteColumn(new String[] {newColumn.name()}, true));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), newName);
- assertionsTableInfo(newName, tableComment, columns, properties, indexes, load);
-
- TableChange deleteColumn = TableChange.deleteColumn(new String[] {newColumn.name()}, false);
- IllegalArgumentException illegalArgumentException =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME.toString(), newName, deleteColumn));
- Assertions.assertEquals(
- "Delete column does not exist: " + newColumn.name(), illegalArgumentException.getMessage());
- Assertions.assertDoesNotThrow(
- () ->
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME.toString(),
- newName,
- TableChange.deleteColumn(new String[] {newColumn.name()}, true)));
-
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME.toString(),
- newName,
- TableChange.deleteColumn(new String[] {newColumn.name()}, true));
- Assertions.assertTrue(
- TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), newName), "table should be dropped");
- Assertions.assertFalse(
- TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), newName), "table should be non-existent");
- }
-
- @Test
- public void testAlterTable() {
- String tableName = RandomStringUtils.randomAlphabetic(16) + "_al_table";
- String tableComment = "test_comment";
- List<JdbcColumn> columns = new ArrayList<>();
- JdbcColumn col_1 =
- JdbcColumn.builder()
- .withName("col_1")
- .withType(INT)
- .withComment("id")
- .withNullable(false)
- .build();
- columns.add(col_1);
- JdbcColumn col_2 =
- JdbcColumn.builder()
- .withName("col_2")
- .withType(VARCHAR)
- .withComment("name")
- .withDefaultValue(Literals.of("hello world", VARCHAR))
- .withNullable(false)
- .build();
- columns.add(col_2);
- JdbcColumn col_3 =
- JdbcColumn.builder()
- .withName("col_3")
- .withType(VARCHAR)
- .withComment("name")
- .withDefaultValue(Literals.NULL)
- .build();
- // `col_1` int NOT NULL COMMENT 'id' ,
- // `col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'name' ,
- // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
- columns.add(col_3);
- Map<String, String> properties = new HashMap<>();
-
- Index[] indexes =
- new Index[] {
- Indexes.createMysqlPrimaryKey(new String[][] {{"col_1"}, {"col_2"}}),
- Indexes.unique("uk_2", new String[][] {{"col_1"}, {"col_2"}})
- };
- // create table
- TABLE_OPERATIONS.create(
- TEST_DB_NAME.toString(),
- tableName,
- columns.toArray(new JdbcColumn[0]),
- tableComment,
- properties,
- null,
- Distributions.NONE,
- indexes);
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
- assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
-
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME.toString(),
- tableName,
- TableChange.updateColumnType(new String[] {col_1.name()}, VARCHAR));
-
- load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
-
- // After modifying the type, some attributes of the corresponding column are not
- // supported.
- columns.clear();
- col_1 =
- JdbcColumn.builder()
- .withName(col_1.name())
- .withType(VARCHAR)
- .withComment(col_1.comment())
- .withNullable(col_1.nullable())
- .withDefaultValue(col_1.defaultValue())
- .build();
- columns.add(col_1);
- columns.add(col_2);
- columns.add(col_3);
- assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
-
- String newComment = "new_comment";
- // update table comment and column comment
- // `col_1` int NOT NULL COMMENT 'id' ,
- // `col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'new_comment' ,
- // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME.toString(),
- tableName,
- TableChange.updateColumnType(new String[] {col_1.name()}, INT),
- TableChange.updateColumnComment(new String[] {col_2.name()}, newComment));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
-
- columns.clear();
- col_1 =
- JdbcColumn.builder()
- .withName(col_1.name())
- .withType(INT)
- .withComment(col_1.comment())
- .withAutoIncrement(col_1.autoIncrement())
- .withNullable(col_1.nullable())
- .withDefaultValue(col_1.defaultValue())
- .build();
- col_2 =
- JdbcColumn.builder()
- .withName(col_2.name())
- .withType(col_2.dataType())
- .withComment(newComment)
- .withAutoIncrement(col_2.autoIncrement())
- .withNullable(col_2.nullable())
- .withDefaultValue(col_2.defaultValue())
- .build();
- columns.add(col_1);
- columns.add(col_2);
- columns.add(col_3);
- assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
-
- String newColName_1 = "new_col_1";
- String newColName_2 = "new_col_2";
- // rename column
- // update table comment and column comment
- // `new_col_1` int NOT NULL COMMENT 'id' ,
- // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'new_comment'
- // ,
- // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME.toString(),
- tableName,
- TableChange.renameColumn(new String[] {col_1.name()}, newColName_1),
- TableChange.renameColumn(new String[] {col_2.name()}, newColName_2));
-
- load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
-
- columns.clear();
- col_1 =
- JdbcColumn.builder()
- .withName(newColName_1)
- .withType(col_1.dataType())
- .withComment(col_1.comment())
- .withAutoIncrement(col_1.autoIncrement())
- .withNullable(col_1.nullable())
- .withDefaultValue(col_1.defaultValue())
- .build();
- col_2 =
- JdbcColumn.builder()
- .withName(newColName_2)
- .withType(col_2.dataType())
- .withComment(col_2.comment())
- .withAutoIncrement(col_2.autoIncrement())
- .withNullable(col_2.nullable())
- .withDefaultValue(col_2.defaultValue())
- .build();
- columns.add(col_1);
- columns.add(col_2);
- columns.add(col_3);
- assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
-
- newComment = "txt3";
- String newCol2Comment = "xxx";
- // update column position 、comment and add column、set table properties
- // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'xxx' ,
- // `new_col_1` int NOT NULL COMMENT 'id' ,
- // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
- // `col_4` varchar(255) NOT NULL COMMENT 'txt4' ,
- // `col_5` varchar(255) COMMENT 'hello world' DEFAULT 'hello world' ,
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME.toString(),
- tableName,
- TableChange.updateColumnPosition(
- new String[] {newColName_1}, TableChange.ColumnPosition.after(newColName_2)),
- TableChange.updateComment(newComment),
- TableChange.addColumn(new String[] {"col_4"}, VARCHAR, "txt4", false),
- TableChange.updateColumnComment(new String[] {newColName_2}, newCol2Comment),
- TableChange.addColumn(
- new String[] {"col_5"}, VARCHAR, "txt5", Literals.of("hello world", VARCHAR)));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
-
- columns.clear();
-
- columns.add(
- JdbcColumn.builder()
- .withName(col_2.name())
- .withType(col_2.dataType())
- .withComment(newCol2Comment)
- .withAutoIncrement(col_2.autoIncrement())
- .withDefaultValue(col_2.defaultValue())
- .withNullable(col_2.nullable())
- .build());
- columns.add(col_1);
- columns.add(col_3);
- columns.add(
- JdbcColumn.builder()
- .withName("col_4")
- .withType(VARCHAR)
- .withComment("txt4")
- .withDefaultValue(Column.DEFAULT_VALUE_NOT_SET)
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_5")
- .withType(VARCHAR)
- .withComment("txt5")
- .withDefaultValue(Literals.of("hello world", VARCHAR))
- .withNullable(true)
- .build());
- assertionsTableInfo(tableName, newComment, columns, properties, indexes, load);
-
- // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'xxx' ,
- // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
- // `col_4` varchar(255) NULL COMMENT 'txt4' ,
- // `col_5` varchar(255) COMMENT 'hello world' DEFAULT 'hello world' ,
- // `new_col_1` int NOT NULL COMMENT 'id' ,
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME.toString(),
- tableName,
- TableChange.updateColumnPosition(new String[] {columns.get(1).name()}, null),
- TableChange.updateColumnNullability(
- new String[] {columns.get(3).name()}, !columns.get(3).nullable()));
-
- load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
- col_1 = columns.remove(1);
- JdbcColumn col3 = columns.remove(1);
- JdbcColumn col_4 = columns.remove(1);
- JdbcColumn col_5 = columns.remove(1);
- columns.clear();
-
- columns.add(
- JdbcColumn.builder()
- .withName("new_col_2")
- .withType(VARCHAR)
- .withNullable(false)
- .withComment("xxx")
- .withDefaultValue(Literals.of("hello world", VARCHAR))
- .build());
- columns.add(col3);
- columns.add(
- JdbcColumn.builder()
- .withName(col_4.name())
- .withType(col_4.dataType())
- .withNullable(!col_4.nullable())
- .withComment(col_4.comment())
- .withDefaultValue(col_4.defaultValue())
- .build());
- columns.add(col_5);
- columns.add(col_1);
-
- assertionsTableInfo(tableName, newComment, columns, properties, indexes, load);
-
- TableChange updateColumn =
- TableChange.updateColumnNullability(new String[] {col3.name()}, !col3.nullable());
- IllegalArgumentException exception =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME.toString(), tableName, updateColumn));
- Assertions.assertTrue(
- exception.getMessage().contains("with null default value cannot be changed to not null"));
- }
-
- @Test
- public void testAlterTableUpdateColumnDefaultValue() {
- String tableName = RandomNameUtils.genRandomName("properties_table_");
- String tableComment = "test_comment";
- List<JdbcColumn> columns = new ArrayList<>();
- columns.add(
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.DecimalType.of(10, 2))
- .withComment("test_decimal")
- .withNullable(false)
- .withDefaultValue(Literals.decimalLiteral(Decimal.of("0.00", 10, 2)))
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(Types.LongType.get())
- .withNullable(false)
- .withDefaultValue(Literals.longLiteral(0L))
- .withComment("long type")
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_3")
- .withType(Types.TimestampType.withoutTimeZone())
- // MySQL 5.7 doesn't support nullable timestamp
- .withNullable(false)
- .withComment("timestamp")
- .withDefaultValue(Literals.timestampLiteral(LocalDateTime.parse("2013-01-01T00:00:00")))
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_4")
- .withType(Types.VarCharType.of(255))
- .withNullable(false)
- .withComment("varchar")
- .withDefaultValue(Literals.of("hello", Types.VarCharType.of(255)))
- .build());
- Map<String, String> properties = new HashMap<>();
-
- Index[] indexes =
- new Index[] {
- Indexes.createMysqlPrimaryKey(new String[][] {{"col_2"}}),
- Indexes.unique("uk_col_4", new String[][] {{"col_4"}})
- };
- // create table
- TABLE_OPERATIONS.create(
- TEST_DB_NAME.toString(),
- tableName,
- columns.toArray(new JdbcColumn[0]),
- tableComment,
- properties,
- null,
- Distributions.NONE,
- indexes);
-
- JdbcTable loaded = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
- assertionsTableInfo(tableName, tableComment, columns, properties, indexes, loaded);
-
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME.toString(),
- tableName,
- TableChange.updateColumnDefaultValue(
- new String[] {columns.get(0).name()},
- Literals.decimalLiteral(Decimal.of("1.23", 10, 2))),
- TableChange.updateColumnDefaultValue(
- new String[] {columns.get(1).name()}, Literals.longLiteral(1L)),
- TableChange.updateColumnDefaultValue(
- new String[] {columns.get(2).name()},
- Literals.timestampLiteral(LocalDateTime.parse("2024-04-01T00:00:00"))),
- TableChange.updateColumnDefaultValue(
- new String[] {columns.get(3).name()}, Literals.of("world", Types.VarCharType.of(255))));
-
- loaded = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
- Assertions.assertEquals(
- Literals.decimalLiteral(Decimal.of("1.234", 10, 2)), loaded.columns()[0].defaultValue());
- Assertions.assertEquals(Literals.longLiteral(1L), loaded.columns()[1].defaultValue());
- Assertions.assertEquals(
- Literals.timestampLiteral(LocalDateTime.parse("2024-04-01T00:00:00")),
- loaded.columns()[2].defaultValue());
- Assertions.assertEquals(
- Literals.of("world", Types.VarCharType.of(255)), loaded.columns()[3].defaultValue());
- }
-
- @Test
- public void testCreateAndLoadTable() {
- String tableName = RandomStringUtils.randomAlphabetic(16) + "_cl_table";
- String tableComment = "test_comment";
- List<JdbcColumn> columns = new ArrayList<>();
- columns.add(
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.DecimalType.of(10, 2))
- .withComment("test_decimal")
- .withNullable(false)
- .withDefaultValue(Literals.decimalLiteral(Decimal.of("0.00", 10, 2)))
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(Types.LongType.get())
- .withNullable(false)
- .withDefaultValue(Literals.longLiteral(0L))
- .withComment("long type")
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_3")
- .withType(Types.TimestampType.withoutTimeZone())
- // MySQL 5.7 doesn't support nullable timestamp
- .withNullable(false)
- .withComment("timestamp")
- .withDefaultValue(Literals.timestampLiteral(LocalDateTime.parse("2013-01-01T00:00:00")))
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_4")
- .withType(Types.DateType.get())
- .withNullable(true)
- .withComment("date")
- .withDefaultValue(Column.DEFAULT_VALUE_NOT_SET)
- .build());
- Map<String, String> properties = new HashMap<>();
-
- Index[] indexes =
- new Index[] {
- Indexes.createMysqlPrimaryKey(new String[][] {{"col_2"}}),
- Indexes.unique("uk_col_4", new String[][] {{"col_4"}})
- };
- // create table
- TABLE_OPERATIONS.create(
- TEST_DB_NAME.toString(),
- tableName,
- columns.toArray(new JdbcColumn[0]),
- tableComment,
- properties,
- null,
- Distributions.NONE,
- indexes);
-
- JdbcTable loaded = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
- assertionsTableInfo(tableName, tableComment, columns, properties, indexes, loaded);
- }
-
- @Test
- public void testCreateAllTypeTable() {
- String tableName = RandomNameUtils.genRandomName("type_table_");
- String tableComment = "test_comment";
- List<JdbcColumn> columns = new ArrayList<>();
- columns.add(
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.ByteType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(Types.ShortType.get())
- .withNullable(true)
- .build());
- columns.add(JdbcColumn.builder().withName("col_3").withType(INT).withNullable(false).build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_4")
- .withType(Types.LongType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_5")
- .withType(Types.FloatType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_6")
- .withType(Types.DoubleType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_7")
- .withType(Types.DateType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_8")
- .withType(Types.TimeType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_9")
- .withType(Types.TimestampType.withoutTimeZone())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder().withName("col_10").withType(Types.DecimalType.of(10, 2)).build());
- columns.add(
- JdbcColumn.builder().withName("col_11").withType(VARCHAR).withNullable(false).build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_12")
- .withType(Types.FixedCharType.of(10))
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_13")
- .withType(Types.StringType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_14")
- .withType(Types.BinaryType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_15")
- .withType(Types.FixedCharType.of(10))
- .withNullable(false)
- .build());
-
- // create table
- TABLE_OPERATIONS.create(
- TEST_DB_NAME.toString(),
- tableName,
- columns.toArray(new JdbcColumn[0]),
- tableComment,
- Collections.emptyMap(),
- null,
- Distributions.NONE,
- Indexes.EMPTY_INDEXES);
-
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
- assertionsTableInfo(tableName, tableComment, columns, Collections.emptyMap(), null, load);
- }
-
- @Test
- public void testCreateNotSupportTypeTable() {
- String tableName = RandomNameUtils.genRandomName("type_table_");
- String tableComment = "test_comment";
- List<JdbcColumn> columns = new ArrayList<>();
- List<Type> notSupportType =
- Arrays.asList(
- Types.BooleanType.get(),
- Types.FixedType.of(10),
- Types.IntervalDayType.get(),
- Types.IntervalYearType.get(),
- Types.UUIDType.get(),
- Types.ListType.of(Types.DateType.get(), true),
- Types.MapType.of(Types.StringType.get(), Types.IntegerType.get(), true),
- Types.UnionType.of(Types.IntegerType.get()),
- Types.StructType.of(
- Types.StructType.Field.notNullField("col_1", Types.IntegerType.get())));
-
- for (Type type : notSupportType) {
- columns.clear();
- columns.add(
- JdbcColumn.builder().withName("col_1").withType(type).withNullable(false).build());
-
- JdbcColumn[] jdbcCols = columns.toArray(new JdbcColumn[0]);
- Map<String, String> emptyMap = Collections.emptyMap();
- IllegalArgumentException illegalArgumentException =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () -> {
- TABLE_OPERATIONS.create(
- TEST_DB_NAME.toString(),
- tableName,
- jdbcCols,
- tableComment,
- emptyMap,
- null,
- Distributions.NONE,
- Indexes.EMPTY_INDEXES);
- });
- Assertions.assertTrue(
- illegalArgumentException
- .getMessage()
- .contains(
- String.format(
- "Couldn't convert Gravitino type %s to MySQL type", type.simpleString())));
- }
- }
-
- @Test
- public void testCreateMultipleTables() {
- String test_table_1 = "test_table_1";
- TABLE_OPERATIONS.create(
- TEST_DB_NAME.toString(),
- test_table_1,
- new JdbcColumn[] {
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.DecimalType.of(10, 2))
- .withComment("test_decimal")
- .withNullable(false)
- .withDefaultValue(Literals.decimalLiteral(Decimal.of("0.00")))
- .build()
- },
- "test_comment",
- null,
- null,
- Distributions.NONE,
- Indexes.EMPTY_INDEXES);
-
- String testDb = "test_db_2";
-
- DATABASE_OPERATIONS.create(testDb, null, null);
- List<String> tables = TABLE_OPERATIONS.listTables(testDb);
- Assertions.assertFalse(tables.contains(test_table_1));
-
- String test_table_2 = "test_table_2";
- TABLE_OPERATIONS.create(
- testDb,
- test_table_2,
- new JdbcColumn[] {
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.DecimalType.of(10, 2))
- .withComment("test_decimal")
- .withNullable(false)
- .withDefaultValue(Literals.decimalLiteral(Decimal.of("0.00")))
- .build()
- },
- "test_comment",
- null,
- null,
- Distributions.NONE,
- Indexes.EMPTY_INDEXES);
-
- tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME.toString());
- Assertions.assertFalse(tables.contains(test_table_2));
- }
-
- @Test
- public void testLoadTableDefaultProperties() {
- String test_table_1 = RandomNameUtils.genRandomName("properties_table_");
- TABLE_OPERATIONS.create(
- TEST_DB_NAME.toString(),
- test_table_1,
- new JdbcColumn[] {
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.DecimalType.of(10, 2))
- .withComment("test_decimal")
- .withNullable(false)
- .build()
- },
- "test_comment",
- null,
- null,
- Distributions.NONE,
- Indexes.EMPTY_INDEXES);
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), test_table_1);
- Assertions.assertEquals("InnoDB", load.properties().get(MYSQL_ENGINE_KEY));
- }
-
- @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 = {
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.LongType.get())
- .withComment("id")
- .withAutoIncrement(true)
- .withNullable(false)
- .build(),
- JdbcColumn.builder()
- .withName("col_2")
- .withType(Types.VarCharType.of(255))
- .withComment("city")
- .withNullable(false)
- .build(),
- 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.toString(),
- tableName,
- columns,
- comment,
- properties,
- null,
- Distributions.NONE,
- indexes);
-
- JdbcTable table = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
- assertionsTableInfo(
- tableName,
- comment,
- Arrays.stream(columns).collect(Collectors.toList()),
- properties,
- indexes,
- table);
- TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), 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.toString(),
- tableName,
- columns,
- comment,
- properties,
- null,
- Distributions.NONE,
- indexes);
-
- table = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
- assertionsTableInfo(
- tableName,
- comment,
- Arrays.stream(columns).collect(Collectors.toList()),
- properties,
- indexes,
- table);
- TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), 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.toString(),
- tableName,
- columns,
- comment,
- properties,
- null,
- Distributions.NONE,
- indexes);
-
- table = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
- assertionsTableInfo(
- tableName,
- comment,
- Arrays.stream(columns).collect(Collectors.toList()),
- properties,
- indexes,
- table);
- TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), tableName);
-
- // Test create auto increment fail
- IllegalArgumentException exception =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () ->
- TABLE_OPERATIONS.create(
- TEST_DB_NAME.toString(),
- tableName,
- columns,
- comment,
- properties,
- null,
- Distributions.NONE,
- 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],
- JdbcColumn.builder()
- .withName("col_4")
- .withType(Types.IntegerType.get())
- .withComment("test_id")
- .withAutoIncrement(true)
- .withNullable(false)
- .build()
- };
-
- final Index[] primaryIndex =
- new Index[] {Indexes.createMysqlPrimaryKey(new String[][] {{"col_1"}, {"col_4"}})};
- exception =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () ->
- TABLE_OPERATIONS.create(
- TEST_DB_NAME.toString(),
- tableName,
- newColumns,
- comment,
- properties,
- null,
- Distributions.NONE,
- primaryIndex));
- 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]"));
- }
-}
diff --git a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/TestMysqlAbstractIT.java b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/operation/TestMysql.java
similarity index 87%
rename from catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/TestMysqlAbstractIT.java
rename to catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/operation/TestMysql.java
index 456f43054c..7db171d5f9 100644
--- a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/TestMysqlAbstractIT.java
+++ b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/operation/TestMysql.java
@@ -2,16 +2,14 @@
* Copyright 2023 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
-package com.datastrato.gravitino.catalog.mysql.integration.test;
+package com.datastrato.gravitino.catalog.mysql.operation;
+import com.datastrato.gravitino.catalog.jdbc.TestJdbc;
import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
-import com.datastrato.gravitino.catalog.jdbc.integration.test.TestJdbcAbstractIT;
import com.datastrato.gravitino.catalog.jdbc.utils.DataSourceUtils;
import com.datastrato.gravitino.catalog.mysql.converter.MysqlColumnDefaultValueConverter;
import com.datastrato.gravitino.catalog.mysql.converter.MysqlExceptionConverter;
import com.datastrato.gravitino.catalog.mysql.converter.MysqlTypeConverter;
-import com.datastrato.gravitino.catalog.mysql.operation.MysqlDatabaseOperations;
-import com.datastrato.gravitino.catalog.mysql.operation.MysqlTableOperations;
import com.datastrato.gravitino.integration.test.container.ContainerSuite;
import com.datastrato.gravitino.integration.test.container.MySQLContainer;
import com.datastrato.gravitino.integration.test.util.TestDatabaseName;
@@ -22,7 +20,7 @@ import java.util.Map;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeAll;
-public class TestMysqlAbstractIT extends TestJdbcAbstractIT {
+public class TestMysql extends TestJdbc {
public static final String defaultMysqlImageName = "mysql:8.0";
protected static final ContainerSuite containerSuite = ContainerSuite.getInstance();
protected static TestDatabaseName TEST_DB_NAME;
diff --git a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/TestMysqlDatabaseOperations.java b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/operation/TestMysqlDatabaseOperations.java
similarity index 94%
rename from catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/TestMysqlDatabaseOperations.java
rename to catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/operation/TestMysqlDatabaseOperations.java
index 5f24ea7bcf..2cf3d6bc14 100644
--- a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/TestMysqlDatabaseOperations.java
+++ b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/operation/TestMysqlDatabaseOperations.java
@@ -2,7 +2,7 @@
* Copyright 2023 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
-package com.datastrato.gravitino.catalog.mysql.integration.test;
+package com.datastrato.gravitino.catalog.mysql.operation;
import static com.datastrato.gravitino.catalog.mysql.operation.MysqlDatabaseOperations.SYS_MYSQL_DATABASE_NAMES;
@@ -22,7 +22,7 @@ import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@Tag("gravitino-docker-it")
-public class TestMysqlDatabaseOperations extends TestMysqlAbstractIT {
+public class TestMysqlDatabaseOperations extends TestMysql {
@Test
public void testBaseOperationDatabase() {
diff --git a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/operation/TestMysqlTableOperations.java b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/operation/TestMysqlTableOperations.java
index 34e596f35b..9a38960e7c 100644
--- a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/operation/TestMysqlTableOperations.java
+++ b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/operation/TestMysqlTableOperations.java
@@ -4,14 +4,953 @@
*/
package com.datastrato.gravitino.catalog.mysql.operation;
+import static com.datastrato.gravitino.catalog.mysql.MysqlTablePropertiesMetadata.MYSQL_AUTO_INCREMENT_OFFSET_KEY;
+import static com.datastrato.gravitino.catalog.mysql.MysqlTablePropertiesMetadata.MYSQL_ENGINE_KEY;
+
+import com.datastrato.gravitino.catalog.jdbc.JdbcColumn;
+import com.datastrato.gravitino.catalog.jdbc.JdbcTable;
+import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
+import com.datastrato.gravitino.rel.Column;
import com.datastrato.gravitino.rel.TableChange;
+import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
+import com.datastrato.gravitino.rel.expressions.literals.Literals;
import com.datastrato.gravitino.rel.indexes.Index;
import com.datastrato.gravitino.rel.indexes.Indexes;
+import com.datastrato.gravitino.rel.types.Decimal;
+import com.datastrato.gravitino.rel.types.Type;
+import com.datastrato.gravitino.rel.types.Types;
+import com.datastrato.gravitino.utils.RandomNameUtils;
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.Arrays;
+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.lang3.RandomStringUtils;
+import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
-/** Test class for {@link MysqlTableOperations}. */
-public class TestMysqlTableOperations {
+@Tag("gravitino-docker-it")
+public class TestMysqlTableOperations extends TestMysql {
+ private static Type VARCHAR = Types.VarCharType.of(255);
+ private static Type INT = Types.IntegerType.get();
+
+ @Test
+ public void testOperationTable() {
+ String tableName = RandomStringUtils.randomAlphabetic(16) + "_op_table";
+ String tableComment = "test_comment";
+ List<JdbcColumn> columns = new ArrayList<>();
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(VARCHAR)
+ .withComment("test_comment")
+ .withNullable(true)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(INT)
+ .withNullable(false)
+ .withComment("set primary key")
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_3")
+ .withType(INT)
+ .withNullable(true)
+ .withDefaultValue(Literals.NULL)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(VARCHAR)
+ .withDefaultValue(Literals.of("hello world", VARCHAR))
+ .withNullable(false)
+ .build());
+ Map<String, String> properties = new HashMap<>();
+ properties.put(MYSQL_AUTO_INCREMENT_OFFSET_KEY, "10");
+
+ Index[] indexes = new Index[] {Indexes.unique("test", new String[][] {{"col_1"}, {"col_2"}})};
+ // create table
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME.toString(),
+ tableName,
+ columns.toArray(new JdbcColumn[0]),
+ tableComment,
+ properties,
+ null,
+ Distributions.NONE,
+ indexes);
+
+ // list table
+ List<String> tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME.toString());
+ Assertions.assertTrue(tables.contains(tableName));
+
+ // load table
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+ assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
+
+ // rename table
+ String newName = "new_table";
+ Assertions.assertDoesNotThrow(
+ () -> TABLE_OPERATIONS.rename(TEST_DB_NAME.toString(), tableName, newName));
+ Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), newName));
+
+ // alter table
+ JdbcColumn newColumn =
+ JdbcColumn.builder()
+ .withName("col_5")
+ .withType(VARCHAR)
+ .withComment("new_add")
+ .withNullable(true)
+ .withDefaultValue(Literals.of("hello test", VARCHAR))
+ .build();
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME.toString(),
+ newName,
+ TableChange.addColumn(
+ new String[] {newColumn.name()},
+ newColumn.dataType(),
+ newColumn.comment(),
+ TableChange.ColumnPosition.after("col_1"),
+ newColumn.defaultValue()),
+ TableChange.setProperty(MYSQL_ENGINE_KEY, "InnoDB"));
+ properties.put(MYSQL_ENGINE_KEY, "InnoDB");
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), newName);
+ List<JdbcColumn> alterColumns =
+ new ArrayList<JdbcColumn>() {
+ {
+ add(columns.get(0));
+ add(newColumn);
+ add(columns.get(1));
+ add(columns.get(2));
+ add(columns.get(3));
+ }
+ };
+ assertionsTableInfo(newName, tableComment, alterColumns, properties, indexes, load);
+
+ // Detect unsupported properties
+ TableChange setProperty = TableChange.setProperty(MYSQL_ENGINE_KEY, "ABC");
+ GravitinoRuntimeException gravitinoRuntimeException =
+ Assertions.assertThrows(
+ GravitinoRuntimeException.class,
+ () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME.toString(), newName, setProperty));
+ Assertions.assertTrue(
+ StringUtils.contains(
+ gravitinoRuntimeException.getMessage(), "Unknown storage engine 'ABC'"));
+
+ // delete column
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME.toString(),
+ newName,
+ TableChange.deleteColumn(new String[] {newColumn.name()}, true));
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), newName);
+ assertionsTableInfo(newName, tableComment, columns, properties, indexes, load);
+
+ TableChange deleteColumn = TableChange.deleteColumn(new String[] {newColumn.name()}, false);
+ IllegalArgumentException illegalArgumentException =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME.toString(), newName, deleteColumn));
+ Assertions.assertEquals(
+ "Delete column does not exist: " + newColumn.name(), illegalArgumentException.getMessage());
+ Assertions.assertDoesNotThrow(
+ () ->
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME.toString(),
+ newName,
+ TableChange.deleteColumn(new String[] {newColumn.name()}, true)));
+
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME.toString(),
+ newName,
+ TableChange.deleteColumn(new String[] {newColumn.name()}, true));
+ Assertions.assertTrue(
+ TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), newName), "table should be dropped");
+ Assertions.assertFalse(
+ TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), newName), "table should be non-existent");
+ }
+
+ @Test
+ public void testAlterTable() {
+ String tableName = RandomStringUtils.randomAlphabetic(16) + "_al_table";
+ String tableComment = "test_comment";
+ List<JdbcColumn> columns = new ArrayList<>();
+ JdbcColumn col_1 =
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(INT)
+ .withComment("id")
+ .withNullable(false)
+ .build();
+ columns.add(col_1);
+ JdbcColumn col_2 =
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(VARCHAR)
+ .withComment("name")
+ .withDefaultValue(Literals.of("hello world", VARCHAR))
+ .withNullable(false)
+ .build();
+ columns.add(col_2);
+ JdbcColumn col_3 =
+ JdbcColumn.builder()
+ .withName("col_3")
+ .withType(VARCHAR)
+ .withComment("name")
+ .withDefaultValue(Literals.NULL)
+ .build();
+ // `col_1` int NOT NULL COMMENT 'id' ,
+ // `col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'name' ,
+ // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
+ columns.add(col_3);
+ Map<String, String> properties = new HashMap<>();
+
+ Index[] indexes =
+ new Index[] {
+ Indexes.createMysqlPrimaryKey(new String[][] {{"col_1"}, {"col_2"}}),
+ Indexes.unique("uk_2", new String[][] {{"col_1"}, {"col_2"}})
+ };
+ // create table
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME.toString(),
+ tableName,
+ columns.toArray(new JdbcColumn[0]),
+ tableComment,
+ properties,
+ null,
+ Distributions.NONE,
+ indexes);
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+ assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
+
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME.toString(),
+ tableName,
+ TableChange.updateColumnType(new String[] {col_1.name()}, VARCHAR));
+
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+
+ // After modifying the type, some attributes of the corresponding column are not
+ // supported.
+ columns.clear();
+ col_1 =
+ JdbcColumn.builder()
+ .withName(col_1.name())
+ .withType(VARCHAR)
+ .withComment(col_1.comment())
+ .withNullable(col_1.nullable())
+ .withDefaultValue(col_1.defaultValue())
+ .build();
+ columns.add(col_1);
+ columns.add(col_2);
+ columns.add(col_3);
+ assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
+
+ String newComment = "new_comment";
+ // update table comment and column comment
+ // `col_1` int NOT NULL COMMENT 'id' ,
+ // `col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'new_comment' ,
+ // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME.toString(),
+ tableName,
+ TableChange.updateColumnType(new String[] {col_1.name()}, INT),
+ TableChange.updateColumnComment(new String[] {col_2.name()}, newComment));
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+
+ columns.clear();
+ col_1 =
+ JdbcColumn.builder()
+ .withName(col_1.name())
+ .withType(INT)
+ .withComment(col_1.comment())
+ .withAutoIncrement(col_1.autoIncrement())
+ .withNullable(col_1.nullable())
+ .withDefaultValue(col_1.defaultValue())
+ .build();
+ col_2 =
+ JdbcColumn.builder()
+ .withName(col_2.name())
+ .withType(col_2.dataType())
+ .withComment(newComment)
+ .withAutoIncrement(col_2.autoIncrement())
+ .withNullable(col_2.nullable())
+ .withDefaultValue(col_2.defaultValue())
+ .build();
+ columns.add(col_1);
+ columns.add(col_2);
+ columns.add(col_3);
+ assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
+
+ String newColName_1 = "new_col_1";
+ String newColName_2 = "new_col_2";
+ // rename column
+ // update table comment and column comment
+ // `new_col_1` int NOT NULL COMMENT 'id' ,
+ // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'new_comment'
+ // ,
+ // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME.toString(),
+ tableName,
+ TableChange.renameColumn(new String[] {col_1.name()}, newColName_1),
+ TableChange.renameColumn(new String[] {col_2.name()}, newColName_2));
+
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+
+ columns.clear();
+ col_1 =
+ JdbcColumn.builder()
+ .withName(newColName_1)
+ .withType(col_1.dataType())
+ .withComment(col_1.comment())
+ .withAutoIncrement(col_1.autoIncrement())
+ .withNullable(col_1.nullable())
+ .withDefaultValue(col_1.defaultValue())
+ .build();
+ col_2 =
+ JdbcColumn.builder()
+ .withName(newColName_2)
+ .withType(col_2.dataType())
+ .withComment(col_2.comment())
+ .withAutoIncrement(col_2.autoIncrement())
+ .withNullable(col_2.nullable())
+ .withDefaultValue(col_2.defaultValue())
+ .build();
+ columns.add(col_1);
+ columns.add(col_2);
+ columns.add(col_3);
+ assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
+
+ newComment = "txt3";
+ String newCol2Comment = "xxx";
+ // update column position 、comment and add column、set table properties
+ // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'xxx' ,
+ // `new_col_1` int NOT NULL COMMENT 'id' ,
+ // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
+ // `col_4` varchar(255) NOT NULL COMMENT 'txt4' ,
+ // `col_5` varchar(255) COMMENT 'hello world' DEFAULT 'hello world' ,
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME.toString(),
+ tableName,
+ TableChange.updateColumnPosition(
+ new String[] {newColName_1}, TableChange.ColumnPosition.after(newColName_2)),
+ TableChange.updateComment(newComment),
+ TableChange.addColumn(new String[] {"col_4"}, VARCHAR, "txt4", false),
+ TableChange.updateColumnComment(new String[] {newColName_2}, newCol2Comment),
+ TableChange.addColumn(
+ new String[] {"col_5"}, VARCHAR, "txt5", Literals.of("hello world", VARCHAR)));
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+
+ columns.clear();
+
+ columns.add(
+ JdbcColumn.builder()
+ .withName(col_2.name())
+ .withType(col_2.dataType())
+ .withComment(newCol2Comment)
+ .withAutoIncrement(col_2.autoIncrement())
+ .withDefaultValue(col_2.defaultValue())
+ .withNullable(col_2.nullable())
+ .build());
+ columns.add(col_1);
+ columns.add(col_3);
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(VARCHAR)
+ .withComment("txt4")
+ .withDefaultValue(Column.DEFAULT_VALUE_NOT_SET)
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_5")
+ .withType(VARCHAR)
+ .withComment("txt5")
+ .withDefaultValue(Literals.of("hello world", VARCHAR))
+ .withNullable(true)
+ .build());
+ assertionsTableInfo(tableName, newComment, columns, properties, indexes, load);
+
+ // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'xxx' ,
+ // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
+ // `col_4` varchar(255) NULL COMMENT 'txt4' ,
+ // `col_5` varchar(255) COMMENT 'hello world' DEFAULT 'hello world' ,
+ // `new_col_1` int NOT NULL COMMENT 'id' ,
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME.toString(),
+ tableName,
+ TableChange.updateColumnPosition(new String[] {columns.get(1).name()}, null),
+ TableChange.updateColumnNullability(
+ new String[] {columns.get(3).name()}, !columns.get(3).nullable()));
+
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+ col_1 = columns.remove(1);
+ JdbcColumn col3 = columns.remove(1);
+ JdbcColumn col_4 = columns.remove(1);
+ JdbcColumn col_5 = columns.remove(1);
+ columns.clear();
+
+ columns.add(
+ JdbcColumn.builder()
+ .withName("new_col_2")
+ .withType(VARCHAR)
+ .withNullable(false)
+ .withComment("xxx")
+ .withDefaultValue(Literals.of("hello world", VARCHAR))
+ .build());
+ columns.add(col3);
+ columns.add(
+ JdbcColumn.builder()
+ .withName(col_4.name())
+ .withType(col_4.dataType())
+ .withNullable(!col_4.nullable())
+ .withComment(col_4.comment())
+ .withDefaultValue(col_4.defaultValue())
+ .build());
+ columns.add(col_5);
+ columns.add(col_1);
+
+ assertionsTableInfo(tableName, newComment, columns, properties, indexes, load);
+
+ TableChange updateColumn =
+ TableChange.updateColumnNullability(new String[] {col3.name()}, !col3.nullable());
+ IllegalArgumentException exception =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME.toString(), tableName, updateColumn));
+ Assertions.assertTrue(
+ exception.getMessage().contains("with null default value cannot be changed to not null"));
+ }
+
+ @Test
+ public void testAlterTableUpdateColumnDefaultValue() {
+ String tableName = RandomNameUtils.genRandomName("properties_table_");
+ String tableComment = "test_comment";
+ List<JdbcColumn> columns = new ArrayList<>();
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.DecimalType.of(10, 2))
+ .withComment("test_decimal")
+ .withNullable(false)
+ .withDefaultValue(Literals.decimalLiteral(Decimal.of("0.00", 10, 2)))
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(Types.LongType.get())
+ .withNullable(false)
+ .withDefaultValue(Literals.longLiteral(0L))
+ .withComment("long type")
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_3")
+ .withType(Types.TimestampType.withoutTimeZone())
+ // MySQL 5.7 doesn't support nullable timestamp
+ .withNullable(false)
+ .withComment("timestamp")
+ .withDefaultValue(Literals.timestampLiteral(LocalDateTime.parse("2013-01-01T00:00:00")))
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(Types.VarCharType.of(255))
+ .withNullable(false)
+ .withComment("varchar")
+ .withDefaultValue(Literals.of("hello", Types.VarCharType.of(255)))
+ .build());
+ Map<String, String> properties = new HashMap<>();
+
+ Index[] indexes =
+ new Index[] {
+ Indexes.createMysqlPrimaryKey(new String[][] {{"col_2"}}),
+ Indexes.unique("uk_col_4", new String[][] {{"col_4"}})
+ };
+ // create table
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME.toString(),
+ tableName,
+ columns.toArray(new JdbcColumn[0]),
+ tableComment,
+ properties,
+ null,
+ Distributions.NONE,
+ indexes);
+
+ JdbcTable loaded = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+ assertionsTableInfo(tableName, tableComment, columns, properties, indexes, loaded);
+
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME.toString(),
+ tableName,
+ TableChange.updateColumnDefaultValue(
+ new String[] {columns.get(0).name()},
+ Literals.decimalLiteral(Decimal.of("1.23", 10, 2))),
+ TableChange.updateColumnDefaultValue(
+ new String[] {columns.get(1).name()}, Literals.longLiteral(1L)),
+ TableChange.updateColumnDefaultValue(
+ new String[] {columns.get(2).name()},
+ Literals.timestampLiteral(LocalDateTime.parse("2024-04-01T00:00:00"))),
+ TableChange.updateColumnDefaultValue(
+ new String[] {columns.get(3).name()}, Literals.of("world", Types.VarCharType.of(255))));
+
+ loaded = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+ Assertions.assertEquals(
+ Literals.decimalLiteral(Decimal.of("1.234", 10, 2)), loaded.columns()[0].defaultValue());
+ Assertions.assertEquals(Literals.longLiteral(1L), loaded.columns()[1].defaultValue());
+ Assertions.assertEquals(
+ Literals.timestampLiteral(LocalDateTime.parse("2024-04-01T00:00:00")),
+ loaded.columns()[2].defaultValue());
+ Assertions.assertEquals(
+ Literals.of("world", Types.VarCharType.of(255)), loaded.columns()[3].defaultValue());
+ }
+
+ @Test
+ public void testCreateAndLoadTable() {
+ String tableName = RandomStringUtils.randomAlphabetic(16) + "_cl_table";
+ String tableComment = "test_comment";
+ List<JdbcColumn> columns = new ArrayList<>();
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.DecimalType.of(10, 2))
+ .withComment("test_decimal")
+ .withNullable(false)
+ .withDefaultValue(Literals.decimalLiteral(Decimal.of("0.00", 10, 2)))
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(Types.LongType.get())
+ .withNullable(false)
+ .withDefaultValue(Literals.longLiteral(0L))
+ .withComment("long type")
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_3")
+ .withType(Types.TimestampType.withoutTimeZone())
+ // MySQL 5.7 doesn't support nullable timestamp
+ .withNullable(false)
+ .withComment("timestamp")
+ .withDefaultValue(Literals.timestampLiteral(LocalDateTime.parse("2013-01-01T00:00:00")))
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(Types.DateType.get())
+ .withNullable(true)
+ .withComment("date")
+ .withDefaultValue(Column.DEFAULT_VALUE_NOT_SET)
+ .build());
+ Map<String, String> properties = new HashMap<>();
+
+ Index[] indexes =
+ new Index[] {
+ Indexes.createMysqlPrimaryKey(new String[][] {{"col_2"}}),
+ Indexes.unique("uk_col_4", new String[][] {{"col_4"}})
+ };
+ // create table
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME.toString(),
+ tableName,
+ columns.toArray(new JdbcColumn[0]),
+ tableComment,
+ properties,
+ null,
+ Distributions.NONE,
+ indexes);
+
+ JdbcTable loaded = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+ assertionsTableInfo(tableName, tableComment, columns, properties, indexes, loaded);
+ }
+
+ @Test
+ public void testCreateAllTypeTable() {
+ String tableName = RandomNameUtils.genRandomName("type_table_");
+ String tableComment = "test_comment";
+ List<JdbcColumn> columns = new ArrayList<>();
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.ByteType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(Types.ShortType.get())
+ .withNullable(true)
+ .build());
+ columns.add(JdbcColumn.builder().withName("col_3").withType(INT).withNullable(false).build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(Types.LongType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_5")
+ .withType(Types.FloatType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_6")
+ .withType(Types.DoubleType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_7")
+ .withType(Types.DateType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_8")
+ .withType(Types.TimeType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_9")
+ .withType(Types.TimestampType.withoutTimeZone())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder().withName("col_10").withType(Types.DecimalType.of(10, 2)).build());
+ columns.add(
+ JdbcColumn.builder().withName("col_11").withType(VARCHAR).withNullable(false).build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_12")
+ .withType(Types.FixedCharType.of(10))
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_13")
+ .withType(Types.StringType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_14")
+ .withType(Types.BinaryType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_15")
+ .withType(Types.FixedCharType.of(10))
+ .withNullable(false)
+ .build());
+
+ // create table
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME.toString(),
+ tableName,
+ columns.toArray(new JdbcColumn[0]),
+ tableComment,
+ Collections.emptyMap(),
+ null,
+ Distributions.NONE,
+ Indexes.EMPTY_INDEXES);
+
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+ assertionsTableInfo(tableName, tableComment, columns, Collections.emptyMap(), null, load);
+ }
+
+ @Test
+ public void testCreateNotSupportTypeTable() {
+ String tableName = RandomNameUtils.genRandomName("type_table_");
+ String tableComment = "test_comment";
+ List<JdbcColumn> columns = new ArrayList<>();
+ List<Type> notSupportType =
+ Arrays.asList(
+ Types.BooleanType.get(),
+ Types.FixedType.of(10),
+ Types.IntervalDayType.get(),
+ Types.IntervalYearType.get(),
+ Types.UUIDType.get(),
+ Types.ListType.of(Types.DateType.get(), true),
+ Types.MapType.of(Types.StringType.get(), Types.IntegerType.get(), true),
+ Types.UnionType.of(Types.IntegerType.get()),
+ Types.StructType.of(
+ Types.StructType.Field.notNullField("col_1", Types.IntegerType.get())));
+
+ for (Type type : notSupportType) {
+ columns.clear();
+ columns.add(
+ JdbcColumn.builder().withName("col_1").withType(type).withNullable(false).build());
+
+ JdbcColumn[] jdbcCols = columns.toArray(new JdbcColumn[0]);
+ Map<String, String> emptyMap = Collections.emptyMap();
+ IllegalArgumentException illegalArgumentException =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> {
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME.toString(),
+ tableName,
+ jdbcCols,
+ tableComment,
+ emptyMap,
+ null,
+ Distributions.NONE,
+ Indexes.EMPTY_INDEXES);
+ });
+ Assertions.assertTrue(
+ illegalArgumentException
+ .getMessage()
+ .contains(
+ String.format(
+ "Couldn't convert Gravitino type %s to MySQL type", type.simpleString())));
+ }
+ }
+
+ @Test
+ public void testCreateMultipleTables() {
+ String test_table_1 = "test_table_1";
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME.toString(),
+ test_table_1,
+ new JdbcColumn[] {
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.DecimalType.of(10, 2))
+ .withComment("test_decimal")
+ .withNullable(false)
+ .withDefaultValue(Literals.decimalLiteral(Decimal.of("0.00")))
+ .build()
+ },
+ "test_comment",
+ null,
+ null,
+ Distributions.NONE,
+ Indexes.EMPTY_INDEXES);
+
+ String testDb = "test_db_2";
+
+ DATABASE_OPERATIONS.create(testDb, null, null);
+ List<String> tables = TABLE_OPERATIONS.listTables(testDb);
+ Assertions.assertFalse(tables.contains(test_table_1));
+
+ String test_table_2 = "test_table_2";
+ TABLE_OPERATIONS.create(
+ testDb,
+ test_table_2,
+ new JdbcColumn[] {
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.DecimalType.of(10, 2))
+ .withComment("test_decimal")
+ .withNullable(false)
+ .withDefaultValue(Literals.decimalLiteral(Decimal.of("0.00")))
+ .build()
+ },
+ "test_comment",
+ null,
+ null,
+ Distributions.NONE,
+ Indexes.EMPTY_INDEXES);
+
+ tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME.toString());
+ Assertions.assertFalse(tables.contains(test_table_2));
+ }
+
+ @Test
+ public void testLoadTableDefaultProperties() {
+ String test_table_1 = RandomNameUtils.genRandomName("properties_table_");
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME.toString(),
+ test_table_1,
+ new JdbcColumn[] {
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.DecimalType.of(10, 2))
+ .withComment("test_decimal")
+ .withNullable(false)
+ .build()
+ },
+ "test_comment",
+ null,
+ null,
+ Distributions.NONE,
+ Indexes.EMPTY_INDEXES);
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), test_table_1);
+ Assertions.assertEquals("InnoDB", load.properties().get(MYSQL_ENGINE_KEY));
+ }
+
+ @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 = {
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.LongType.get())
+ .withComment("id")
+ .withAutoIncrement(true)
+ .withNullable(false)
+ .build(),
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(Types.VarCharType.of(255))
+ .withComment("city")
+ .withNullable(false)
+ .build(),
+ 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.toString(),
+ tableName,
+ columns,
+ comment,
+ properties,
+ null,
+ Distributions.NONE,
+ indexes);
+
+ JdbcTable table = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+ assertionsTableInfo(
+ tableName,
+ comment,
+ Arrays.stream(columns).collect(Collectors.toList()),
+ properties,
+ indexes,
+ table);
+ TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), 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.toString(),
+ tableName,
+ columns,
+ comment,
+ properties,
+ null,
+ Distributions.NONE,
+ indexes);
+
+ table = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+ assertionsTableInfo(
+ tableName,
+ comment,
+ Arrays.stream(columns).collect(Collectors.toList()),
+ properties,
+ indexes,
+ table);
+ TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), 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.toString(),
+ tableName,
+ columns,
+ comment,
+ properties,
+ null,
+ Distributions.NONE,
+ indexes);
+
+ table = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
+ assertionsTableInfo(
+ tableName,
+ comment,
+ Arrays.stream(columns).collect(Collectors.toList()),
+ properties,
+ indexes,
+ table);
+ TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), tableName);
+
+ // Test create auto increment fail
+ IllegalArgumentException exception =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME.toString(),
+ tableName,
+ columns,
+ comment,
+ properties,
+ null,
+ Distributions.NONE,
+ 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],
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(Types.IntegerType.get())
+ .withComment("test_id")
+ .withAutoIncrement(true)
+ .withNullable(false)
+ .build()
+ };
+
+ final Index[] primaryIndex =
+ new Index[] {Indexes.createMysqlPrimaryKey(new String[][] {{"col_1"}, {"col_4"}})};
+ exception =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME.toString(),
+ tableName,
+ newColumns,
+ comment,
+ properties,
+ null,
+ Distributions.NONE,
+ primaryIndex));
+ 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]"));
+ }
@Test
public void testAppendIndexesBuilder() {
diff --git a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/PostgreSqlTableOperationsIT.java b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/PostgreSqlTableOperationsIT.java
deleted file mode 100644
index 7290b7b794..0000000000
--- a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/PostgreSqlTableOperationsIT.java
+++ /dev/null
@@ -1,624 +0,0 @@
-/*
- * Copyright 2023 Datastrato Pvt Ltd.
- * This software is licensed under the Apache License version 2.
- */
-package com.datastrato.gravitino.catalog.postgresql.integration.test;
-
-import com.datastrato.gravitino.catalog.jdbc.JdbcColumn;
-import com.datastrato.gravitino.catalog.jdbc.JdbcTable;
-import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
-import com.datastrato.gravitino.catalog.jdbc.utils.DataSourceUtils;
-import com.datastrato.gravitino.catalog.jdbc.utils.JdbcConnectorUtils;
-import com.datastrato.gravitino.catalog.postgresql.converter.PostgreSqlColumnDefaultValueConverter;
-import com.datastrato.gravitino.catalog.postgresql.converter.PostgreSqlTypeConverter;
-import com.datastrato.gravitino.catalog.postgresql.operation.PostgreSqlSchemaOperations;
-import com.datastrato.gravitino.catalog.postgresql.operation.PostgreSqlTableOperations;
-import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
-import com.datastrato.gravitino.exceptions.NoSuchTableException;
-import com.datastrato.gravitino.rel.TableChange;
-import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
-import com.datastrato.gravitino.rel.expressions.literals.Literals;
-import com.datastrato.gravitino.rel.indexes.Index;
-import com.datastrato.gravitino.rel.indexes.Indexes;
-import com.datastrato.gravitino.rel.types.Type;
-import com.datastrato.gravitino.rel.types.Types;
-import com.datastrato.gravitino.utils.RandomNameUtils;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import javax.sql.DataSource;
-import org.apache.commons.lang3.StringUtils;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Tag;
-import org.junit.jupiter.api.Test;
-import org.testcontainers.shaded.com.google.common.collect.Maps;
-
-@Tag("gravitino-docker-it")
-public class PostgreSqlTableOperationsIT extends TestPostgreSqlAbstractIT {
-
- private static Type VARCHAR = Types.VarCharType.of(255);
- private static Type INT = Types.IntegerType.get();
-
- @Test
- public void testOperationTable() {
- String tableName = RandomNameUtils.genRandomName("op_table_");
- String tableComment = "test_comment";
- List<JdbcColumn> columns = new ArrayList<>();
- columns.add(
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.LongType.get())
- .withComment("increment key")
- .withNullable(false)
- .withAutoIncrement(true)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(INT)
- .withNullable(false)
- .withComment("set test key")
- .build());
- columns.add(JdbcColumn.builder().withName("col_3").withType(INT).withNullable(true).build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_4")
- .withType(VARCHAR)
- .withDefaultValue(Literals.of("hello world", VARCHAR))
- .withNullable(false)
- .build());
- Map<String, String> properties = new HashMap<>();
- // create table
- TABLE_OPERATIONS.create(
- TEST_DB_NAME,
- tableName,
- columns.toArray(new JdbcColumn[0]),
- tableComment,
- properties,
- null,
- Distributions.NONE,
- Indexes.EMPTY_INDEXES);
-
- // list table
- List<String> tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME);
- Assertions.assertTrue(tables.contains(tableName));
-
- // load table
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
- assertionsTableInfo(tableName, tableComment, columns, properties, null, load);
-
- // rename table
- String newName = "new_table";
- Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.rename(TEST_DB_NAME, tableName, newName));
- Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.load(TEST_DB_NAME, newName));
-
- // alter table
- JdbcColumn newColumn =
- JdbcColumn.builder()
- .withName("col_5")
- .withType(VARCHAR)
- .withComment("new_add")
- .withNullable(true)
- .build();
- JdbcColumn newColumn1 =
- JdbcColumn.builder()
- .withName("col_6")
- .withType(Types.BooleanType.get())
- .withComment("new_add")
- .withDefaultValue(Literals.of("true", Types.BooleanType.get()))
- .build();
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
- newName,
- TableChange.addColumn(
- new String[] {newColumn.name()}, newColumn.dataType(), newColumn.comment()),
- TableChange.addColumn(
- new String[] {newColumn1.name()},
- newColumn1.dataType(),
- newColumn1.comment(),
- newColumn1.defaultValue()),
- TableChange.updateColumnComment(new String[] {columns.get(0).name()}, "test_new_comment"),
- TableChange.updateColumnType(
- new String[] {columns.get(1).name()}, Types.DecimalType.of(10, 2)),
- TableChange.updateColumnDefaultValue(
- new String[] {columns.get(3).name()}, Literals.of("new hello world", VARCHAR)),
- TableChange.deleteColumn(new String[] {columns.get(2).name()}, true));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME, newName);
- List<JdbcColumn> alterColumns = new ArrayList<JdbcColumn>();
- alterColumns.add(
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.LongType.get())
- .withComment("test_new_comment")
- .withNullable(false)
- .withAutoIncrement(true)
- .build());
- alterColumns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(Types.DecimalType.of(10, 2))
- .withNullable(false)
- .withComment("set test key")
- .build());
- alterColumns.add(
- JdbcColumn.builder()
- .withName("col_4")
- .withType(VARCHAR)
- .withDefaultValue(Literals.of("new hello world", VARCHAR))
- .withNullable(false)
- .build());
- alterColumns.add(newColumn);
- alterColumns.add(newColumn1);
- assertionsTableInfo(newName, tableComment, alterColumns, properties, null, load);
-
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
- newName,
- TableChange.renameColumn(new String[] {"col_1"}, "col_1_new"),
- TableChange.updateColumnType(new String[] {"col_2"}, Types.DoubleType.get()));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME, newName);
- alterColumns.clear();
- alterColumns.add(
- JdbcColumn.builder()
- .withName("col_1_new")
- .withType(Types.LongType.get())
- .withComment("test_new_comment")
- .withNullable(false)
- .withAutoIncrement(true)
- .build());
- alterColumns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(Types.DoubleType.get())
- .withNullable(false)
- .withComment("set test key")
- .build());
- alterColumns.add(
- JdbcColumn.builder()
- .withName("col_4")
- .withType(VARCHAR)
- .withDefaultValue(Literals.of("new hello world", VARCHAR))
- .withNullable(false)
- .build());
- alterColumns.add(newColumn);
- alterColumns.add(newColumn1);
- assertionsTableInfo(newName, tableComment, alterColumns, properties, null, load);
-
- // alter column Nullability
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME, newName, TableChange.updateColumnNullability(new String[] {"col_2"}, true));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME, newName);
- alterColumns.clear();
- alterColumns.add(
- JdbcColumn.builder()
- .withName("col_1_new")
- .withType(Types.LongType.get())
- .withComment("test_new_comment")
- .withNullable(false)
- .withAutoIncrement(true)
- .build());
- alterColumns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(Types.DoubleType.get())
- .withNullable(true)
- .withComment("set test key")
- .build());
- alterColumns.add(
- JdbcColumn.builder()
- .withName("col_4")
- .withType(VARCHAR)
- .withDefaultValue(Literals.of("new hello world", VARCHAR))
- .withNullable(false)
- .build());
- alterColumns.add(newColumn);
- alterColumns.add(newColumn1);
- assertionsTableInfo(newName, tableComment, alterColumns, properties, null, load);
-
- // delete column
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
- newName,
- TableChange.deleteColumn(new String[] {newColumn.name()}, true),
- TableChange.deleteColumn(new String[] {newColumn1.name()}, true));
-
- load = TABLE_OPERATIONS.load(TEST_DB_NAME, newName);
- alterColumns.remove(newColumn);
- alterColumns.remove(newColumn1);
- assertionsTableInfo(newName, tableComment, alterColumns, properties, null, load);
-
- TableChange deleteColumn = TableChange.deleteColumn(new String[] {newColumn.name()}, false);
- IllegalArgumentException illegalArgumentException =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME, newName, deleteColumn));
- Assertions.assertEquals(
- "Delete column does not exist: " + newColumn.name(), illegalArgumentException.getMessage());
-
- Assertions.assertDoesNotThrow(
- () ->
- TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
- newName,
- TableChange.deleteColumn(new String[] {newColumn.name()}, true)));
- Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.drop(TEST_DB_NAME, newName));
- Assertions.assertFalse(TABLE_OPERATIONS.drop(TEST_DB_NAME, newName));
- }
-
- @Test
- public void testCreateAllTypeTable() {
- String tableName = RandomNameUtils.genRandomName("type_table_");
- String tableComment = "test_comment";
- List<JdbcColumn> columns = new ArrayList<>();
- columns.add(
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.BooleanType.get())
- .withNullable(true)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(Types.ShortType.get())
- .withNullable(false)
- .build());
- columns.add(JdbcColumn.builder().withName("col_3").withType(INT).withNullable(true).build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_4")
- .withType(Types.LongType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_5")
- .withType(Types.FloatType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_6")
- .withType(Types.DoubleType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_7")
- .withType(Types.DateType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_8")
- .withType(Types.TimeType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_9")
- .withType(Types.TimestampType.withoutTimeZone())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_10")
- .withType(Types.TimestampType.withTimeZone())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_11")
- .withType(Types.DecimalType.of(10, 2))
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder().withName("col_12").withType(VARCHAR).withNullable(false).build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_13")
- .withType(Types.FixedCharType.of(10))
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_14")
- .withType(Types.StringType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_15")
- .withType(Types.BinaryType.get())
- .withNullable(false)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_16")
- .withType(Types.ListType.of(Types.IntegerType.get(), false))
- .withNullable(true)
- .build());
-
- // create table
- TABLE_OPERATIONS.create(
- TEST_DB_NAME,
- tableName,
- columns.toArray(new JdbcColumn[0]),
- tableComment,
- Collections.emptyMap(),
- null,
- Distributions.NONE,
- Indexes.EMPTY_INDEXES);
-
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
- assertionsTableInfo(tableName, tableComment, columns, Collections.emptyMap(), null, load);
- }
-
- @Test
- public void testCreateMultipleTable() throws SQLException {
- String testDbName = RandomNameUtils.genRandomName("test_db_");
- try (Connection connection = DATA_SOURCE.getConnection()) {
- JdbcConnectorUtils.executeUpdate(connection, "CREATE DATABASE " + testDbName);
- }
- HashMap<String, String> properties = Maps.newHashMap();
- properties.put(JdbcConfig.JDBC_DRIVER.getKey(), CONTAINER.getDriverClassName());
- String jdbcUrl =
- StringUtils.substring(CONTAINER.getJdbcUrl(), 0, CONTAINER.getJdbcUrl().lastIndexOf("/"));
- properties.put(JdbcConfig.JDBC_URL.getKey(), jdbcUrl + "/" + testDbName);
- properties.put(JdbcConfig.USERNAME.getKey(), CONTAINER.getUsername());
- properties.put(JdbcConfig.PASSWORD.getKey(), CONTAINER.getPassword());
- DataSource dataSource = DataSourceUtils.createDataSource(properties);
- PostgreSqlSchemaOperations postgreSqlSchemaOperations = new PostgreSqlSchemaOperations();
- Map<String, String> config =
- new HashMap<String, String>() {
- {
- put(JdbcConfig.JDBC_DATABASE.getKey(), testDbName);
- }
- };
- postgreSqlSchemaOperations.initialize(dataSource, JDBC_EXCEPTION_CONVERTER, config);
- postgreSqlSchemaOperations.create(TEST_DB_NAME, null, null);
-
- PostgreSqlTableOperations postgreSqlTableOperations = new PostgreSqlTableOperations();
-
- postgreSqlTableOperations.initialize(
- dataSource,
- JDBC_EXCEPTION_CONVERTER,
- new PostgreSqlTypeConverter(),
- new PostgreSqlColumnDefaultValueConverter(),
- config);
-
- String table_1 = "table_multiple_1";
- postgreSqlTableOperations.create(
- TEST_DB_NAME,
- table_1,
- new JdbcColumn[] {
- JdbcColumn.builder()
- .withName("col_1")
- .withType(VARCHAR)
- .withComment("test_comment_col1")
- .withNullable(true)
- .build()
- },
- null,
- null,
- null,
- Distributions.NONE,
- Indexes.EMPTY_INDEXES);
-
- List<String> tableNames = TABLE_OPERATIONS.listTables(TEST_DB_NAME);
- Assertions.assertFalse(tableNames.contains(table_1));
-
- Assertions.assertThrows(
- NoSuchTableException.class, () -> TABLE_OPERATIONS.load(TEST_DB_NAME, table_1));
-
- Assertions.assertThrows(
- NoSuchTableException.class, () -> TABLE_OPERATIONS.load("other_schema", table_1));
- Assertions.assertThrows(
- NoSuchTableException.class, () -> postgreSqlTableOperations.load("other_schema", table_1));
-
- String table_2 = "table_multiple_2";
- TABLE_OPERATIONS.create(
- TEST_DB_NAME,
- table_2,
- new JdbcColumn[] {
- JdbcColumn.builder()
- .withName("col_1")
- .withType(VARCHAR)
- .withComment("test_comment_col1")
- .withNullable(true)
- .build()
- },
- null,
- null,
- null,
- Distributions.NONE,
- Indexes.EMPTY_INDEXES);
- tableNames = postgreSqlTableOperations.listTables(TEST_DB_NAME);
- Assertions.assertFalse(tableNames.contains(table_2));
-
- Assertions.assertThrows(
- NoSuchTableException.class, () -> postgreSqlTableOperations.load(TEST_DB_NAME, table_2));
- Assertions.assertThrows(
- NoSuchTableException.class, () -> postgreSqlTableOperations.load("other_schema", table_2));
- Assertions.assertThrows(
- NoSuchTableException.class, () -> TABLE_OPERATIONS.load("other_schema", table_2));
- postgreSqlTableOperations.drop(TEST_DB_NAME, table_1);
-
- Assertions.assertThrows(
- NoSuchTableException.class,
- () -> {
- postgreSqlTableOperations.load(TEST_DB_NAME, table_1);
- });
-
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, table_2);
- Assertions.assertEquals(table_2, load.name());
- }
-
- @Test
- public void testCreateAutoIncrementTable() {
- String tableName = RandomNameUtils.genRandomName("increment_table_");
- String tableComment = "test_comment";
- List<JdbcColumn> columns = new ArrayList<>();
- columns.add(
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.LongType.get())
- .withComment("increment key")
- .withNullable(false)
- .withAutoIncrement(true)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(INT)
- .withNullable(false)
- .withComment("set test key")
- .build());
- Map<String, String> properties = new HashMap<>();
- // create table
- TABLE_OPERATIONS.create(
- TEST_DB_NAME,
- tableName,
- columns.toArray(new JdbcColumn[0]),
- tableComment,
- properties,
- null,
- Distributions.NONE,
- Indexes.EMPTY_INDEXES);
-
- // list table
- List<String> tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME);
- Assertions.assertTrue(tables.contains(tableName));
-
- // load table
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
- assertionsTableInfo(tableName, tableComment, columns, properties, null, load);
-
- columns.clear();
- columns.add(
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.ShortType.get())
- .withComment("increment key")
- .withNullable(false)
- .withAutoIncrement(true)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(INT)
- .withNullable(false)
- .withComment("set test key")
- .build());
-
- // Testing does not support auto increment column types
- String randomName = RandomNameUtils.genRandomName("increment_table_");
- JdbcColumn[] jdbcColumns = columns.toArray(new JdbcColumn[0]);
- IllegalArgumentException illegalArgumentException =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () -> {
- TABLE_OPERATIONS.create(
- TEST_DB_NAME,
- randomName,
- jdbcColumns,
- tableComment,
- properties,
- null,
- Distributions.NONE,
- Indexes.EMPTY_INDEXES);
- });
-
- Assertions.assertTrue(
- StringUtils.contains(illegalArgumentException.getMessage(), "Unsupported auto-increment"));
- }
-
- @Test
- public void testCreateIndexTable() {
- String tableName = RandomNameUtils.genRandomName("index_table_");
- String tableComment = "test_comment";
- List<JdbcColumn> columns = new ArrayList<>();
- columns.add(
- JdbcColumn.builder()
- .withName("col_1")
- .withType(Types.LongType.get())
- .withComment("increment key")
- .withNullable(false)
- .withAutoIncrement(true)
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_2")
- .withType(INT)
- .withNullable(false)
- .withComment("id-1")
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_3")
- .withType(VARCHAR)
- .withNullable(false)
- .withComment("name")
- .build());
- columns.add(
- JdbcColumn.builder()
- .withName("col_4")
- .withType(VARCHAR)
- .withNullable(false)
- .withComment("city")
- .build());
- Map<String, String> properties = new HashMap<>();
-
- Index[] indexes =
- new Index[] {
- Indexes.primary("test_pk", new String[][] {{"col_1"}, {"col_2"}}),
- Indexes.unique("u1_key", new String[][] {{"col_2"}, {"col_3"}}),
- Indexes.unique("u2_key", new String[][] {{"col_3"}, {"col_4"}})
- };
-
- // Test create table index success.
- TABLE_OPERATIONS.create(
- TEST_DB_NAME,
- tableName,
- columns.toArray(new JdbcColumn[0]),
- tableComment,
- properties,
- null,
- Distributions.NONE,
- indexes);
-
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
- assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
-
- TABLE_OPERATIONS.drop(TEST_DB_NAME, tableName);
-
- // Test create table index failed.
- JdbcColumn[] jdbcColumns = columns.toArray(new JdbcColumn[0]);
- Index[] primaryIndex =
- new Index[] {
- Indexes.primary("no_exist_pk", new String[][] {{"no_exist_1"}}),
- Indexes.unique("no_exist_key", new String[][] {{"no_exist_2"}, {"no_exist_3"}})
- };
- GravitinoRuntimeException gravitinoRuntimeException =
- Assertions.assertThrows(
- GravitinoRuntimeException.class,
- () -> {
- TABLE_OPERATIONS.create(
- TEST_DB_NAME,
- tableName,
- jdbcColumns,
- tableComment,
- properties,
- null,
- Distributions.NONE,
- primaryIndex);
- });
- Assertions.assertTrue(
- StringUtils.contains(
- gravitinoRuntimeException.getMessage(),
- "column \"no_exist_1\" named in key does not exist"));
- }
-}
diff --git a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/TestPostgreSqlAbstractIT.java b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSql.java
similarity index 82%
rename from catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/TestPostgreSqlAbstractIT.java
rename to catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSql.java
index 4976f12f95..80a3415f32 100644
--- a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/TestPostgreSqlAbstractIT.java
+++ b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSql.java
@@ -2,15 +2,13 @@
* Copyright 2023 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
-package com.datastrato.gravitino.catalog.postgresql.integration.test;
+package com.datastrato.gravitino.catalog.postgresql.operation;
+import com.datastrato.gravitino.catalog.jdbc.TestJdbc;
import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
-import com.datastrato.gravitino.catalog.jdbc.integration.test.TestJdbcAbstractIT;
import com.datastrato.gravitino.catalog.postgresql.converter.PostgreSqlColumnDefaultValueConverter;
import com.datastrato.gravitino.catalog.postgresql.converter.PostgreSqlExceptionConverter;
import com.datastrato.gravitino.catalog.postgresql.converter.PostgreSqlTypeConverter;
-import com.datastrato.gravitino.catalog.postgresql.operation.PostgreSqlSchemaOperations;
-import com.datastrato.gravitino.catalog.postgresql.operation.PostgreSqlTableOperations;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
@@ -18,7 +16,7 @@ import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.PostgreSQLContainer;
-public class TestPostgreSqlAbstractIT extends TestJdbcAbstractIT {
+public class TestPostgreSql extends TestJdbc {
public static final String DEFAULT_POSTGRES_IMAGE = "postgres:13";
@@ -31,7 +29,7 @@ public class TestPostgreSqlAbstractIT extends TestJdbcAbstractIT {
.withPassword("root");
DATABASE_OPERATIONS = new PostgreSqlSchemaOperations();
JDBC_EXCEPTION_CONVERTER = new PostgreSqlExceptionConverter();
- TestJdbcAbstractIT.startup();
+ TestJdbc.startup();
String jdbcUrl = CONTAINER.getJdbcUrl();
try {
String database =
diff --git a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/TestPostgreSqlSchemaOperations.java b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSqlSchemaOperations.java
similarity index 96%
rename from catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/TestPostgreSqlSchemaOperations.java
rename to catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSqlSchemaOperations.java
index b17ebfda02..33292a1dda 100644
--- a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/TestPostgreSqlSchemaOperations.java
+++ b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSqlSchemaOperations.java
@@ -2,7 +2,7 @@
* Copyright 2023 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
-package com.datastrato.gravitino.catalog.postgresql.integration.test;
+package com.datastrato.gravitino.catalog.postgresql.operation;
import static com.datastrato.gravitino.catalog.postgresql.operation.PostgreSqlSchemaOperations.SYS_PG_DATABASE_NAMES;
@@ -10,7 +10,6 @@ import com.datastrato.gravitino.catalog.jdbc.JdbcSchema;
import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
import com.datastrato.gravitino.catalog.jdbc.utils.DataSourceUtils;
import com.datastrato.gravitino.catalog.jdbc.utils.JdbcConnectorUtils;
-import com.datastrato.gravitino.catalog.postgresql.operation.PostgreSqlSchemaOperations;
import com.datastrato.gravitino.exceptions.NoSuchSchemaException;
import com.datastrato.gravitino.utils.RandomNameUtils;
import java.sql.Connection;
@@ -26,7 +25,7 @@ import org.junit.jupiter.api.Test;
import org.testcontainers.shaded.com.google.common.collect.Maps;
@Tag("gravitino-docker-it")
-public class TestPostgreSqlSchemaOperations extends TestPostgreSqlAbstractIT {
+public class TestPostgreSqlSchemaOperations extends TestPostgreSql {
@Test
public void testBaseOperationSchema() {
diff --git a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSqlTableOperations.java b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSqlTableOperations.java
index a624d502fa..ae2ee11f44 100644
--- a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSqlTableOperations.java
+++ b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSqlTableOperations.java
@@ -1,18 +1,624 @@
/*
- * Copyright 2024 Datastrato Pvt Ltd.
+ * Copyright 2023 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.catalog.postgresql.operation;
+import com.datastrato.gravitino.catalog.jdbc.JdbcColumn;
+import com.datastrato.gravitino.catalog.jdbc.JdbcTable;
+import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
+import com.datastrato.gravitino.catalog.jdbc.utils.DataSourceUtils;
+import com.datastrato.gravitino.catalog.jdbc.utils.JdbcConnectorUtils;
+import com.datastrato.gravitino.catalog.postgresql.converter.PostgreSqlColumnDefaultValueConverter;
+import com.datastrato.gravitino.catalog.postgresql.converter.PostgreSqlTypeConverter;
+import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
+import com.datastrato.gravitino.exceptions.NoSuchTableException;
import com.datastrato.gravitino.rel.TableChange;
+import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
+import com.datastrato.gravitino.rel.expressions.literals.Literals;
import com.datastrato.gravitino.rel.indexes.Index;
import com.datastrato.gravitino.rel.indexes.Indexes;
+import com.datastrato.gravitino.rel.types.Type;
+import com.datastrato.gravitino.rel.types.Types;
+import com.datastrato.gravitino.utils.RandomNameUtils;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
+import org.testcontainers.shaded.com.google.common.collect.Maps;
-/** Unit test for {@link PostgreSqlTableOperations}. */
-public class TestPostgreSqlTableOperations {
+@Tag("gravitino-docker-it")
+public class TestPostgreSqlTableOperations extends TestPostgreSql {
+
+ private static Type VARCHAR = Types.VarCharType.of(255);
+ private static Type INT = Types.IntegerType.get();
+
+ @Test
+ public void testOperationTable() {
+ String tableName = RandomNameUtils.genRandomName("op_table_");
+ String tableComment = "test_comment";
+ List<JdbcColumn> columns = new ArrayList<>();
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.LongType.get())
+ .withComment("increment key")
+ .withNullable(false)
+ .withAutoIncrement(true)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(INT)
+ .withNullable(false)
+ .withComment("set test key")
+ .build());
+ columns.add(JdbcColumn.builder().withName("col_3").withType(INT).withNullable(true).build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(VARCHAR)
+ .withDefaultValue(Literals.of("hello world", VARCHAR))
+ .withNullable(false)
+ .build());
+ Map<String, String> properties = new HashMap<>();
+ // create table
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME,
+ tableName,
+ columns.toArray(new JdbcColumn[0]),
+ tableComment,
+ properties,
+ null,
+ Distributions.NONE,
+ Indexes.EMPTY_INDEXES);
+
+ // list table
+ List<String> tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME);
+ Assertions.assertTrue(tables.contains(tableName));
+
+ // load table
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ assertionsTableInfo(tableName, tableComment, columns, properties, null, load);
+
+ // rename table
+ String newName = "new_table";
+ Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.rename(TEST_DB_NAME, tableName, newName));
+ Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.load(TEST_DB_NAME, newName));
+
+ // alter table
+ JdbcColumn newColumn =
+ JdbcColumn.builder()
+ .withName("col_5")
+ .withType(VARCHAR)
+ .withComment("new_add")
+ .withNullable(true)
+ .build();
+ JdbcColumn newColumn1 =
+ JdbcColumn.builder()
+ .withName("col_6")
+ .withType(Types.BooleanType.get())
+ .withComment("new_add")
+ .withDefaultValue(Literals.of("true", Types.BooleanType.get()))
+ .build();
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME,
+ newName,
+ TableChange.addColumn(
+ new String[] {newColumn.name()}, newColumn.dataType(), newColumn.comment()),
+ TableChange.addColumn(
+ new String[] {newColumn1.name()},
+ newColumn1.dataType(),
+ newColumn1.comment(),
+ newColumn1.defaultValue()),
+ TableChange.updateColumnComment(new String[] {columns.get(0).name()}, "test_new_comment"),
+ TableChange.updateColumnType(
+ new String[] {columns.get(1).name()}, Types.DecimalType.of(10, 2)),
+ TableChange.updateColumnDefaultValue(
+ new String[] {columns.get(3).name()}, Literals.of("new hello world", VARCHAR)),
+ TableChange.deleteColumn(new String[] {columns.get(2).name()}, true));
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME, newName);
+ List<JdbcColumn> alterColumns = new ArrayList<JdbcColumn>();
+ alterColumns.add(
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.LongType.get())
+ .withComment("test_new_comment")
+ .withNullable(false)
+ .withAutoIncrement(true)
+ .build());
+ alterColumns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(Types.DecimalType.of(10, 2))
+ .withNullable(false)
+ .withComment("set test key")
+ .build());
+ alterColumns.add(
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(VARCHAR)
+ .withDefaultValue(Literals.of("new hello world", VARCHAR))
+ .withNullable(false)
+ .build());
+ alterColumns.add(newColumn);
+ alterColumns.add(newColumn1);
+ assertionsTableInfo(newName, tableComment, alterColumns, properties, null, load);
+
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME,
+ newName,
+ TableChange.renameColumn(new String[] {"col_1"}, "col_1_new"),
+ TableChange.updateColumnType(new String[] {"col_2"}, Types.DoubleType.get()));
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME, newName);
+ alterColumns.clear();
+ alterColumns.add(
+ JdbcColumn.builder()
+ .withName("col_1_new")
+ .withType(Types.LongType.get())
+ .withComment("test_new_comment")
+ .withNullable(false)
+ .withAutoIncrement(true)
+ .build());
+ alterColumns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(Types.DoubleType.get())
+ .withNullable(false)
+ .withComment("set test key")
+ .build());
+ alterColumns.add(
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(VARCHAR)
+ .withDefaultValue(Literals.of("new hello world", VARCHAR))
+ .withNullable(false)
+ .build());
+ alterColumns.add(newColumn);
+ alterColumns.add(newColumn1);
+ assertionsTableInfo(newName, tableComment, alterColumns, properties, null, load);
+
+ // alter column Nullability
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME, newName, TableChange.updateColumnNullability(new String[] {"col_2"}, true));
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME, newName);
+ alterColumns.clear();
+ alterColumns.add(
+ JdbcColumn.builder()
+ .withName("col_1_new")
+ .withType(Types.LongType.get())
+ .withComment("test_new_comment")
+ .withNullable(false)
+ .withAutoIncrement(true)
+ .build());
+ alterColumns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(Types.DoubleType.get())
+ .withNullable(true)
+ .withComment("set test key")
+ .build());
+ alterColumns.add(
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(VARCHAR)
+ .withDefaultValue(Literals.of("new hello world", VARCHAR))
+ .withNullable(false)
+ .build());
+ alterColumns.add(newColumn);
+ alterColumns.add(newColumn1);
+ assertionsTableInfo(newName, tableComment, alterColumns, properties, null, load);
+
+ // delete column
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME,
+ newName,
+ TableChange.deleteColumn(new String[] {newColumn.name()}, true),
+ TableChange.deleteColumn(new String[] {newColumn1.name()}, true));
+
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME, newName);
+ alterColumns.remove(newColumn);
+ alterColumns.remove(newColumn1);
+ assertionsTableInfo(newName, tableComment, alterColumns, properties, null, load);
+
+ TableChange deleteColumn = TableChange.deleteColumn(new String[] {newColumn.name()}, false);
+ IllegalArgumentException illegalArgumentException =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME, newName, deleteColumn));
+ Assertions.assertEquals(
+ "Delete column does not exist: " + newColumn.name(), illegalArgumentException.getMessage());
+
+ Assertions.assertDoesNotThrow(
+ () ->
+ TABLE_OPERATIONS.alterTable(
+ TEST_DB_NAME,
+ newName,
+ TableChange.deleteColumn(new String[] {newColumn.name()}, true)));
+ Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.drop(TEST_DB_NAME, newName));
+ Assertions.assertFalse(TABLE_OPERATIONS.drop(TEST_DB_NAME, newName));
+ }
+
+ @Test
+ public void testCreateAllTypeTable() {
+ String tableName = RandomNameUtils.genRandomName("type_table_");
+ String tableComment = "test_comment";
+ List<JdbcColumn> columns = new ArrayList<>();
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.BooleanType.get())
+ .withNullable(true)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(Types.ShortType.get())
+ .withNullable(false)
+ .build());
+ columns.add(JdbcColumn.builder().withName("col_3").withType(INT).withNullable(true).build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(Types.LongType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_5")
+ .withType(Types.FloatType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_6")
+ .withType(Types.DoubleType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_7")
+ .withType(Types.DateType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_8")
+ .withType(Types.TimeType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_9")
+ .withType(Types.TimestampType.withoutTimeZone())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_10")
+ .withType(Types.TimestampType.withTimeZone())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_11")
+ .withType(Types.DecimalType.of(10, 2))
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder().withName("col_12").withType(VARCHAR).withNullable(false).build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_13")
+ .withType(Types.FixedCharType.of(10))
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_14")
+ .withType(Types.StringType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_15")
+ .withType(Types.BinaryType.get())
+ .withNullable(false)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_16")
+ .withType(Types.ListType.of(Types.IntegerType.get(), false))
+ .withNullable(true)
+ .build());
+
+ // create table
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME,
+ tableName,
+ columns.toArray(new JdbcColumn[0]),
+ tableComment,
+ Collections.emptyMap(),
+ null,
+ Distributions.NONE,
+ Indexes.EMPTY_INDEXES);
+
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ assertionsTableInfo(tableName, tableComment, columns, Collections.emptyMap(), null, load);
+ }
+
+ @Test
+ public void testCreateMultipleTable() throws SQLException {
+ String testDbName = RandomNameUtils.genRandomName("test_db_");
+ try (Connection connection = DATA_SOURCE.getConnection()) {
+ JdbcConnectorUtils.executeUpdate(connection, "CREATE DATABASE " + testDbName);
+ }
+ HashMap<String, String> properties = Maps.newHashMap();
+ properties.put(JdbcConfig.JDBC_DRIVER.getKey(), CONTAINER.getDriverClassName());
+ String jdbcUrl =
+ StringUtils.substring(CONTAINER.getJdbcUrl(), 0, CONTAINER.getJdbcUrl().lastIndexOf("/"));
+ properties.put(JdbcConfig.JDBC_URL.getKey(), jdbcUrl + "/" + testDbName);
+ properties.put(JdbcConfig.USERNAME.getKey(), CONTAINER.getUsername());
+ properties.put(JdbcConfig.PASSWORD.getKey(), CONTAINER.getPassword());
+ DataSource dataSource = DataSourceUtils.createDataSource(properties);
+ PostgreSqlSchemaOperations postgreSqlSchemaOperations = new PostgreSqlSchemaOperations();
+ Map<String, String> config =
+ new HashMap<String, String>() {
+ {
+ put(JdbcConfig.JDBC_DATABASE.getKey(), testDbName);
+ }
+ };
+ postgreSqlSchemaOperations.initialize(dataSource, JDBC_EXCEPTION_CONVERTER, config);
+ postgreSqlSchemaOperations.create(TEST_DB_NAME, null, null);
+
+ PostgreSqlTableOperations postgreSqlTableOperations = new PostgreSqlTableOperations();
+
+ postgreSqlTableOperations.initialize(
+ dataSource,
+ JDBC_EXCEPTION_CONVERTER,
+ new PostgreSqlTypeConverter(),
+ new PostgreSqlColumnDefaultValueConverter(),
+ config);
+
+ String table_1 = "table_multiple_1";
+ postgreSqlTableOperations.create(
+ TEST_DB_NAME,
+ table_1,
+ new JdbcColumn[] {
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(VARCHAR)
+ .withComment("test_comment_col1")
+ .withNullable(true)
+ .build()
+ },
+ null,
+ null,
+ null,
+ Distributions.NONE,
+ Indexes.EMPTY_INDEXES);
+
+ List<String> tableNames = TABLE_OPERATIONS.listTables(TEST_DB_NAME);
+ Assertions.assertFalse(tableNames.contains(table_1));
+
+ Assertions.assertThrows(
+ NoSuchTableException.class, () -> TABLE_OPERATIONS.load(TEST_DB_NAME, table_1));
+
+ Assertions.assertThrows(
+ NoSuchTableException.class, () -> TABLE_OPERATIONS.load("other_schema", table_1));
+ Assertions.assertThrows(
+ NoSuchTableException.class, () -> postgreSqlTableOperations.load("other_schema", table_1));
+
+ String table_2 = "table_multiple_2";
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME,
+ table_2,
+ new JdbcColumn[] {
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(VARCHAR)
+ .withComment("test_comment_col1")
+ .withNullable(true)
+ .build()
+ },
+ null,
+ null,
+ null,
+ Distributions.NONE,
+ Indexes.EMPTY_INDEXES);
+ tableNames = postgreSqlTableOperations.listTables(TEST_DB_NAME);
+ Assertions.assertFalse(tableNames.contains(table_2));
+
+ Assertions.assertThrows(
+ NoSuchTableException.class, () -> postgreSqlTableOperations.load(TEST_DB_NAME, table_2));
+ Assertions.assertThrows(
+ NoSuchTableException.class, () -> postgreSqlTableOperations.load("other_schema", table_2));
+ Assertions.assertThrows(
+ NoSuchTableException.class, () -> TABLE_OPERATIONS.load("other_schema", table_2));
+ postgreSqlTableOperations.drop(TEST_DB_NAME, table_1);
+
+ Assertions.assertThrows(
+ NoSuchTableException.class,
+ () -> {
+ postgreSqlTableOperations.load(TEST_DB_NAME, table_1);
+ });
+
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, table_2);
+ Assertions.assertEquals(table_2, load.name());
+ }
+
+ @Test
+ public void testCreateAutoIncrementTable() {
+ String tableName = RandomNameUtils.genRandomName("increment_table_");
+ String tableComment = "test_comment";
+ List<JdbcColumn> columns = new ArrayList<>();
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.LongType.get())
+ .withComment("increment key")
+ .withNullable(false)
+ .withAutoIncrement(true)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(INT)
+ .withNullable(false)
+ .withComment("set test key")
+ .build());
+ Map<String, String> properties = new HashMap<>();
+ // create table
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME,
+ tableName,
+ columns.toArray(new JdbcColumn[0]),
+ tableComment,
+ properties,
+ null,
+ Distributions.NONE,
+ Indexes.EMPTY_INDEXES);
+
+ // list table
+ List<String> tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME);
+ Assertions.assertTrue(tables.contains(tableName));
+
+ // load table
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ assertionsTableInfo(tableName, tableComment, columns, properties, null, load);
+
+ columns.clear();
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.ShortType.get())
+ .withComment("increment key")
+ .withNullable(false)
+ .withAutoIncrement(true)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(INT)
+ .withNullable(false)
+ .withComment("set test key")
+ .build());
+
+ // Testing does not support auto increment column types
+ String randomName = RandomNameUtils.genRandomName("increment_table_");
+ JdbcColumn[] jdbcColumns = columns.toArray(new JdbcColumn[0]);
+ IllegalArgumentException illegalArgumentException =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> {
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME,
+ randomName,
+ jdbcColumns,
+ tableComment,
+ properties,
+ null,
+ Distributions.NONE,
+ Indexes.EMPTY_INDEXES);
+ });
+
+ Assertions.assertTrue(
+ StringUtils.contains(illegalArgumentException.getMessage(), "Unsupported auto-increment"));
+ }
+
+ @Test
+ public void testCreateIndexTable() {
+ String tableName = RandomNameUtils.genRandomName("index_table_");
+ String tableComment = "test_comment";
+ List<JdbcColumn> columns = new ArrayList<>();
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_1")
+ .withType(Types.LongType.get())
+ .withComment("increment key")
+ .withNullable(false)
+ .withAutoIncrement(true)
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_2")
+ .withType(INT)
+ .withNullable(false)
+ .withComment("id-1")
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_3")
+ .withType(VARCHAR)
+ .withNullable(false)
+ .withComment("name")
+ .build());
+ columns.add(
+ JdbcColumn.builder()
+ .withName("col_4")
+ .withType(VARCHAR)
+ .withNullable(false)
+ .withComment("city")
+ .build());
+ Map<String, String> properties = new HashMap<>();
+
+ Index[] indexes =
+ new Index[] {
+ Indexes.primary("test_pk", new String[][] {{"col_1"}, {"col_2"}}),
+ Indexes.unique("u1_key", new String[][] {{"col_2"}, {"col_3"}}),
+ Indexes.unique("u2_key", new String[][] {{"col_3"}, {"col_4"}})
+ };
+
+ // Test create table index success.
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME,
+ tableName,
+ columns.toArray(new JdbcColumn[0]),
+ tableComment,
+ properties,
+ null,
+ Distributions.NONE,
+ indexes);
+
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
+
+ TABLE_OPERATIONS.drop(TEST_DB_NAME, tableName);
+
+ // Test create table index failed.
+ JdbcColumn[] jdbcColumns = columns.toArray(new JdbcColumn[0]);
+ Index[] primaryIndex =
+ new Index[] {
+ Indexes.primary("no_exist_pk", new String[][] {{"no_exist_1"}}),
+ Indexes.unique("no_exist_key", new String[][] {{"no_exist_2"}, {"no_exist_3"}})
+ };
+ GravitinoRuntimeException gravitinoRuntimeException =
+ Assertions.assertThrows(
+ GravitinoRuntimeException.class,
+ () -> {
+ TABLE_OPERATIONS.create(
+ TEST_DB_NAME,
+ tableName,
+ jdbcColumns,
+ tableComment,
+ properties,
+ null,
+ Distributions.NONE,
+ primaryIndex);
+ });
+ Assertions.assertTrue(
+ StringUtils.contains(
+ gravitinoRuntimeException.getMessage(),
+ "column \"no_exist_1\" named in key does not exist"));
+ }
@Test
public void testAppendIndexesSql() {