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.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
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
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
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,
0107 null,
0108 functionInfo.getDisplayName(),
0109 "",
0110 (functionInfo.isGenericUDTF() ?
0111 DatabaseMetaData.functionReturnsTable
0112 : DatabaseMetaData.functionNoTable),
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
0127
0128
0129 @Override
0130 public TableSchema getResultSetSchema() throws HiveSQLException {
0131 assertState(OperationState.FINISHED);
0132 return RESULT_SET_SCHEMA;
0133 }
0134
0135
0136
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 }