-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-23933][SQL] Add map_from_arrays function #21258
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
Changes from 1 commit
e4171e1
95d92d8
1df6bb5
2075770
d5ff7be
4eee89d
7b66ab4
2fcbb80
228fcc6
6d53a96
a4b3ec2
a0b4ac5
38d0868
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,12 +255,8 @@ case class CreateMapFromArrays(left: Expression, right: Expression) | |
|
|
||
| override def checkInputDataTypes(): TypeCheckResult = { | ||
| (left.dataType, right.dataType) match { | ||
| case (ArrayType(_, cn), ArrayType(_, _)) => | ||
| if (!cn) { | ||
| TypeCheckResult.TypeCheckSuccess | ||
| } else { | ||
| TypeCheckResult.TypeCheckFailure("All of the given keys should be non-null") | ||
| } | ||
| case (ArrayType(_, _), ArrayType(_, _)) => | ||
| TypeCheckResult.TypeCheckSuccess | ||
| case _ => | ||
| TypeCheckResult.TypeCheckFailure("The given two arguments should be an array") | ||
| } | ||
|
|
@@ -281,18 +277,39 @@ case class CreateMapFromArrays(left: Expression, right: Expression) | |
| if (keyArrayData.numElements != valueArrayData.numElements) { | ||
| throw new RuntimeException("The given two arrays should have the same length") | ||
| } | ||
| val leftArrayType = left.dataType.asInstanceOf[ArrayType] | ||
| if (leftArrayType.containsNull) { | ||
| if (keyArrayData.toArray(leftArrayType.elementType).contains(null)) { | ||
| throw new RuntimeException("Cannot use null as map key!") | ||
| } | ||
| } | ||
| new ArrayBasedMapData(keyArrayData.copy(), valueArrayData.copy()) | ||
| } | ||
|
|
||
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| nullSafeCodeGen(ctx, ev, (keyArrayData, valueArrayData) => { | ||
| val arrayBasedMapData = classOf[ArrayBasedMapData].getName | ||
| val leftArrayType = left.dataType.asInstanceOf[ArrayType] | ||
| val keyArrayElemNullCheck = if (!leftArrayType.containsNull) "" else { | ||
| val leftArrayTypeTerm = ctx.addReferenceObj("leftArrayType", leftArrayType.elementType) | ||
| val array = ctx.freshName("array") | ||
| val i = ctx.freshName("i") | ||
| s""" | ||
| |Object[] $array = $keyArrayData.toObjectArray($leftArrayTypeTerm); | ||
| |for (int $i = 0; $i < $array.length; $i++) { | ||
| | if ($array[$i] == null) { | ||
| | throw new RuntimeException("Cannot use null as map key!"); | ||
| | } | ||
| |} | ||
|
||
| """.stripMargin | ||
| } | ||
| s""" | ||
| |if ($keyArrayData.numElements() != $valueArrayData.numElements()) { | ||
| | throw new RuntimeException("The given two arrays should have the same length"); | ||
| |} | ||
| |$keyArrayElemNullCheck | ||
| |${ev.value} = new $arrayBasedMapData($keyArrayData.copy(), $valueArrayData.copy()); | ||
| """ | ||
| """.stripMargin | ||
| }) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -195,21 +195,23 @@ class ComplexTypeSuite extends SparkFunSuite with ExpressionEvalHelper { | |
| val intSeq = Seq(5, 10, 15, 20, 25) | ||
| val longSeq = intSeq.map(_.toLong) | ||
| val strSeq = intSeq.map(_.toString) | ||
| val intDupSeq = Seq(5, 10, 15, 15, 5) | ||
| val integerSeq = Seq[java.lang.Integer](5, 10, 15, 20, 25) | ||
| val intWithNullSeq = Seq[java.lang.Integer](5, 10, null, 20, 25) | ||
| val longWithNullSeq = intSeq.map(java.lang.Long.valueOf(_)) | ||
|
|
||
| val intArray = Literal.create(intSeq, ArrayType(IntegerType, false)) | ||
| val longArray = Literal.create(longSeq, ArrayType(LongType, false)) | ||
| val strArray = Literal.create(strSeq, ArrayType(StringType, false)) | ||
|
|
||
| val integerArray = Literal.create(integerSeq, ArrayType(IntegerType, true)) | ||
| val intwithNullArray = Literal.create(intWithNullSeq, ArrayType(IntegerType, true)) | ||
|
||
| val longwithNullArray = Literal.create(longWithNullSeq, ArrayType(LongType, true)) | ||
|
||
|
|
||
| val nullArray = Literal.create(null, ArrayType(StringType, false)) | ||
|
|
||
| checkEvaluation(CreateMapFromArrays(intArray, longArray), createMap(intSeq, longSeq)) | ||
| checkEvaluation(CreateMapFromArrays(intArray, strArray), createMap(intSeq, strSeq)) | ||
| checkEvaluation(CreateMapFromArrays(integerArray, strArray), createMap(integerSeq, strSeq)) | ||
|
|
||
| checkEvaluation( | ||
| CreateMapFromArrays(strArray, intwithNullArray), createMap(strSeq, intWithNullSeq)) | ||
|
|
@@ -219,6 +221,9 @@ class ComplexTypeSuite extends SparkFunSuite with ExpressionEvalHelper { | |
| CreateMapFromArrays(strArray, longwithNullArray), createMap(strSeq, longWithNullSeq)) | ||
| checkEvaluation(CreateMapFromArrays(nullArray, nullArray), null) | ||
|
|
||
| intercept[RuntimeException] { | ||
| checkEvaluation(CreateMapFromArrays(intwithNullArray, strArray), null) | ||
| } | ||
| intercept[RuntimeException] { | ||
| checkEvaluation( | ||
| CreateMapFromArrays(intArray, Literal.create(Seq(1), ArrayType(IntegerType))), null) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use loop to null-check without converting to object array?