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