You are returning the type StructType not an instance of a struct (i.e.
StringType instead of "string").  If you'd like to return a struct you
should return a case class.

case class StringInfo(numChars: Int, firstLetter: String)
udf((s: String) => StringInfo(s.size, s.head))

If you'd like to take a struct as input, use Row as the type.

On Thu, Nov 5, 2015 at 9:53 PM, Rishabh Bhardwaj <rbnex...@gmail.com> wrote:

> Hi all,
>
> I am unable to register a UDF with return type as StructType:
>
> scala> def test(r:StructType):StructType = { r }
>>
>> test: (r:
>>> org.apache.spark.sql.types.StructType)org.apache.spark.sql.types.StructType
>>
>>
>>> scala> sqlContext.udf.register("test",test _ )
>>
>> scala.MatchError: org.apache.spark.sql.types.StructType (of class
>>> scala.reflect.internal.Types$TypeRef$$anon$6)
>>
>> at
>>> org.apache.spark.sql.catalyst.ScalaReflection$class.schemaFor(ScalaReflection.scala:101)
>>
>> at
>>> org.apache.spark.sql.catalyst.ScalaReflection$.schemaFor(ScalaReflection.scala:29)
>>
>> at
>>> org.apache.spark.sql.catalyst.ScalaReflection$class.schemaFor(ScalaReflection.scala:64)
>>
>>
> Can someone throw some light on this ? and Is there any work around for it
> ?
>
> TIA.
>
> Regards,
> Rishabh.
>

Reply via email to