Search in sources :

Example 1 with SettableHiveCharObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveCharObjectInspector in project hive by apache.

the class GenericUDFToChar method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 1) {
        throw new UDFArgumentException("CHAR cast requires a value argument");
    }
    try {
        argumentOI = (PrimitiveObjectInspector) arguments[0];
    } catch (ClassCastException e) {
        throw new UDFArgumentException("The function CHAR takes only primitive types");
    }
    // Check if this UDF has been provided with type params for the output char type
    SettableHiveCharObjectInspector outputOI;
    outputOI = (SettableHiveCharObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
    converter = new HiveCharConverter(argumentOI, outputOI);
    return outputOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) HiveCharConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.HiveCharConverter) SettableHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveCharObjectInspector)

Example 2 with SettableHiveCharObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveCharObjectInspector in project hive by apache.

the class GenericUDFCastFormat method convert.

private Object convert(Object o) throws HiveException {
    Object input;
    switch(inputOI.getPrimitiveCategory()) {
        case STRING:
            input = ((StringObjectInspector) inputOI).getPrimitiveJavaObject(o);
            break;
        case CHAR:
            input = ((HiveCharObjectInspector) inputOI).getPrimitiveJavaObject(o).getStrippedValue();
            break;
        case VARCHAR:
            input = ((HiveVarcharObjectInspector) inputOI).getPrimitiveJavaObject(o).toString();
            break;
        case TIMESTAMP:
            input = ((TimestampObjectInspector) inputOI).getPrimitiveWritableObject(o).getTimestamp();
            break;
        case DATE:
            input = ((DateObjectInspector) inputOI).getPrimitiveWritableObject(o).get();
            break;
        default:
            throw new HiveException("Input type " + inputOI.getPrimitiveCategory() + " not valid");
    }
    // format here
    Object formattedOutput = null;
    if (inputOI.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.DATE || inputOI.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.TIMESTAMP) {
        if (inputOI.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.DATE) {
            try {
                formattedOutput = formatter.format((Date) input);
            } catch (IllegalArgumentException e) {
                return null;
            }
        } else {
            try {
                formattedOutput = formatter.format((Timestamp) input);
            } catch (IllegalArgumentException e) {
                return null;
            }
        }
        if (formattedOutput == null) {
            return null;
        }
    }
    // parse and create Writables
    switch(outputOI.getPrimitiveCategory()) {
        case STRING:
            return new Text((String) formattedOutput);
        case CHAR:
            return ((SettableHiveCharObjectInspector) outputOI).create(new HiveChar((String) formattedOutput, -1));
        case VARCHAR:
            return ((SettableHiveVarcharObjectInspector) outputOI).create(new HiveVarchar((String) formattedOutput, -1));
        case TIMESTAMP:
            try {
                Timestamp t = formatter.parseTimestamp((String) input);
                if (t == null) {
                    return null;
                }
                return ((SettableTimestampObjectInspector) outputOI).create(t);
            } catch (IllegalArgumentException e) {
                return null;
            }
        case DATE:
            try {
                Date d = formatter.parseDate((String) input);
                if (d == null) {
                    return null;
                }
                return ((SettableDateObjectInspector) outputOI).create(d);
            } catch (IllegalArgumentException e) {
                return null;
            }
        default:
            throw new HiveException("Output type " + outputOI.getPrimitiveCategory() + " not valid");
    }
}
Also used : DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) SettableDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableDateObjectInspector) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) Text(org.apache.hadoop.io.Text) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) Date(org.apache.hadoop.hive.common.type.Date) SettableTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableTimestampObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) HiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveVarcharObjectInspector) SettableHiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveVarcharObjectInspector) SettableTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableTimestampObjectInspector) SettableHiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveVarcharObjectInspector) HiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector) SettableHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveCharObjectInspector) SettableDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableDateObjectInspector) SettableHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveCharObjectInspector)

Aggregations

SettableHiveCharObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveCharObjectInspector)2 Date (org.apache.hadoop.hive.common.type.Date)1 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)1 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)1 Timestamp (org.apache.hadoop.hive.common.type.Timestamp)1 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)1 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)1 DateObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector)1 HiveCharObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector)1 HiveVarcharObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveVarcharObjectInspector)1 HiveCharConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.HiveCharConverter)1 SettableDateObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableDateObjectInspector)1 SettableHiveVarcharObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveVarcharObjectInspector)1 SettableTimestampObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableTimestampObjectInspector)1 TimestampObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector)1 Text (org.apache.hadoop.io.Text)1