Github user liancheng commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14344#discussion_r72045939
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala
 ---
    @@ -501,6 +501,143 @@ case class MapObjects private(
       }
     }
     
    +object ExternalMapToCatalyst {
    +  private val curId = new java.util.concurrent.atomic.AtomicInteger()
    +
    +  def apply(
    +      inputMap: Expression,
    +      keyType: DataType,
    +      keyConverter: Expression => Expression,
    +      valueType: DataType,
    +      valueConverter: Expression => Expression): ExternalMapToCatalyst = {
    +    val id = curId.getAndIncrement()
    +    val keyName = "ExternalMapToCatalyst_key" + id
    +    val valueName = "ExternalMapToCatalyst_value" + id
    +    val valueIsNull = "ExternalMapToCatalyst_value_isNull" + id
    +
    +    ExternalMapToCatalyst(
    +      keyName,
    +      keyType,
    +      keyConverter(LambdaVariable(keyName, "false", keyType)),
    +      valueName,
    +      valueIsNull,
    +      valueType,
    +      valueConverter(LambdaVariable(valueName, valueIsNull, valueType)),
    +      inputMap
    +    )
    +  }
    +}
    +
    +case class ExternalMapToCatalyst private(
    +    key: String,
    +    keyType: DataType,
    +    keyConverter: Expression,
    +    value: String,
    +    valueIsNull: String,
    +    valueType: DataType,
    +    valueConverter: Expression,
    +    child: Expression)
    +  extends UnaryExpression with NonSQLExpression {
    +
    +  override def foldable: Boolean = false
    +
    +  override def dataType: MapType = MapType(keyConverter.dataType, 
valueConverter.dataType)
    +
    +  override def eval(input: InternalRow): Any =
    +    throw new UnsupportedOperationException("Only code-generated 
evaluation is supported")
    +
    +  override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): 
ExprCode = {
    +    val inputMap = child.genCode(ctx)
    +    val genKeyConverter = keyConverter.genCode(ctx)
    +    val genValueConverter = valueConverter.genCode(ctx)
    +    val length = ctx.freshName("length")
    +    val index = ctx.freshName("index")
    +    val convertedKeys = ctx.freshName("convertedKeys")
    +    val convertedValues = ctx.freshName("convertedValues")
    +    val entry = ctx.freshName("entry")
    +    val entries = ctx.freshName("entries")
    +
    +    val (defineEntries, defineKeyValue) = child.dataType match {
    +      case ObjectType(cls) if classOf[java.util.Map[_, 
_]].isAssignableFrom(cls) =>
    +        val javaIteratorCls = classOf[java.util.Iterator[_]].getName
    +        val javaMapEntryCls = classOf[java.util.Map.Entry[_, _]].getName
    +
    +        val defineEntries =
    +          s"final $javaIteratorCls $entries = 
${inputMap.value}.entrySet().iterator();"
    +
    +        val defineKeyValue =
    +          s"""
    +            final $javaMapEntryCls $entry = ($javaMapEntryCls) 
$entries.next();
    +            ${ctx.javaType(keyType)} $key = (${ctx.boxedType(keyType)}) 
$entry.getKey();
    +            ${ctx.javaType(valueType)} $value = 
(${ctx.boxedType(valueType)}) $entry.getValue();
    +          """
    +
    +        defineEntries -> defineKeyValue
    +
    +      case ObjectType(cls) if classOf[scala.collection.Map[_, 
_]].isAssignableFrom(cls) =>
    +        val scalaIteratorCls = classOf[Iterator[_]].getName
    +        val scalaMapEntryCls = classOf[Tuple2[_, _]].getName
    +
    +        val defineEntries = s"final $scalaIteratorCls $entries = 
${inputMap.value}.iterator();"
    +
    +        val defineKeyValue =
    +          s"""
    +            final $scalaMapEntryCls $entry = ($scalaMapEntryCls) 
$entries.next();
    +            ${ctx.javaType(keyType)} $key = (${ctx.boxedType(keyType)}) 
$entry._1();
    +            ${ctx.javaType(valueType)} $value = 
(${ctx.boxedType(valueType)}) $entry._2();
    +          """
    +
    +        defineEntries -> defineKeyValue
    +    }
    +
    +    val valueNullCheck = if (ctx.isPrimitiveType(valueType)) {
    +      s"boolean $valueIsNull = false;"
    +    } else {
    +      s"boolean $valueIsNull = $value == null;"
    +    }
    +
    +    val arrayCls = classOf[GenericArrayData].getName
    +    val mapCls = classOf[ArrayBasedMapData].getName
    +    val convertedKeyType = ctx.boxedType(keyConverter.dataType)
    +    val convertedValueType = ctx.boxedType(valueConverter.dataType)
    +    val code =
    +      s"""
    +        ${inputMap.code}
    +        ${ctx.javaType(dataType)} ${ev.value} = 
${ctx.defaultValue(dataType)};
    +        if (!${inputMap.isNull}) {
    +          final int $length = ${inputMap.value}.size();
    +          final Object[] $convertedKeys = new Object[$length];
    +          final Object[] $convertedValues = new Object[$length];
    +          int $index = 0;
    +          $defineEntries
    +          while($entries.hasNext()) {
    +            $defineKeyValue
    +            $valueNullCheck
    +
    +            ${genKeyConverter.code}
    +            if (${genKeyConverter.isNull}) {
    +              throw new RuntimeException("Cannot use null as map key!");
    +            } else {
    +              $convertedKeys[$index] = ($convertedKeyType) 
${genKeyConverter.value};
    --- End diff --
    
    Hm, I'd expect auto-boxing happens here, but I can be wrong...


---
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