spark-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From marmb...@apache.org
Subject spark git commit: [SPARK-11679][SQL] Invoking method " apply(fields: java.util.List[StructField])" in "StructType" gets ClassCastException
Date Tue, 17 Nov 2015 19:16:01 GMT
Repository: spark
Updated Branches:
  refs/heads/branch-1.6 d07a7e1e6 -> a3f0c77c9


[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.

(cherry picked from commit e8833dd12c71b23a242727e86684d2d868ff84b3)
Signed-off-by: Michael Armbrust <michael@databricks.com>


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/a3f0c77c
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/a3f0c77c
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/a3f0c77c

Branch: refs/heads/branch-1.6
Commit: a3f0c77c96cc4a7480c0dd74cf53ed701e753b2b
Parents: d07a7e1
Author: mayuanwen <mayuanwen@qiyi.com>
Authored: Tue Nov 17 11:15:46 2015 -0800
Committer: Michael Armbrust <michael@databricks.com>
Committed: Tue Nov 17 11:15:57 2015 -0800

----------------------------------------------------------------------
 .../scala/org/apache/spark/sql/types/StructType.scala  |  3 ++-
 .../test/org/apache/spark/sql/JavaDataFrameSuite.java  | 13 +++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/a3f0c77c/sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala
----------------------------------------------------------------------
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 11fce4b..9778df2 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 =

http://git-wip-us.apache.org/repos/asf/spark/blob/a3f0c77c/sql/core/src/test/java/test/org/apache/spark/sql/JavaDataFrameSuite.java
----------------------------------------------------------------------
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 d191b50..567bddd 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) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org


Mime
View raw message