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
Prev Previous commit
Next Next commit
Fix CreateMap.
  • Loading branch information
ueshin committed Jul 9, 2018
commit 01c9ff398b86ef3289360f543773c8adbef7bcec
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@ case class CreateMap(children: Seq[Expression]) extends Expression {
if (children.size % 2 != 0) {
TypeCheckResult.TypeCheckFailure(
s"$prettyName expects a positive even number of arguments.")
} else if (keys.map(_.dataType).distinct.length > 1) {
} else if (keys.length > 1 &&
keys.map(_.dataType).sliding(2, 1).exists { case Seq(t1, t2) => !t1.sameType(t2) }) {
TypeCheckResult.TypeCheckFailure(
"The given keys of function map should all be the same type, but they are " +
keys.map(_.dataType.simpleString).mkString("[", ", ", "]"))
} else if (values.map(_.dataType).distinct.length > 1) {
} else if (values.length > 1 &&
values.map(_.dataType).sliding(2, 1).exists { case Seq(t1, t2) => !t1.sameType(t2) }) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The checks for keys and values are very similar. Would it be possible to separate the common logic into a private method?

TypeCheckResult.TypeCheckFailure(
"The given values of function map should all be the same type, but they are " +
values.map(_.dataType.simpleString).mkString("[", ", ", "]"))
Expand All @@ -194,8 +196,10 @@ case class CreateMap(children: Seq[Expression]) extends Expression {

override def dataType: DataType = {
MapType(
keyType = keys.headOption.map(_.dataType).getOrElse(StringType),
valueType = values.headOption.map(_.dataType).getOrElse(StringType),
keyType =
TypeCoercion.findWiderNullablilityType(keys.map(_.dataType)).getOrElse(StringType),
valueType =
TypeCoercion.findWiderNullablilityType(values.map(_.dataType)).getOrElse(StringType),
valueContainsNull = values.exists(_.nullable))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ class ComplexTypeSuite extends SparkFunSuite with ExpressionEvalHelper {
CreateMap(interlace(strWithNull, intSeq.map(Literal(_)))),
null, null)
}

val map = CreateMap(Seq(
Literal.create(intSeq, ArrayType(IntegerType, containsNull = false)),
Literal.create(strSeq, ArrayType(StringType, containsNull = false)),
Literal.create(intSeq :+ null, ArrayType(IntegerType, containsNull = true)),
Literal.create(strSeq :+ null, ArrayType(StringType, containsNull = true))))
assert(map.dataType ===
MapType(
ArrayType(IntegerType, containsNull = true),
ArrayType(StringType, containsNull = true),
valueContainsNull = false))
checkEvaluation(map, createMap(Seq(intSeq, intSeq :+ null), Seq(strSeq, strSeq :+ null)))
}

test("MapFromArrays") {
Expand Down