@@ -30,10 +30,10 @@ import org.apache.spark.sql.catalyst.expressions._
30
30
import org .apache .spark .sql .catalyst .expressions .codegen .{GenerateSafeProjection , GenerateUnsafeProjection }
31
31
import org .apache .spark .sql .catalyst .InternalRow
32
32
import 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 }
34
34
35
35
/**
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
37
37
* internal row format using catalyst expressions and code generation. By default, the
38
38
* expressions used to retrieve values from an input row when producing an object will be created as
39
39
* follows:
@@ -44,20 +44,21 @@ import org.apache.spark.sql.types.{NullType, StructField, ObjectType, StructType
44
44
* to the name `value`.
45
45
*/
46
46
object ExpressionEncoder {
47
- def apply [T : TypeTag ](flat : Boolean = false ): ExpressionEncoder [T ] = {
47
+ def apply [T : TypeTag ](): ExpressionEncoder [T ] = {
48
48
// We convert the not-serializable TypeTag into StructType and ClassTag.
49
49
val mirror = typeTag[T ].mirror
50
50
val cls = mirror.runtimeClass(typeTag[T ].tpe)
51
+ val flat = ! classOf [Product ].isAssignableFrom(cls)
51
52
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 ]
55
56
56
57
new ExpressionEncoder [T ](
57
- extractExpression .dataType,
58
+ toRowExpression .dataType,
58
59
flat,
59
- extractExpression .flatten,
60
- constructExpression ,
60
+ toRowExpression .flatten,
61
+ fromRowExpression ,
61
62
ClassTag [T ](cls))
62
63
}
63
64
0 commit comments