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;
}
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");
}
}
Aggregations