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;
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
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
0096
0097
0098
0099
0100
0101
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
0125
0126
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
0137
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 }