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());
}
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));
}
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;
}
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;
}
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();
}
Aggregations