Im struggling to create a udf to extract some column data. The column is tricky since sometimes its string but in many cases can be struct. I want to take into consideration only the time when the column is struct and extract the needed information for it.
Assuming this example:
SELECT annoyingCol.data From SomeDf
annoyingCol.data equals to string OR struct in order to avoid getting error like this one: need struct type but got string;
. I'm wondering if i can just create a udf which check the column type, e.g:
SELECT
case when isStruct(annoyingCol.data) then annoyingCol.data.my_data else null end
FROM SomeDf
I tried this
val isStruct = udf((r: Row) => {
import org.apache.spark.sql.Row
import org.apache.spark.sql.types.BooleanType
import scala.util.Try
Try(r.getAs[String]("estimation_data_inc_waypoints")).isSuccess
}
)
spark.udf.register("isStruct", isStruct)
but failed, I know i missing something. any help will be much appreciated.