private HiveFuncHolder matchAndCreateGenericUDFHolder(Class<? extends GenericUDF> udfClazz, MajorType[] argTypes, ObjectInspector[] argOIs) { // probe UDF to find if the arg types and acceptable // if acceptable create a holder object try { GenericUDF udfInstance = udfClazz.newInstance(); ObjectInspector returnOI = udfInstance.initialize(argOIs); return new HiveFuncHolder( udfClazz, argTypes, returnOI, Types.optional(ObjectInspectorHelper.getDrillType(returnOI)), nonDeterministicUDFs.contains(udfClazz)); } catch (IllegalAccessException | InstantiationException e) { logger.debug("Failed to instantiate class", e); } catch (Exception e) { /*ignore this*/ } return null; }
private TypeInfo getOutputTypeInfo(GenericUDF genericUdfClone, List<ObjectInspector> objectInspectorList) throws HiveException { ObjectInspector[] array = objectInspectorList.toArray(new ObjectInspector[objectInspectorList.size()]); ObjectInspector outputObjectInspector = genericUdfClone.initialize(array); return TypeInfoUtils.getTypeInfoFromObjectInspector(outputObjectInspector); }
private TypeInfo getOutputTypeInfo(GenericUDF genericUdfClone, List<ObjectInspector> objectInspectorList) throws HiveException { ObjectInspector[] array = objectInspectorList.toArray(new ObjectInspector[objectInspectorList.size()]); ObjectInspector outputObjectInspector = genericUdfClone.initialize(array); return TypeInfoUtils.getTypeInfoFromObjectInspector(outputObjectInspector); }
@Test(expected = UDFArgumentException.class) public void initialize_UnionNotAUnionThrowsException() throws UDFArgumentException { underTest.initialize(new ObjectInspector[] { PrimitiveObjectInspectorFactory.writableIntObjectInspector, tagOI }); }
private TypeInfo getOutputTypeInfo(GenericUDF genericUdfClone, List<ObjectInspector> objectInspectorList) throws HiveException { ObjectInspector[] array = objectInspectorList.toArray(new ObjectInspector[objectInspectorList.size()]); ObjectInspector outputObjectInspector = genericUdfClone.initialize(array); return TypeInfoUtils.getTypeInfoFromObjectInspector(outputObjectInspector); }
@Test(expected = UDFArgumentException.class) public void initialize_TooManyArgumentsThrowsException() throws UDFArgumentException { underTest.initialize( new ObjectInspector[] { unionOI, tagOI, PrimitiveObjectInspectorFactory.writableIntObjectInspector }); }
private HiveFuncHolder matchAndCreateUDFHolder(String udfName, Class<? extends UDF> udfClazz, MajorType[] argTypes, ObjectInspector[] argOIs) { try { GenericUDF udfInstance = new GenericUDFBridge(udfName, false/* is operator */, udfClazz.getName()); ObjectInspector returnOI = udfInstance.initialize(argOIs); return new HiveFuncHolder( udfName, udfClazz, argTypes, returnOI, Types.optional(ObjectInspectorHelper.getDrillType(returnOI)), nonDeterministicUDFs.contains(udfClazz)); } catch (Exception e) { /*ignore this*/ } return null; }
@Test(expected = UDFArgumentException.class) public void initialize_TagNotAnIntThrowsException() throws UDFArgumentException { underTest.initialize(new ObjectInspector[] { unionOI, PrimitiveObjectInspectorFactory.writableIntObjectInspector }); }
@Test(expected = UDFArgumentException.class) public void initialize_NoArgumentsThrowsException() throws UDFArgumentException { underTest.initialize(new ObjectInspector[] {}); }
@Test(expected = UDFArgumentException.class) public void initialize_NegativeTagThrowsException() throws UDFArgumentException { when(tagOI.getWritableConstantValue()).thenReturn(new IntWritable(-1)); underTest.initialize(new ObjectInspector[] { unionOI, tagOI }); }
@Test(expected = UDFArgumentException.class) public void initialize_OutOfBoundsTagThrowsException() throws UDFArgumentException { when(unionOI.getObjectInspectors()).thenReturn( ImmutableList.<ObjectInspector> of( PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableLongObjectInspector)); when(tagOI.getWritableConstantValue()).thenReturn(new IntWritable(2)); underTest.initialize(new ObjectInspector[] { unionOI, tagOI }); }
private void runAndVerify(String expResult, GenericUDF udf, ObjectInspector[] initArgs, DeferredObject[] evalArgs) throws HiveException { udf.initialize(initArgs); Text output = (Text) udf.evaluate(evalArgs); assertEquals("frist_day() test ", expResult, output.toString()); } }
@Test public void initialize_UnionAndTagArguments() throws UDFArgumentException { when(unionOI.getObjectInspectors()).thenReturn( ImmutableList.<ObjectInspector> of( PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableLongObjectInspector)); when(tagOI.getWritableConstantValue()).thenReturn(new IntWritable(0)); ObjectInspector result = underTest.initialize(new ObjectInspector[] { unionOI, tagOI }); assertThat(result, is((ObjectInspector) PrimitiveObjectInspectorFactory.writableStringObjectInspector)); }
@Test public void initialize_SingleArgumentOnly() throws UDFArgumentException { when(objectInspectorConverter.convert(unionOI)).thenReturn(initialiseResult); ObjectInspector result = underTest.initialize(new ObjectInspector[] { unionOI }); assertThat(result, is(initialiseResult)); }
@Test public void evaluate_UnionAndTagArguments_NotMatchesTag() throws HiveException { when(unionOI.getObjectInspectors()).thenReturn( ImmutableList.<ObjectInspector> of( PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableLongObjectInspector)); when(tagOI.getWritableConstantValue()).thenReturn(new IntWritable(0)); when(deferredObject.get()).thenReturn(value); when(unionOI.getTag(value)).thenReturn((byte) 1); underTest.initialize(new ObjectInspector[] { unionOI, tagOI }); Object result = underTest.evaluate(new DeferredObject[] { deferredObject }); assertThat(result, is(nullValue())); }
@Test public void evaluate_SingleArgumentOnly() throws HiveException { when(deferredObject.get()).thenReturn(value); when(valueConverter.convert(value, unionOI)).thenReturn(converted); underTest.initialize(new ObjectInspector[] { unionOI }); Object result = underTest.evaluate(new DeferredObject[] { deferredObject }); assertThat(result, is(converted)); }
@Test public void evaluate_UnionAndTagArguments_MatchesTag() throws HiveException { when(unionOI.getObjectInspectors()).thenReturn( ImmutableList.<ObjectInspector> of( PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableLongObjectInspector)); when(tagOI.getWritableConstantValue()).thenReturn(new IntWritable(0)); when(deferredObject.get()).thenReturn(value); when(unionOI.getTag(value)).thenReturn((byte) 0); when(unionOI.getField(value)).thenReturn("foo"); underTest.initialize(new ObjectInspector[] { unionOI, tagOI }); Object result = underTest.evaluate(new DeferredObject[] { deferredObject }); assertThat(result, is((Object) "foo")); }
public void init() throws HiveException, UDFArgumentException { genericUDF = expr.getGenericUDF(); deferredChildren = new GenericUDF.DeferredObject[expr.getChildren().size()]; childrenOIs = new ObjectInspector[expr.getChildren().size()]; writers = VectorExpressionWriterFactory.getExpressionWriters(expr.getChildren()); for (int i = 0; i < childrenOIs.length; i++) { childrenOIs[i] = writers[i].getObjectInspector(); } MapredContext context = MapredContext.get(); if (context != null) { context.setup(genericUDF); } outputTypeInfo = expr.getTypeInfo(); outputVectorAssignRow = new VectorAssignRow(); outputVectorAssignRow.init(outputTypeInfo, outputColumnNum); genericUDF.initialize(childrenOIs); // Initialize constant arguments for (int i = 0; i < argDescs.length; i++) { if (argDescs[i].isConstant()) { argDescs[i].prepareConstant(); } } }
public void init() throws HiveException, UDFArgumentException { genericUDF = expr.getGenericUDF(); deferredChildren = new GenericUDF.DeferredObject[expr.getChildren().size()]; childrenOIs = new ObjectInspector[expr.getChildren().size()]; writers = VectorExpressionWriterFactory.getExpressionWriters(expr.getChildren()); for (int i = 0; i < childrenOIs.length; i++) { childrenOIs[i] = writers[i].getObjectInspector(); } MapredContext context = MapredContext.get(); if (context != null) { context.setup(genericUDF); } outputOI = VectorExpressionWriterFactory.genVectorExpressionWritable(expr) .getObjectInspector(); genericUDF.initialize(childrenOIs); // Initialize constant arguments for (int i = 0; i < argDescs.length; i++) { if (argDescs[i].isConstant()) { argDescs[i].prepareConstant(); } } }
protected void verifyReturnType(GenericUDF udf, String typeStr1, String typeStr2, String expectedTypeStr) throws HiveException { // Lookup type infos for our input types and expected return type PrimitiveTypeInfo type1 = TypeInfoFactory.getPrimitiveTypeInfo(typeStr1); PrimitiveTypeInfo type2 = TypeInfoFactory.getPrimitiveTypeInfo(typeStr2); PrimitiveTypeInfo expectedType = TypeInfoFactory.getPrimitiveTypeInfo(expectedTypeStr); // Initialize UDF which will output the return type for the UDF. ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(type1), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(type2) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals("Return type for " + udf.getDisplayString(new String[] {typeStr1, typeStr2}), expectedType, oi.getTypeInfo()); } }