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 org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType;
0022 import org.apache.hive.service.cli.FetchOrientation;
0023 import org.apache.hive.service.cli.HiveSQLException;
0024 import org.apache.hive.service.cli.OperationState;
0025 import org.apache.hive.service.cli.OperationType;
0026 import org.apache.hive.service.cli.RowSet;
0027 import org.apache.hive.service.cli.RowSetFactory;
0028 import org.apache.hive.service.cli.TableSchema;
0029 import org.apache.hive.service.cli.Type;
0030 import org.apache.hive.service.cli.session.HiveSession;
0031 
0032 /**
0033  * GetTypeInfoOperation.
0034  *
0035  */
0036 public class GetTypeInfoOperation extends MetadataOperation {
0037 
0038   private static final TableSchema RESULT_SET_SCHEMA = new TableSchema()
0039   .addPrimitiveColumn("TYPE_NAME", Type.STRING_TYPE,
0040       "Type name")
0041   .addPrimitiveColumn("DATA_TYPE", Type.INT_TYPE,
0042       "SQL data type from java.sql.Types")
0043   .addPrimitiveColumn("PRECISION", Type.INT_TYPE,
0044       "Maximum precision")
0045   .addPrimitiveColumn("LITERAL_PREFIX", Type.STRING_TYPE,
0046       "Prefix used to quote a literal (may be null)")
0047   .addPrimitiveColumn("LITERAL_SUFFIX", Type.STRING_TYPE,
0048       "Suffix used to quote a literal (may be null)")
0049   .addPrimitiveColumn("CREATE_PARAMS", Type.STRING_TYPE,
0050       "Parameters used in creating the type (may be null)")
0051   .addPrimitiveColumn("NULLABLE", Type.SMALLINT_TYPE,
0052       "Can you use NULL for this type")
0053   .addPrimitiveColumn("CASE_SENSITIVE", Type.BOOLEAN_TYPE,
0054       "Is it case sensitive")
0055   .addPrimitiveColumn("SEARCHABLE", Type.SMALLINT_TYPE,
0056       "Can you use \"WHERE\" based on this type")
0057   .addPrimitiveColumn("UNSIGNED_ATTRIBUTE", Type.BOOLEAN_TYPE,
0058       "Is it unsigned")
0059   .addPrimitiveColumn("FIXED_PREC_SCALE", Type.BOOLEAN_TYPE,
0060       "Can it be a money value")
0061   .addPrimitiveColumn("AUTO_INCREMENT", Type.BOOLEAN_TYPE,
0062       "Can it be used for an auto-increment value")
0063   .addPrimitiveColumn("LOCAL_TYPE_NAME", Type.STRING_TYPE,
0064       "Localized version of type name (may be null)")
0065   .addPrimitiveColumn("MINIMUM_SCALE", Type.SMALLINT_TYPE,
0066       "Minimum scale supported")
0067   .addPrimitiveColumn("MAXIMUM_SCALE", Type.SMALLINT_TYPE,
0068       "Maximum scale supported")
0069   .addPrimitiveColumn("SQL_DATA_TYPE", Type.INT_TYPE,
0070       "Unused")
0071   .addPrimitiveColumn("SQL_DATETIME_SUB", Type.INT_TYPE,
0072       "Unused")
0073   .addPrimitiveColumn("NUM_PREC_RADIX", Type.INT_TYPE,
0074       "Usually 2 or 10");
0075 
0076   protected final RowSet rowSet;
0077 
0078   protected GetTypeInfoOperation(HiveSession parentSession) {
0079     super(parentSession, OperationType.GET_TYPE_INFO);
0080     rowSet = RowSetFactory.create(RESULT_SET_SCHEMA, getProtocolVersion());
0081   }
0082 
0083   @Override
0084   public void runInternal() throws HiveSQLException {
0085     setState(OperationState.RUNNING);
0086     if (isAuthV2Enabled()) {
0087       authorizeMetaGets(HiveOperationType.GET_TYPEINFO, null);
0088     }
0089     try {
0090       for (Type type : Type.values()) {
0091         Object[] rowData = new Object[] {
0092             type.getName(), // TYPE_NAME
0093             type.toJavaSQLType(), // DATA_TYPE
0094             type.getMaxPrecision(), // PRECISION
0095             type.getLiteralPrefix(), // LITERAL_PREFIX
0096             type.getLiteralSuffix(), // LITERAL_SUFFIX
0097             type.getCreateParams(), // CREATE_PARAMS
0098             type.getNullable(), // NULLABLE
0099             type.isCaseSensitive(), // CASE_SENSITIVE
0100             type.getSearchable(), // SEARCHABLE
0101             type.isUnsignedAttribute(), // UNSIGNED_ATTRIBUTE
0102             type.isFixedPrecScale(), // FIXED_PREC_SCALE
0103             type.isAutoIncrement(), // AUTO_INCREMENT
0104             type.getLocalizedName(), // LOCAL_TYPE_NAME
0105             type.getMinimumScale(), // MINIMUM_SCALE
0106             type.getMaximumScale(), // MAXIMUM_SCALE
0107             null, // SQL_DATA_TYPE, unused
0108             null, // SQL_DATETIME_SUB, unused
0109             type.getNumPrecRadix() //NUM_PREC_RADIX
0110         };
0111         rowSet.addRow(rowData);
0112       }
0113       setState(OperationState.FINISHED);
0114     } catch (Exception e) {
0115       setState(OperationState.ERROR);
0116       throw new HiveSQLException(e);
0117     }
0118   }
0119 
0120 
0121   /* (non-Javadoc)
0122    * @see org.apache.hive.service.cli.Operation#getResultSetSchema()
0123    */
0124   @Override
0125   public TableSchema getResultSetSchema() throws HiveSQLException {
0126     assertState(OperationState.FINISHED);
0127     return RESULT_SET_SCHEMA;
0128   }
0129 
0130   /* (non-Javadoc)
0131    * @see org.apache.hive.service.cli.Operation#getNextRowSet(org.apache.hive.service.cli.FetchOrientation, long)
0132    */
0133   @Override
0134   public RowSet getNextRowSet(FetchOrientation orientation, long maxRows) throws HiveSQLException {
0135     assertState(OperationState.FINISHED);
0136     validateDefaultFetchOrientation(orientation);
0137     if (orientation.equals(FetchOrientation.FETCH_FIRST)) {
0138       rowSet.setStartOffset(0);
0139     }
0140     return rowSet.extractSubset((int)maxRows);
0141   }
0142 }