Consider an array_compact UDF that removes null values from an array. There is no easy way to implement this UDF because an explicit return type with a TypeTag is required by code generation.
The interesting observation here is that the output type of `array_compact` is the same as its input type. In general, there is a broad class of UDFs, especially collection-oriented ones, whose output types are functions of the input types. In our Spark work we have found collection manipulation UDFs to be very powerful for cleaning up data and substantially improving performance, in particular, avoiding explode followed by groupBy. It would be nice if Spark made adding these types of UDFs very easy.
I won't go into possible ways to implement this under the covers as there are many options but I do want to point out that it is possible to communicate the right type information to Spark without changing the signature for UDF registration using placeholder types, e.g.,