Github user ueshin commented on a diff in the pull request:
https://github.com/apache/spark/pull/21258#discussion_r193929951
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/complexTypeCreator.scala
---
@@ -236,6 +236,76 @@ case class CreateMap(children: Seq[Expression])
extends Expression {
override def prettyName: String = "map"
}
+/**
+ * Returns a catalyst Map containing the two arrays in children
expressions as keys and values.
+ */
+@ExpressionDescription(
+ usage = """
+ _FUNC_(keys, values) - Creates a map with a pair of the given
key/value arrays. All elements
+ in keys should not be null""",
+ examples = """
+ Examples:
+ > SELECT _FUNC_([1.0, 3.0], ['2', '4']);
+ {1.0:"2",3.0:"4"}
+ """, since = "2.4.0")
+case class MapFromArrays(left: Expression, right: Expression)
+ extends BinaryExpression with ExpectsInputTypes {
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(ArrayType,
ArrayType)
+
+ 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 nullSafeEval(keyArray: Any, valueArray: Any): Any = {
+ val keyArrayData = keyArray.asInstanceOf[ArrayData]
+ val valueArrayData = valueArray.asInstanceOf[ArrayData]
+ 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) {
+ 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())
+ }
+
+ 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 i = ctx.freshName("i")
+ s"""
+ |for (int $i = 0; $i < $keyArrayData.numElements(); $i++) {
+ | if ($keyArrayData.isNullAt($i)) {
+ | 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
+ })
+ }
+
+ override def prettyName: String = "create_map_from_arrays"
--- End diff --
`map_from_arrays`?
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]