From d08c27ec6d034d5748ad5cd00709ff9459ef83b4 Mon Sep 17 00:00:00 2001 From: Reetika Agrawal Date: Tue, 11 Feb 2025 01:16:14 +0530 Subject: [PATCH] Fix duplicate TABLE_TYPE listing for view from system.jdbc.tables --- .../presto/jdbc/TestJdbcConnection.java | 35 +++++++++++++++++++ .../connector/system/jdbc/TableJdbcTable.java | 18 ++++++---- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/presto-jdbc/src/test/java/com/facebook/presto/jdbc/TestJdbcConnection.java b/presto-jdbc/src/test/java/com/facebook/presto/jdbc/TestJdbcConnection.java index 7e80977af6349..7b626d7908d2a 100644 --- a/presto-jdbc/src/test/java/com/facebook/presto/jdbc/TestJdbcConnection.java +++ b/presto-jdbc/src/test/java/com/facebook/presto/jdbc/TestJdbcConnection.java @@ -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 diff --git a/presto-main/src/main/java/com/facebook/presto/connector/system/jdbc/TableJdbcTable.java b/presto-main/src/main/java/com/facebook/presto/connector/system/jdbc/TableJdbcTable.java index 543919acf9d11..b9d5539fcbd17 100644 --- a/presto-main/src/main/java/com/facebook/presto/connector/system/jdbc/TableJdbcTable.java +++ b/presto-main/src/main/java/com/facebook/presto/connector/system/jdbc/TableJdbcTable.java @@ -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; @@ -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 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")); + } } } }