Skip to content

Commit 06129cd

Browse files
YanTangZhaimarmbrus
authored andcommitted
[SPARK-4676][SQL] JavaSchemaRDD.schema may throw NullType MatchError if sql has null
val jsc = new org.apache.spark.api.java.JavaSparkContext(sc) val jhc = new org.apache.spark.sql.hive.api.java.JavaHiveContext(jsc) val nrdd = jhc.hql("select null from spark_test.for_test") println(nrdd.schema) Then the error is thrown as follows: scala.MatchError: NullType (of class org.apache.spark.sql.catalyst.types.NullType$) at org.apache.spark.sql.types.util.DataTypeConversions$.asJavaDataType(DataTypeConversions.scala:43) Author: YanTangZhai <hakeemzhai@tencent.com> Author: yantangzhai <tyz0303@163.com> Author: Michael Armbrust <michael@databricks.com> Closes #3538 from YanTangZhai/MatchNullType and squashes the following commits: e052dff [yantangzhai] [SPARK-4676] [SQL] JavaSchemaRDD.schema may throw NullType MatchError if sql has null 4b4bb34 [yantangzhai] [SPARK-4676] [SQL] JavaSchemaRDD.schema may throw NullType MatchError if sql has null 896c7b7 [yantangzhai] fix NullType MatchError in JavaSchemaRDD when sql has null 6e643f8 [YanTangZhai] Merge pull request #11 from apache/master e249846 [YanTangZhai] Merge pull request #10 from apache/master d26d982 [YanTangZhai] Merge pull request #9 from apache/master 76d4027 [YanTangZhai] Merge pull request #8 from apache/master 03b62b0 [YanTangZhai] Merge pull request #7 from apache/master 8a00106 [YanTangZhai] Merge pull request #6 from apache/master cbcba66 [YanTangZhai] Merge pull request #3 from apache/master cdef539 [YanTangZhai] Merge pull request #1 from apache/master (cherry picked from commit 1066427) Signed-off-by: Michael Armbrust <michael@databricks.com>
1 parent aa3d369 commit 06129cd

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

sql/core/src/main/java/org/apache/spark/sql/api/java/DataType.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public abstract class DataType {
8282
*/
8383
public static final ShortType ShortType = new ShortType();
8484

85+
/**
86+
* Gets the NullType object.
87+
*/
88+
public static final NullType NullType = new NullType();
89+
8590
/**
8691
* Creates an ArrayType by specifying the data type of elements ({@code elementType}).
8792
* The field of {@code containsNull} is set to {@code true}.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.api.java;
19+
20+
/**
21+
* The data type representing null and NULL values.
22+
*
23+
* {@code NullType} is represented by the singleton object {@link DataType#NullType}.
24+
*/
25+
public class NullType extends DataType {
26+
protected NullType() {}
27+
}

sql/core/src/main/scala/org/apache/spark/sql/package.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,16 @@ package object sql {
263263
@DeveloperApi
264264
val ShortType = catalyst.types.ShortType
265265

266+
/**
267+
* :: DeveloperApi ::
268+
*
269+
* The data type representing `NULL` values.
270+
*
271+
* @group dataType
272+
*/
273+
@DeveloperApi
274+
val NullType = catalyst.types.NullType
275+
266276
/**
267277
* :: DeveloperApi ::
268278
*

sql/core/src/main/scala/org/apache/spark/sql/types/util/DataTypeConversions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ protected[sql] object DataTypeConversions {
6262
case IntegerType => JDataType.IntegerType
6363
case LongType => JDataType.LongType
6464
case ShortType => JDataType.ShortType
65+
case NullType => JDataType.NullType
6566

6667
case arrayType: ArrayType => JDataType.createArrayType(
6768
asJavaDataType(arrayType.elementType), arrayType.containsNull)

sql/core/src/test/scala/org/apache/spark/sql/api/java/JavaSQLSuite.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ class JavaSQLSuite extends FunSuite {
6868
javaSqlCtx.sql("SELECT * FROM people").collect()
6969
}
7070

71+
test("schema with null from JavaBeans") {
72+
val person = new PersonBean
73+
person.setName("Michael")
74+
person.setAge(29)
75+
76+
val rdd = javaCtx.parallelize(person :: Nil)
77+
val schemaRDD = javaSqlCtx.applySchema(rdd, classOf[PersonBean])
78+
79+
schemaRDD.registerTempTable("people")
80+
val nullRDD = javaSqlCtx.sql("SELECT null FROM people")
81+
val structFields = nullRDD.schema.getFields()
82+
assert(structFields.size == 1)
83+
assert(structFields(0).getDataType().isInstanceOf[NullType])
84+
assert(nullRDD.collect.head.row === Seq(null))
85+
}
86+
7187
test("all types in JavaBeans") {
7288
val bean = new AllTypesBean
7389
bean.setStringField("")

0 commit comments

Comments
 (0)