use of org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException in project hive by apache.
the class GenericUDFBaseNwayCompare method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length < 2) {
throw new UDFArgumentLengthException(getFuncName() + " requires at least 2 arguments, got " + arguments.length);
}
if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
throw new UDFArgumentException(getFuncName() + " only takes primitive types, got " + arguments[0].getTypeName());
}
argumentOIs = arguments;
converters = new Converter[arguments.length];
TypeInfo commonInfo = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]);
for (int i = 1; i < arguments.length; i++) {
PrimitiveTypeInfo currInfo = (PrimitiveTypeInfo) TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[i]);
commonInfo = FunctionRegistry.getCommonClassForComparison(commonInfo, currInfo);
}
resultOI = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo((commonInfo == null) ? TypeInfoFactory.doubleTypeInfo : commonInfo);
for (int i = 0; i < arguments.length; i++) {
converters[i] = ObjectInspectorConverters.getConverter(arguments[i], resultOI);
}
return resultOI;
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException in project hive by apache.
the class GenericUDFNamedStruct method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
int numFields = arguments.length;
if (numFields % 2 == 1) {
throw new UDFArgumentLengthException("NAMED_STRUCT expects an even number of arguments.");
}
ret = new Object[numFields / 2];
ArrayList<String> fname = new ArrayList<String>(numFields / 2);
ArrayList<ObjectInspector> retOIs = new ArrayList<ObjectInspector>(numFields / 2);
for (int f = 0; f < numFields; f += 2) {
if (!(arguments[f] instanceof ConstantObjectInspector)) {
throw new UDFArgumentTypeException(f, "Even arguments" + " to NAMED_STRUCT must be a constant STRING." + arguments[f].toString());
}
ConstantObjectInspector constantOI = (ConstantObjectInspector) arguments[f];
fname.add(constantOI.getWritableConstantValue().toString());
retOIs.add(arguments[f + 1]);
}
StructObjectInspector soi = ObjectInspectorFactory.getStandardStructObjectInspector(fname, retOIs);
return soi;
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException in project hive by apache.
the class GenericUDFIndex method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 2) {
throw new UDFArgumentLengthException("The function INDEX accepts exactly 2 arguments.");
}
if (arguments[0] instanceof MapObjectInspector) {
// index into a map
mapOI = (MapObjectInspector) arguments[0];
listOI = null;
} else if (arguments[0] instanceof ListObjectInspector) {
// index into a list
listOI = (ListObjectInspector) arguments[0];
mapOI = null;
} else {
throw new UDFArgumentTypeException(0, "\"" + Category.MAP.toString().toLowerCase() + "\" or \"" + Category.LIST.toString().toLowerCase() + "\" is expected at function INDEX, but \"" + arguments[0].getTypeName() + "\" is found");
}
// index has to be a primitive
if (!(arguments[1] instanceof PrimitiveObjectInspector)) {
throw new UDFArgumentTypeException(1, "Primitive Type is expected but " + arguments[1].getTypeName() + "\" is found");
}
PrimitiveObjectInspector inputOI = (PrimitiveObjectInspector) arguments[1];
ObjectInspector returnOI;
ObjectInspector indexOI;
if (mapOI != null) {
indexOI = ObjectInspectorConverters.getConvertedOI(inputOI, mapOI.getMapKeyObjectInspector());
returnOI = mapOI.getMapValueObjectInspector();
} else {
indexOI = PrimitiveObjectInspectorFactory.writableIntObjectInspector;
returnOI = listOI.getListElementObjectInspector();
}
converter = ObjectInspectorConverters.getConverter(inputOI, indexOI);
return returnOI;
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException in project hive by apache.
the class GenericUDFCurrentDate method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 0) {
throw new UDFArgumentLengthException("The function CURRENT_DATE does not take any arguments, but found " + arguments.length);
}
if (currentDate == null) {
Date dateVal = Date.valueOf(SessionState.get().getQueryCurrentTimestamp().toString().substring(0, 10));
currentDate = new DateWritable(dateVal);
}
return PrimitiveObjectInspectorFactory.writableDateObjectInspector;
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException in project hive by apache.
the class GenericUDFLength method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 1) {
throw new UDFArgumentLengthException("LENGTH requires 1 argument, got " + arguments.length);
}
if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
throw new UDFArgumentException("LENGTH only takes primitive types, got " + argumentOI.getTypeName());
}
argumentOI = (PrimitiveObjectInspector) arguments[0];
PrimitiveObjectInspector.PrimitiveCategory inputType = argumentOI.getPrimitiveCategory();
ObjectInspector outputOI = null;
switch(inputType) {
case CHAR:
case VARCHAR:
case STRING:
isInputString = true;
stringConverter = new PrimitiveObjectInspectorConverter.StringConverter(argumentOI);
break;
case BINARY:
isInputString = false;
binaryConverter = new PrimitiveObjectInspectorConverter.BinaryConverter(argumentOI, PrimitiveObjectInspectorFactory.writableBinaryObjectInspector);
break;
default:
throw new UDFArgumentException(" LENGTH() only takes STRING/CHAR/VARCHAR/BINARY types as first argument, got " + inputType);
}
outputOI = PrimitiveObjectInspectorFactory.writableIntObjectInspector;
return outputOI;
}
Aggregations