Github user yhuai commented on a diff in the pull request:
https://github.com/apache/spark/pull/9019#discussion_r41566470
--- 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 {
--- End diff --
Do we need to take care java types like `Integer` at here?
---
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]