diff --git a/presto-base-jdbc/src/main/java/com/facebook/presto/plugin/jdbc/BaseJdbcClient.java b/presto-base-jdbc/src/main/java/com/facebook/presto/plugin/jdbc/BaseJdbcClient.java index 337f855313a7..6c18edb3b291 100644 --- a/presto-base-jdbc/src/main/java/com/facebook/presto/plugin/jdbc/BaseJdbcClient.java +++ b/presto-base-jdbc/src/main/java/com/facebook/presto/plugin/jdbc/BaseJdbcClient.java @@ -384,7 +384,7 @@ protected ResultSet getTables(Connection connection, String schemaName, String t connection.getCatalog(), escapeNamePattern(schemaName, escape), escapeNamePattern(tableName, escape), - new String[] {"TABLE"}); + new String[] {"TABLE", "VIEW"}); } protected SchemaTableName getSchemaTableName(ResultSet resultSet) diff --git a/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestJdbcClient.java b/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestJdbcClient.java index e7b295d97eb4..f99d9d7b3de5 100644 --- a/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestJdbcClient.java +++ b/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestJdbcClient.java @@ -56,7 +56,10 @@ public void testMetadata() throws Exception { assertTrue(jdbcClient.getSchemaNames().containsAll(ImmutableSet.of("example", "tpch"))); - assertEquals(jdbcClient.getTableNames("example"), ImmutableList.of(new SchemaTableName("example", "numbers"))); + assertEquals(jdbcClient.getTableNames("example"), ImmutableList.of( + new SchemaTableName("example", "numbers"), + new SchemaTableName("example", "view_source"), + new SchemaTableName("example", "view"))); assertEquals(jdbcClient.getTableNames("tpch"), ImmutableList.of( new SchemaTableName("tpch", "lineitem"), new SchemaTableName("tpch", "orders"))); diff --git a/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestJdbcMetadata.java b/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestJdbcMetadata.java index ce0f0cdbd66d..867ac5df68ed 100644 --- a/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestJdbcMetadata.java +++ b/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestJdbcMetadata.java @@ -138,13 +138,17 @@ public void testListTables() // all schemas assertEquals(ImmutableSet.copyOf(metadata.listTables(SESSION, null)), ImmutableSet.of( new SchemaTableName("example", "numbers"), + new SchemaTableName("example", "view_source"), + new SchemaTableName("example", "view"), new SchemaTableName("tpch", "orders"), new SchemaTableName("tpch", "lineitem"), new SchemaTableName("exa_ple", "num_ers"))); // specific schema assertEquals(ImmutableSet.copyOf(metadata.listTables(SESSION, "example")), ImmutableSet.of( - new SchemaTableName("example", "numbers"))); + new SchemaTableName("example", "numbers"), + new SchemaTableName("example", "view_source"), + new SchemaTableName("example", "view"))); assertEquals(ImmutableSet.copyOf(metadata.listTables(SESSION, "tpch")), ImmutableSet.of( new SchemaTableName("tpch", "orders"), new SchemaTableName("tpch", "lineitem"))); diff --git a/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestingDatabase.java b/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestingDatabase.java index df18a9d97689..007608fb61f5 100644 --- a/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestingDatabase.java +++ b/presto-base-jdbc/src/test/java/com/facebook/presto/plugin/jdbc/TestingDatabase.java @@ -60,6 +60,8 @@ public TestingDatabase() "('eleven', 11)," + "('twelve', 12)" + ""); + connection.createStatement().execute("CREATE TABLE example.view_source(id varchar primary key)"); + connection.createStatement().execute("CREATE VIEW example.view AS SELECT id FROM example.view_source"); connection.createStatement().execute("CREATE SCHEMA tpch"); connection.createStatement().execute("CREATE TABLE tpch.orders(orderkey bigint primary key, custkey bigint)"); connection.createStatement().execute("CREATE TABLE tpch.lineitem(orderkey bigint primary key, partkey bigint)"); diff --git a/presto-docs/src/main/sphinx/release/release-0.130.rst b/presto-docs/src/main/sphinx/release/release-0.130.rst index 1752b785e86a..8f5e08c92e76 100644 --- a/presto-docs/src/main/sphinx/release/release-0.130.rst +++ b/presto-docs/src/main/sphinx/release/release-0.130.rst @@ -14,3 +14,4 @@ General Changes ``columnar_processing_dictionary`` session property. * Improve performance of aggregation queries with large numbers of groups. * Improve performance for queries that use :ref:`array_type` type. +* Fix querying remote views in MySQL and PostgreSQL connectors. diff --git a/presto-mysql/src/main/java/com/facebook/presto/plugin/mysql/MySqlClient.java b/presto-mysql/src/main/java/com/facebook/presto/plugin/mysql/MySqlClient.java index a20521fd10c1..43e78cbbe0ed 100644 --- a/presto-mysql/src/main/java/com/facebook/presto/plugin/mysql/MySqlClient.java +++ b/presto-mysql/src/main/java/com/facebook/presto/plugin/mysql/MySqlClient.java @@ -94,7 +94,7 @@ protected ResultSet getTables(Connection connection, String schemaName, String t schemaName, null, escapeNamePattern(tableName, escape), - new String[] {"TABLE"}); + new String[] {"TABLE", "VIEW"}); } @Override diff --git a/presto-mysql/src/test/java/com/facebook/presto/plugin/mysql/TestMySqlDistributedQueries.java b/presto-mysql/src/test/java/com/facebook/presto/plugin/mysql/TestMySqlDistributedQueries.java index f09a13881e89..d5d63c28d460 100644 --- a/presto-mysql/src/test/java/com/facebook/presto/plugin/mysql/TestMySqlDistributedQueries.java +++ b/presto-mysql/src/test/java/com/facebook/presto/plugin/mysql/TestMySqlDistributedQueries.java @@ -19,6 +19,11 @@ import org.testng.annotations.AfterClass; import org.testng.annotations.Test; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + import static com.facebook.presto.plugin.mysql.MySqlQueryRunner.createMySqlQueryRunner; import static io.airlift.testing.Closeables.closeAllRuntimeException; import static org.testng.Assert.assertFalse; @@ -59,4 +64,24 @@ public void testDropTable() assertUpdate("DROP TABLE test_drop"); assertFalse(queryRunner.tableExists(getSession(), "test_drop")); } + + @Test + public void testViews() + throws Exception + { + execute("CREATE OR REPLACE VIEW tpch.test_view AS SELECT * FROM tpch.orders"); + + assertQuery("SELECT orderkey FROM test_view", "SELECT orderkey FROM orders"); + + execute("DROP VIEW IF EXISTS tpch.test_view"); + } + + private void execute(String sql) + throws SQLException + { + try (Connection connection = DriverManager.getConnection(mysqlServer.getJdbcUrl()); + Statement statement = connection.createStatement()) { + statement.execute(sql); + } + } } diff --git a/presto-postgresql/src/test/java/com/facebook/presto/plugin/postgresql/TestPostgreSqlDistributedQueries.java b/presto-postgresql/src/test/java/com/facebook/presto/plugin/postgresql/TestPostgreSqlDistributedQueries.java index 44703b6a256c..54ec652f4484 100644 --- a/presto-postgresql/src/test/java/com/facebook/presto/plugin/postgresql/TestPostgreSqlDistributedQueries.java +++ b/presto-postgresql/src/test/java/com/facebook/presto/plugin/postgresql/TestPostgreSqlDistributedQueries.java @@ -20,6 +20,10 @@ import org.testng.annotations.Test; import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; import static com.facebook.presto.plugin.postgresql.PostgreSqlQueryRunner.createPostgreSqlQueryRunner; import static io.airlift.testing.Closeables.closeAllRuntimeException; @@ -62,4 +66,24 @@ public void testDropTable() assertUpdate("DROP TABLE test_drop"); assertFalse(queryRunner.tableExists(getSession(), "test_drop")); } + + @Test + public void testViews() + throws Exception + { + execute("CREATE OR REPLACE VIEW tpch.test_view AS SELECT * FROM tpch.orders"); + + assertQuery("SELECT orderkey FROM test_view", "SELECT orderkey FROM orders"); + + execute("DROP VIEW IF EXISTS tpch.test_view"); + } + + private void execute(String sql) + throws SQLException + { + try (Connection connection = DriverManager.getConnection(postgreSqlServer.getJdbcUrl()); + Statement statement = connection.createStatement()) { + statement.execute(sql); + } + } }