@@ -30,10 +30,10 @@ import org.apache.spark.sql.catalyst.expressions._
3030import org .apache .spark .sql .catalyst .expressions .codegen .{GenerateSafeProjection , GenerateUnsafeProjection }
3131import org .apache .spark .sql .catalyst .InternalRow
3232import org .apache .spark .sql .catalyst .ScalaReflection
33- import org .apache .spark .sql .types .{NullType , StructField , ObjectType , StructType }
33+ import org .apache .spark .sql .types .{StructField , ObjectType , StructType }
3434
3535/**
36- * A factory for constructing encoders that convert objects and primitves to and from the
36+ * A factory for constructing encoders that convert objects and primitives to and from the
3737 * internal row format using catalyst expressions and code generation. By default, the
3838 * expressions used to retrieve values from an input row when producing an object will be created as
3939 * follows:
@@ -44,20 +44,21 @@ import org.apache.spark.sql.types.{NullType, StructField, ObjectType, StructType
4444 * to the name `value`.
4545 */
4646object ExpressionEncoder {
47- def apply [T : TypeTag ](flat : Boolean = false ): ExpressionEncoder [T ] = {
47+ def apply [T : TypeTag ](): ExpressionEncoder [T ] = {
4848 // We convert the not-serializable TypeTag into StructType and ClassTag.
4949 val mirror = typeTag[T ].mirror
5050 val cls = mirror.runtimeClass(typeTag[T ].tpe)
51+ val flat = ! classOf [Product ].isAssignableFrom(cls)
5152
52- val inputObject = BoundReference (0 , ObjectType (cls) , nullable = true )
53- val extractExpression = ScalaReflection .extractorsFor[T ](inputObject)
54- val constructExpression = ScalaReflection .constructorFor[T ]
53+ val inputObject = BoundReference (0 , ScalaReflection .dataTypeFor[ T ] , nullable = true )
54+ val toRowExpression = ScalaReflection .extractorsFor[T ](inputObject)
55+ val fromRowExpression = ScalaReflection .constructorFor[T ]
5556
5657 new ExpressionEncoder [T ](
57- extractExpression .dataType,
58+ toRowExpression .dataType,
5859 flat,
59- extractExpression .flatten,
60- constructExpression ,
60+ toRowExpression .flatten,
61+ fromRowExpression ,
6162 ClassTag [T ](cls))
6263 }
6364
0 commit comments