use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo in project hive by apache.
the class GenericUDFUpper method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 1) {
throw new UDFArgumentLengthException("UPPER requires 1 argument, got " + arguments.length);
}
if (arguments[0].getCategory() != Category.PRIMITIVE) {
throw new UDFArgumentException("UPPER only takes primitive types, got " + arguments[0].getTypeName());
}
argumentOI = (PrimitiveObjectInspector) arguments[0];
stringConverter = new PrimitiveObjectInspectorConverter.StringConverter(argumentOI);
PrimitiveCategory inputType = argumentOI.getPrimitiveCategory();
ObjectInspector outputOI = null;
BaseCharTypeInfo typeInfo;
switch(inputType) {
case CHAR:
// return type should have same length as the input.
returnType = inputType;
typeInfo = TypeInfoFactory.getCharTypeInfo(GenericUDFUtils.StringHelper.getFixedStringSizeForType(argumentOI));
outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
break;
case VARCHAR:
// return type should have same length as the input.
returnType = inputType;
typeInfo = TypeInfoFactory.getVarcharTypeInfo(GenericUDFUtils.StringHelper.getFixedStringSizeForType(argumentOI));
outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
break;
default:
returnType = PrimitiveCategory.STRING;
outputOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
break;
}
returnHelper = new GenericUDFUtils.StringHelper(returnType);
return outputOI;
}
use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo in project hive by apache.
the class TestGenericUDFFloor method testVarchar.
@Test
public void testVarchar() throws HiveException {
GenericUDFFloor udf = new GenericUDFFloor();
HiveVarchar vc = new HiveVarchar("32300.004747", 12);
HiveVarcharWritable input = new HiveVarcharWritable(vc);
VarcharTypeInfo inputTypeInfo = TypeInfoFactory.getVarcharTypeInfo(12);
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(inputTypeInfo) };
DeferredObject[] args = { new DeferredJavaObject(input) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(TypeInfoFactory.longTypeInfo, oi.getTypeInfo());
LongWritable res = (LongWritable) udf.evaluate(args);
Assert.assertEquals(32300L, res.get());
}
use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo in project hive by apache.
the class GenericUDFConcat method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
// Loop through all the inputs to determine the appropriate return type/length.
// Return type:
// All CHAR inputs: return CHAR
// All VARCHAR inputs: return VARCHAR
// All CHAR/VARCHAR inputs: return VARCHAR
// All BINARY inputs: return BINARY
// Otherwise return STRING
argumentOIs = arguments;
PrimitiveCategory currentCategory;
PrimitiveObjectInspector poi;
boolean fixedLengthReturnValue = true;
// Only for char/varchar return types
int returnLength = 0;
for (int idx = 0; idx < arguments.length; ++idx) {
if (arguments[idx].getCategory() != Category.PRIMITIVE) {
throw new UDFArgumentException("CONCAT only takes primitive arguments");
}
poi = (PrimitiveObjectInspector) arguments[idx];
currentCategory = poi.getPrimitiveCategory();
if (idx == 0) {
returnType = currentCategory;
}
switch(currentCategory) {
case BINARY:
fixedLengthReturnValue = false;
if (returnType != currentCategory) {
// mix of binary/non-binary args
returnType = PrimitiveCategory.STRING;
}
break;
case CHAR:
case VARCHAR:
if (!fixedLengthReturnValue) {
returnType = PrimitiveCategory.STRING;
}
if (fixedLengthReturnValue && currentCategory == PrimitiveCategory.VARCHAR) {
returnType = PrimitiveCategory.VARCHAR;
}
break;
default:
returnType = PrimitiveCategory.STRING;
fixedLengthReturnValue = false;
break;
}
// max length for the char/varchar, then the return type reverts to string.
if (fixedLengthReturnValue) {
returnLength += GenericUDFUtils.StringHelper.getFixedStringSizeForType(poi);
if ((returnType == PrimitiveCategory.VARCHAR && returnLength > HiveVarchar.MAX_VARCHAR_LENGTH) || (returnType == PrimitiveCategory.CHAR && returnLength > HiveChar.MAX_CHAR_LENGTH)) {
returnType = PrimitiveCategory.STRING;
fixedLengthReturnValue = false;
}
}
}
if (returnType == PrimitiveCategory.BINARY) {
bw = new BytesWritable[arguments.length];
return PrimitiveObjectInspectorFactory.writableBinaryObjectInspector;
} else {
// treat all inputs as string, the return value will be converted to the appropriate type.
createStringConverters();
returnHelper = new GenericUDFUtils.StringHelper(returnType);
BaseCharTypeInfo typeInfo;
switch(returnType) {
case STRING:
return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
case CHAR:
typeInfo = TypeInfoFactory.getCharTypeInfo(returnLength);
return PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
case VARCHAR:
typeInfo = TypeInfoFactory.getVarcharTypeInfo(returnLength);
return PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
default:
throw new UDFArgumentException("Unexpected CONCAT return type of " + returnType);
}
}
}
Aggregations