Skip to content

Commit

Permalink
[apache#1736] feat(postgresql): Support PostgreSQL index.
Browse files Browse the repository at this point in the history
  • Loading branch information
Clearvive authored and Clearvive committed Jan 31, 2024
1 parent 5ab650b commit 24934e9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 37 deletions.
13 changes: 13 additions & 0 deletions catalogs/catalog-jdbc-postgresql/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 24934e9

Please sign in to comment.