use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveTypeEntry in project hive by apache.
the class GenericUDFReflect2 method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length < 2) {
throw new UDFArgumentLengthException("The function GenericUDFReflect2(arg0,method[,arg1[,arg2]...])" + " accepts 2 or more arguments.");
}
if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
throw new UDFArgumentTypeException(1, "The target instance should be a primitive type.");
}
targetOI = (PrimitiveObjectInspector) arguments[0];
if (!(arguments[1] instanceof StringObjectInspector)) {
throw new UDFArgumentTypeException(1, "The method name should be string type.");
}
if (!(arguments[1] instanceof ConstantObjectInspector)) {
throw new UDFArgumentTypeException(1, "The method name should be a constant.");
}
Text methodName = (Text) ((ConstantObjectInspector) arguments[1]).getWritableConstantValue();
if (methodName.toString().equals("hashCode") && arguments.length == 2) {
// it's non-deterministic
throw new UDFArgumentTypeException(1, "Use hash() UDF instead of this.");
}
setupParameterOIs(arguments, 2);
Class<?> targetClass = PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveCategory(targetOI.getPrimitiveCategory()).primitiveJavaClass;
try {
method = findMethod(targetClass, methodName.toString(), null, true);
// Note: type param is not available here.
PrimitiveTypeEntry typeEntry = getTypeFor(method.getReturnType());
returnOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeEntry.primitiveCategory);
returnObj = (Writable) returnOI.getPrimitiveWritableClass().newInstance();
} catch (Exception e) {
throw new UDFArgumentException(e);
}
return returnOI;
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveTypeEntry in project hive by apache.
the class DynamicSerDe method dynamicSerDeStructBaseToObjectInspector.
public static ObjectInspector dynamicSerDeStructBaseToObjectInspector(DynamicSerDeTypeBase bt) throws SerDeException {
if (bt.isList()) {
return ObjectInspectorFactory.getStandardListObjectInspector(dynamicSerDeStructBaseToObjectInspector(((DynamicSerDeTypeList) bt).getElementType()));
} else if (bt.isMap()) {
DynamicSerDeTypeMap btMap = (DynamicSerDeTypeMap) bt;
return ObjectInspectorFactory.getStandardMapObjectInspector(dynamicSerDeStructBaseToObjectInspector(btMap.getKeyType()), dynamicSerDeStructBaseToObjectInspector(btMap.getValueType()));
} else if (bt.isPrimitive()) {
PrimitiveTypeEntry pte = PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveJavaClass(bt.getRealType());
return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(pte.primitiveCategory);
} else {
// Must be a struct
DynamicSerDeStructBase btStruct = (DynamicSerDeStructBase) bt;
DynamicSerDeFieldList fieldList = btStruct.getFieldList();
DynamicSerDeField[] fields = fieldList.getChildren();
ArrayList<String> fieldNames = new ArrayList<String>(fields.length);
ArrayList<ObjectInspector> fieldObjectInspectors = new ArrayList<ObjectInspector>(fields.length);
for (DynamicSerDeField field : fields) {
fieldNames.add(field.name);
fieldObjectInspectors.add(dynamicSerDeStructBaseToObjectInspector(field.getFieldType().getMyType()));
}
return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldObjectInspectors);
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveTypeEntry in project hive by apache.
the class TypeInfoFactory method createPrimitiveTypeInfo.
/**
* Create PrimitiveTypeInfo instance for the given full name of the type. The returned
* type is one of the parameterized type info such as VarcharTypeInfo.
*
* @param fullName Fully qualified name of the type
* @return PrimitiveTypeInfo instance
*/
private static PrimitiveTypeInfo createPrimitiveTypeInfo(String fullName) {
String baseName = TypeInfoUtils.getBaseName(fullName);
PrimitiveTypeEntry typeEntry = PrimitiveObjectInspectorUtils.getTypeEntryFromTypeName(baseName);
if (null == typeEntry) {
throw new RuntimeException("Unknown type " + fullName);
}
TypeInfoUtils.PrimitiveParts parts = TypeInfoUtils.parsePrimitiveParts(fullName);
if (parts.typeParams == null || parts.typeParams.length < 1) {
return null;
}
switch(typeEntry.primitiveCategory) {
case CHAR:
if (parts.typeParams.length != 1) {
return null;
}
return new CharTypeInfo(Integer.valueOf(parts.typeParams[0]));
case VARCHAR:
if (parts.typeParams.length != 1) {
return null;
}
return new VarcharTypeInfo(Integer.valueOf(parts.typeParams[0]));
case DECIMAL:
if (parts.typeParams.length != 2) {
return null;
}
return new DecimalTypeInfo(Integer.valueOf(parts.typeParams[0]), Integer.valueOf(parts.typeParams[1]));
default:
return null;
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveTypeEntry in project hive by apache.
the class PrimitiveObjectInspectorUtils method getJavaPrimitiveClassFromObjectInspector.
public static Class<?> getJavaPrimitiveClassFromObjectInspector(ObjectInspector oi) {
if (oi.getCategory() != Category.PRIMITIVE) {
return null;
}
PrimitiveObjectInspector poi = (PrimitiveObjectInspector) oi;
PrimitiveTypeEntry t = getTypeEntryFromPrimitiveCategory(poi.getPrimitiveCategory());
return t == null ? null : t.primitiveJavaClass;
}
Aggregations