title: cannot resolve explode due to type mismatch in spark while parsing xml file
body: <p>I have a data frame with below schema</p>
  
  <pre><code>root
   |-- DataPartition: long (nullable = true)
   |-- TimeStamp: string (nullable = true)
   |-- _organizationId: long (nullable = true)
   |-- _segmentId: long (nullable = true)
   |-- seg:BusinessSegments: struct (nullable = true)
   |    |-- seg:BusinessSegment: array (nullable = true)
   |    |    |-- element: struct (containsNull = true)
   |    |    |    |-- _VALUE: string (nullable = true)
   |    |    |    |-- _hierarchicalCode: long (nullable = true)
   |    |    |    |-- _industryId: long (nullable = true)
   |    |    |    |-- _ranking: long (nullable = true)
   |-- seg:GeographicSegments: struct (nullable = true)
   |    |-- seg:GeographicSegment: struct (nullable = true)
   |    |    |-- _geographyId: long (nullable = true)
   |    |    |-- seg:IsSubtracted: boolean (nullable = true)
   |    |    |-- seg:Sequence: long (nullable = true)
   |-- seg:IsCorporate: boolean (nullable = true)
   |-- seg:IsElimination: boolean (nullable = true)
   |-- seg:IsOperatingSegment: boolean (nullable = true)
   |-- seg:IsOther: boolean (nullable = true)
   |-- seg:IsShariaCompliant: boolean (nullable = true)
   |-- seg:PredecessorSegments: struct (nullable = true)
   |    |-- seg:PredecessorSegment: long (nullable = true)
   |-- seg:SegmentLocalLanguageLabel: struct (nullable = true)
   |    |-- _VALUE: string (nullable = true)
   |    |-- _languageId: long (nullable = true)
   |-- seg:SegmentName: struct (nullable = true)
   |    |-- _VALUE: string (nullable = true)
   |    |-- _languageId: long (nullable = true)
   |-- seg:SegmentType: string (nullable = true)
   |-- seg:SegmentTypeId: long (nullable = true)
   |-- seg:ValidFromPeriodEndDate: string (nullable = true)
   |-- _action: string (nullable = true)
  </code></pre>
  
  <p>Now I want to get <code>seg:BusinessSegments.seg:BusinessSegment</code> value from the schema.</p>
  
  <p>But my issue is when I do this using explode </p>
  
  <pre><code>val GeographicSegmentchildDF = parentDF.select($"DataPartition".as("DataPartition"), $"TimeStamp".as("TimeStamp"), $"_organizationId", $"_segmentId", explode($"seg:GeographicSegments.seg:GeographicSegment").as("GeographicSegments"), $"_action")
  val GeographicSegmentchildArrayDF = GeographicSegmentchildDF.select(getDataPartition($"DataPartition").as("DataPartition"), $"TimeStamp".as("TimeStamp"), $"_organizationId".as("OrganizationId"), $"_segmentId".as("SegmentId"), $"GeographicSegments.*", getFFActionChild($"_action").as("FFAction|!|"))
  </code></pre>
  
  <p>So in first line I am exploding and in the next line I am doing * or expand on that <code>$"GeographicSegments.*",</code> .</p>
  
  <p>I get error like 
  This is what I am doing </p>
  
  <blockquote>
    <p>Exception in thread "main" org.apache.spark.sql.AnalysisException:
    cannot resolve
    'explode(<code>seg:GeographicSegments</code>.<code>seg:GeographicSegment</code>)' due to
    data type mismatch:</p>
  </blockquote>
  
  <p>I know the issue because in schema I get <code>seg:GeographicSegment</code> as struct not as array and that is why I am getting .</p>
  
  <p>So the real issue is I don't have fixed schema .</p>
  
  <p><strong>When there are two records in xml file then <code>seg:GeographicSegment</code> becomes as array and then my code is working fine but when I get only one record then it work as struct and my code fails .</strong></p>
  
  <p>How can I handle this in my code .
  Do I have to put condition while parsing schema ?
  Or is there anyway I </p>
  
  <p>Here is one of the case which is not working </p>
  
  <pre><code>val columnTypePredecessorSegments = parentDF.select($"seg:PredecessorSegments.seg:PredecessorSegment").schema.map(_.dataType).head.toString().startsWith("LongType")
      //if column type is struct then use .* and array function to convert the struct to array else just use array
      val PredecessorSegmentschildDF = if (columnTypePredecessorSegments) {
        parentDF.select($"DataPartition".as("DataPartition"), $"TimeStamp".as("TimeStamp"), $"_organizationId", $"_segmentId", explode(array($"seg:PredecessorSegments.seg:PredecessorSegment")).as("PredecessorSegments"), $"_action")
      } else {
        parentDF.select($"DataPartition".as("DataPartition"), $"TimeStamp".as("TimeStamp"), $"_organizationId", $"_segmentId", explode($"seg:PredecessorSegments.seg:PredecessorSegment").as("PredecessorSegments"), $"_action")
      }
      val PredecessorSegmentsDFFinalChilddDF = PredecessorSegmentschildDF.select(getDataPartition($"DataPartition").as("DataPartition"), $"TimeStamp".as("TimeStamp"), $"_organizationId".as("OrganizationId"), $"_segmentId".as("SuccessorSegment"), $"PredecessorSegments.*", getFFActionChild($"_action").as("FFAction|!|"))
      PredecessorSegmentsDFFinalChilddDF.show(false)
  </code></pre>