title: Exploding Map keys as Column names
body: <p>I've been struggling with this for a while and can't wrap my head around it. So far I've only found examples which <code>explode()</code> a <code>MapType</code> column to <code>n</code> Row entries.</p>
  <p>What I'm trying to achieve is having, for example, a Map with 5 entries as 5 columns within the same Row.</p>
  <p>Taking this DF as an example...</p>
  <pre class="lang-scala prettyprint-override"><code>case class SampleRow(one: String, two: String, three: String, four: String, five: Map[String, String])       
  val df = List(
            SampleRow(
              &quot;one&quot;, 
              &quot;two&quot;, 
              &quot;three&quot;, 
              &quot;four&quot;, 
              Map(&quot;sample_one&quot; -&gt; &quot;hey&quot;, &quot;sample_two&quot; -&gt; &quot;hey&quot;))
          ).toDF()
  </code></pre>
  <p>After exploding column <code>five</code>, it should become</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>one</th>
  <th>two</th>
  <th>three</th>
  <th>four</th>
  <th>sample_one</th>
  <th>sample_two</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>one</td>
  <td>two</td>
  <td>three</td>
  <td>four</td>
  <td>hey</td>
  <td>hey</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>What I tried until now is the following.</p>
  <pre class="lang-scala prettyprint-override"><code>val explodedDS = originDS
        .select(cols :+ $&quot;key&quot;.as(&quot;outerMap&quot;) :+ $&quot;value.*&quot;:_*) // Column 'value' as a previous Map has been exploded before
  </code></pre>
  <p>But doing that prompts the following error into the console.</p>
  <blockquote>
  <p>Exception in thread &quot;main&quot; org.apache.spark.sql.AnalysisException: Can only star expand struct data types. Attribute: <code>ArrayBuffer(value)</code>;</p>
  </blockquote>
  <p>I understand that exploding a Map to Columns generates the issue of not being able to infer a schema until all Row objects contain the exact same number of Columns, either null or with a value, right?</p>
  <p>But apart from that, would there be any option to accomplish this despite the schema problem?</p>