Search in sources :

Example 31 with GenericUDF

use of org.apache.hadoop.hive.ql.udf.generic.GenericUDF in project hive by apache.

the class TestVectorUDFAdaptor method testGenericUDF.

// test the UDF adaptor for a generic UDF (as opposed to a legacy UDF)
@Test
public void testGenericUDF() {
    // create a syntax tree for a function call 'myisnull(col0, "UNKNOWN")'
    ExprNodeGenericFuncDesc funcDesc;
    GenericUDF genericUDF = new GenericUDFIsNull();
    TypeInfo typeInfoStr = TypeInfoFactory.stringTypeInfo;
    List<ExprNodeDesc> children = new ArrayList<ExprNodeDesc>();
    children.add(new ExprNodeColumnDesc(typeInfoStr, "col0", "tablename", false));
    children.add(new ExprNodeConstantDesc(typeInfoStr, "UNKNOWN"));
    VectorUDFArgDesc[] argDescs = new VectorUDFArgDesc[2];
    for (int i = 0; i < 2; i++) {
        argDescs[i] = new VectorUDFArgDesc();
    }
    argDescs[0].setVariable(0);
    argDescs[1].setConstant((ExprNodeConstantDesc) children.get(1));
    funcDesc = new ExprNodeGenericFuncDesc(typeInfoStr, genericUDF, "myisnull", children);
    // create the adaptor for this function call to work in vector mode
    VectorUDFAdaptor vudf = null;
    try {
        vudf = new VectorUDFAdaptor(funcDesc, 3, "String", argDescs);
    } catch (HiveException e) {
        // We should never get here.
        assertTrue(false);
    }
    VectorizedRowBatch b;
    byte[] red = null;
    byte[] unknown = null;
    try {
        red = "red".getBytes("UTF-8");
        unknown = "UNKNOWN".getBytes("UTF-8");
    } catch (Exception e) {
        ;
    }
    BytesColumnVector out;
    // with nulls
    b = getBatchStrDblLongWithStrOut();
    b.cols[0].noNulls = false;
    // set 1st entry to null
    b.cols[0].isNull[0] = true;
    vudf.evaluate(b);
    out = (BytesColumnVector) b.cols[3];
    // verify outputs
    int cmp = StringExpr.compare(red, 0, red.length, out.vector[1], out.start[1], out.length[1]);
    assertEquals(0, cmp);
    cmp = StringExpr.compare(unknown, 0, unknown.length, out.vector[0], out.start[0], out.length[0]);
    assertEquals(0, cmp);
    // output entry should not be null for null input for this particular generic UDF
    assertTrue(out.noNulls || !out.isNull[0]);
}
Also used : ExprNodeConstantDesc(org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) ArrayList(java.util.ArrayList) ExprNodeGenericFuncDesc(org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc) GenericUDFIsNull(org.apache.hadoop.hive.ql.exec.vector.udf.generic.GenericUDFIsNull) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) VectorizedRowBatch(org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch) GenericUDF(org.apache.hadoop.hive.ql.udf.generic.GenericUDF) ExprNodeColumnDesc(org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc) BytesColumnVector(org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) Test(org.junit.Test)

Example 32 with GenericUDF

use of org.apache.hadoop.hive.ql.udf.generic.GenericUDF in project drill by apache.

the class HiveFunctionRegistry method matchAndCreateGenericUDFHolder.

private HiveFuncHolder matchAndCreateGenericUDFHolder(Class<? extends GenericUDF> udfClazz, MajorType[] argTypes, ObjectInspector[] argOIs) {
    // 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;
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) GenericUDF(org.apache.hadoop.hive.ql.udf.generic.GenericUDF) UserException(org.apache.drill.common.exceptions.UserException)

Example 33 with GenericUDF

use of org.apache.hadoop.hive.ql.udf.generic.GenericUDF in project drill by apache.

the class HiveFunctionRegistry method matchAndCreateUDFHolder.

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;
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) GenericUDF(org.apache.hadoop.hive.ql.udf.generic.GenericUDF) GenericUDFBridge(org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge) UserException(org.apache.drill.common.exceptions.UserException)

Aggregations

GenericUDF (org.apache.hadoop.hive.ql.udf.generic.GenericUDF)33 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)25 ExprNodeGenericFuncDesc (org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc)24 ArrayList (java.util.ArrayList)18 ExprNodeColumnDesc (org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc)17 Test (org.junit.Test)13 ExprNodeConstantDesc (org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc)12 GenericUDFBridge (org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge)6 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)6 VectorExpression (org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression)5 ExprNodeFieldDesc (org.apache.hadoop.hive.ql.plan.ExprNodeFieldDesc)5 GenericUDFBaseCompare (org.apache.hadoop.hive.ql.udf.generic.GenericUDFBaseCompare)5 DynamicValueVectorExpression (org.apache.hadoop.hive.ql.exec.vector.expressions.DynamicValueVectorExpression)4 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)4 GenericUDFOPAnd (org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPAnd)4 List (java.util.List)3 SelectOperator (org.apache.hadoop.hive.ql.exec.SelectOperator)3 GenericUDFOPEqualOrLessThan (org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPEqualOrLessThan)3 GenericUDFToUnixTimeStamp (org.apache.hadoop.hive.ql.udf.generic.GenericUDFToUnixTimeStamp)3 InputSplit (org.apache.hadoop.mapred.InputSplit)3