title: Spark Scala UDF : java.lang.UnsupportedOperationException: Schema for type Any is not supported
body: <p>I am trying to return a map from the UDF with in if else  and getting the below exception , Any pointers please ?</p>
  <blockquote>
  <p>java.lang.UnsupportedOperationException: Schema for type Any is not
  supported</p>
  </blockquote>
  <pre><code>import org.apache.spark.sql.functions.{col, udf}
  import org.apache.spark.sql.functions._
  val df2  = Seq(
    (&quot;1&quot;, Map(&quot;Fld1&quot; -&gt; &quot;USA&quot;,&quot;Fld2&quot; -&gt; &quot;UK&quot;)),
    (&quot;2&quot;, Map(&quot;Fld1&quot; -&gt; &quot;Germany&quot;, &quot;Fld2&quot; -&gt; &quot;Portugal&quot;)),
  (&quot;3&quot;, Map(&quot;Fld1&quot; -&gt; &quot;France&quot;, &quot;Fld2&quot; -&gt; &quot;Paris&quot;))
  ).toDF(&quot;id&quot;, &quot;map&quot;)
  
  val getmapUdf  = udf((map1: Map[String, String]) =&gt; { 
     
      val fl1 = map1.getOrElse(&quot;Fld1&quot;,&quot;unknown&quot;)   
      val fl2 = map1.getOrElse(&quot;Fld2&quot;,&quot;unknown&quot;)   
  
       if (fl1 ==&quot;Germany&quot;)
      {
              Map(&quot;key1&quot; -&gt; &quot;G&quot;)
      }
      else if(fl1 ==&quot;France&quot;) 
      {
          if (fl2 ==&quot;UK&quot;)
          {
              Map(&quot;key1&quot; -&gt;&quot;U&quot;)
          }
          else
          {
              Map(&quot;key1&quot; -&gt;&quot;Y&quot;)
          }
      }
      else if(fl1 ==&quot;France&quot;) 
      {
             Map(&quot;key1&quot; -&gt;&quot;G&quot;)
      }
      
  })
  var temp2 = df2.withColumn(&quot;mymap&quot;, getmapUdf($&quot;map&quot;))
  temp2.show(false)
  </code></pre>