aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormayuanwen <mayuanwen@qiyi.com>2015-11-17 11:15:46 -0800
committerMichael Armbrust <michael@databricks.com>2015-11-17 11:15:46 -0800
commite8833dd12c71b23a242727e86684d2d868ff84b3 (patch)
treef6173a0fbb1a7c2b54d5778a238f4a52dff40457
parent21fac5434174389e8b83a2f11341fa7c9e360bfd (diff)
downloadspark-e8833dd12c71b23a242727e86684d2d868ff84b3.tar.gz
spark-e8833dd12c71b23a242727e86684d2d868ff84b3.tar.bz2
spark-e8833dd12c71b23a242727e86684d2d868ff84b3.zip
[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.
-rw-r--r--sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala3
-rw-r--r--sql/core/src/test/java/test/org/apache/spark/sql/JavaDataFrameSuite.java13
2 files changed, 15 insertions, 1 deletions
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala
index 11fce4beaf..9778df271d 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala
@@ -328,7 +328,8 @@ object StructType extends AbstractDataType {
def apply(fields: Seq[StructField]): StructType = StructType(fields.toArray)
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 =
diff --git a/sql/core/src/test/java/test/org/apache/spark/sql/JavaDataFrameSuite.java b/sql/core/src/test/java/test/org/apache/spark/sql/JavaDataFrameSuite.java
index d191b50fa8..567bdddece 100644
--- a/sql/core/src/test/java/test/org/apache/spark/sql/JavaDataFrameSuite.java
+++ b/sql/core/src/test/java/test/org/apache/spark/sql/JavaDataFrameSuite.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
+import java.util.ArrayList;
import scala.collection.JavaConverters;
import scala.collection.Seq;
@@ -209,6 +210,18 @@ public class JavaDataFrameSuite {
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>() {
@Override
public int compare(Row row1, Row row2) {