Search in sources :

Example 6 with GenericUDFBridge

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

the class TestVectorizationContext method testMathFunctions.

@Test
public void testMathFunctions() throws HiveException {
    ExprNodeGenericFuncDesc mathFuncExpr = new ExprNodeGenericFuncDesc();
    mathFuncExpr.setTypeInfo(TypeInfoFactory.doubleTypeInfo);
    ExprNodeColumnDesc colDesc1 = new ExprNodeColumnDesc(Integer.class, "a", "table", false);
    ExprNodeColumnDesc colDesc2 = new ExprNodeColumnDesc(Double.class, "b", "table", false);
    List<ExprNodeDesc> children1 = new ArrayList<ExprNodeDesc>();
    List<ExprNodeDesc> children2 = new ArrayList<ExprNodeDesc>();
    children1.add(colDesc1);
    children2.add(colDesc2);
    List<String> columns = new ArrayList<String>();
    columns.add("b");
    columns.add("a");
    VectorizationContext vc = new VectorizationContext("name", columns);
    // Sin(double)
    GenericUDFBridge gudfBridge = new GenericUDFBridge("sin", false, UDFSin.class.getName());
    mathFuncExpr.setGenericUDF(gudfBridge);
    mathFuncExpr.setChildren(children2);
    VectorExpression ve = vc.getVectorExpression(mathFuncExpr, VectorExpressionDescriptor.Mode.PROJECTION);
    Assert.assertEquals(FuncSinDoubleToDouble.class, ve.getClass());
    // Round without digits
    GenericUDFRound udfRound = new GenericUDFRound();
    mathFuncExpr.setGenericUDF(udfRound);
    mathFuncExpr.setChildren(children2);
    ve = vc.getVectorExpression(mathFuncExpr);
    Assert.assertEquals(FuncRoundDoubleToDouble.class, ve.getClass());
    // BRound without digits
    GenericUDFBRound udfBRound = new GenericUDFBRound();
    mathFuncExpr.setGenericUDF(udfBRound);
    ve = vc.getVectorExpression(mathFuncExpr);
    Assert.assertEquals(FuncBRoundDoubleToDouble.class, ve.getClass());
    // Round with digits
    mathFuncExpr.setGenericUDF(udfRound);
    children2.add(new ExprNodeConstantDesc(4));
    mathFuncExpr.setChildren(children2);
    ve = vc.getVectorExpression(mathFuncExpr);
    Assert.assertEquals(RoundWithNumDigitsDoubleToDouble.class, ve.getClass());
    Assert.assertEquals(4, ((RoundWithNumDigitsDoubleToDouble) ve).getDecimalPlaces().get());
    // BRound with digits
    mathFuncExpr.setGenericUDF(udfBRound);
    ve = vc.getVectorExpression(mathFuncExpr);
    Assert.assertEquals(BRoundWithNumDigitsDoubleToDouble.class, ve.getClass());
    Assert.assertEquals(4, ((BRoundWithNumDigitsDoubleToDouble) ve).getDecimalPlaces().get());
    // Logger with int base
    gudfBridge = new GenericUDFBridge("log", false, UDFLog.class.getName());
    mathFuncExpr.setGenericUDF(gudfBridge);
    children2.clear();
    children2.add(new ExprNodeConstantDesc(4.0));
    children2.add(colDesc2);
    mathFuncExpr.setChildren(children2);
    ve = vc.getVectorExpression(mathFuncExpr);
    Assert.assertEquals(FuncLogWithBaseDoubleToDouble.class, ve.getClass());
    Assert.assertTrue(4 == ((FuncLogWithBaseDoubleToDouble) ve).getBase());
    // Logger with default base
    children2.clear();
    children2.add(colDesc2);
    mathFuncExpr.setChildren(children2);
    ve = vc.getVectorExpression(mathFuncExpr);
    Assert.assertEquals(FuncLnDoubleToDouble.class, ve.getClass());
    // Log with double base
    children2.clear();
    children2.add(new ExprNodeConstantDesc(4.5));
    children2.add(colDesc2);
    mathFuncExpr.setChildren(children2);
    ve = vc.getVectorExpression(mathFuncExpr);
    Assert.assertEquals(FuncLogWithBaseDoubleToDouble.class, ve.getClass());
    Assert.assertTrue(4.5 == ((FuncLogWithBaseDoubleToDouble) ve).getBase());
    // Log with int input and double base
    children2.clear();
    children2.add(new ExprNodeConstantDesc(4.5));
    children2.add(colDesc1);
    mathFuncExpr.setChildren(children2);
    ve = vc.getVectorExpression(mathFuncExpr);
    Assert.assertEquals(FuncLogWithBaseLongToDouble.class, ve.getClass());
    Assert.assertTrue(4.5 == ((FuncLogWithBaseLongToDouble) ve).getBase());
    // Power with double power
    children2.clear();
    children2.add(colDesc2);
    children2.add(new ExprNodeConstantDesc(4.5));
    mathFuncExpr.setGenericUDF(new GenericUDFPower());
    mathFuncExpr.setChildren(children2);
    ve = vc.getVectorExpression(mathFuncExpr);
    Assert.assertEquals(FuncPowerDoubleToDouble.class, ve.getClass());
    Assert.assertTrue(4.5 == ((FuncPowerDoubleToDouble) ve).getPower());
    // Round with default decimal places
    mathFuncExpr.setGenericUDF(udfRound);
    children2.clear();
    children2.add(colDesc2);
    mathFuncExpr.setChildren(children2);
    ve = vc.getVectorExpression(mathFuncExpr);
    Assert.assertEquals(FuncRoundDoubleToDouble.class, ve.getClass());
}
Also used : ExprNodeConstantDesc(org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc) GenericUDFPower(org.apache.hadoop.hive.ql.udf.generic.GenericUDFPower) FuncLogWithBaseLongToDouble(org.apache.hadoop.hive.ql.exec.vector.expressions.FuncLogWithBaseLongToDouble) FuncPowerDoubleToDouble(org.apache.hadoop.hive.ql.exec.vector.expressions.FuncPowerDoubleToDouble) ArrayList(java.util.ArrayList) ExprNodeGenericFuncDesc(org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc) UDFSin(org.apache.hadoop.hive.ql.udf.UDFSin) GenericUDFBRound(org.apache.hadoop.hive.ql.udf.generic.GenericUDFBRound) GenericUDFRound(org.apache.hadoop.hive.ql.udf.generic.GenericUDFRound) GenericUDFBridge(org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge) BRoundWithNumDigitsDoubleToDouble(org.apache.hadoop.hive.ql.exec.vector.expressions.BRoundWithNumDigitsDoubleToDouble) RoundWithNumDigitsDoubleToDouble(org.apache.hadoop.hive.ql.exec.vector.expressions.RoundWithNumDigitsDoubleToDouble) BRoundWithNumDigitsDoubleToDouble(org.apache.hadoop.hive.ql.exec.vector.expressions.BRoundWithNumDigitsDoubleToDouble) ExprNodeColumnDesc(org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc) VectorExpression(org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression) DynamicValueVectorExpression(org.apache.hadoop.hive.ql.exec.vector.expressions.DynamicValueVectorExpression) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) FuncLogWithBaseDoubleToDouble(org.apache.hadoop.hive.ql.exec.vector.expressions.FuncLogWithBaseDoubleToDouble) Test(org.junit.Test)

