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.math.BigDecimal;
0022 import java.sql.Date;
0023 import java.sql.Timestamp;
0024
0025 import org.apache.hadoop.hive.common.type.HiveChar;
0026 import org.apache.hadoop.hive.common.type.HiveIntervalDayTime;
0027 import org.apache.hadoop.hive.common.type.HiveIntervalYearMonth;
0028 import org.apache.hadoop.hive.common.type.HiveVarchar;
0029 import org.apache.hadoop.hive.serde2.thrift.Type;
0030 import org.apache.hive.service.rpc.thrift.TBoolValue;
0031 import org.apache.hive.service.rpc.thrift.TByteValue;
0032 import org.apache.hive.service.rpc.thrift.TColumnValue;
0033 import org.apache.hive.service.rpc.thrift.TDoubleValue;
0034 import org.apache.hive.service.rpc.thrift.TI16Value;
0035 import org.apache.hive.service.rpc.thrift.TI32Value;
0036 import org.apache.hive.service.rpc.thrift.TI64Value;
0037 import org.apache.hive.service.rpc.thrift.TStringValue;
0038
0039 import org.apache.spark.unsafe.types.UTF8String;
0040
0041
0042
0043
0044
0045 public class ColumnValue {
0046
0047 private static TColumnValue booleanValue(Boolean value) {
0048 TBoolValue tBoolValue = new TBoolValue();
0049 if (value != null) {
0050 tBoolValue.setValue(value);
0051 }
0052 return TColumnValue.boolVal(tBoolValue);
0053 }
0054
0055 private static TColumnValue byteValue(Byte value) {
0056 TByteValue tByteValue = new TByteValue();
0057 if (value != null) {
0058 tByteValue.setValue(value);
0059 }
0060 return TColumnValue.byteVal(tByteValue);
0061 }
0062
0063 private static TColumnValue shortValue(Short value) {
0064 TI16Value tI16Value = new TI16Value();
0065 if (value != null) {
0066 tI16Value.setValue(value);
0067 }
0068 return TColumnValue.i16Val(tI16Value);
0069 }
0070
0071 private static TColumnValue intValue(Integer value) {
0072 TI32Value tI32Value = new TI32Value();
0073 if (value != null) {
0074 tI32Value.setValue(value);
0075 }
0076 return TColumnValue.i32Val(tI32Value);
0077 }
0078
0079 private static TColumnValue longValue(Long value) {
0080 TI64Value tI64Value = new TI64Value();
0081 if (value != null) {
0082 tI64Value.setValue(value);
0083 }
0084 return TColumnValue.i64Val(tI64Value);
0085 }
0086
0087 private static TColumnValue floatValue(Float value) {
0088 TDoubleValue tDoubleValue = new TDoubleValue();
0089 if (value != null) {
0090 tDoubleValue.setValue(value);
0091 }
0092 return TColumnValue.doubleVal(tDoubleValue);
0093 }
0094
0095 private static TColumnValue doubleValue(Double value) {
0096 TDoubleValue tDoubleValue = new TDoubleValue();
0097 if (value != null) {
0098 tDoubleValue.setValue(value);
0099 }
0100 return TColumnValue.doubleVal(tDoubleValue);
0101 }
0102
0103 private static TColumnValue stringValue(String value) {
0104 TStringValue tStringValue = new TStringValue();
0105 if (value != null) {
0106 tStringValue.setValue(value);
0107 }
0108 return TColumnValue.stringVal(tStringValue);
0109 }
0110
0111 private static TColumnValue stringValue(HiveChar value) {
0112 TStringValue tStringValue = new TStringValue();
0113 if (value != null) {
0114 tStringValue.setValue(value.toString());
0115 }
0116 return TColumnValue.stringVal(tStringValue);
0117 }
0118
0119 private static TColumnValue stringValue(HiveVarchar value) {
0120 TStringValue tStringValue = new TStringValue();
0121 if (value != null) {
0122 tStringValue.setValue(value.toString());
0123 }
0124 return TColumnValue.stringVal(tStringValue);
0125 }
0126
0127 private static TColumnValue stringValue(HiveIntervalYearMonth value) {
0128 TStringValue tStrValue = new TStringValue();
0129 if (value != null) {
0130 tStrValue.setValue(value.toString());
0131 }
0132 return TColumnValue.stringVal(tStrValue);
0133 }
0134
0135 private static TColumnValue stringValue(HiveIntervalDayTime value) {
0136 TStringValue tStrValue = new TStringValue();
0137 if (value != null) {
0138 tStrValue.setValue(value.toString());
0139 }
0140 return TColumnValue.stringVal(tStrValue);
0141 }
0142
0143 public static TColumnValue toTColumnValue(TypeDescriptor typeDescriptor, Object value) {
0144 Type type = typeDescriptor.getType();
0145
0146 switch (type) {
0147 case BOOLEAN_TYPE:
0148 return booleanValue((Boolean)value);
0149 case TINYINT_TYPE:
0150 return byteValue((Byte)value);
0151 case SMALLINT_TYPE:
0152 return shortValue((Short)value);
0153 case INT_TYPE:
0154 return intValue((Integer)value);
0155 case BIGINT_TYPE:
0156 return longValue((Long)value);
0157 case FLOAT_TYPE:
0158 return floatValue((Float)value);
0159 case DOUBLE_TYPE:
0160 return doubleValue((Double)value);
0161 case STRING_TYPE:
0162 return stringValue((String)value);
0163 case CHAR_TYPE:
0164 return stringValue((HiveChar)value);
0165 case VARCHAR_TYPE:
0166 return stringValue((HiveVarchar)value);
0167 case DATE_TYPE:
0168 case TIMESTAMP_TYPE:
0169
0170 return stringValue((String)value);
0171 case INTERVAL_YEAR_MONTH_TYPE:
0172 return stringValue((HiveIntervalYearMonth) value);
0173 case INTERVAL_DAY_TIME_TYPE:
0174 return stringValue((HiveIntervalDayTime) value);
0175 case DECIMAL_TYPE:
0176 String plainStr = value == null ? null : ((BigDecimal)value).toPlainString();
0177 return stringValue(plainStr);
0178 case BINARY_TYPE:
0179 String strVal = value == null ? null : UTF8String.fromBytes((byte[])value).toString();
0180 return stringValue(strVal);
0181 case ARRAY_TYPE:
0182 case MAP_TYPE:
0183 case STRUCT_TYPE:
0184 case UNION_TYPE:
0185 case USER_DEFINED_TYPE:
0186 return stringValue((String)value);
0187 case NULL_TYPE:
0188 return stringValue((String)value);
0189 default:
0190 return null;
0191 }
0192 }
0193
0194 private static Boolean getBooleanValue(TBoolValue tBoolValue) {
0195 if (tBoolValue.isSetValue()) {
0196 return tBoolValue.isValue();
0197 }
0198 return null;
0199 }
0200
0201 private static Byte getByteValue(TByteValue tByteValue) {
0202 if (tByteValue.isSetValue()) {
0203 return tByteValue.getValue();
0204 }
0205 return null;
0206 }
0207
0208 private static Short getShortValue(TI16Value tI16Value) {
0209 if (tI16Value.isSetValue()) {
0210 return tI16Value.getValue();
0211 }
0212 return null;
0213 }
0214
0215 private static Integer getIntegerValue(TI32Value tI32Value) {
0216 if (tI32Value.isSetValue()) {
0217 return tI32Value.getValue();
0218 }
0219 return null;
0220 }
0221
0222 private static Long getLongValue(TI64Value tI64Value) {
0223 if (tI64Value.isSetValue()) {
0224 return tI64Value.getValue();
0225 }
0226 return null;
0227 }
0228
0229 private static Double getDoubleValue(TDoubleValue tDoubleValue) {
0230 if (tDoubleValue.isSetValue()) {
0231 return tDoubleValue.getValue();
0232 }
0233 return null;
0234 }
0235
0236 private static String getStringValue(TStringValue tStringValue) {
0237 if (tStringValue.isSetValue()) {
0238 return tStringValue.getValue();
0239 }
0240 return null;
0241 }
0242
0243 private static Date getDateValue(TStringValue tStringValue) {
0244 if (tStringValue.isSetValue()) {
0245 return Date.valueOf(tStringValue.getValue());
0246 }
0247 return null;
0248 }
0249
0250 private static Timestamp getTimestampValue(TStringValue tStringValue) {
0251 if (tStringValue.isSetValue()) {
0252 return Timestamp.valueOf(tStringValue.getValue());
0253 }
0254 return null;
0255 }
0256
0257 private static byte[] getBinaryValue(TStringValue tString) {
0258 if (tString.isSetValue()) {
0259 return tString.getValue().getBytes();
0260 }
0261 return null;
0262 }
0263
0264 private static BigDecimal getBigDecimalValue(TStringValue tStringValue) {
0265 if (tStringValue.isSetValue()) {
0266 return new BigDecimal(tStringValue.getValue());
0267 }
0268 return null;
0269 }
0270
0271 public static Object toColumnValue(TColumnValue value) {
0272 TColumnValue._Fields field = value.getSetField();
0273 switch (field) {
0274 case BOOL_VAL:
0275 return getBooleanValue(value.getBoolVal());
0276 case BYTE_VAL:
0277 return getByteValue(value.getByteVal());
0278 case I16_VAL:
0279 return getShortValue(value.getI16Val());
0280 case I32_VAL:
0281 return getIntegerValue(value.getI32Val());
0282 case I64_VAL:
0283 return getLongValue(value.getI64Val());
0284 case DOUBLE_VAL:
0285 return getDoubleValue(value.getDoubleVal());
0286 case STRING_VAL:
0287 return getStringValue(value.getStringVal());
0288 }
0289 throw new IllegalArgumentException("never");
0290 }
0291 }