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

    https://github.com/apache/spark/pull/22745#discussion_r227356448
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala
 ---
    @@ -1787,3 +1787,75 @@ case class ValidateExternalType(child: Expression, 
expected: DataType)
         ev.copy(code = code, isNull = input.isNull)
       }
     }
    +
    +object GetArrayFromMap {
    +  abstract class Source
    +  case class Key() extends Source
    +  case class Value() extends Source
    +}
    +
    +/**
    + * Extracts a key/value array from a Map expression.
    + *
    + * @param child a Map expression to extract array from
    + * @param source source of array elements, can be `Key` or `Value`
    + */
    +case class GetArrayFromMap(
    +    child: Expression,
    +    source: GetArrayFromMap.Source) extends Expression with 
NonSQLExpression {
    +
    +  import GetArrayFromMap._
    +
    +  private val functionName: String = source match {
    +    case Key() => "keyArray"
    +    case Value() => "valueArray"
    +  }
    +
    +  private lazy val encodedFunctionName = 
TermName(functionName).encodedName.toString
    +
    +  override def nullable: Boolean = child.nullable
    +  override def children: Seq[Expression] = child :: Nil
    +
    +  lazy val dataType: DataType = {
    +    child.dataType match {
    +      case MapType(kt, vt, _) =>
    +        source match {
    +          case Key() => ArrayType(kt)
    +          case Value() => ArrayType(vt)
    +        }
    +      case other =>
    +        throw new RuntimeException(
    +          s"Can't extract array from $child: need map type but got 
${other.catalogString}")
    +    }
    +  }
    +
    +  override def eval(input: InternalRow): Any = {
    +    val mapObj = child.eval(input)
    +    if (!mapObj.isInstanceOf[MapData]) {
    +      throw new RuntimeException(
    +        s"Can't extract array from $child: need map type but got 
${mapObj.getClass}")
    +    }
    +    if (mapObj == null) {
    +      null
    +    } else {
    +      val method = mapObj.getClass.getDeclaredMethod(functionName)
    --- End diff --
    
    well this does reuse the `functionName`, but performance is more important 
here. how about
    ```
    private lazy val arrayGetter: MapData => ArrayData = if (source) ...
    
    def eval...
      arrayGetter( input.asInstanceOf[MapData])
    ```


---

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

Reply via email to