Example 7 with GenericUDFBridge

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

the class TestUDFUUID method testUUID.

@Test
public void testUUID() throws Exception {
    UDFUUID udf = new UDFUUID();
    String id1 = udf.evaluate().toString();
    String id2 = udf.evaluate().toString();
    assertFalse(id1.equals(id2));
    assertEquals(id1.length(), 36);
    assertEquals(id2.length(), 36);
    GenericUDFBridge bridge = new GenericUDFBridge("uuid", false, UDFUUID.class.getName());
    assertFalse(FunctionRegistry.isDeterministic(bridge));
}
Also used : GenericUDFBridge(org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge) Test(org.junit.Test)

Example 8 with GenericUDFBridge

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

the class ConstantPropagateProcFactory method isDeterministicUdf.

private static boolean isDeterministicUdf(GenericUDF udf, List<ExprNodeDesc> children) {
    UDFType udfType = udf.getClass().getAnnotation(UDFType.class);
    if (udf instanceof GenericUDFBridge) {
        udfType = ((GenericUDFBridge) udf).getUdfClass().getAnnotation(UDFType.class);
    }
    if (udfType.deterministic() == false) {
        if (udf.getClass().equals(GenericUDFUnixTimeStamp.class) && children != null && children.size() > 0) {
            // unix_timestamp is polymorphic (ignore class annotations)
            return true;
        }
        return false;
    }
    // If udf is requiring additional jars, we can't determine the result in
    // compile time.
    String[] files;
    String[] jars;
    if (udf instanceof GenericUDFBridge) {
        GenericUDFBridge bridge = (GenericUDFBridge) udf;
        String udfClassName = bridge.getUdfClassName();
        try {
            UDF udfInternal = (UDF) Class.forName(bridge.getUdfClassName(), true, Utilities.getSessionSpecifiedClassLoader()).newInstance();
            files = udfInternal.getRequiredFiles();
            jars = udfInternal.getRequiredJars();
        } catch (Exception e) {
            LOG.error("The UDF implementation class '" + udfClassName + "' is not present in the class path");
            return false;
        }
    } else {
        files = udf.getRequiredFiles();
        jars = udf.getRequiredJars();
    }
    if (files != null || jars != null) {
        return false;
    }
    return true;
}
Also used : UDF(org.apache.hadoop.hive.ql.exec.UDF) GenericUDF(org.apache.hadoop.hive.ql.udf.generic.GenericUDF) UDFType(org.apache.hadoop.hive.ql.udf.UDFType) GenericUDFBridge(org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException)

