private Converter checkTextArguments(ObjectInspector[] arguments, int i) throws UDFArgumentException { if (arguments[i].getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new UDFArgumentTypeException(i, "Only primitive type arguments are accepted but " + arguments[i].getTypeName() + " is passed."); } Converter converter = ObjectInspectorConverters.getConverter((PrimitiveObjectInspector) arguments[i], PrimitiveObjectInspectorFactory.writableStringObjectInspector); return converter; }
private static void checkArgumentTypes(TypeInfo[] parameters) throws UDFArgumentTypeException { if (parameters.length != 2) { throw new UDFArgumentTypeException(parameters.length - 1, "Exactly two arguments are expected."); } if (parameters[0].getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted but " + parameters[0].getTypeName() + " is passed."); } if (parameters[1].getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new UDFArgumentTypeException(1, "Only primitive type arguments are accepted but " + parameters[1].getTypeName() + " is passed."); } if (!acceptedPrimitiveCategory(((PrimitiveTypeInfo) parameters[0]).getPrimitiveCategory())) { throw new UDFArgumentTypeException(0, "Only numeric type arguments are accepted but " + parameters[0].getTypeName() + " is passed."); } if (!acceptedPrimitiveCategory(((PrimitiveTypeInfo) parameters[1]).getPrimitiveCategory())) { throw new UDFArgumentTypeException(1, "Only numeric type arguments are accepted but " + parameters[1].getTypeName() + " is passed."); } }
@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; }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length != 1) { throw new UDFArgumentLengthException( "ASSERT_TRUE() expects one argument."); } if (arguments[0].getCategory() != Category.PRIMITIVE) { throw new UDFArgumentTypeException(0, "Argument to ASSERT_TRUE() should be primitive."); } conditionConverter = ObjectInspectorConverters.getConverter(arguments[0], PrimitiveObjectInspectorFactory.writableBooleanObjectInspector); return PrimitiveObjectInspectorFactory.writableVoidObjectInspector; }
@Override public GenericUDAFEvaluator getEvaluator(TypeInfo[] parameters) throws SemanticException { if (parameters.length != 1) { throw new IllegalArgumentException("Function only takes 1 parameter"); } else if (parameters[0].getCategory() != ObjectInspector.Category.PRIMITIVE && parameters[0].getCategory() != ObjectInspector.Category.STRUCT) { throw new UDFArgumentTypeException(1, "Only primitive/struct rows are accepted but " + parameters[0].getTypeName() + " was passed."); } return new CountApproximateDistinctEvaluator(); } }
@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."); throw new UDFArgumentTypeException(1, "Use hash() UDF instead of this."); targetOI.getPrimitiveCategory()).primitiveJavaClass; returnOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector( typeEntry.primitiveCategory); returnObj = (Writable) returnOI.getPrimitiveWritableClass().newInstance(); } catch (Exception e) { throw new UDFArgumentException(e);
protected void obtainIntConverter(ObjectInspector[] arguments, int i, PrimitiveCategory[] inputTypes, Converter[] converters) throws UDFArgumentTypeException { PrimitiveObjectInspector inOi = (PrimitiveObjectInspector) arguments[i]; PrimitiveCategory inputType = inOi.getPrimitiveCategory(); switch (inputType) { case BYTE: case SHORT: case INT: case VOID: break; default: throw new UDFArgumentTypeException(i, getFuncName() + " only takes INT/SHORT/BYTE types as " + getArgOrder(i) + " argument, got " + inputType); } Converter converter = ObjectInspectorConverters.getConverter( arguments[i], PrimitiveObjectInspectorFactory.writableIntObjectInspector); converters[i] = converter; inputTypes[i] = inputType; }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length != 1) { throw new UDFArgumentLengthException("The function MAP_KEYS only accepts one argument."); } else if (!(arguments[0] instanceof MapObjectInspector)) { throw new UDFArgumentTypeException(0, "\"" + Category.MAP.toString().toLowerCase() + "\" is expected at function MAP_KEYS, " + "but \"" + arguments[0].getTypeName() + "\" is found"); } mapOI = (MapObjectInspector) arguments[0]; ObjectInspector mapKeyOI = mapOI.getMapKeyObjectInspector(); return ObjectInspectorFactory.getStandardListObjectInspector(mapKeyOI); }
void setupParameterOIs(ObjectInspector[] arguments, int start) throws UDFArgumentTypeException { int length = arguments.length - start; parameterOIs = new PrimitiveObjectInspector[length]; parameterTypes = new PrimitiveTypeEntry[length]; parameterClasses = new Class[length]; parameterJavaValues = new Object[length]; for (int i = 0; i < length; i++) { if (arguments[i + start].getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new UDFArgumentTypeException(i, "The parameters of GenericUDFReflect(class,method[,arg1[,arg2]...])" + " must be primitive (int, double, string, etc)."); } parameterOIs[i] = (PrimitiveObjectInspector)arguments[i + start]; parameterTypes[i] = PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveCategory( parameterOIs[i].getPrimitiveCategory()); parameterClasses[i] = parameterTypes[i].primitiveJavaType == null ? parameterTypes[i].primitiveJavaClass : parameterTypes[i].primitiveJavaType; } }
@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 " + Category.PRIMITIVE.toString().toLowerCase() + " type, but " + category.toString().toLowerCase() + " is found"); resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector( deriveResultTypeInfo()); converterLeft = ObjectInspectorConverters.getConverter(leftOI, resultOI); converterRight = ObjectInspectorConverters.getConverter(rightOI, resultOI);
protected void checkIfPrimitive(ObjectInspector[] arguments, int i, String argOrder) throws UDFArgumentTypeException { ObjectInspector.Category oiCat = arguments[i].getCategory(); if (oiCat != ObjectInspector.Category.PRIMITIVE) { throw new UDFArgumentTypeException(i, getFuncName() + " only takes primitive types as " + argOrder + " argument, got " + oiCat); } }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length < 2) { throw new UDFArgumentException( "The function FIELD(str, str1, str2, ...) needs at least two arguments."); } argumentOIs = arguments; for (int i = 0; i < arguments.length; i++) { Category category = arguments[i].getCategory(); if (category != Category.PRIMITIVE) { throw new UDFArgumentTypeException(i, "The " + GenericUDFUtils.getOrdinal(i + 1) + " argument of function FIELD is expected to a " + Category.PRIMITIVE.toString().toLowerCase() + " type, but " + category.toString().toLowerCase() + " is found"); } } return PrimitiveObjectInspectorFactory.writableIntObjectInspector; }
private static void verifyFractionType(ObjectInspector oi) throws UDFArgumentTypeException { PrimitiveCategory pc = ((PrimitiveObjectInspector)oi).getPrimitiveCategory(); switch(pc) { case FLOAT: case DOUBLE: case DECIMAL: break; default: throw new UDFArgumentTypeException(1, "Only a floating point or decimal, or " + "floating point or decimal array argument is accepted as parameter 2, but " + pc + " was passed instead."); } }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { GenericUDFUtils.ReturnObjectInspectorResolver returnOIResolver = new GenericUDFUtils.ReturnObjectInspectorResolver(true); for (int i = 0; i < arguments.length; i++) { if (!returnOIResolver.update(arguments[i])) { throw new UDFArgumentTypeException(i, "Argument type \"" + arguments[i].getTypeName() + "\" is different from preceding arguments. " + "Previous type was \"" + arguments[i - 1].getTypeName() + "\""); } } converters = new Converter[arguments.length]; ObjectInspector returnOI = returnOIResolver.get(PrimitiveObjectInspectorFactory.javaStringObjectInspector); for (int i = 0; i < arguments.length; i++) { converters[i] = ObjectInspectorConverters.getConverter(arguments[i], returnOI); } return ObjectInspectorFactory.getStandardListObjectInspector(returnOI); }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (!arguments[0].getTypeName().equals("boolean")) { throw new UDFArgumentTypeException(0, "First argument for BETWEEN should be boolean type"); } egt.initialize(new ObjectInspector[] {arguments[1], arguments[2]}); elt.initialize(new ObjectInspector[] {arguments[1], arguments[3]}); argumentOIs = arguments; return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector; }
@Override public GenericUDAFEvaluator getEvaluator(TypeInfo[] parameters) throws SemanticException { if (parameters.length > 2) { throw new UDFArgumentTypeException(2, "At most 2 arguments expected"); } if (parameters.length > 1 && !parameters[1].equals(TypeInfoFactory.booleanTypeInfo)) { throw new UDFArgumentTypeException(1, "second argument must be a boolean expression"); } return createEvaluator(); }
} catch (UDFArgumentTypeException e) { throw new SemanticException(ErrorMsg.INVALID_ARGUMENT_TYPE.getMsg(expr .getChild(childrenBegin + e.getArgumentId()), e.getMessage()), e); } catch (UDFArgumentLengthException e) { throw new SemanticException(ErrorMsg.INVALID_ARGUMENT_LENGTH.getMsg( expr, e.getMessage()), e); } catch (UDFArgumentException e) { throw new SemanticException(ErrorMsg.INVALID_ARGUMENT.getMsg(expr, e .getMessage()), e);
@Override public GenericUDAFEvaluator getEvaluator(TypeInfo[] parameters) throws SemanticException { if (parameters.length != 0) { throw new UDFArgumentTypeException(parameters.length - 1, "No argument is expected."); } return new GenericUDAFRowNumberEvaluator(); }
public void testSoundexWrongType0() throws HiveException { @SuppressWarnings("resource") GenericUDFSoundex udf = new GenericUDFSoundex(); ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableIntObjectInspector; ObjectInspector[] arguments = { valueOI0 }; try { udf.initialize(arguments); assertTrue("soundex test. UDFArgumentTypeException is expected", false); } catch (UDFArgumentTypeException e) { assertEquals("soundex test", "soundex only takes STRING_GROUP types as 1st argument, got INT", e.getMessage()); } }
@Override public GenericUDAFEvaluator getEvaluator(TypeInfo[] parameters) throws SemanticException { if (parameters.length != 1) { throw new UDFArgumentTypeException(parameters.length - 1, "Exactly one argument is expected."); } if (parameters[0].getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted but " + parameters[0].getTypeName() + " is passed."); } switch (((PrimitiveTypeInfo) parameters[0]).getPrimitiveCategory()) { case LONG: return new SumZeroIfEmpty(); default: throw new UDFArgumentTypeException(0, "Only bigint type arguments are accepted but " + parameters[0].getTypeName() + " is passed."); } }