0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 package org.apache.hive.service.cli.operation;
0020
0021 import java.util.ArrayList;
0022 import java.util.List;
0023
0024 import org.apache.hadoop.hive.conf.HiveConf;
0025 import org.apache.hadoop.hive.metastore.IMetaStoreClient;
0026 import org.apache.hadoop.hive.metastore.api.Table;
0027 import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType;
0028 import org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObject;
0029 import org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObjectUtils;
0030 import org.apache.hive.service.cli.FetchOrientation;
0031 import org.apache.hive.service.cli.HiveSQLException;
0032 import org.apache.hive.service.cli.OperationState;
0033 import org.apache.hive.service.cli.OperationType;
0034 import org.apache.hive.service.cli.RowSet;
0035 import org.apache.hive.service.cli.RowSetFactory;
0036 import org.apache.hive.service.cli.TableSchema;
0037 import org.apache.hive.service.cli.session.HiveSession;
0038
0039
0040
0041
0042
0043 public class GetTablesOperation extends MetadataOperation {
0044
0045 private final String catalogName;
0046 private final String schemaName;
0047 private final String tableName;
0048 private final List<String> tableTypes = new ArrayList<String>();
0049 protected final RowSet rowSet;
0050 private final TableTypeMapping tableTypeMapping;
0051
0052
0053 private static final TableSchema RESULT_SET_SCHEMA = new TableSchema()
0054 .addStringColumn("TABLE_CAT", "Catalog name. NULL if not applicable.")
0055 .addStringColumn("TABLE_SCHEM", "Schema name.")
0056 .addStringColumn("TABLE_NAME", "Table name.")
0057 .addStringColumn("TABLE_TYPE", "The table type, e.g. \"TABLE\", \"VIEW\", etc.")
0058 .addStringColumn("REMARKS", "Comments about the table.");
0059
0060 protected GetTablesOperation(HiveSession parentSession,
0061 String catalogName, String schemaName, String tableName,
0062 List<String> tableTypes) {
0063 super(parentSession, OperationType.GET_TABLES);
0064 this.catalogName = catalogName;
0065 this.schemaName = schemaName;
0066 this.tableName = tableName;
0067 String tableMappingStr = getParentSession().getHiveConf()
0068 .getVar(HiveConf.ConfVars.HIVE_SERVER2_TABLE_TYPE_MAPPING);
0069 tableTypeMapping =
0070 TableTypeMappingFactory.getTableTypeMapping(tableMappingStr);
0071 if (tableTypes != null) {
0072 this.tableTypes.addAll(tableTypes);
0073 }
0074 this.rowSet = RowSetFactory.create(RESULT_SET_SCHEMA, getProtocolVersion());
0075 }
0076
0077 @Override
0078 public void runInternal() throws HiveSQLException {
0079 setState(OperationState.RUNNING);
0080 try {
0081 IMetaStoreClient metastoreClient = getParentSession().getMetaStoreClient();
0082 String schemaPattern = convertSchemaPattern(schemaName);
0083 List<String> matchingDbs = metastoreClient.getDatabases(schemaPattern);
0084 if(isAuthV2Enabled()){
0085 List<HivePrivilegeObject> privObjs = HivePrivilegeObjectUtils.getHivePrivDbObjects(matchingDbs);
0086 String cmdStr = "catalog : " + catalogName + ", schemaPattern : " + schemaName;
0087 authorizeMetaGets(HiveOperationType.GET_TABLES, privObjs, cmdStr);
0088 }
0089
0090 String tablePattern = convertIdentifierPattern(tableName, true);
0091 for (String dbName : metastoreClient.getDatabases(schemaPattern)) {
0092 List<String> tableNames = metastoreClient.getTables(dbName, tablePattern);
0093 for (Table table : metastoreClient.getTableObjectsByName(dbName, tableNames)) {
0094 Object[] rowData = new Object[] {
0095 DEFAULT_HIVE_CATALOG,
0096 table.getDbName(),
0097 table.getTableName(),
0098 tableTypeMapping.mapToClientType(table.getTableType()),
0099 table.getParameters().get("comment")
0100 };
0101 if (tableTypes.isEmpty() || tableTypes.contains(
0102 tableTypeMapping.mapToClientType(table.getTableType()))) {
0103 rowSet.addRow(rowData);
0104 }
0105 }
0106 }
0107 setState(OperationState.FINISHED);
0108 } catch (Exception e) {
0109 setState(OperationState.ERROR);
0110 throw new HiveSQLException(e);
0111 }
0112 }
0113
0114
0115
0116
0117 @Override
0118 public TableSchema getResultSetSchema() throws HiveSQLException {
0119 assertState(OperationState.FINISHED);
0120 return RESULT_SET_SCHEMA;
0121 }
0122
0123
0124
0125
0126 @Override
0127 public RowSet getNextRowSet(FetchOrientation orientation, long maxRows) throws HiveSQLException {
0128 assertState(OperationState.FINISHED);
0129 validateDefaultFetchOrientation(orientation);
0130 if (orientation.equals(FetchOrientation.FETCH_FIRST)) {
0131 rowSet.setStartOffset(0);
0132 }
0133 return rowSet.extractSubset((int)maxRows);
0134 }
0135 }