Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13909#discussion_r93812285
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/complexTypeCreator.scala
 ---
    @@ -133,49 +183,72 @@ case class CreateMap(children: Seq[Expression]) 
extends Expression {
       }
     
       override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
    -    val arrayClass = classOf[GenericArrayData].getName
         val mapClass = classOf[ArrayBasedMapData].getName
         val keyArray = ctx.freshName("keyArray")
         val valueArray = ctx.freshName("valueArray")
    -    ctx.addMutableState("Object[]", keyArray, s"this.$keyArray = null;")
    -    ctx.addMutableState("Object[]", valueArray, s"this.$valueArray = 
null;")
     
    -    val keyData = s"new $arrayClass($keyArray)"
    -    val valueData = s"new $arrayClass($valueArray)"
    -    ev.copy(code = s"""
    -      $keyArray = new Object[${keys.size}];
    -      $valueArray = new Object[${values.size}];""" +
    -      ctx.splitExpressions(
    -        ctx.INPUT_ROW,
    -        keys.zipWithIndex.map { case (key, i) =>
    -          val eval = key.genCode(ctx)
    -          s"""
    -            ${eval.code}
    -            if (${eval.isNull}) {
    -              throw new RuntimeException("Cannot use null as map key!");
    -            } else {
    -              $keyArray[$i] = ${eval.value};
    -            }
    -          """
    -        }) +
    -      ctx.splitExpressions(
    -        ctx.INPUT_ROW,
    -        values.zipWithIndex.map { case (value, i) =>
    -          val eval = value.genCode(ctx)
    -          s"""
    -            ${eval.code}
    -            if (${eval.isNull}) {
    -              $valueArray[$i] = null;
    -            } else {
    -              $valueArray[$i] = ${eval.value};
    -            }
    -          """
    -        }) +
    -      s"""
    -        final MapData ${ev.value} = new $mapClass($keyData, $valueData);
    -        this.$keyArray = null;
    -        this.$valueArray = null;
    -      """, isNull = "false")
    +    val MapType(keyDt, valueDt, _) = dataType
    +    val evalKeys = keys.map(e => e.genCode(ctx))
    +    val isPrimitiveArrayKey = ctx.isPrimitiveType(keyDt)
    +    val evalValues = values.map(e => e.genCode(ctx))
    +    val isPrimitiveArrayValue = ctx.isPrimitiveType(valueDt)
    +    val (preprocessKeyData, keyDataArray) =
    +      GenArrayData.getCodeArrayData(ctx, keyDt, keys.size, 
isPrimitiveArrayKey, keyArray)
    +    val (preprocessValueData, valueDataArray) =
    +      GenArrayData.getCodeArrayData(ctx, valueDt, values.size, 
isPrimitiveArrayValue, valueArray)
    +
    +    val assignKeys = if (isPrimitiveArrayKey) {
    +      val primitiveKeyTypeName = ctx.primitiveTypeName(keyDt)
    +      evalKeys.zipWithIndex.map { case (eval, i) =>
    +        eval.code + s"""
    +         if (${eval.isNull}) {
    +           $keyDataArray.setNullAt($i);
    +         } else {
    +           $keyDataArray.set$primitiveKeyTypeName($i, ${eval.value});
    +         }
    +       """
    +      }
    +    } else {
    +      evalKeys.zipWithIndex.map { case (eval, i) =>
    +        eval.code + s"""
    +         if (${eval.isNull}) {
    +           throw new RuntimeException("Cannot use null as map key!");
    +         } else {
    +           $keyArray[$i] = ${eval.value};
    +         }
    +       """
    +      }
    +    }
    +
    +    val assignValues = if (isPrimitiveArrayValue) {
    --- End diff --
    
    seems the assignment code can also be put in the util function, we need to 
pass more parameters though, and a `allowNull: Boolean` flag


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to