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;
0020 
0021 import java.util.List;
0022 
0023 import org.apache.hadoop.hive.serde2.thrift.Type;
0024 import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
0025 import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
0026 import org.apache.hive.service.rpc.thrift.TPrimitiveTypeEntry;
0027 import org.apache.hive.service.rpc.thrift.TTypeDesc;
0028 import org.apache.hive.service.rpc.thrift.TTypeEntry;
0029 
0030 /**
0031  * TypeDescriptor.
0032  *
0033  */
0034 public class TypeDescriptor {
0035 
0036   private final Type type;
0037   private String typeName = null;
0038   private TypeQualifiers typeQualifiers = null;
0039 
0040   public TypeDescriptor(Type type) {
0041     this.type = type;
0042   }
0043 
0044   public TypeDescriptor(TTypeDesc tTypeDesc) {
0045     List<TTypeEntry> tTypeEntries = tTypeDesc.getTypes();
0046     TPrimitiveTypeEntry top = tTypeEntries.get(0).getPrimitiveEntry();
0047     this.type = Type.getType(top.getType());
0048     if (top.isSetTypeQualifiers()) {
0049       setTypeQualifiers(TypeQualifiers.fromTTypeQualifiers(top.getTypeQualifiers()));
0050     }
0051   }
0052 
0053   public TypeDescriptor(String typeName) {
0054     this.type = Type.getType(typeName);
0055     if (this.type.isComplexType()) {
0056       this.typeName = typeName;
0057     } else if (this.type.isQualifiedType()) {
0058       PrimitiveTypeInfo pti = TypeInfoFactory.getPrimitiveTypeInfo(typeName);
0059       setTypeQualifiers(TypeQualifiers.fromTypeInfo(pti));
0060     }
0061   }
0062 
0063   public Type getType() {
0064     return type;
0065   }
0066 
0067   public TTypeDesc toTTypeDesc() {
0068     TPrimitiveTypeEntry primitiveEntry = new TPrimitiveTypeEntry(type.toTType());
0069     if (getTypeQualifiers() != null) {
0070       primitiveEntry.setTypeQualifiers(getTypeQualifiers().toTTypeQualifiers());
0071     }
0072     TTypeEntry entry = TTypeEntry.primitiveEntry(primitiveEntry);
0073 
0074     TTypeDesc desc = new TTypeDesc();
0075     desc.addToTypes(entry);
0076     return desc;
0077   }
0078 
0079   public String getTypeName() {
0080     if (typeName != null) {
0081       return typeName;
0082     } else {
0083       return type.getName();
0084     }
0085   }
0086 
0087   public TypeQualifiers getTypeQualifiers() {
0088     return typeQualifiers;
0089   }
0090 
0091   public void setTypeQualifiers(TypeQualifiers typeQualifiers) {
0092     this.typeQualifiers = typeQualifiers;
0093   }
0094 
0095   /**
0096    * The column size for this type.
0097    * For numeric data this is the maximum precision.
0098    * For character data this is the length in characters.
0099    * For datetime types this is the length in characters of the String representation
0100    * (assuming the maximum allowed precision of the fractional seconds component).
0101    * For binary data this is the length in bytes.
0102    * Null is returned for data types where the column size is not applicable.
0103    */
0104   public Integer getColumnSize() {
0105     if (type.isNumericType()) {
0106       return getPrecision();
0107     }
0108     switch (type) {
0109     case STRING_TYPE:
0110     case BINARY_TYPE:
0111       return Integer.MAX_VALUE;
0112     case CHAR_TYPE:
0113     case VARCHAR_TYPE:
0114       return typeQualifiers.getCharacterMaximumLength();
0115     case DATE_TYPE:
0116       return 10;
0117     case TIMESTAMP_TYPE:
0118       return 29;
0119     default:
0120       return null;
0121     }
0122   }
0123 
0124   /**
0125    * Maximum precision for numeric types.
0126    * Returns null for non-numeric types.
0127    * @return
0128    */
0129   public Integer getPrecision() {
0130     if (this.type == Type.DECIMAL_TYPE) {
0131       return typeQualifiers.getPrecision();
0132     }
0133     return this.type.getMaxPrecision();
0134   }
0135 
0136   /**
0137    * The number of fractional digits for this type.
0138    * Null is returned for data types where this is not applicable.
0139    */
0140   public Integer getDecimalDigits() {
0141     switch (this.type) {
0142     case BOOLEAN_TYPE:
0143     case TINYINT_TYPE:
0144     case SMALLINT_TYPE:
0145     case INT_TYPE:
0146     case BIGINT_TYPE:
0147       return 0;
0148     case FLOAT_TYPE:
0149       return 7;
0150     case DOUBLE_TYPE:
0151       return 15;
0152     case DECIMAL_TYPE:
0153       return typeQualifiers.getScale();
0154     case TIMESTAMP_TYPE:
0155       return 9;
0156     default:
0157       return null;
0158     }
0159   }
0160 }