Skip to content

Commit 4b4bb34

Browse files
committed
[SPARK-4676] [SQL] JavaSchemaRDD.schema may throw NullType MatchError if sql has null
1 parent 896c7b7 commit 4b4bb34

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
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/types/util/DataTypeConversions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +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.StringType
65+
case NullType => JDataType.NullType
6666

6767
case arrayType: ArrayType => JDataType.createArrayType(
6868
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)