Q:

How to overload UDFs in Spark2 using Spark Session

How to achieve method overloading UDFs in Spark2 using Spark Session.
scala> spark.udf.register("func",(a:String)=>a.length)   

scala> spark.udf.register("func",(a:Int)=>a*1000)

Following is my Hive table named 'orc'  and its description
scala> spark.sql("desc orc").collect.foreach(println)
[id,int,null]
[name,string,null]
[time_stamp,timestamp,null]

There are two records in my table
scala> spark.sql("select * from orc").collect.foreach(println)
[1,Pratap Chandra Dhan,null]
[2,Dyuti Ranjan Nayak,2016-01-01 00:00:00.0]

When I query using spark session, the second function takes effect, preventing method overloading
scala> spark.sql("select func(id),func(name) from  orc").collect.foreach(println)
[1000,null]
[2000,null]

A:

If really required (refer comment) you can define func to handle multiple types of inputs as below and register:
scala> val func = (a: Any) => {
     |       val i = a match {
     |       case s:String => s.length
     |       case d:Integer => d * 1000
     |       //case _ => 0
     |       }
     |       i
     |       }
func: Any => Int = <function1>

scala> spark.udf.register("func",func)

