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