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.Arrays;
0023 import java.util.List;
0024 
0025 import org.apache.hadoop.hive.conf.HiveConf;
0026 import org.apache.hadoop.hive.metastore.IMetaStoreClient;
0027 import org.apache.hadoop.hive.metastore.api.TableMeta;
0028 import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType;
0029 import org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObject;
0030 import org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObjectUtils;
0031 import org.apache.hive.service.cli.FetchOrientation;
0032 import org.apache.hive.service.cli.HiveSQLException;
0033 import org.apache.hive.service.cli.OperationState;
0034 import org.apache.hive.service.cli.OperationType;
0035 import org.apache.hive.service.cli.RowSet;
0036 import org.apache.hive.service.cli.RowSetFactory;
0037 import org.apache.hive.service.cli.TableSchema;
0038 import org.apache.hive.service.cli.session.HiveSession;
0039 
0040 /**
0041  * GetTablesOperation.
0042  *
0043  */
0044 public class GetTablesOperation extends MetadataOperation {
0045 
0046   private final String catalogName;
0047   private final String schemaName;
0048   private final String tableName;
0049   private final List<String> tableTypeList;
0050   protected final RowSet rowSet;
0051   private final TableTypeMapping tableTypeMapping;
0052 
0053 
0054   private static final TableSchema RESULT_SET_SCHEMA = new TableSchema()
0055   .addStringColumn("TABLE_CAT", "Catalog name. NULL if not applicable.")
0056   .addStringColumn("TABLE_SCHEM", "Schema name.")
0057   .addStringColumn("TABLE_NAME", "Table name.")
0058   .addStringColumn("TABLE_TYPE", "The table type, e.g. \"TABLE\", \"VIEW\", etc.")
0059   .addStringColumn("REMARKS", "Comments about the table.")
0060   .addStringColumn("TYPE_CAT", "The types catalog.")
0061   .addStringColumn("TYPE_SCHEM", "The types schema.")
0062   .addStringColumn("TYPE_NAME", "Type name.")
0063   .addStringColumn("SELF_REFERENCING_COL_NAME",
0064       "Name of the designated \"identifier\" column of a typed table.")
0065   .addStringColumn("REF_GENERATION",
0066       "Specifies how values in SELF_REFERENCING_COL_NAME are created.");
0067 
0068   protected GetTablesOperation(HiveSession parentSession,
0069       String catalogName, String schemaName, String tableName,
0070       List<String> tableTypes) {
0071     super(parentSession, OperationType.GET_TABLES);
0072     this.catalogName = catalogName;
0073     this.schemaName = schemaName;
0074     this.tableName = tableName;
0075     String tableMappingStr = getParentSession().getHiveConf()
0076         .getVar(HiveConf.ConfVars.HIVE_SERVER2_TABLE_TYPE_MAPPING);
0077     tableTypeMapping =
0078         TableTypeMappingFactory.getTableTypeMapping(tableMappingStr);
0079     if (tableTypes != null) {
0080       tableTypeList = new ArrayList<String>();
0081       for (String tableType : tableTypes) {
0082         tableTypeList.addAll(Arrays.asList(tableTypeMapping.mapToHiveType(tableType.trim())));
0083       }
0084     } else {
0085       tableTypeList = null;
0086     }
0087     this.rowSet = RowSetFactory.create(RESULT_SET_SCHEMA, getProtocolVersion(), false);
0088   }
0089 
0090   @Override
0091   public void runInternal() throws HiveSQLException {
0092     setState(OperationState.RUNNING);
0093     try {
0094       IMetaStoreClient metastoreClient = getParentSession().getMetaStoreClient();
0095       String schemaPattern = convertSchemaPattern(schemaName);
0096       List<String> matchingDbs = metastoreClient.getDatabases(schemaPattern);
0097       if(isAuthV2Enabled()){
0098         List<HivePrivilegeObject> privObjs = HivePrivilegeObjectUtils.getHivePrivDbObjects(matchingDbs);
0099         String cmdStr = "catalog : " + catalogName + ", schemaPattern : " + schemaName;
0100         authorizeMetaGets(HiveOperationType.GET_TABLES, privObjs, cmdStr);
0101       }
0102 
0103       String tablePattern = convertIdentifierPattern(tableName, true);
0104       for (TableMeta tableMeta :
0105           metastoreClient.getTableMeta(schemaPattern, tablePattern, tableTypeList)) {
0106         rowSet.addRow(new Object[] {
0107               DEFAULT_HIVE_CATALOG,
0108               tableMeta.getDbName(),
0109               tableMeta.getTableName(),
0110               tableTypeMapping.mapToClientType(tableMeta.getTableType()),
0111               tableMeta.getComments(),
0112               null, null, null, null, null
0113               });
0114       }
0115       setState(OperationState.FINISHED);
0116     } catch (Exception e) {
0117       setState(OperationState.ERROR);
0118       throw new HiveSQLException(e);
0119     }
0120   }
0121 
0122   /* (non-Javadoc)
0123    * @see org.apache.hive.service.cli.Operation#getResultSetSchema()
0124    */
0125   @Override
0126   public TableSchema getResultSetSchema() throws HiveSQLException {
0127     assertState(OperationState.FINISHED);
0128     return RESULT_SET_SCHEMA;
0129   }
0130 
0131   /* (non-Javadoc)
0132    * @see org.apache.hive.service.cli.Operation#getNextRowSet(org.apache.hive.service.cli.FetchOrientation, long)
0133    */
0134   @Override
0135   public RowSet getNextRowSet(FetchOrientation orientation, long maxRows) throws HiveSQLException {
0136     assertState(OperationState.FINISHED);
0137     validateDefaultFetchOrientation(orientation);
0138     if (orientation.equals(FetchOrientation.FETCH_FIRST)) {
0139       rowSet.setStartOffset(0);
0140     }
0141     return rowSet.extractSubset((int)maxRows);
0142   }
0143 }