You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by aarondav <gi...@git.apache.org> on 2015/04/10 01:13:02 UTC

[GitHub] spark pull request: [SQL] [SPARK-6620] Speed up toDF() and rdd() f...

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

    https://github.com/apache/spark/pull/5279#discussion_r28111433
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/CatalystTypeConverters.scala ---
    @@ -0,0 +1,294 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.catalyst
    +
    +import java.util.{Map => JavaMap}
    +
    +import scala.collection.mutable.HashMap
    +
    +import org.apache.spark.sql.catalyst.expressions._
    +import org.apache.spark.sql.types._
    +
    +/**
    + * Functions to convert Scala types to Catalyst types and vice versa.
    + */
    +object CatalystTypeConverters {
    +  // The Predef.Map is scala.collection.immutable.Map.
    +  // Since the map values can be mutable, we explicitly import scala.collection.Map at here.
    +  import scala.collection.Map
    +
    +  /**
    +   * Converts Scala objects to catalyst rows / types. This method is slow, and for batch
    +   * conversion you should be using converter produced by createToCatalystConverter.
    +   * Note: This is always called after schemaFor has been called.
    +   *       This ordering is important for UDT registration.
    +   */
    +  def convertToCatalyst(a: Any, dataType: DataType): Any = (a, dataType) match {
    +    // Check UDT first since UDTs can override other types
    +    case (obj, udt: UserDefinedType[_]) =>
    +      udt.serialize(obj)
    +
    +    case (o: Option[_], _) =>
    +      o.map(convertToCatalyst(_, dataType)).orNull
    +
    +    case (s: Seq[_], arrayType: ArrayType) =>
    +      s.map(convertToCatalyst(_, arrayType.elementType))
    +
    +    case (s: Array[_], arrayType: ArrayType) =>
    +      s.toSeq.map(convertToCatalyst(_, arrayType.elementType))
    +
    +    case (m: Map[_, _], mapType: MapType) =>
    +      m.map { case (k, v) =>
    +        convertToCatalyst(k, mapType.keyType) -> convertToCatalyst(v, mapType.valueType)
    +      }
    +
    +    case (jmap: JavaMap[_, _], mapType: MapType) =>
    +      val iter = jmap.entrySet.iterator
    +      var listOfEntries: List[(Any, Any)] = List()
    +      while (iter.hasNext) {
    +        val entry = iter.next()
    +        listOfEntries :+= (convertToCatalyst(entry.getKey, mapType.keyType),
    +          convertToCatalyst(entry.getValue, mapType.valueType))
    +      }
    +      listOfEntries.toMap
    +
    +    case (p: Product, structType: StructType) =>
    +      val ar = new Array[Any](structType.size)
    +      val iter = p.productIterator
    +      var idx = 0
    +      while (idx < structType.size) {
    +        ar(idx) = convertToCatalyst(iter.next(), structType.fields(idx).dataType)
    +        idx += 1
    +      }
    +      new GenericRowWithSchema(ar, structType)
    +
    +    case (d: BigDecimal, _) =>
    +      Decimal(d)
    +
    +    case (d: java.math.BigDecimal, _) =>
    +      Decimal(d)
    +
    +    case (d: java.sql.Date, _) =>
    +      DateUtils.fromJavaDate(d)
    +
    +    case (r: Row, structType: StructType) =>
    +      val converters = structType.fields.map {
    +        f => (item: Any) => convertToCatalyst(item, f.dataType)
    +      }
    +      convertRowWithConverters(r, structType, converters)
    +
    +    case (other, _) =>
    +      other
    +  }
    +
    +  /**
    +   * Creates a converter function that will convert Scala objects to the specified catalyst type.
    +   * Typical use case would be converting a collection of rows that have the same schema. You will
    +   * call this function once to get a converter, and apply it to every row.
    +   */
    +  private[sql] def createToCatalystConverter(dataType: DataType): Any => Any = {
    +    def extractOption(item: Any): Any = item match {
    +      case opt: Option[_] => opt.orNull
    +      case other => other
    +    }
    +
    +    dataType match {
    +      // Check UDT first since UDTs can override other types
    +      case udt: UserDefinedType[_] =>
    +        (item) => extractOption(item) match {
    +          case null => null
    +          case other => udt.serialize(other)
    +        }
    +
    +      case arrayType: ArrayType =>
    +        val elementConverter = createToCatalystConverter(arrayType.elementType)
    +        (item: Any) => {
    +          extractOption(item) match {
    +            case a: Array[_] => a.toSeq.map(elementConverter)
    +            case s: Seq[_] => s.map(elementConverter)
    +            case null => null
    +          }
    +        }
    +
    +      case mapType: MapType =>
    +        val keyConverter = createToCatalystConverter(mapType.keyType)
    +        val valueConverter = createToCatalystConverter(mapType.valueType)
    +        (item: Any) => {
    +          extractOption(item) match {
    +            case m: Map[_, _] =>
    +              m.map { case (k, v) =>
    +                keyConverter(k) -> valueConverter(v)
    +              }
    +
    +            case jmap: JavaMap[_, _] =>
    +              val iter = jmap.entrySet.iterator
    +              val convertedMap: HashMap[Any, Any] = HashMap()
    +              while (iter.hasNext) {
    +                val entry = iter.next()
    +                convertedMap += Tuple2(keyConverter(entry.getKey), valueConverter(entry.getValue))
    --- End diff --
    
    nit: use 
    ```convertedMap(keyConverter(entry.getKey)) = valueConverter(entry.getValue)```
    to avoid creating a tuple.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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