score:2

Accepted answer

udf cannot return row objects. return type has to be one of the types enumerated in the column value type in scala in the data types table.

good news is there should be no need for udf here. if object1 and object2 have the same schema (it wouldn't work otherwise anyway) you can use array function:

import org.apache.spark.sql.functions._

df.select(array(col("object1"), col("object2"))

or

df.select(array(col("path.to.object1"), col("path.to.object2"))

if object1 and object2 are not top level columns.

score:0

i would like to suggest one alternative way which can be used if schema for object1 and object2 are different and you get to return the row. basically to return row , you simply return a case class having the schema of row objects which in this case is object1 and object2 which themselves seem to be rows

so do the following

case class object1(<add the schema here>)

case class object2(<add the schema here>)

case class record(object1:object1,object2:object2)

now inside the udf , you can create object1 and object2 using firstobject and secondobject

then

val record = record(object1,object2)

then you can return the record

in this you can return rows even if schema not same or some processing required.

i know that this doesn't actually pertain to your question , but this question seemed a correct opportunity to tell about this concept.


Related Query

More Query from same tag