-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[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 |
|---|---|---|
|
|
@@ -248,29 +248,18 @@ case class CreateMap(children: Seq[Expression]) extends Expression { | |
| > SELECT _FUNC_([1.0, 3.0], ['2', '4']); | ||
| {1.0:"2",3.0:"4"} | ||
| """, since = "2.4.0") | ||
| case class CreateMapFromArrays(left: Expression, right: Expression) | ||
| case class MapFromArrays(left: Expression, right: Expression) | ||
| extends BinaryExpression with ExpectsInputTypes { | ||
|
||
|
|
||
| override def inputTypes: Seq[AbstractDataType] = Seq(ArrayType, ArrayType) | ||
|
|
||
| override def checkInputDataTypes(): TypeCheckResult = { | ||
| (left.dataType, right.dataType) match { | ||
| case (ArrayType(_, _), ArrayType(_, _)) => | ||
| TypeCheckResult.TypeCheckSuccess | ||
| case _ => | ||
| TypeCheckResult.TypeCheckFailure("The given two arguments should be an array") | ||
| } | ||
| } | ||
|
|
||
| override def dataType: DataType = { | ||
| MapType( | ||
| keyType = left.dataType.asInstanceOf[ArrayType].elementType, | ||
| valueType = right.dataType.asInstanceOf[ArrayType].elementType, | ||
| valueContainsNull = right.dataType.asInstanceOf[ArrayType].containsNull) | ||
| } | ||
|
|
||
| override def nullable: Boolean = left.nullable || right.nullable | ||
|
|
||
| override def nullSafeEval(keyArray: Any, valueArray: Any): Any = { | ||
| val keyArrayData = keyArray.asInstanceOf[ArrayData] | ||
|
||
| val valueArrayData = valueArray.asInstanceOf[ArrayData] | ||
|
|
@@ -279,8 +268,12 @@ case class CreateMapFromArrays(left: Expression, right: Expression) | |
| } | ||
| 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!") | ||
| var i = 0 | ||
| while (i < keyArrayData.numElements) { | ||
| if (keyArrayData.isNullAt(i)) { | ||
| throw new RuntimeException("Cannot use null as map key!") | ||
| } | ||
| i += 1 | ||
| } | ||
|
||
| } | ||
| new ArrayBasedMapData(keyArrayData.copy(), valueArrayData.copy()) | ||
|
|
@@ -291,13 +284,10 @@ case class CreateMapFromArrays(left: Expression, right: Expression) | |
| 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) { | ||
| |for (int $i = 0; $i < $keyArrayData.numElements(); $i++) { | ||
| | if ($keyArrayData.isNullAt($i)) { | ||
| | throw new RuntimeException("Cannot use null as map key!"); | ||
| | } | ||
| |} | ||
|
||
|
|
||
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.
and duplicated?