Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-4676] [SQL] JavaSchemaRDD.schema may throw NullType MatchError if sql has null #3538

Closed
wants to merge 11 commits into from
Closed
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is off here.


/**
* 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() {}
}
10 changes: 10 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ package object sql {
@DeveloperApi
val ShortType = catalyst.types.ShortType

/**
* :: DeveloperApi ::
*
* The data type representing `NULL` values.
*
* @group dataType
*/
@DeveloperApi
val NullType = catalyst.types.NullType

/**
* :: DeveloperApi ::
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ protected[sql] object DataTypeConversions {
case IntegerType => JDataType.IntegerType
case LongType => JDataType.LongType
case ShortType => JDataType.ShortType
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