* @param o
* @param targetPhoenixType
* @return Object
*/
public static Object castPigTypeToPhoenix(Object o, byte objectType, PDataType targetPhoenixType) {
PDataType inferredPType = getType(o, objectType);
if(inferredPType == null) {
return null;
}
if(inferredPType == PDataType.VARBINARY && targetPhoenixType != PDataType.VARBINARY) {
try {
o = castBytes(o, targetPhoenixType);
inferredPType = getType(o, DataType.findType(o));
} catch (IOException e) {
throw new RuntimeException("Error while casting bytes for object " +o);
}
}
if(inferredPType == PDataType.DATE) {
int inferredSqlType = targetPhoenixType.getSqlType();
if(inferredSqlType == Types.DATE) {
return new Date(((DateTime)o).getMillis());
}
if(inferredSqlType == Types.TIME) {
return new Time(((DateTime)o).getMillis());
}
if(inferredSqlType == Types.TIMESTAMP) {
return new Timestamp(((DateTime)o).getMillis());
}
}
if (targetPhoenixType == inferredPType || inferredPType.isCoercibleTo(targetPhoenixType)) {
return inferredPType.toObject(o, targetPhoenixType);
}
throw new RuntimeException(o.getClass().getName()
+ " cannot be coerced to "+targetPhoenixType.toString());
}