Skip to content

Commit

Permalink
Add negative test for map type.
Browse files Browse the repository at this point in the history
  • Loading branch information
viirya committed Sep 3, 2017
1 parent 444c64d commit ec9199a
Showing 1 changed file with 47 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,38 @@ class PredicateSuite extends SparkFunSuite with ExpressionEvalHelper {

test("IN with different types") {
def testWithRandomDataGeneration(dataType: DataType, nullable: Boolean): Unit = {
val dataGen = RandomDataGenerator.forType(dataType, nullable = nullable)
if (dataGen.isDefined) {
val inputData = Seq.fill(10) {
val value = dataGen.get.apply()
value match {
case d: Double if d.isNaN => 0.0d
case f: Float if f.isNaN => 0.0f
case _ => value
}
val maybeDataGen = RandomDataGenerator.forType(dataType, nullable = nullable)
// Actually we won't pass in unsupported data types, this is a safety check.
val dataGen = maybeDataGen.getOrElse(
fail(s"Failed to create data generator for type $dataType"))
val inputData = Seq.fill(10) {
val value = dataGen.apply()
def cleanData(value: Any) = value match {
case d: Double if d.isNaN => 0.0d
case f: Float if f.isNaN => 0.0f
case _ => value
}
val input = inputData.map(NonFoldableLiteral.create(_, dataType))
val expected = if (inputData(0) == null) {
null
} else if (inputData.slice(1, 10).contains(inputData(0))) {
true
} else if (inputData.slice(1, 10).contains(null)) {
null
} else {
false
value match {
case s: Seq[_] => s.map(cleanData(_))
case m: Map[_, _] =>
val pair = m.unzip
val newKeys = pair._1.map(cleanData(_))
val newValues = pair._2.map(cleanData(_))
newKeys.zip(newValues).toMap
case _ => cleanData(value)
}
checkEvaluation(In(input(0), input.slice(1, 10)), expected)
}
val input = inputData.map(NonFoldableLiteral.create(_, dataType))
val expected = if (inputData(0) == null) {
null
} else if (inputData.slice(1, 10).contains(inputData(0))) {
true
} else if (inputData.slice(1, 10).contains(null)) {
null
} else {
false
}
checkEvaluation(In(input(0), input.slice(1, 10)), expected)
}

val atomicTypes = DataTypeTestUtils.atomicTypes.filter { t =>
Expand Down Expand Up @@ -209,6 +219,24 @@ class PredicateSuite extends SparkFunSuite with ExpressionEvalHelper {
StructField("a", colOneType) :: StructField("b", colTwoType) :: Nil)
testWithRandomDataGeneration(structType, nullable)
}

// Map types: not supported
for (
keyType <- atomicTypes;
valueType <- atomicTypes;
nullable <- Seq(true, false)) {
val mapType = MapType(keyType, valueType)
val e = intercept[Exception] {
testWithRandomDataGeneration(mapType, nullable)
}
if (e.getMessage.contains("Code generation of")) {
// If the `value` expression is null, `eval` will be short-circuited.
// Codegen version evaluation will be run then.
assert(e.getMessage.contains("cannot generate equality code for un-comparable type"))
} else {
assert(e.getMessage.contains("Exception evaluating"))
}
}
}

test("INSET") {
Expand Down

0 comments on commit ec9199a

Please sign in to comment.