use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping in project hive by apache.
the class GenericUDFToDate method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length < 1) {
throw new UDFArgumentLengthException("The function CAST as DATE requires at least one argument, got " + arguments.length);
}
try {
argumentOI = (PrimitiveObjectInspector) arguments[0];
PrimitiveCategory pc = argumentOI.getPrimitiveCategory();
PrimitiveGrouping pg = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(pc);
switch(pg) {
case DATE_GROUP:
case STRING_GROUP:
case VOID_GROUP:
break;
default:
throw new UDFArgumentException("CAST as DATE only allows date,string, or timestamp types");
}
} catch (ClassCastException e) {
throw new UDFArgumentException("The function CAST as DATE takes only primitive types");
}
dc = new DateConverter(argumentOI, PrimitiveObjectInspectorFactory.writableDateObjectInspector);
return PrimitiveObjectInspectorFactory.writableDateObjectInspector;
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping in project hive by apache.
the class TypeInfoUtils method implicitConvertible.
/**
* Test if it's implicitly convertible for data comparison.
*/
public static boolean implicitConvertible(PrimitiveCategory from, PrimitiveCategory to) {
if (from == to) {
return true;
}
PrimitiveGrouping fromPg = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(from);
PrimitiveGrouping toPg = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(to);
// Allow implicit String to Double conversion
if (fromPg == PrimitiveGrouping.STRING_GROUP && to == PrimitiveCategory.DOUBLE) {
return true;
}
// Void can be converted to any type
if (from == PrimitiveCategory.VOID) {
return true;
}
// Allow implicit String to Date conversion
if (fromPg == PrimitiveGrouping.DATE_GROUP && toPg == PrimitiveGrouping.STRING_GROUP) {
return true;
}
// Allow implicit Numeric to String conversion
if (fromPg == PrimitiveGrouping.NUMERIC_GROUP && toPg == PrimitiveGrouping.STRING_GROUP) {
return true;
}
// Allow implicit String to varchar conversion, and vice versa
if (fromPg == PrimitiveGrouping.STRING_GROUP && toPg == PrimitiveGrouping.STRING_GROUP) {
return true;
}
// Allow implicit conversion from Byte -> Integer -> Long -> Float -> Double
// Decimal -> String
Integer f = numericTypes.get(from);
Integer t = numericTypes.get(to);
if (f == null || t == null) {
return false;
}
if (f.intValue() > t.intValue()) {
return false;
}
return true;
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping in project hive by apache.
the class FunctionRegistry method getCommonClassForComparison.
/**
* Find a common class that objects of both TypeInfo a and TypeInfo b can
* convert to. This is used for comparing objects of type a and type b.
*
* When we are comparing string and double, we will always convert both of
* them to double and then compare.
*
* @return null if no common class could be found.
*/
public static synchronized TypeInfo getCommonClassForComparison(TypeInfo a, TypeInfo b) {
// If same return one of them
if (a.equals(b)) {
return a;
}
if (a.getCategory() != Category.PRIMITIVE || b.getCategory() != Category.PRIMITIVE) {
return null;
}
PrimitiveCategory pcA = ((PrimitiveTypeInfo) a).getPrimitiveCategory();
PrimitiveCategory pcB = ((PrimitiveTypeInfo) b).getPrimitiveCategory();
if (pcA == pcB) {
// Rely on getTypeInfoForPrimitiveCategory() to sort out the type params.
return getTypeInfoForPrimitiveCategory((PrimitiveTypeInfo) a, (PrimitiveTypeInfo) b, pcA);
}
PrimitiveGrouping pgA = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(pcA);
PrimitiveGrouping pgB = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(pcB);
if (pgA == pgB) {
// grouping is same, but category is not.
if (pgA == PrimitiveGrouping.DATE_GROUP) {
// we got timestamp & date and timestamp has higher precedence than date
return TypeInfoFactory.timestampTypeInfo;
}
}
// handle string types properly
if (pgA == PrimitiveGrouping.STRING_GROUP && pgB == PrimitiveGrouping.STRING_GROUP) {
// Compare as strings. Char comparison semantics may be different if/when implemented.
return getTypeInfoForPrimitiveCategory((PrimitiveTypeInfo) a, (PrimitiveTypeInfo) b, PrimitiveCategory.STRING);
}
// timestamp/date is higher precedence than String_GROUP
if (pgA == PrimitiveGrouping.STRING_GROUP && pgB == PrimitiveGrouping.DATE_GROUP) {
return b;
}
// date/timestamp is higher precedence than String_GROUP
if (pgB == PrimitiveGrouping.STRING_GROUP && pgA == PrimitiveGrouping.DATE_GROUP) {
return a;
}
// Another special case, because timestamp is not implicitly convertible to numeric types.
if ((pgA == PrimitiveGrouping.NUMERIC_GROUP || pgB == PrimitiveGrouping.NUMERIC_GROUP) && (pcA == PrimitiveCategory.TIMESTAMP || pcB == PrimitiveCategory.TIMESTAMP)) {
return TypeInfoFactory.doubleTypeInfo;
}
for (PrimitiveCategory t : TypeInfoUtils.numericTypeList) {
if (TypeInfoUtils.implicitConvertible(pcA, t) && TypeInfoUtils.implicitConvertible(pcB, t)) {
return getTypeInfoForPrimitiveCategory((PrimitiveTypeInfo) a, (PrimitiveTypeInfo) b, t);
}
}
return null;
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping in project hive by apache.
the class GenericUDFNullif method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
argumentOIs = arguments;
checkArgsSize(arguments, 2, 2);
returnOIResolver = new GenericUDFUtils.ReturnObjectInspectorResolver(true);
returnOIResolver.update(arguments[0]);
boolean isPrimitive = (arguments[0] instanceof PrimitiveObjectInspector);
if (isPrimitive) {
PrimitiveObjectInspector primitive0 = (PrimitiveObjectInspector) arguments[0];
PrimitiveObjectInspector primitive1 = (PrimitiveObjectInspector) arguments[1];
PrimitiveGrouping pcat0 = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(primitive0.getPrimitiveCategory());
PrimitiveGrouping pcat1 = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(primitive1.getPrimitiveCategory());
if (pcat0 == PrimitiveGrouping.VOID_GROUP) {
throw new UDFArgumentTypeException(0, "NULLIF may not accept types belonging to " + pcat0 + " as first argument");
}
if (pcat1 != PrimitiveGrouping.VOID_GROUP && pcat0 != pcat1) {
throw new UDFArgumentTypeException(1, "The expressions after NULLIF should belong to the same category: \"" + pcat0 + "\" is expected but \"" + pcat1 + "\" is found");
}
} else {
String typeName0 = arguments[0].getTypeName();
String typeName1 = arguments[1].getTypeName();
if (!typeName0.equals(typeName1)) {
throw new UDFArgumentTypeException(1, "The expressions after NULLIF should all have the same type: \"" + typeName0 + "\" is expected but \"" + typeName1 + "\" is found");
}
}
return returnOIResolver.get();
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping in project hive by apache.
the class GenericUDF method checkArgGroups.
protected void checkArgGroups(ObjectInspector[] arguments, int i, PrimitiveCategory[] inputTypes, PrimitiveGrouping... grps) throws UDFArgumentTypeException {
PrimitiveCategory inputType = ((PrimitiveObjectInspector) arguments[i]).getPrimitiveCategory();
for (PrimitiveGrouping grp : grps) {
if (PrimitiveObjectInspectorUtils.getPrimitiveGrouping(inputType) == grp) {
inputTypes[i] = inputType;
return;
}
}
// build error message
StringBuilder sb = new StringBuilder();
sb.append(getFuncName());
sb.append(" only takes ");
sb.append(grps[0]);
for (int j = 1; j < grps.length; j++) {
sb.append(", ");
sb.append(grps[j]);
}
sb.append(" types as ");
sb.append(getArgOrder(i));
sb.append(" argument, got ");
sb.append(inputType);
throw new UDFArgumentTypeException(i, sb.toString());
}
Aggregations