Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIR-32802 use catalog parameter in getTables() & getColumns() #404

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.stream.IntStream;

import static java.lang.String.format;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -56,22 +58,29 @@ void getSchemas() throws SQLException {

@ParameterizedTest
@CsvSource(value = {
",",
",information_schema",
",information%",
",%schema",
",%form%",
"{database},",
"{database},information_schema",
"{database},information%",
"{database},%schema",
"{database},%form%",
",,information_schema",
",information_schema,information_schema",
",information%,information_schema",
",%schema,information_schema",
",%form%,information_schema",
"{database},,information_schema",
"{database},information_schema,information_schema",
"{database},information%,information_schema",
"{database},%schema,information_schema",
"{database},%form%,information_schema",

"wrong_catalog,,",
"wrong_catalog,%form%,",
})
@Tag("v2")
void getSchemasInformationSchema(String catalog, String schemaPattern) throws SQLException {
void getSchemasInformationSchema(String catalog, String schemaPattern, String expectedSchemasStr) throws SQLException {
String database = integration.ConnectionInfo.getInstance().getDatabase();
String cat = catalog == null ? null : catalog.replace("{database}", database);
assertEquals(List.of(List.of("information_schema", database)), getSchemas(dbmd -> dbmd.getSchemas(cat, schemaPattern)));
List<List<String>> expectedSchemas = expectedSchemasStr == null ?
List.of()
:
Arrays.stream(expectedSchemasStr.split(";")).map(schema -> List.of(schema, database)).collect(toList());
assertEquals(expectedSchemas, getSchemas(dbmd -> dbmd.getSchemas(cat, schemaPattern)));
}

@ParameterizedTest
Expand Down Expand Up @@ -112,6 +121,7 @@ void getSchemasInformationSchema(String catalog, String schemaPattern) throws SQ
",,,VIEW;TABLE,engines,",
",,%in%,VIEW;TABLE,engines,tables",
",,,TABLE,,",
"wrong_catalog,%form%,%in%,VIEW,,engines",
})
@Tag("v2")
void getTables(String catalog, String schemaPattern, String tableNamePattern, String types, String requiredTableName, String forbiddenTableName) throws SQLException {
Expand All @@ -129,7 +139,7 @@ void getTables(String catalog, String schemaPattern, String tableNamePattern, St
System.out.println(row.get(2));
}
if (requiredTableName == null) {
assertTrue(tables.isEmpty());
assertTrue(tables.isEmpty(), "List of tables must be empty but it was not");
} else {
assertTrue(tables.contains(requiredTableName), format("Required table %s is not found", requiredTableName));
}
Expand Down Expand Up @@ -176,6 +186,7 @@ void getTables(String catalog, String schemaPattern, String tableNamePattern, St
",,,nobody,,information_schema.columns.column_name",
",,%in%,no-one,,information_schema.columns.column_name",
",,,does-not-exist,,information_schema.columns.column_name",
"wrong_catalog,%form%,%in%,type,,information_schema.engines.type",
})
@Tag("v2")
void getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern, String requiredColumn, String forbiddenColumn) throws SQLException {
Expand All @@ -191,7 +202,7 @@ void getColumns(String catalog, String schemaPattern, String tableNamePattern, S
columns.add(IntStream.of(1, 2, 3).boxed().map(i -> (String)row.get(i)).collect(joining(".")));
}
if (requiredColumn == null) {
assertTrue(columns.isEmpty());
assertTrue(columns.isEmpty(), "List of columns must be empty but it was not");
} else {
assertTrue(columns.contains(requiredColumn), format("Required column %s is not found", requiredColumn));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public boolean supportsTransactionIsolationLevel(int level) {
public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)
throws SQLException {
List<List<?>> rows = new ArrayList<>();
String query = MetadataUtil.getColumnsQuery(schemaPattern, tableNamePattern, columnNamePattern);
String query = MetadataUtil.getColumnsQuery(catalog, schemaPattern, tableNamePattern, columnNamePattern);
try (Statement statement = connection.createStatement();
ResultSet columnDescription = statement.executeQuery(query)) {
while (columnDescription.next()) {
Expand Down Expand Up @@ -1258,7 +1258,7 @@ public ResultSet getProcedures(String catalog, String schemaPattern, String proc
@Override
public ResultSet getColumnPrivileges(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException {
List<List<?>> rows = new ArrayList<>();
String query = MetadataUtil.getColumnsQuery(schemaPattern, tableNamePattern, columnNamePattern);
String query = MetadataUtil.getColumnsQuery(catalog, schemaPattern, tableNamePattern, columnNamePattern);
try (Statement statement = connection.createStatement();
ResultSet columnDescription = statement.executeQuery(query)) {
while (columnDescription.next()) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/firebolt/jdbc/metadata/MetadataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MetadataUtil {
// return queryBuilder.conditions(conditions).build().toSql();
// }

public String getColumnsQuery(String schemaPattern, String tableNamePattern, String columnNamePattern) {
public String getColumnsQuery(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) {
Query.QueryBuilder queryBuilder = Query.builder().select(
"table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position")
.from("information_schema.columns");
Expand All @@ -44,15 +44,15 @@ public String getColumnsQuery(String schemaPattern, String tableNamePattern, Str
ofNullable(tableNamePattern).ifPresent(pattern -> conditions.add(format("table_name LIKE '%s'", pattern)));
ofNullable(columnNamePattern).ifPresent(pattern -> conditions.add(format("column_name LIKE '%s'", pattern)));
ofNullable(schemaPattern).ifPresent(pattern -> conditions.add(format("table_schema LIKE '%s'", pattern)));
ofNullable(catalog).ifPresent(pattern -> conditions.add(String.format("table_catalog LIKE '%s'", pattern)));
return queryBuilder.conditions(conditions).build().toSql();
}

public String getTablesQuery(@SuppressWarnings("java:S1172") String catalog, String schema, String tableName, String[] types) {
public String getTablesQuery(String catalog, String schema, String tableName, String[] types) {
Query.QueryBuilder queryBuilder = Query.builder().select("table_schema, table_name, table_type").from("information_schema.tables");
List<String> conditions = new ArrayList<>();
conditions.add(format("table_type IN (%s)", Arrays.stream(types).map(t -> format("'%s'", t)).collect(joining(", "))));
// Uncomment once table catalogs are supported. Remove suppress warning ava:S1172 from the first parameter also.
//ofNullable(catalog).ifPresent(pattern -> conditions.add(String.format("table_catalog LIKE '%s'",pattern)));
ofNullable(catalog).ifPresent(pattern -> conditions.add(String.format("table_catalog LIKE '%s'", pattern)));
ofNullable(schema).ifPresent(pattern -> conditions.add(format("table_schema LIKE '%s'", pattern)));
ofNullable(tableName).ifPresent(pattern -> conditions.add(format("table_name LIKE '%s'", pattern)));
return queryBuilder.conditions(conditions).orderBy("table_schema, table_name").build().toSql();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ void shouldReturnFalseWhenIncorrectTransactionIsolationLevelIsSpecified() throws

@Test
void shouldGetColumns() throws SQLException {
String expectedQuery = "SELECT table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position FROM information_schema.columns WHERE table_name LIKE 'c' AND column_name LIKE 'd' AND table_schema LIKE 'b'";
String expectedQuery = "SELECT table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position " +
"FROM information_schema.columns WHERE table_name LIKE 'c' AND column_name LIKE 'd' " +
"AND table_schema LIKE 'b' AND table_catalog LIKE 'a'";

ResultSet expectedResultSet = FireboltResultSet.of(QueryResult.builder()
.columns(Arrays.asList(Column.builder().name(TABLE_CAT).type(TEXT).build(),
Expand Down Expand Up @@ -256,7 +258,9 @@ void shouldGetColumns() throws SQLException {

@Test
void shouldGetColumnPrivileges() throws SQLException {
String expectedQuery = "SELECT table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position FROM information_schema.columns WHERE table_name LIKE 'c' AND column_name LIKE 'd' AND table_schema LIKE 'b'";
String expectedQuery = "SELECT table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position " +
"FROM information_schema.columns WHERE table_name LIKE 'c' AND column_name LIKE 'd' " +
"AND table_schema LIKE 'b' AND table_catalog LIKE 'a'";

ResultSet expectedResultSet = FireboltResultSet.of(QueryResult.builder()
.columns(Arrays.asList(Column.builder().name(TABLE_CAT).type(TEXT).build(),
Expand Down Expand Up @@ -292,7 +296,9 @@ void shouldGetTypeInfo() throws SQLException {

@Test
void shouldGetTables() throws SQLException {
String expectedSql = "SELECT table_schema, table_name, table_type FROM information_schema.tables WHERE table_type IN ('BASE TABLE', 'DIMENSION', 'FACT', 'VIEW') AND table_schema LIKE 'def%' AND table_name LIKE 'tab%' order by table_schema, table_name";
String expectedSql = "SELECT table_schema, table_name, table_type FROM information_schema.tables " +
"WHERE table_type IN ('BASE TABLE', 'DIMENSION', 'FACT', 'VIEW') AND table_catalog LIKE 'catalog' " +
"AND table_schema LIKE 'def%' AND table_name LIKE 'tab%' order by table_schema, table_name";
when(statement.executeQuery(expectedSql)).thenReturn(createResultSet(getInputStreamForGetTables()));
ResultSet resultSet = fireboltDatabaseMetadata.getTables("catalog", "def%", "tab%", null);
verify(statement).executeQuery(expectedSql);
Expand All @@ -319,7 +325,9 @@ void shouldGetTables() throws SQLException {

@Test
void shouldGetTablePrivileges() throws SQLException {
String expectedSql = "SELECT table_schema, table_name, table_type FROM information_schema.tables WHERE table_type IN ('BASE TABLE', 'DIMENSION', 'FACT') AND table_schema LIKE 'def%' AND table_name LIKE 'tab%' order by table_schema, table_name";
String expectedSql = "SELECT table_schema, table_name, table_type FROM information_schema.tables " +
"WHERE table_type IN ('BASE TABLE', 'DIMENSION', 'FACT') AND table_catalog LIKE 'catalog' " +
"AND table_schema LIKE 'def%' AND table_name LIKE 'tab%' order by table_schema, table_name";
when(statement.executeQuery(expectedSql)).thenReturn(createResultSet(getInputStreamForGetTables()));
ResultSet resultSet = fireboltDatabaseMetadata.getTablePrivileges("catalog", "def%", "tab%");
verify(statement).executeQuery(expectedSql);
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/firebolt/jdbc/metadata/MetadataUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class MetadataUtilTest {
@Test
void shouldGetTablesQueryWhenGettingQueryWithArguments() {
assertEquals(
"SELECT table_schema, table_name, table_type FROM information_schema.tables WHERE table_type IN ('FACT', 'DIMENSION') AND table_schema LIKE 'db' AND table_name LIKE 'tableName' order by table_schema, table_name",
"SELECT table_schema, table_name, table_type FROM information_schema.tables WHERE table_type IN ('FACT', 'DIMENSION') AND table_catalog LIKE 'catalog' AND table_schema LIKE 'db' AND table_name LIKE 'tableName' order by table_schema, table_name",
MetadataUtil.getTablesQuery("catalog", "db", "tableName", new String[] {"FACT", "DIMENSION"}));
}
@Test
void shouldGetViewQueryWhenGettingQueryWithArguments() {
assertEquals(
"SELECT table_schema, table_name, table_type FROM information_schema.tables WHERE table_type IN ('VIEW') AND table_schema LIKE 'db' AND table_name LIKE 'tableName' order by table_schema, table_name",
"SELECT table_schema, table_name, table_type FROM information_schema.tables WHERE table_type IN ('VIEW') AND table_catalog LIKE 'catalog' AND table_schema LIKE 'db' AND table_name LIKE 'tableName' order by table_schema, table_name",
MetadataUtil.getTablesQuery("catalog", "db", "tableName", new String[] {"VIEW"}));
}

Expand All @@ -42,14 +42,14 @@ void shouldGetTablesQueryWhenGettingQueryWithoutArguments() {
@Test
void shouldGetColumnsQueryWhenGettingQueryWithArguments() {
assertEquals(
"SELECT table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position FROM information_schema.columns WHERE table_name LIKE 'tableName' AND column_name LIKE 'col%' AND table_schema LIKE 'schema'",
MetadataUtil.getColumnsQuery("schema", "tableName", "col%"));
"SELECT table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position FROM information_schema.columns WHERE table_name LIKE 'tableName' AND column_name LIKE 'col%' AND table_schema LIKE 'schema' AND table_catalog LIKE 'db'",
MetadataUtil.getColumnsQuery("db", "schema", "tableName", "col%"));
}

@Test
void shouldGetColumnsQueryWhenGettingQueryWithoutArguments() {
assertEquals(
"SELECT table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position FROM information_schema.columns",
MetadataUtil.getColumnsQuery(null, null, null));
MetadataUtil.getColumnsQuery(null, null, null, null));
}
}
Loading