Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements.  See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership.  The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License.  You may obtain a copy of the License at
0009  *
0010  *     http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
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  * GetTablesOperation.
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   /* (non-Javadoc)
0115    * @see org.apache.hive.service.cli.Operation#getResultSetSchema()
0116    */
0117   @Override
0118   public TableSchema getResultSetSchema() throws HiveSQLException {
0119     assertState(OperationState.FINISHED);
0120     return RESULT_SET_SCHEMA;
0121   }
0122 
0123   /* (non-Javadoc)
0124    * @see org.apache.hive.service.cli.Operation#getNextRowSet(org.apache.hive.service.cli.FetchOrientation, long)
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 }