I am trying to write generic add method for which can take any argument type and return result of that type
def addExactUDF[T](x: T, y: T)(implicit num: Numeric[T]): T = {
import num._
x + y
}
def addExact(value1: Column, value2: Column, dataType: String): Column =
dataType match {
case "Int" => expr(s"addExactUDF(cast($value1 AS INT), cast($value2 AS INT))")
case "Double" => expr(s"addExactUDF(cast($value1 AS DOUBLE), cast($value2 AS DOUBLE))")
}
Now when I try to register the UDF
object FilterFunctionsUtil extends MathFunctionsNameSpace with StringFunctionsNameSpace {
lazy val registerAsSparkUdf: UserDefinedFunction = {
val sqlContext = SparkSessionFactory.getSparkSession(Map(), Nil).sqlContext
sqlContext.udf.register("addExactUDF", addExactUDF _)
sqlContext.udf.register("subtractExactUDF", subtractExactUDF _)
}
}
It throw me error
Error:(36, 44) could not find implicit value for parameter num: Numeric[Nothing]
sqlContext.udf.register("addExactUDF", addExactUDF _)
Error:(36, 44) not enough arguments for method addExactUDF: (implicit num: Numeric[Nothing])Nothing.
Unspecified value parameter num.
sqlContext.udf.register("addExactUDF", addExactUDF _)
how do I add the implicit to make this work