Skip to content

Commit

Permalink
[SPARK-4676] [SQL] JavaSchemaRDD.schema may throw NullType MatchError…
Browse files Browse the repository at this point in the history
… if sql has null
  • Loading branch information
YanTangZhai committed Dec 2, 2014
1 parent 896c7b7 commit 4b4bb34
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public abstract class DataType {
*/
public static final ShortType ShortType = new ShortType();

/**
* Gets the NullType object.
*/
public static final NullType NullType = new NullType();

/**
* Creates an ArrayType by specifying the data type of elements ({@code elementType}).
* The field of {@code containsNull} is set to {@code true}.
Expand Down
27 changes: 27 additions & 0 deletions sql/core/src/main/java/org/apache/spark/sql/api/java/NullType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.api.java;

/**
* The data type representing null and NULL values.
*
* {@code NullType} is represented by the singleton object {@link DataType#NullType}.
*/
public class NullType extends DataType {
protected NullType() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected[sql] object DataTypeConversions {
case IntegerType => JDataType.IntegerType
case LongType => JDataType.LongType
case ShortType => JDataType.ShortType
case NullType => JDataType.StringType
case NullType => JDataType.NullType

case arrayType: ArrayType => JDataType.createArrayType(
asJavaDataType(arrayType.elementType), arrayType.containsNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ class JavaSQLSuite extends FunSuite {
javaSqlCtx.sql("SELECT * FROM people").collect()
}

test("schema with null from JavaBeans") {
val person = new PersonBean
person.setName("Michael")
person.setAge(29)

val rdd = javaCtx.parallelize(person :: Nil)
val schemaRDD = javaSqlCtx.applySchema(rdd, classOf[PersonBean])

schemaRDD.registerTempTable("people")
val nullRDD = javaSqlCtx.sql("SELECT null FROM people")
val structFields = nullRDD.schema.getFields()
assert(structFields.size == 1)
assert(structFields(0).getDataType().isInstanceOf[NullType])
assert(nullRDD.collect.head.row === Seq(null))
}

test("all types in JavaBeans") {
val bean = new AllTypesBean
bean.setStringField("")
Expand Down

0 comments on commit 4b4bb34

Please sign in to comment.