Skip to content

Commit

Permalink
Add support for views to JDBC connectors
Browse files Browse the repository at this point in the history
  • Loading branch information
harunurhan authored and electrum committed Dec 15, 2015
1 parent cb2b172 commit be0d12a
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
Expand Down
1 change: 1 addition & 0 deletions presto-docs/src/main/sphinx/release/release-0.130.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit be0d12a

Please sign in to comment.