Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,12 @@ public void getHiveTableDetail(Context ctx) {

TableIdentifier tableIdentifier = TableIdentifier.of(catalog, db, table);
HiveTableInfo hiveTableInfo;
Table hiveTable = HiveTableUtil.loadHmsTable(hmsClientPool, tableIdentifier);
Table hiveTable = HiveTableUtil.loadPhysicalHmsTable(hmsClientPool, tableIdentifier);
Preconditions.checkState(
hiveTable.getSd() != null,
"Hive table %s.%s does not have a storage descriptor",
db,
table);
List<AMSColumnInfo> schema = transformHiveSchemaToAMSColumnInfo(hiveTable.getSd().getCols());
List<AMSColumnInfo> partitionColumnInfos =
transformHiveSchemaToAMSColumnInfo(hiveTable.getPartitionKeys());
Expand Down Expand Up @@ -727,6 +732,9 @@ private void putMainBranchFirst(List<TagOrBranchInfo> branchInfos) {
}

private List<AMSColumnInfo> transformHiveSchemaToAMSColumnInfo(List<FieldSchema> fields) {
if (fields == null) {
return Collections.emptyList();
}
return fields.stream()
.map(
f -> {
Expand Down
12 changes: 12 additions & 0 deletions amoro-common/src/main/java/org/apache/amoro/hive/HMSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.hadoop.hive.metastore.api.Partition;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.metastore.api.TableMeta;
import org.apache.thrift.TException;

import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -88,4 +89,15 @@ void alterPartitions(
InvocationTargetException, ClassNotFoundException;

List<Table> getTableObjectsByName(String dbName, List<String> tableNames) throws TException;

/**
* Returns lightweight table metadata matching the database, table, and table type patterns.
*
* @param databasePattern Hive database name or pattern
* @param tablePattern Hive table name or pattern
* @param tableTypes Hive table types to include
* @return matching lightweight table metadata
*/
List<TableMeta> getTableMeta(String databasePattern, String tablePattern, List<String> tableTypes)
throws TException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.hadoop.hive.metastore.api.Partition;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.metastore.api.TableMeta;
import org.apache.thrift.TException;

import java.util.List;
Expand Down Expand Up @@ -187,4 +188,10 @@ public List<Table> getTableObjectsByName(String dbName, List<String> tableNames)
throws TException {
return getClient().getTableObjectsByName(dbName, tableNames);
}

@Override
public List<TableMeta> getTableMeta(
String databasePattern, String tablePattern, List<String> tableTypes) throws TException {
return getClient().getTableMeta(databasePattern, tablePattern, tableTypes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.amoro.hive;

import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.metastore.api.TableMeta;
import org.apache.thrift.TException;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;

/** Utilities for classifying Hive Metastore table objects. */
public final class HiveTableTypeUtil {

private static final String VIRTUAL_VIEW_TYPE = "VIRTUAL_VIEW";
private static final String MATERIALIZED_VIEW_TYPE = "MATERIALIZED_VIEW";
private static final List<String> VIEW_TYPES =
Collections.unmodifiableList(Arrays.asList(VIRTUAL_VIEW_TYPE, MATERIALIZED_VIEW_TYPE));

private HiveTableTypeUtil() {}

/** Returns the Hive table type names representing virtual or materialized views. */
public static List<String> viewTypes() {
return VIEW_TYPES;
}

/** Returns whether the Hive table is a virtual or materialized view. */
public static boolean isView(Table table) {
return table != null && isViewType(table.getTableType());
}

/** Returns whether the lightweight Hive table metadata describes a view. */
public static boolean isView(TableMeta table) {
return table != null && isViewType(table.getTableType());
}

/**
* Returns the normalized names of Hive views among the candidate tables.
*
* @param client Hive Metastore client
* @param database database containing the candidate tables
* @param candidateTableNames table names to inspect
* @return lowercase names of virtual and materialized views
*/
public static Set<String> listViewNames(
HMSClient client, String database, List<String> candidateTableNames) throws TException {
if (candidateTableNames == null || candidateTableNames.isEmpty()) {
return Collections.emptySet();
}

List<TableMeta> views = client.getTableMeta(database, "*", viewTypes());
if (views == null) {
throw new IllegalStateException(
"Hive Metastore returned null while loading table metadata from database: " + database);
}
return views.stream()
.filter(HiveTableTypeUtil::isView)
.map(TableMeta::getTableName)
.filter(name -> name != null)
.map(name -> name.toLowerCase(Locale.ROOT))
.collect(Collectors.toCollection(HashSet::new));
}

private static boolean isViewType(String tableType) {
return VIRTUAL_VIEW_TYPE.equalsIgnoreCase(tableType)
|| MATERIALIZED_VIEW_TYPE.equalsIgnoreCase(tableType);
}
}
36 changes: 36 additions & 0 deletions amoro-common/src/test/java/org/apache/amoro/hive/TestHMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@
import org.apache.amoro.SingletonResourceUtil;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
import org.apache.hadoop.hive.metastore.TableType;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.SerDeInfo;
import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.thrift.TException;
import org.junit.rules.ExternalResource;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class TestHMS extends ExternalResource {
private static final Logger LOG = LoggerFactory.getLogger(TestHMS.class);
Expand Down Expand Up @@ -71,6 +80,33 @@ public HiveMetaStoreClient getHiveClient() {
return mockHms.getClient();
}

public void createView(String database, String viewName) throws TException {
createView(database, viewName, Collections.emptyMap());
}

public void createView(String database, String viewName, Map<String, String> parameters)
throws TException {
StorageDescriptor storageDescriptor = new StorageDescriptor();
storageDescriptor.setCols(
Collections.singletonList(new FieldSchema("id", "int", "view column")));
storageDescriptor.setSerdeInfo(new SerDeInfo());
Table view =
new Table(
viewName,
database,
System.getProperty("user.name"),
(int) (System.currentTimeMillis() / 1000),
0,
0,
storageDescriptor,
Collections.emptyList(),
new HashMap<>(parameters),
"select 1 as id",
"select 1 as id",
TableType.VIRTUAL_VIEW.name());
getHiveClient().createTable(view);
}

public int getMetastorePort() {
return mockHms.getMetastorePort();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.amoro.hive;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import org.apache.hadoop.hive.metastore.TableType;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.metastore.api.TableMeta;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;

public class TestHiveTableTypeUtil {

@Test
public void testViewTypes() {
assertTrue(HiveTableTypeUtil.isView(tableWithType(TableType.VIRTUAL_VIEW.name())));
assertTrue(HiveTableTypeUtil.isView(tableWithType("MATERIALIZED_VIEW")));
assertFalse(HiveTableTypeUtil.isView(tableWithType(TableType.EXTERNAL_TABLE.name())));
assertFalse(HiveTableTypeUtil.isView(tableWithType(null)));
assertTrue(
HiveTableTypeUtil.isView(new TableMeta("database", "view", TableType.VIRTUAL_VIEW.name())));
}

@Test
public void testListViewNames() throws Exception {
HMSClient client = mock(HMSClient.class);
when(client.getTableMeta("database", "*", HiveTableTypeUtil.viewTypes()))
.thenReturn(
Arrays.asList(
new TableMeta("database", "Hive_View", "VIRTUAL_VIEW"),
new TableMeta("database", "Materialized_View", "MATERIALIZED_VIEW"),
new TableMeta("database", "physical_table", "EXTERNAL_TABLE")));

assertEquals(
new HashSet<>(Arrays.asList("hive_view", "materialized_view")),
HiveTableTypeUtil.listViewNames(
client, "database", Arrays.asList("Hive_View", "Materialized_View", "physical_table")));
}

@Test
public void testListViewNamesSkipsEmptyCandidates() throws Exception {
HMSClient client = mock(HMSClient.class);

assertTrue(
HiveTableTypeUtil.listViewNames(client, "database", Collections.emptyList()).isEmpty());
assertTrue(HiveTableTypeUtil.listViewNames(client, "database", null).isEmpty());
verifyNoInteractions(client);
}

private static Table tableWithType(String tableType) {
Table table = new Table();
table.setTableType(tableType);
return table;
}
}
Loading
Loading