I have encountered a peculiar issue while working with spark, I am not quite sure what is going, would be great if someone can help. My problem is have a function similar to the one below, that is casting dataframes to datasets of sometype, that is decided in runtime. I need to work with Datasets, because the underlying case classes have some annotations that i would like to use.
def ret(spark: SparkSession, dss: DataFrame, typ: String): Dataset[_ <: Product] = {
import spark.implicits._
typ match {
case "t1" => dss.as[T1]
case "t2" => dss.as[T2]
}
}
I am able to cast a dataframe to dataset with the following function call val ds = ret(spark,dataframe,"t1")
Everything works well with this function, now i want to extend the existing function to return a Dataset[(String,_<:Product)]
so i modify my function like this,
def ret(spark: SparkSession, dss: DataFrame,typ: String):Dataset[(String,_ <: Product)] = {
import spark.implicits._
typ match {
case "t1" => dss.as[(String,T1)]
case "t2" => dss.as[(String,T2)]
}
}
This gives me a compile error saying, type (String,T1)
, does not match expected type (String,_<:Product)
. What is actually happening here? any ideas how I can fix this? Any hints would be much appreciated!
Thanks a lot!!
Update: The upper bound <: Product refers to scala.Product and T1,T2 can be any case classes for exampple,
case class T1(name: String, age: Int)
case class T2(name: String, max: Int, min: Int)
But it can be really anything