Skip to content

Commit

Permalink
Expose $properties table for views in Hive connector
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Aug 10, 2021
1 parent 71ce3cb commit f9aac40
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import io.trino.spi.connector.TableNotFoundException;
import io.trino.spi.type.Type;
import io.trino.spi.type.VarcharType;
import org.apache.hadoop.hive.metastore.TableType;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -58,7 +57,7 @@ public Optional<SystemTable> getSystemTable(HiveMetadata metadata, ConnectorSess

SchemaTableName sourceTableName = PROPERTIES.getSourceTableName(tableName);
Optional<Table> table = metadata.getMetastore().getTable(new HiveIdentity(session), sourceTableName.getSchemaName(), sourceTableName.getTableName());
if (table.isEmpty() || table.get().getTableType().equals(TableType.VIRTUAL_VIEW.name())) {
if (table.isEmpty()) {
throw new TableNotFoundException(tableName);
}
Map<String, String> sortedTableParameters = ImmutableSortedMap.copyOf(table.get().getParameters());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.tests.product.hive;

import io.trino.tempto.assertions.QueryAssert.Row;
import org.testng.annotations.Test;

import java.sql.SQLException;
import java.util.List;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.tempto.assertions.QueryAssert.Row.row;
import static io.trino.tempto.assertions.QueryAssert.assertThat;
import static io.trino.tempto.query.QueryExecutor.query;
import static io.trino.tests.product.TestGroups.HIVE_VIEWS;
import static io.trino.tests.product.utils.QueryExecutors.onHive;
import static io.trino.tests.product.utils.QueryExecutors.onTrino;

public class TestHivePropertiesTable
extends HiveProductTest
{
@Test
public void testTrinoViewPropertiesTable()
throws Exception
{
onTrino().executeQuery("DROP TABLE IF EXISTS test_trino_view_properties_base");
onTrino().executeQuery("DROP VIEW IF EXISTS test_trino_view_properties");
onTrino().executeQuery("CREATE TABLE test_trino_view_properties_base (col INT)");
onTrino().executeQuery("CREATE VIEW test_trino_view_properties AS SELECT * FROM test_trino_view_properties_base");

assertThat(query("SHOW COLUMNS FROM \"test_trino_view_properties$properties\""))
.containsExactlyInOrder(
row("comment", "varchar", "", ""),
row("presto_query_id", "varchar", "", ""),
row("presto_version", "varchar", "", ""),
row("presto_view", "varchar", "", ""),
row("transient_lastddltime", "varchar", "", ""),
row("trino_created_by", "varchar", "", ""));

assertThat(query("SELECT * FROM \"test_trino_view_properties$properties\""))
.hasRowsCount(1)
.containsExactlyInOrder(new Row(getTablePropertiesOnHive("test_trino_view_properties")));

onTrino().executeQuery("DROP VIEW IF EXISTS test_trino_view_properties");
onTrino().executeQuery("DROP TABLE IF EXISTS test_trino_view_properties_base");
}

@Test(groups = HIVE_VIEWS)
public void testHiveViewPropertiesTable()
throws Exception
{
onTrino().executeQuery("DROP TABLE IF EXISTS test_hive_view_properties_base");
onTrino().executeQuery("DROP VIEW IF EXISTS test_hive_view_properties");
onTrino().executeQuery("CREATE TABLE test_hive_view_properties_base (col INT)");
onHive().executeQuery("CREATE VIEW test_hive_view_properties AS SELECT * FROM test_hive_view_properties_base");

// Use "contains" method because the table properties for Hive views aren't identical among testing environments
assertThat(query("SHOW COLUMNS FROM \"test_hive_view_properties$properties\""))
.contains(row("transient_lastddltime", "varchar", "", ""));

assertThat(query("SELECT * FROM \"test_hive_view_properties$properties\""))
.hasRowsCount(1)
.containsExactlyInOrder(new Row(getTablePropertiesOnHive("test_hive_view_properties")));

onTrino().executeQuery("DROP VIEW IF EXISTS test_hive_view_properties");
onTrino().executeQuery("DROP TABLE IF EXISTS test_hive_view_properties_base");
}

private static List<String> getTablePropertiesOnHive(String tableName)
throws SQLException
{
return onHive().executeQuery("SHOW TBLPROPERTIES " + tableName).rows().stream()
.map(row -> (String) row.get(1))
.collect(toImmutableList());
}
}

0 comments on commit f9aac40

Please sign in to comment.