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.sql.DatabaseMetaData;
0022 import java.util.List;
0023 import java.util.Set;
0024 
0025 import org.apache.hadoop.hive.metastore.IMetaStoreClient;
0026 import org.apache.hadoop.hive.ql.exec.FunctionInfo;
0027 import org.apache.hadoop.hive.ql.exec.FunctionRegistry;
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.CLIServiceUtils;
0032 import org.apache.hive.service.cli.FetchOrientation;
0033 import org.apache.hive.service.cli.HiveSQLException;
0034 import org.apache.hive.service.cli.OperationState;
0035 import org.apache.hive.service.cli.OperationType;
0036 import org.apache.hive.service.cli.RowSet;
0037 import org.apache.hive.service.cli.RowSetFactory;
0038 import org.apache.hive.service.cli.TableSchema;
0039 import org.apache.hive.service.cli.Type;
0040 import org.apache.hive.service.cli.session.HiveSession;
0041 import org.apache.thrift.TException;
0042 
0043 /**
0044  * GetFunctionsOperation.
0045  *
0046  */
0047 public class GetFunctionsOperation extends MetadataOperation {
0048   private static final TableSchema RESULT_SET_SCHEMA = new TableSchema()
0049   .addPrimitiveColumn("FUNCTION_CAT", Type.STRING_TYPE,
0050       "Function catalog (may be null)")
0051   .addPrimitiveColumn("FUNCTION_SCHEM", Type.STRING_TYPE,
0052       "Function schema (may be null)")
0053   .addPrimitiveColumn("FUNCTION_NAME", Type.STRING_TYPE,
0054       "Function name. This is the name used to invoke the function")
0055   .addPrimitiveColumn("REMARKS", Type.STRING_TYPE,
0056       "Explanatory comment on the function")
0057   .addPrimitiveColumn("FUNCTION_TYPE", Type.INT_TYPE,
0058       "Kind of function.")
0059   .addPrimitiveColumn("SPECIFIC_NAME", Type.STRING_TYPE,
0060       "The name which uniquely identifies this function within its schema");
0061 
0062   private final String catalogName;
0063   private final String schemaName;
0064   private final String functionName;
0065 
0066   protected final RowSet rowSet;
0067 
0068   public GetFunctionsOperation(HiveSession parentSession,
0069       String catalogName, String schemaName, String functionName) {
0070     super(parentSession, OperationType.GET_FUNCTIONS);
0071     this.catalogName = catalogName;
0072     this.schemaName = schemaName;
0073     this.functionName = functionName;
0074     this.rowSet = RowSetFactory.create(RESULT_SET_SCHEMA, getProtocolVersion());
0075   }
0076 
0077   @Override
0078   public void runInternal() throws HiveSQLException {
0079     setState(OperationState.RUNNING);
0080     if (isAuthV2Enabled()) {
0081       // get databases for schema pattern
0082       IMetaStoreClient metastoreClient = getParentSession().getMetaStoreClient();
0083       String schemaPattern = convertSchemaPattern(schemaName);
0084       List<String> matchingDbs;
0085       try {
0086         matchingDbs = metastoreClient.getDatabases(schemaPattern);
0087       } catch (TException e) {
0088         setState(OperationState.ERROR);
0089         throw new HiveSQLException(e);
0090       }
0091       // authorize this call on the schema objects
0092       List<HivePrivilegeObject> privObjs = HivePrivilegeObjectUtils
0093           .getHivePrivDbObjects(matchingDbs);
0094       String cmdStr = "catalog : " + catalogName + ", schemaPattern : " + schemaName;
0095       authorizeMetaGets(HiveOperationType.GET_FUNCTIONS, privObjs, cmdStr);
0096     }
0097 
0098     try {
0099       if ((null == catalogName || "".equals(catalogName))
0100           && (null == schemaName || "".equals(schemaName))) {
0101         Set<String> functionNames =  FunctionRegistry
0102             .getFunctionNames(CLIServiceUtils.patternToRegex(functionName));
0103         for (String functionName : functionNames) {
0104           FunctionInfo functionInfo = FunctionRegistry.getFunctionInfo(functionName);
0105           Object[] rowData = new Object[] {
0106               null, // FUNCTION_CAT
0107               null, // FUNCTION_SCHEM
0108               functionInfo.getDisplayName(), // FUNCTION_NAME
0109               "", // REMARKS
0110               (functionInfo.isGenericUDTF() ?
0111                   DatabaseMetaData.functionReturnsTable
0112                   : DatabaseMetaData.functionNoTable), // FUNCTION_TYPE
0113              functionInfo.getClass().getCanonicalName()
0114           };
0115           rowSet.addRow(rowData);
0116         }
0117       }
0118       setState(OperationState.FINISHED);
0119     } catch (Exception e) {
0120       setState(OperationState.ERROR);
0121       throw new HiveSQLException(e);
0122     }
0123   }
0124 
0125 
0126   /* (non-Javadoc)
0127    * @see org.apache.hive.service.cli.Operation#getResultSetSchema()
0128    */
0129   @Override
0130   public TableSchema getResultSetSchema() throws HiveSQLException {
0131     assertState(OperationState.FINISHED);
0132     return RESULT_SET_SCHEMA;
0133   }
0134 
0135   /* (non-Javadoc)
0136    * @see org.apache.hive.service.cli.Operation#getNextRowSet(org.apache.hive.service.cli.FetchOrientation, long)
0137    */
0138   @Override
0139   public RowSet getNextRowSet(FetchOrientation orientation, long maxRows) throws HiveSQLException {
0140     assertState(OperationState.FINISHED);
0141     validateDefaultFetchOrientation(orientation);
0142     if (orientation.equals(FetchOrientation.FETCH_FIRST)) {
0143       rowSet.setStartOffset(0);
0144     }
0145     return rowSet.extractSubset((int)maxRows);
0146   }
0147 }