Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ object JacksonUtils {

case at: ArrayType => verifyType(name, at.elementType)

case mt: MapType => verifyType(name, mt.keyType)
// For MapType, its keys are treated as a string (i.e. calling `toString`) basically when
// generating JSON, so we only care if the values are valid for JSON.
case mt: MapType => verifyType(name, mt.valueType)

case udt: UserDefinedType[_] => verifyType(name, udt.sqlType)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.catalyst.json

import java.io.CharArrayWriter
import java.util.TimeZone

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.RandomDataGenerator
import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow}
import org.apache.spark.sql.types._

class JacksonUtilsSuite extends SparkFunSuite {

test("verifySchema") {
Copy link
Member

@HyukjinKwon HyukjinKwon Sep 8, 2017

Choose a reason for hiding this comment

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

@viirya, would you mind if I ask to leave a simple e2e test alone for this PR? This one looks quite a simple fix to me and I think this won't require such many tests alone for this issue. I want to backport this but only leave strictly related changes here.

I think we could do something like SELECT to_json(struct(map(interval 1 second, 'a'))) or SELECT to_json(struct(map('a', interval 1 second))).

Copy link
Member

Choose a reason for hiding this comment

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

OK, I am fine with those test coverage improvement too together. Up to you.

def verfifyJSONGenerate(schema: StructType): Unit = {
val convertToInternalRow = CatalystTypeConverters.createToCatalystConverter(schema)
val maybeDataGen = RandomDataGenerator.forType(schema, nullable = false)
val dataGen = maybeDataGen.getOrElse(
fail(s"Failed to create data generator for type $schema"))
val row = convertToInternalRow(dataGen.apply()).asInstanceOf[InternalRow]
val gen = new JacksonGenerator(
schema, new CharArrayWriter(), new JSONOptions(Map.empty, TimeZone.getDefault.getID))
gen.write(row)
}

// The valid schema
val atomicTypes = DataTypeTestUtils.atomicTypes
val atomicArrayTypes = atomicTypes.map(ArrayType(_, containsNull = false))
val atomicMapTypes = for (keyType <- atomicTypes;
valueType <- atomicTypes) yield MapType(keyType, valueType, false)
Copy link
Member

Choose a reason for hiding this comment

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

I'd do this as below:

val atomicMapTypes = for {
  keyType <- atomicTypes
  valueType <- atomicTypes
} yield MapType(keyType, valueType, false)


(atomicTypes ++ atomicArrayTypes ++ atomicMapTypes).foreach { dataType =>
val schema = StructType(StructField("a", dataType, nullable = false) :: Nil)
JacksonUtils.verifySchema(schema)
verfifyJSONGenerate(schema)
}

val invalidTypes = Seq(CalendarIntervalType)

// For MapType, its keys are treated as a string basically when generating JSON, so we only
// care if the values are valid for JSON.
val alsoValidMapTypes = for (keyType <- atomicTypes ++ invalidTypes;
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to move these supported cases into 45L so we loop twice, one for the vaild and one for invalid one?

valueType <- atomicTypes) yield MapType(keyType, valueType, true)
alsoValidMapTypes.foreach { dataType =>
val schema = StructType(StructField("a", dataType, nullable = false) :: Nil)
JacksonUtils.verifySchema(schema)
verfifyJSONGenerate(schema)
}

// The invalid schema
val invalidArrayTypes = invalidTypes.map(ArrayType(_, containsNull = false))
val invalidMapTypes = for (keyType <- atomicTypes ++ invalidTypes;
valueType <- invalidTypes) yield MapType(keyType, valueType, false)

(invalidTypes ++ invalidArrayTypes ++ invalidMapTypes).foreach { dataType =>
val schema = StructType(StructField("a", dataType, nullable = false) :: Nil)
intercept[UnsupportedOperationException] {
JacksonUtils.verifySchema(schema)
}
intercept[RuntimeException] {
verfifyJSONGenerate(schema)
}
}
}
}