@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length != 1) { throw new UDFArgumentLengthException( "ADD10() requires 1 argument, got " + arguments.length); if (arguments[0].getCategory() != Category.PRIMITIVE) { throw new UDFArgumentException( "ADD10 only takes primitive types, got " + arguments[0].getTypeName()); inputType = argumentOI.getPrimitiveCategory(); ObjectInspector outputOI = null; switch (inputType) { case STRING: case DOUBLE: inputConverter = ObjectInspectorConverters.getConverter(arguments[0], PrimitiveObjectInspectorFactory.writableDoubleObjectInspector); outputOI = PrimitiveObjectInspectorFactory.writableDoubleObjectInspector; break; case DECIMAL: outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector( ((PrimitiveObjectInspector) arguments[0]).getTypeInfo()); break; default: throw new UDFArgumentException( "ADD10 only takes SHORT/BYTE/INT/LONG/DOUBLE/FLOAT/STRING/DECIMAL types, got " + inputType);
/** * Constructor. * * @param funcClass * The UDF or UDAF class. * @param argTypeInfos * The list of argument types that lead to an ambiguity. * @param methods * All potential matches. */ public UDFArgumentException(String message, Class<?> funcClass, List<TypeInfo> argTypeInfos, List<Method> methods) { super(getMessage(message, funcClass, argTypeInfos, methods)); this.funcClass = funcClass; this.argTypeInfos = argTypeInfos; this.methods = methods; }
private static String getMessage(String message, Class<?> funcClass, List<TypeInfo> argTypeInfos, List<Method> methods) { StringBuilder sb = new StringBuilder(); sb.append(message); if (methods != null) { // Sort the methods before omitting them. sortMethods(methods); sb.append(". Possible choices: "); for (Method m: methods) { Type[] types = m.getGenericParameterTypes(); sb.append("_FUNC_("); List<String> typeNames = new ArrayList<String>(types.length); for (int t = 0; t < types.length; t++) { if (t > 0) { sb.append(", "); } sb.append(ObjectInspectorUtils.getTypeNameFromJavaClass(types[t])); } sb.append(") "); } } return sb.toString(); }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length != 1) { throw new UDFArgumentLengthException( "CHARACTER_LENGTH requires 1 argument, got " + arguments.length); if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new UDFArgumentException( "CHARACTER_LENGTH only takes primitive types, got " + argumentOI.getTypeName()); PrimitiveObjectInspector.PrimitiveCategory inputType = argumentOI.getPrimitiveCategory(); ObjectInspector outputOI = null; switch (inputType) { throw new UDFArgumentException( " CHARACTER_LENGTH() only takes STRING/CHAR/VARCHAR/BINARY types as first argument, got " + inputType);
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length != 1) { throw new UDFArgumentException(opName + " requires one argument."); } Category category = arguments[0].getCategory(); if (category != Category.PRIMITIVE) { throw new UDFArgumentTypeException(0, "The " + GenericUDFUtils.getOrdinal(1) + " argument of " + opName + " is expected to a " + Category.PRIMITIVE.toString().toLowerCase() + " type, but " + category.toString().toLowerCase() + " is found"); } inputOI = (PrimitiveObjectInspector) arguments[0]; if (!FunctionRegistry.isNumericType(inputOI.getTypeInfo()) && (inputOI.getTypeInfo() != TypeInfoFactory.intervalDayTimeTypeInfo) && (inputOI.getTypeInfo() != TypeInfoFactory.intervalYearMonthTypeInfo)) { throw new UDFArgumentTypeException(0, "The " + GenericUDFUtils.getOrdinal(1) + " argument of " + opName + " is expected to be a " + "numeric or interval type, but " + inputOI.getTypeName() + " is found"); } PrimitiveTypeInfo resultTypeInfo = deriveResultTypeInfo(inputOI.getTypeInfo()); resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(resultTypeInfo); converter = ObjectInspectorConverters.getConverter(inputOI, resultOI); return resultOI; }
throw new UDFArgumentException("parse_url_tuple() takes at least two arguments: " + "the url string and a part name"); if (args[i].getCategory() != ObjectInspector.Category.PRIMITIVE || !args[i].getTypeName().equals(serdeConstants.STRING_TYPE_NAME)) { throw new UDFArgumentException("parse_url_tuple()'s arguments have to be string type"); return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length != 1) { throw new UDFArgumentLengthException( "LOWER requires 1 argument, got " + arguments.length); if (arguments[0].getCategory() != Category.PRIMITIVE) { throw new UDFArgumentException( "LOWER only takes primitive types, got " + arguments[0].getTypeName()); PrimitiveCategory inputType = argumentOI.getPrimitiveCategory(); ObjectInspector outputOI = null; BaseCharTypeInfo typeInfo; typeInfo = TypeInfoFactory.getCharTypeInfo( GenericUDFUtils.StringHelper.getFixedStringSizeForType(argumentOI)); outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo); break; case VARCHAR: typeInfo = TypeInfoFactory.getVarcharTypeInfo( GenericUDFUtils.StringHelper.getFixedStringSizeForType(argumentOI)); outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo); break; default:
@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."); throw new UDFArgumentTypeException(1, "The method name should be string type."); throw new UDFArgumentTypeException(1, "The method name should be a constant."); targetOI.getPrimitiveCategory()).primitiveJavaClass; returnOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector( typeEntry.primitiveCategory); returnObj = (Writable) returnOI.getPrimitiveWritableClass().newInstance(); } catch (Exception e) { throw new UDFArgumentException(e);
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { for (int idx = 0; idx < Math.min(arguments.length, 3); ++idx) { if (arguments[idx].getCategory() != Category.PRIMITIVE || PrimitiveObjectInspectorUtils.getPrimitiveGrouping( ((PrimitiveObjectInspector) arguments[idx]).getPrimitiveCategory()) != PrimitiveGrouping.STRING_GROUP) { throw new UDFArgumentException("All argument should be string/character type"); } } soi_text = ObjectInspectorConverters.getConverter(arguments[0], PrimitiveObjectInspectorFactory.javaStringObjectInspector); if (arguments.length > 1) { soi_de1 = ObjectInspectorConverters.getConverter(arguments[1], PrimitiveObjectInspectorFactory.javaStringObjectInspector); } if (arguments.length > 2) { soi_de2 = ObjectInspectorConverters.getConverter(arguments[2], PrimitiveObjectInspectorFactory.javaStringObjectInspector); } return ObjectInspectorFactory.getStandardMapObjectInspector( PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaStringObjectInspector); }
@Override public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException { if (args.length < 2) { throw new UDFArgumentException("UDTFReplicateRows() expects at least two arguments."); } if (!(args[0] instanceof LongObjectInspector)) { throw new UDFArgumentException( "The first argument to UDTFReplicateRows() must be a long (got " + args[0].getTypeName() + " instead)."); } ArrayList<String> fieldNames = new ArrayList<String>(); ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>(); for (int index = 0; index < args.length; ++index) { fieldNames.add("col" + index); fieldOIs.add(args[index]); } argOIs = fieldOIs; return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs); }
} else if (arguments.length == 2) { for (int i = 0; i < 2; i++) { if (arguments[i].getCategory() != Category.PRIMITIVE) { throw new UDFArgumentTypeException(0, "SURROGATE_KEY input only takes primitive types, got " + arguments[i].getTypeName()); throw new UDFArgumentException("Write ID bits must be between 1 and 62 (value: " + writeIdBits + ")"); throw new UDFArgumentException("Task ID bits must be between 1 and 62 (value: " + taskIdBits + ")"); throw new UDFArgumentException("Write ID bits + Task ID bits must be less than 63 (value: " + (writeIdBits + taskIdBits) + ")"); throw new UDFArgumentLengthException( "The function SURROGATE_KEY takes 0 or 2 integer arguments (write id bits, taks id bits), but found " + arguments.length);
@Override public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException { if (args.length != 1) { throw new UDFArgumentException("explode() takes only one argument"); } if (args[0].getCategory() != ObjectInspector.Category.LIST) { throw new UDFArgumentException("explode() takes an array as a parameter"); } listOI = (ListObjectInspector) args[0]; ArrayList<String> fieldNames = new ArrayList<String>(); ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>(); fieldNames.add("col1"); fieldNames.add("col2"); fieldOIs.add(listOI.getListElementObjectInspector()); fieldOIs.add(listOI.getListElementObjectInspector()); return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs); }
@Override public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException { if (args.length != 1) { throw new UDFArgumentException("posexplode() takes only one argument"); } if (args[0].getCategory() != ObjectInspector.Category.LIST) { throw new UDFArgumentException("posexplode() takes an array as a parameter"); } listOI = (ListObjectInspector) args[0]; ArrayList<String> fieldNames = new ArrayList<String>(); ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>(); fieldNames.add("pos"); fieldNames.add("val"); fieldOIs.add(PrimitiveObjectInspectorFactory .getPrimitiveJavaObjectInspector(PrimitiveCategory.INT)); fieldOIs.add(listOI.getListElementObjectInspector()); return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs); }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length != 1) { throw new UDFArgumentLengthException(String.format("%s needs 1 argument, got %d", udfName, arguments.length)); } if (arguments[0].getCategory() != Category.PRIMITIVE || ((PrimitiveObjectInspector) arguments[0]).getPrimitiveCategory() != inputType) { String actual = arguments[0].getCategory() + (arguments[0].getCategory() == Category.PRIMITIVE ? "[" + ((PrimitiveObjectInspector) arguments[0]).getPrimitiveCategory() + "]" : ""); throw new UDFArgumentException( String.format("%s only takes primitive type %s, got %s", udfName, inputType, actual)); } argumentOI = arguments[0]; return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(outputType); }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length != 2) { throw new UDFArgumentException(opName + " requires two arguments."); Category category = arguments[i].getCategory(); if (category != Category.PRIMITIVE) { throw new UDFArgumentTypeException(i, "The " + GenericUDFUtils.getOrdinal(i + 1) + " argument of " + opName + " is expected to a " resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector( deriveResultTypeInfo()); converterLeft = ObjectInspectorConverters.getConverter(leftOI, resultOI); converterRight = ObjectInspectorConverters.getConverter(rightOI, resultOI);
@Override public StructObjectInspector initialize(ObjectInspector[] ois) throws UDFArgumentException { //There should be one argument that is a array of struct if (ois.length!=1){ throw new UDFArgumentException("UDF tables only one argument"); } if (ois[0].getCategory()!= Category.LIST){ throw new UDFArgumentException("Top level object must be an array but " + "was "+ois[0].getTypeName()); } li = (ListObjectInspector) ois[0]; ObjectInspector sub=li.getListElementObjectInspector(); if (sub.getCategory() != Category.STRUCT){ throw new UDFArgumentException("The sub element must be struct, but was "+sub.getTypeName()); } return (StructObjectInspector) sub; }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length != 1) { throw new UDFArgumentException(udfName + " requires one value argument. Found :" + arguments.length); } PrimitiveObjectInspector argumentOI; if(arguments[0] instanceof PrimitiveObjectInspector) { argumentOI = (PrimitiveObjectInspector) arguments[0]; } else { throw new UDFArgumentException(udfName + " takes only primitive types. found " + arguments[0].getTypeName()); } switch (argumentOI.getPrimitiveCategory()) { case STRING: case CHAR: case VARCHAR: break; default: throw new UDFArgumentException(udfName + " takes only STRING/CHAR/VARCHAR types. Found " + argumentOI.getPrimitiveCategory()); } converter = new TextConverter(argumentOI); return PrimitiveObjectInspectorFactory.writableStringObjectInspector; }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length != 1) { throw new UDFArgumentException("Usage : map_key_values( map) "); } if (!arguments[0].getCategory().equals(Category.MAP)) { throw new UDFArgumentException("Usage : map_key_values( map) "); } moi = (MapObjectInspector) arguments[0]; //// List<String> structFieldNames = new ArrayList<String>(); List<ObjectInspector> structFieldObjectInspectors = new ArrayList<ObjectInspector>(); structFieldNames.add("key"); structFieldObjectInspectors.add(moi.getMapKeyObjectInspector()); structFieldNames.add("value"); structFieldObjectInspectors.add(moi.getMapValueObjectInspector()); ObjectInspector keyOI = ObjectInspectorFactory.getStandardStructObjectInspector(structFieldNames, structFieldObjectInspectors); ObjectInspector arrayOI = ObjectInspectorFactory.getStandardListObjectInspector(keyOI); return arrayOI; }
@Override public ObjectInspector initialize(ObjectInspector[] arg0) throws UDFArgumentException { ObjectInspector first = arg0[0]; if (first.getCategory() == Category.LIST) { listInspector = (ListObjectInspector) first; } else { throw new UDFArgumentException(" Expecting an array and an int as arguments "); } ObjectInspector second = arg0[1]; if (second.getCategory() == Category.PRIMITIVE) { PrimitiveObjectInspector secondPrim = (PrimitiveObjectInspector) second; if (secondPrim.getPrimitiveCategory() == PrimitiveCategory.INT) { intInspector = (IntObjectInspector) second; } else { throw new UDFArgumentException(" Expecting an array and an int as arguments "); } } else { throw new UDFArgumentException(" Expecting an array and an int as arguments "); } returnInspector = ObjectInspectorFactory.getStandardListObjectInspector( listInspector.getListElementObjectInspector()); return returnInspector; }
checkNotNull(bodyDesc, "bodyDesc"); if(colTypes.size() != arguments.length) { throw new UDFArgumentLengthException( "The macro " + macroName + " accepts exactly " + colTypes.size() + " arguments."); body = ExprNodeEvaluatorFactory.get(bodyDesc); } catch (HiveException ex) { throw new UDFArgumentException(ex); colObjectInspectors.add(objectInspector); converters[index] = ObjectInspectorConverters.getConverter(arguments[index], objectInspector); .getStandardStructObjectInspector(colNames, colObjectInspectors); try { return body.initialize(structOI); } catch (HiveException ex) { throw new UDFArgumentException(ex);