Skip to content
Snippets Groups Projects
Commit e8833dd1 authored by mayuanwen's avatar mayuanwen Committed by Michael Armbrust
Browse files

[SPARK-11679][SQL] Invoking method " apply(fields:...

[SPARK-11679][SQL] Invoking method " apply(fields: java.util.List[StructField])" in "StructType" gets ClassCastException

In the previous method, fields.toArray will cast java.util.List[StructField] into Array[Object] which can not cast into Array[StructField], thus when invoking this method will throw "java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lorg.apache.spark.sql.types.StructField;"
I directly cast java.util.List[StructField] into Array[StructField]  in this patch.

Author: mayuanwen <mayuanwen@qiyi.com>

Closes #9649 from jackieMaKing/Spark-11679.
parent 21fac543
No related branches found
No related tags found
No related merge requests found
...@@ -328,7 +328,8 @@ object StructType extends AbstractDataType { ...@@ -328,7 +328,8 @@ object StructType extends AbstractDataType {
def apply(fields: Seq[StructField]): StructType = StructType(fields.toArray) def apply(fields: Seq[StructField]): StructType = StructType(fields.toArray)
def apply(fields: java.util.List[StructField]): StructType = { def apply(fields: java.util.List[StructField]): StructType = {
StructType(fields.toArray.asInstanceOf[Array[StructField]]) import scala.collection.JavaConverters._
StructType(fields.asScala)
} }
protected[sql] def fromAttributes(attributes: Seq[Attribute]): StructType = protected[sql] def fromAttributes(attributes: Seq[Attribute]): StructType =
......
...@@ -22,6 +22,7 @@ import java.util.Arrays; ...@@ -22,6 +22,7 @@ import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.ArrayList;
import scala.collection.JavaConverters; import scala.collection.JavaConverters;
import scala.collection.Seq; import scala.collection.Seq;
...@@ -209,6 +210,18 @@ public class JavaDataFrameSuite { ...@@ -209,6 +210,18 @@ public class JavaDataFrameSuite {
Assert.assertEquals(1, result.length); Assert.assertEquals(1, result.length);
} }
@Test
public void testCreateStructTypeFromList(){
List<StructField> fields1 = new ArrayList<>();
fields1.add(new StructField("id", DataTypes.StringType, true, Metadata.empty()));
StructType schema1 = StructType$.MODULE$.apply(fields1);
Assert.assertEquals(0, schema1.fieldIndex("id"));
List<StructField> fields2 = Arrays.asList(new StructField("id", DataTypes.StringType, true, Metadata.empty()));
StructType schema2 = StructType$.MODULE$.apply(fields2);
Assert.assertEquals(0, schema2.fieldIndex("id"));
}
private static final Comparator<Row> crosstabRowComparator = new Comparator<Row>() { private static final Comparator<Row> crosstabRowComparator = new Comparator<Row>() {
@Override @Override
public int compare(Row row1, Row row2) { public int compare(Row row1, Row row2) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment