Skip to content

[SPARK-11750][SQL] revert SPARK-11727 and code clean up #9726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions sql/catalyst/src/main/scala/org/apache/spark/sql/Encoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ trait Encoder[T] extends Serializable {
*/
object Encoders {

def BOOLEAN: Encoder[java.lang.Boolean] = ExpressionEncoder(flat = true)
def BYTE: Encoder[java.lang.Byte] = ExpressionEncoder(flat = true)
def SHORT: Encoder[java.lang.Short] = ExpressionEncoder(flat = true)
def INT: Encoder[java.lang.Integer] = ExpressionEncoder(flat = true)
def LONG: Encoder[java.lang.Long] = ExpressionEncoder(flat = true)
def FLOAT: Encoder[java.lang.Float] = ExpressionEncoder(flat = true)
def DOUBLE: Encoder[java.lang.Double] = ExpressionEncoder(flat = true)
def STRING: Encoder[java.lang.String] = ExpressionEncoder(flat = true)
def BOOLEAN: Encoder[java.lang.Boolean] = ExpressionEncoder()
def BYTE: Encoder[java.lang.Byte] = ExpressionEncoder()
def SHORT: Encoder[java.lang.Short] = ExpressionEncoder()
def INT: Encoder[java.lang.Integer] = ExpressionEncoder()
def LONG: Encoder[java.lang.Long] = ExpressionEncoder()
def FLOAT: Encoder[java.lang.Float] = ExpressionEncoder()
def DOUBLE: Encoder[java.lang.Double] = ExpressionEncoder()
def STRING: Encoder[java.lang.String] = ExpressionEncoder()

/**
* (Scala-specific) Creates an encoder that serializes objects of type T using Kryo.
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.codegen.{GenerateSafeProjection, GenerateUnsafeProjection}
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.ScalaReflection
import org.apache.spark.sql.types.{NullType, StructField, ObjectType, StructType}
import org.apache.spark.sql.types.{StructField, ObjectType, StructType}

/**
* A factory for constructing encoders that convert objects and primitves to and from the
* A factory for constructing encoders that convert objects and primitives to and from the
* internal row format using catalyst expressions and code generation. By default, the
* expressions used to retrieve values from an input row when producing an object will be created as
* follows:
Expand All @@ -44,20 +44,21 @@ import org.apache.spark.sql.types.{NullType, StructField, ObjectType, StructType
* to the name `value`.
*/
object ExpressionEncoder {
def apply[T : TypeTag](flat: Boolean = false): ExpressionEncoder[T] = {
def apply[T : TypeTag](): ExpressionEncoder[T] = {
// We convert the not-serializable TypeTag into StructType and ClassTag.
val mirror = typeTag[T].mirror
val cls = mirror.runtimeClass(typeTag[T].tpe)
val flat = !classOf[Product].isAssignableFrom(cls)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't going to work for java. We should just leave this as it was.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we need a way to detect flatness automatically, sometimes we only have the type T and nothing else, like when we wanna use encoders for ScalaUDF. How about flat = !ScalaReflection.schemaFor[T].isInstanceOf[StructType])?


val inputObject = BoundReference(0, ObjectType(cls), nullable = true)
val extractExpression = ScalaReflection.extractorsFor[T](inputObject)
val constructExpression = ScalaReflection.constructorFor[T]
val inputObject = BoundReference(0, ScalaReflection.dataTypeFor[T], nullable = true)
val toRowExpression = ScalaReflection.extractorsFor[T](inputObject)
val fromRowExpression = ScalaReflection.constructorFor[T]

new ExpressionEncoder[T](
extractExpression.dataType,
toRowExpression.dataType,
flat,
extractExpression.flatten,
constructExpression,
toRowExpression.flatten,
fromRowExpression,
ClassTag[T](cls))
}

Expand Down

This file was deleted.

Loading