Example 9 with GenericUDFBridge

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

the class ExprNodeGenericFuncDesc method isSame.

@Override
public boolean isSame(Object o) {
    if (!(o instanceof ExprNodeGenericFuncDesc)) {
        return false;
    }
    ExprNodeGenericFuncDesc dest = (ExprNodeGenericFuncDesc) o;
    if (!typeInfo.equals(dest.getTypeInfo()) || !genericUDF.getClass().equals(dest.getGenericUDF().getClass())) {
        return false;
    }
    if (genericUDF instanceof GenericUDFBridge) {
        GenericUDFBridge bridge = (GenericUDFBridge) genericUDF;
        GenericUDFBridge bridge2 = (GenericUDFBridge) dest.getGenericUDF();
        if (!bridge.getUdfClassName().equals(bridge2.getUdfClassName()) || !bridge.getUdfName().equals(bridge2.getUdfName()) || bridge.isOperator() != bridge2.isOperator()) {
            return false;
        }
    }
    if (genericUDF instanceof GenericUDFMacro) {
        // if getMacroName is null, we always treat it different from others.
        if (((GenericUDFMacro) genericUDF).getMacroName() == null || !(((GenericUDFMacro) genericUDF).getMacroName().equals(((GenericUDFMacro) dest.genericUDF).getMacroName()))) {
            return false;
        }
    }
    if (chidren.size() != dest.getChildren().size()) {
        return false;
    }
    for (int pos = 0; pos < chidren.size(); pos++) {
        if (!chidren.get(pos).isSame(dest.getChildren().get(pos))) {
            return false;
        }
    }
    return true;
}
Also used : GenericUDFMacro(org.apache.hadoop.hive.ql.udf.generic.GenericUDFMacro) GenericUDFBridge(org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge)

Example 10 with GenericUDFBridge

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

the class ExprNodeGenericFuncDesc method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(genericUDF.getClass().getSimpleName());
    if (genericUDF instanceof GenericUDFBridge) {
        GenericUDFBridge genericUDFBridge = (GenericUDFBridge) genericUDF;
        sb.append(" ==> ");
        sb.append(genericUDFBridge.getUdfName());
        sb.append(" ");
    }
    sb.append("(");
    if (chidren != null) {
        for (int i = 0; i < chidren.size(); i++) {
            if (i > 0) {
                sb.append(", ");
            }
            sb.append(chidren.get(i));
        }
    }
    sb.append(")");
    return sb.toString();
}
Also used : GenericUDFBridge(org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge)

Aggregations

GenericUDFBridge (org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge)18 ExprNodeColumnDesc (org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc)8 ExprNodeGenericFuncDesc (org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc)8 GenericUDF (org.apache.hadoop.hive.ql.udf.generic.GenericUDF)8 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)6 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)4 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)4 UDF (org.apache.hadoop.hive.ql.exec.UDF)3 DynamicValueVectorExpression (org.apache.hadoop.hive.ql.exec.vector.expressions.DynamicValueVectorExpression)3 VectorExpression (org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression)3 GenericUDFToUnixTimeStamp (org.apache.hadoop.hive.ql.udf.generic.GenericUDFToUnixTimeStamp)3 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)2 VectorizedRowBatch (org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch)2 ExprNodeConstantDesc (org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc)2 GenericUDFToBinary (org.apache.hadoop.hive.ql.udf.generic.GenericUDFToBinary)2 GenericUDFToChar (org.apache.hadoop.hive.ql.udf.generic.GenericUDFToChar)2 GenericUDFToDate (org.apache.hadoop.hive.ql.udf.generic.GenericUDFToDate)2 GenericUDFToDecimal (org.apache.hadoop.hive.ql.udf.generic.GenericUDFToDecimal)2