From 24934e9de9dd1d10a47cd63d8ec4ab3e44c07732 Mon Sep 17 00:00:00 2001 From: Clearvive Date: Wed, 31 Jan 2024 14:04:28 +0800 Subject: [PATCH] [#1736] feat(postgresql): Support PostgreSQL index. --- .../catalog-jdbc-postgresql/build.gradle.kts | 13 ++++ .../operation/PostgreSqlTableOperations.java | 2 +- .../UTPostgreSqlTableOperations.java | 67 +++++++++++++++++++ .../TestPostgreSqlTableOperations.java | 36 ---------- 4 files changed, 81 insertions(+), 37 deletions(-) create mode 100644 catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/UTPostgreSqlTableOperations.java diff --git a/catalogs/catalog-jdbc-postgresql/build.gradle.kts b/catalogs/catalog-jdbc-postgresql/build.gradle.kts index c1924569197..e3e3183a003 100644 --- a/catalogs/catalog-jdbc-postgresql/build.gradle.kts +++ b/catalogs/catalog-jdbc-postgresql/build.gradle.kts @@ -20,6 +20,19 @@ dependencies { implementation(libs.commons.lang3) implementation(libs.commons.collections4) implementation(libs.jsqlparser) + + testImplementation(libs.guava) + testImplementation(libs.commons.lang3) + testImplementation(libs.junit.jupiter.api) + testImplementation(libs.junit.jupiter.params) + testRuntimeOnly(libs.junit.jupiter.engine) + testImplementation(libs.mockito.core) + testImplementation(libs.jersey.test.framework.core) { + exclude(group = "org.junit.jupiter") + } + testImplementation(libs.jersey.test.framework.provider.jetty) { + exclude(group = "org.junit.jupiter") + } } tasks { diff --git a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java index f0017d4ad2a..073b2b5ca15 100644 --- a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java +++ b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java @@ -119,7 +119,7 @@ protected String generateCreateTableSql( } @VisibleForTesting - public static void appendIndexesSql(Index[] indexes, StringBuilder sqlBuilder) { + static void appendIndexesSql(Index[] indexes, StringBuilder sqlBuilder) { for (Index index : indexes) { String fieldStr = Arrays.stream(index.fieldNames()) diff --git a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/UTPostgreSqlTableOperations.java b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/UTPostgreSqlTableOperations.java new file mode 100644 index 00000000000..db9ab64b392 --- /dev/null +++ b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/UTPostgreSqlTableOperations.java @@ -0,0 +1,67 @@ +/* + * Copyright 2024 Datastrato Pvt Ltd. + * This software is licensed under the Apache License version 2. + */ +package com.datastrato.gravitino.catalog.postgresql.operation; + +import com.datastrato.gravitino.rel.indexes.Index; +import com.datastrato.gravitino.rel.indexes.Indexes; +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** Unit test for {@link PostgreSqlTableOperations}. */ +public class UTPostgreSqlTableOperations { + + @Test + public void testAppendIndexesSql() { + // Test append index sql success. + 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"}}) + }; + StringBuilder successBuilder = new StringBuilder(); + PostgreSqlTableOperations.appendIndexesSql(indexes, successBuilder); + Assertions.assertEquals( + ",\n" + + "CONSTRAINT \"test_pk\" PRIMARY KEY (\"col_1\", \"col_2\"),\n" + + "CONSTRAINT \"u1_key\" UNIQUE (\"col_2\", \"col_3\"),\n" + + "CONSTRAINT \"u2_key\" UNIQUE (\"col_3\", \"col_4\")", + successBuilder.toString()); + + // Test append index sql not have name. + indexes = + new Index[] { + Indexes.primary(null, new String[][] {{"col_1"}, {"col_2"}}), + Indexes.unique(null, new String[][] {{"col_2"}, {"col_3"}}), + Indexes.unique(null, new String[][] {{"col_3"}, {"col_4"}}) + }; + successBuilder = new StringBuilder(); + PostgreSqlTableOperations.appendIndexesSql(indexes, successBuilder); + Assertions.assertEquals( + ",\n" + + " PRIMARY KEY (\"col_1\", \"col_2\"),\n" + + " UNIQUE (\"col_2\", \"col_3\"),\n" + + " UNIQUE (\"col_3\", \"col_4\")", + successBuilder.toString()); + + // Test append index sql failed. + IllegalArgumentException illegalArgumentException = + Assertions.assertThrows( + IllegalArgumentException.class, + () -> + PostgreSqlTableOperations.appendIndexesSql( + 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"}}) + }, + new StringBuilder())); + Assertions.assertTrue( + StringUtils.contains( + illegalArgumentException.getMessage(), + "Index does not support complex fields in PostgreSQL")); + } +} diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlTableOperations.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlTableOperations.java index 76962c1426a..f96b3f126a2 100644 --- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlTableOperations.java +++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlTableOperations.java @@ -562,40 +562,4 @@ public void testCreateIndexTable() { gravitinoRuntimeException.getMessage(), "column \"no_exist_1\" named in key does not exist")); } - - @Test - public void testAppendIndexesSql() { - // Test append index sql success. - 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"}}) - }; - StringBuilder successBuilder = new StringBuilder(); - PostgreSqlTableOperations.appendIndexesSql(indexes, successBuilder); - Assertions.assertEquals( - ",\n" - + "CONSTRAINT \"test_pk\" PRIMARY KEY (\"col_1\", \"col_2\"),\n" - + "CONSTRAINT \"u1_key\" UNIQUE (\"col_2\", \"col_3\"),\n" - + "CONSTRAINT \"u2_key\" UNIQUE (\"col_3\", \"col_4\")", - successBuilder.toString()); - - // Test append index sql failed. - IllegalArgumentException illegalArgumentException = - Assertions.assertThrows( - IllegalArgumentException.class, - () -> - PostgreSqlTableOperations.appendIndexesSql( - 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"}}) - }, - new StringBuilder())); - Assertions.assertTrue( - StringUtils.contains( - illegalArgumentException.getMessage(), - "Index does not support complex fields in PostgreSQL")); - } }