I have a dataframe which has one of the column called "Query" having the select statement present. Want to execute this query and create a new column having actual results from the TempView.
+--------------+-----------+-----+----------------------------------------+
|DIFFCOLUMNNAME|DATATYPE |ISSUE|QUERY |
+--------------+-----------+-----+----------------------------------------+
|Firstname |StringType |YES |Select Firstname from TempView limit 1 |
|LastName |StringType |NO |Select LastName from TempView limit 1 |
|Designation |StringType |YES |Select Designation from TempView limit 1|
|Salary |IntegerType|YES |Select Salary from TempView limit 1 |
+--------------+-----------+-----+----------------------------------------+
Getting error as Type mismatch, Required String found column. Do I need to use UDF here. But not sure how to write and use. Please suggest
DF.withColumn("QueryResult", spark.sql(col("QUERY")))
TempView is Temporary View which I have created having all the required columns. Expected final Dataframe will be something like this with the new column added QUERYRESULT.
+--------------+-----------+-----+----------------------------------------+------------+
|DIFFCOLUMNNAME|DATATYPE |ISSUE|QUERY | QUERY RESULT
+--------------+-----------+-----+----------------------------------------+------------+
|Firstname |StringType |YES |Select Firstname from TempView limit 1 | Bunny |
|LastName |StringType |NO |Select LastName from TempView limit 1 | Gummy |
|Designation |StringType |YES |Select Designation from TempView limit 1| Developer |
|Salary |IntegerType|YES |Select Salary from TempView limit 1 | 100 |
+--------------+-----------+-----+----------------------------------------+------------+