Skip to content

Commit

Permalink
Fix duplicate TABLE_TYPE listing for view from system.jdbc.tables
Browse files Browse the repository at this point in the history
  • Loading branch information
agrawalreetika committed Feb 13, 2025
1 parent ddb5bae commit d08c27e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,41 @@ public void testResetAutoCommit()
}
}

@Test
public void testTableType()
throws SQLException
{
try (Connection connection = createConnection()) {
assertThat(connection.getCatalog()).isEqualTo("hive");
assertThat(connection.getSchema()).isEqualTo("default");

try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE test_table_type (x bigint)");
statement.execute("CREATE VIEW table_type_view AS SELECT * FROM test_table_type");
ResultSet rs = statement.executeQuery("SELECT TABLE_NAME, TABLE_TYPE FROM system.jdbc.tables WHERE TABLE_SCHEM = 'default' AND TABLE_NAME = 'table_type_view'");
int rowCount = 0;
while (rs.next()) {
assertEquals(rs.getString("TABLE_NAME"), "table_type_view");
assertEquals(rs.getString("TABLE_TYPE"), "VIEW");
rowCount++;
}
assertEquals(rowCount, 1);

rowCount = 0;
rs = statement.executeQuery("SELECT TABLE_NAME, TABLE_TYPE FROM system.jdbc.tables WHERE TABLE_SCHEM = 'default' AND TABLE_NAME = 'test_table_type'");
while (rs.next()) {
assertEquals(rs.getString("TABLE_NAME"), "test_table_type");
assertEquals(rs.getString("TABLE_TYPE"), "TABLE");
rowCount++;
}
assertEquals(rowCount, 1);

statement.execute("DROP TABLE test_table_type");
statement.execute("DROP VIEW table_type_view");
}
}
}

@Test
public void testRollback()
throws SQLException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import com.facebook.presto.spi.SchemaTableName;
import com.facebook.presto.spi.connector.ConnectorTransactionHandle;
import com.facebook.presto.spi.security.AccessControl;
import com.google.common.collect.ImmutableSet;

import javax.inject.Inject;

import java.util.Optional;
import java.util.Set;

import static com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType;
import static com.facebook.presto.connector.system.SystemConnectorSessionUtil.toSession;
Expand Down Expand Up @@ -88,15 +90,19 @@ public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, Connect
for (String catalog : filter(listCatalogs(session, metadata, accessControl).keySet(), catalogFilter)) {
QualifiedTablePrefix prefix = tablePrefix(catalog, schemaFilter, tableFilter);

if (FilterUtil.emptyOrEquals(typeFilter, "TABLE")) {
for (SchemaTableName name : listTables(session, metadata, accessControl, prefix)) {
table.addRow(tableRow(catalog, name, "TABLE"));
Set<SchemaTableName> views = ImmutableSet.of();
if (FilterUtil.emptyOrEquals(typeFilter, "VIEW")) {
views = ImmutableSet.copyOf(listViews(session, metadata, accessControl, prefix));
for (SchemaTableName name : views) {
table.addRow(tableRow(catalog, name, "VIEW"));
}
}

if (FilterUtil.emptyOrEquals(typeFilter, "VIEW")) {
for (SchemaTableName name : listViews(session, metadata, accessControl, prefix)) {
table.addRow(tableRow(catalog, name, "VIEW"));
if (FilterUtil.emptyOrEquals(typeFilter, "TABLE")) {
for (SchemaTableName name : listTables(session, metadata, accessControl, prefix)) {
if (!views.contains(name)) {
table.addRow(tableRow(catalog, name, "TABLE"));
}
}
}
}
Expand Down

0 comments on commit d08c27e

Please sign in to comment.