Search in sources :

Example 46 with DecimalTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo in project drill by apache.

the class HiveUtilities method convertPartitionType.

/** Partition value is received in string format. Convert it into appropriate object based on the type. */
public static Object convertPartitionType(TypeInfo typeInfo, String value, final String defaultPartitionValue) {
    if (typeInfo.getCategory() != Category.PRIMITIVE) {
        // In Hive only primitive types are allowed as partition column types.
        throw new DrillRuntimeException("Non-Primitive types are not allowed as partition column type in Hive, " + "but received one: " + typeInfo.getCategory());
    }
    if (defaultPartitionValue.equals(value)) {
        return null;
    }
    final PrimitiveCategory pCat = ((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory();
    try {
        switch(pCat) {
            case BINARY:
                return value.getBytes();
            case BOOLEAN:
                return Boolean.parseBoolean(value);
            case DECIMAL:
                {
                    DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) typeInfo;
                    return HiveDecimalUtils.enforcePrecisionScale(HiveDecimal.create(value), decimalTypeInfo.precision(), decimalTypeInfo.scale());
                }
            case DOUBLE:
                return Double.parseDouble(value);
            case FLOAT:
                return Float.parseFloat(value);
            case BYTE:
            case SHORT:
            case INT:
                return Integer.parseInt(value);
            case LONG:
                return Long.parseLong(value);
            case STRING:
            case VARCHAR:
                return value.getBytes();
            case CHAR:
                return value.trim().getBytes();
            case TIMESTAMP:
                return Timestamp.valueOf(value);
            case DATE:
                return Date.valueOf(value);
        }
    } catch (final Exception e) {
        // In Hive, partition values that can't be converted from string are considered to be NULL.
        logger.trace("Failed to interpret '{}' value from partition value string '{}'", pCat, value);
        return null;
    }
    throwUnsupportedHiveDataTypeError(pCat.toString());
    return null;
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) UserException(org.apache.drill.common.exceptions.UserException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) ExecutionSetupException(org.apache.drill.common.exceptions.ExecutionSetupException)

Aggregations

DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)40 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)14 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)14 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)13 CharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo)12 VarcharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo)12 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)11 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)10 IntWritable (org.apache.hadoop.io.IntWritable)10 Test (org.junit.Test)10 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)9 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)9 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)9 Timestamp (java.sql.Timestamp)8 Date (java.sql.Date)7 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)7 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)7 BooleanWritable (org.apache.hadoop.io.BooleanWritable)7 BytesWritable (org.apache.hadoop.io.BytesWritable)7 FloatWritable (org.apache.hadoop.io.FloatWritable)7