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

    https://github.com/apache/spark/pull/9019#discussion_r41567674
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/ScalaReflection.scala 
---
    @@ -75,6 +76,242 @@ trait ScalaReflection {
        */
       private def localTypeOf[T: TypeTag]: `Type` = typeTag[T].in(mirror).tpe
     
    +  /**
    +   * Returns the Spark SQL DataType for a given scala type.  Where this is 
not an exact mapping
    +   * to a native type, an ObjectType is returned. Special handling is also 
used for Arrays including
    +   * those that hold primitive types.
    +   */
    +  def dataTypeFor(tpe: `Type`): DataType = tpe match {
    +    case t if t <:< definitions.IntTpe => IntegerType
    +    case t if t <:< definitions.LongTpe => LongType
    +    case t if t <:< definitions.DoubleTpe => DoubleType
    +    case t if t <:< definitions.FloatTpe => FloatType
    +    case t if t <:< definitions.ShortTpe => ShortType
    +    case t if t <:< definitions.ByteTpe => ByteType
    +    case t if t <:< definitions.BooleanTpe => BooleanType
    +    case t if t <:< localTypeOf[Array[Byte]] => BinaryType
    +    case _ =>
    +      val className: String = tpe.erasure.typeSymbol.asClass.fullName
    +      className match {
    +        case "scala.Array" =>
    +          val TypeRef(_, _, Seq(arrayType)) = tpe
    +          val cls = arrayType match {
    +            case t if t <:< definitions.IntTpe => classOf[Array[Int]]
    +            case t if t <:< definitions.LongTpe => classOf[Array[Long]]
    +            case t if t <:< definitions.DoubleTpe => classOf[Array[Double]]
    +            case t if t <:< definitions.FloatTpe => classOf[Array[Float]]
    +            case t if t <:< definitions.ShortTpe => classOf[Array[Short]]
    +            case t if t <:< definitions.ByteTpe => classOf[Array[Byte]]
    +            case t if t <:< definitions.BooleanTpe => 
classOf[Array[Boolean]]
    +            case other =>
    +              // There is probably a better way to do this, but I couldn't 
find it...
    +              val elementType = 
dataTypeFor(other).asInstanceOf[ObjectType].cls
    +              java.lang.reflect.Array.newInstance(elementType, 1).getClass
    +
    +          }
    +          ObjectType(cls)
    +        case other => ObjectType(Utils.classForName(className))
    +      }
    +  }
    +
    +  /** Returns expressions for extracting all the fields from the given 
type. */
    +  def extractorsFor[T : TypeTag](inputObject: Expression): Seq[Expression] 
= {
    +    ScalaReflectionLock.synchronized {
    +      extractorFor(inputObject, 
typeTag[T].tpe).asInstanceOf[CreateStruct].children
    +    }
    +  }
    +
    +  /** Helper for extracting internal fields from a case class. */
    +  protected def extractorFor(
    +      inputObject: Expression,
    +      tpe: `Type`): Expression = ScalaReflectionLock.synchronized {
    +    if (!inputObject.dataType.isInstanceOf[ObjectType]) {
    +      inputObject
    +    } else {
    +      tpe match {
    +        case t if t <:< localTypeOf[Option[_]] =>
    +          val TypeRef(_, _, Seq(optType)) = t
    +          optType match {
    +            // For primitive types we must manually unbox the value of the 
object.
    +            case t if t <:< definitions.IntTpe =>
    +              Invoke(
    +                UnwrapOption(ObjectType(classOf[java.lang.Integer]), 
inputObject),
    +                "intValue",
    +                IntegerType)
    +            case t if t <:< definitions.LongTpe =>
    +              Invoke(
    +                UnwrapOption(ObjectType(classOf[java.lang.Long]), 
inputObject),
    +                "longValue",
    +                LongType)
    +            case t if t <:< definitions.DoubleTpe =>
    +              Invoke(
    +                UnwrapOption(ObjectType(classOf[java.lang.Double]), 
inputObject),
    +                "doubleValue",
    +                DoubleType)
    +            case t if t <:< definitions.FloatTpe =>
    +              Invoke(
    +                UnwrapOption(ObjectType(classOf[java.lang.Float]), 
inputObject),
    +                "floatValue",
    +                FloatType)
    +            case t if t <:< definitions.ShortTpe =>
    +              Invoke(
    +                UnwrapOption(ObjectType(classOf[java.lang.Short]), 
inputObject),
    +                "shortValue",
    +                ShortType)
    +            case t if t <:< definitions.ByteTpe =>
    +              Invoke(
    +                UnwrapOption(ObjectType(classOf[java.lang.Byte]), 
inputObject),
    +                "byteValue",
    +                ByteType)
    +            case t if t <:< definitions.BooleanTpe =>
    +              Invoke(
    +                UnwrapOption(ObjectType(classOf[java.lang.Boolean]), 
inputObject),
    +                "booleanValue",
    +                BooleanType)
    +
    +            // For non-primitives, we can just extract the object from the 
Option and then recurse.
    +            case other =>
    +              val className: String = 
optType.erasure.typeSymbol.asClass.fullName
    +              val classObj = Utils.classForName(className)
    +              val optionObjectType = ObjectType(classObj)
    +
    +              val unwrapped = UnwrapOption(optionObjectType, inputObject)
    +              expressions.If(
    +                IsNull(unwrapped),
    +                expressions.Literal.create(null, 
schemaFor(optType).dataType),
    +                extractorFor(unwrapped, optType))
    +          }
    +
    +        case t if t <:< localTypeOf[Product] =>
    +          val formalTypeArgs = t.typeSymbol.asClass.typeParams
    +          val TypeRef(_, _, actualTypeArgs) = t
    +          val constructorSymbol = t.member(nme.CONSTRUCTOR)
    +          val params = if (constructorSymbol.isMethod) {
    +            constructorSymbol.asMethod.paramss
    +          } else {
    +            // Find the primary constructor, and use its parameter 
ordering.
    +            val primaryConstructorSymbol: Option[Symbol] =
    +              constructorSymbol.asTerm.alternatives.find(s =>
    +                s.isMethod && s.asMethod.isPrimaryConstructor)
    +
    +            if (primaryConstructorSymbol.isEmpty) {
    +              sys.error("Internal SQL error: Product object did not have a 
primary constructor.")
    +            } else {
    +              primaryConstructorSymbol.get.asMethod.paramss
    +            }
    +          }
    +
    +          CreateStruct(params.head.map { p =>
    --- End diff --
    
    Maybe explain why we need to use `params.head`?


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to