/
RecordEncoder.scala
308 lines (250 loc) · 10.1 KB
/
RecordEncoder.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package frameless
import org.apache.spark.sql.FramelessInternals
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.objects.{
Invoke, NewInstance, UnwrapOption, WrapOption
}
import org.apache.spark.sql.types._
import shapeless._
import shapeless.labelled.FieldType
import shapeless.ops.hlist.IsHCons
import shapeless.ops.record.Keys
import scala.reflect.ClassTag
case class RecordEncoderField(
ordinal: Int,
name: String,
encoder: TypedEncoder[_]
)
trait RecordEncoderFields[T <: HList] extends Serializable {
def value: List[RecordEncoderField]
override def toString: String =
s"""RecordEncoderFields${value.mkString("[", ", ", "]")}"""
}
object RecordEncoderFields {
implicit def deriveRecordLast[K <: Symbol, H]
(implicit
key: Witness.Aux[K],
head: RecordFieldEncoder[H]
): RecordEncoderFields[FieldType[K, H] :: HNil] = new RecordEncoderFields[FieldType[K, H] :: HNil] {
def value: List[RecordEncoderField] = fieldEncoder[K, H] :: Nil
}
implicit def deriveRecordCons[K <: Symbol, H, T <: HList]
(implicit
key: Witness.Aux[K],
head: RecordFieldEncoder[H],
tail: RecordEncoderFields[T]
): RecordEncoderFields[FieldType[K, H] :: T] = new RecordEncoderFields[FieldType[K, H] :: T] {
def value: List[RecordEncoderField] =
fieldEncoder[K, H] :: tail.value.map(x => x.copy(ordinal = x.ordinal + 1))
}
private def fieldEncoder[K <: Symbol, H](implicit key: Witness.Aux[K], e: RecordFieldEncoder[H]): RecordEncoderField = RecordEncoderField(0, key.value.name, e.encoder)
}
/**
* Assists the generation of constructor call parameters from a labelled generic representation.
* As Unit typed fields were removed earlier, we need to put back unit literals in the appropriate positions.
*
* @tparam T labelled generic representation of type fields
*/
trait NewInstanceExprs[T <: HList] extends Serializable {
def from(exprs: List[Expression]): Seq[Expression]
}
object NewInstanceExprs {
implicit def deriveHNil: NewInstanceExprs[HNil] = new NewInstanceExprs[HNil] {
def from(exprs: List[Expression]): Seq[Expression] = Nil
}
implicit def deriveUnit[K <: Symbol, T <: HList]
(implicit
tail: NewInstanceExprs[T]
): NewInstanceExprs[FieldType[K, Unit] :: T] = new NewInstanceExprs[FieldType[K, Unit] :: T] {
def from(exprs: List[Expression]): Seq[Expression] =
Literal.fromObject(()) +: tail.from(exprs)
}
implicit def deriveNonUnit[K <: Symbol, V, T <: HList]
(implicit
notUnit: V =:!= Unit,
tail: NewInstanceExprs[T]
): NewInstanceExprs[FieldType[K, V] :: T] = new NewInstanceExprs[FieldType[K, V] :: T] {
def from(exprs: List[Expression]): Seq[Expression] = exprs.head +: tail.from(exprs.tail)
}
}
/**
* Drops fields with Unit type from labelled generic representation of types.
*
* @tparam L labelled generic representation of type fields
*/
trait DropUnitValues[L <: HList] extends DepFn1[L] with Serializable { type Out <: HList }
object DropUnitValues {
def apply[L <: HList](implicit dropUnitValues: DropUnitValues[L]): Aux[L, dropUnitValues.Out] = dropUnitValues
type Aux[L <: HList, Out0 <: HList] = DropUnitValues[L] { type Out = Out0 }
implicit def deriveHNil[H]: Aux[HNil, HNil] = new DropUnitValues[HNil] {
type Out = HNil
def apply(l: HNil): Out = HNil
}
implicit def deriveUnit[K <: Symbol, T <: HList, OutT <: HList]
(implicit
dropUnitValues : DropUnitValues.Aux[T, OutT]
): Aux[FieldType[K, Unit] :: T, OutT] = new DropUnitValues[FieldType[K, Unit] :: T] {
type Out = OutT
def apply(l : FieldType[K, Unit] :: T): Out = dropUnitValues(l.tail)
}
implicit def deriveNonUnit[K <: Symbol, V, T <: HList, OutH, OutT <: HList]
(implicit
nonUnit: V =:!= Unit,
dropUnitValues : DropUnitValues.Aux[T, OutT]
): Aux[FieldType[K, V] :: T, FieldType[K, V] :: OutT] = new DropUnitValues[FieldType[K, V] :: T] {
type Out = FieldType[K, V] :: OutT
def apply(l : FieldType[K, V] :: T): Out = l.head :: dropUnitValues(l.tail)
}
}
class RecordEncoder[F, G <: HList, H <: HList]
(implicit
i0: LabelledGeneric.Aux[F, G],
i1: DropUnitValues.Aux[G, H],
i2: IsHCons[H],
fields: Lazy[RecordEncoderFields[H]],
newInstanceExprs: Lazy[NewInstanceExprs[G]],
classTag: ClassTag[F]
) extends TypedEncoder[F] {
def nullable: Boolean = false
def jvmRepr: DataType = FramelessInternals.objectTypeFor[F]
def catalystRepr: DataType = {
val structFields = fields.value.value.map { field =>
StructField(
name = field.name,
dataType = field.encoder.catalystRepr,
nullable = field.encoder.nullable,
metadata = Metadata.empty
)
}
StructType(structFields)
}
def toCatalyst(path: Expression): Expression = {
val nameExprs = fields.value.value.map { field =>
Literal(field.name)
}
val valueExprs = fields.value.value.map { field =>
val fieldPath = Invoke(path, field.name, field.encoder.jvmRepr, Nil)
field.encoder.toCatalyst(fieldPath)
}
// the way exprs are encoded in CreateNamedStruct
val exprs = nameExprs.zip(valueExprs).flatMap {
case (nameExpr, valueExpr) => nameExpr :: valueExpr :: Nil
}
val createExpr = CreateNamedStruct(exprs)
val nullExpr = Literal.create(null, createExpr.dataType)
If(IsNull(path), nullExpr, createExpr)
}
def fromCatalyst(path: Expression): Expression = {
val exprs = fields.value.value.map { field =>
field.encoder.fromCatalyst(
GetStructField(path, field.ordinal, Some(field.name)))
}
val newArgs = newInstanceExprs.value.from(exprs)
val newExpr = NewInstance(
classTag.runtimeClass, newArgs, jvmRepr, propagateNull = true)
val nullExpr = Literal.create(null, jvmRepr)
If(IsNull(path), nullExpr, newExpr)
}
}
final class RecordFieldEncoder[T](
val encoder: TypedEncoder[T],
private[frameless] val jvmRepr: DataType,
private[frameless] val fromCatalyst: Expression => Expression,
private[frameless] val toCatalyst: Expression => Expression
) extends Serializable
object RecordFieldEncoder extends RecordFieldEncoderLowPriority {
/**
* @tparam F the value class
* @tparam G the single field of the value class
* @tparam H the single field of the value class (with guarantee it's not a `Unit` value)
* @tparam K the key type for the fields
* @tparam V the inner value type
*/
implicit def optionValueClass[F : IsValueClass, G <: ::[_, HNil], H <: ::[_ <: FieldType[_ <: Symbol, _], HNil], K <: Symbol, V, KS <: ::[_ <: Symbol, HNil]]
(implicit
i0: LabelledGeneric.Aux[F, G],
i1: DropUnitValues.Aux[G, H],
i2: IsHCons.Aux[H, _ <: FieldType[K, V], HNil],
i3: Keys.Aux[H, KS],
i4: IsHCons.Aux[KS, K, HNil],
i5: TypedEncoder[V],
i6: ClassTag[F]
): RecordFieldEncoder[Option[F]] = {
val fieldName = i4.head(i3()).name
val innerJvmRepr = ObjectType(i6.runtimeClass)
val catalyst: Expression => Expression = { path =>
val value = UnwrapOption(innerJvmRepr, path)
val javaValue = Invoke(value, fieldName, i5.jvmRepr, Nil)
i5.toCatalyst(javaValue)
}
val fromCatalyst: Expression => Expression = { path =>
val javaValue = i5.fromCatalyst(path)
val value = NewInstance(i6.runtimeClass, Seq(javaValue), innerJvmRepr)
WrapOption(value, innerJvmRepr)
}
val jvmr = ObjectType(classOf[Option[F]])
new RecordFieldEncoder[Option[F]](
encoder = new TypedEncoder[Option[F]] {
val nullable = true
val jvmRepr = jvmr
@inline def catalystRepr: DataType = i5.catalystRepr
def fromCatalyst(path: Expression): Expression = {
val javaValue = i5.fromCatalyst(path)
val value = NewInstance(
i6.runtimeClass, Seq(javaValue), innerJvmRepr)
WrapOption(value, innerJvmRepr)
}
def toCatalyst(path: Expression): Expression = catalyst(path)
override def toString: String = s"RecordFieldEncoder.optionValueClass[${i6.runtimeClass.getName}]('${fieldName}', $i5)"
},
jvmRepr = jvmr,
fromCatalyst = fromCatalyst,
toCatalyst = catalyst
)
}
/**
* @tparam F the value class
* @tparam G the single field of the value class
* @tparam H the single field of the value class (with guarantee it's not a `Unit` value)
* @tparam V the inner value type
*/
implicit def valueClass[F : IsValueClass, G <: ::[_, HNil], H <: ::[_ <: FieldType[_ <: Symbol, _], HNil], K <: Symbol, V, KS <: ::[_ <: Symbol, HNil]]
(implicit
i0: LabelledGeneric.Aux[F, G],
i1: DropUnitValues.Aux[G, H],
i2: IsHCons.Aux[H, _ <: FieldType[K, V], HNil],
i3: Keys.Aux[H, KS],
i4: IsHCons.Aux[KS, K, HNil],
i5: TypedEncoder[V],
i6: ClassTag[F]
): RecordFieldEncoder[F] = {
val cls = i6.runtimeClass
val jvmr = i5.jvmRepr
val fieldName = i4.head(i3()).name
new RecordFieldEncoder[F](
encoder = new TypedEncoder[F] {
def nullable = i5.nullable
def jvmRepr = jvmr
def catalystRepr: DataType = i5.catalystRepr
def fromCatalyst(path: Expression): Expression =
i5.fromCatalyst(path)
@inline def toCatalyst(path: Expression): Expression =
i5.toCatalyst(path)
override def toString: String = s"RecordFieldEncoder.valueClass[${cls.getName}]('${fieldName}', ${i5})"
},
jvmRepr = FramelessInternals.objectTypeFor[F],
fromCatalyst = { expr: Expression =>
NewInstance(
i6.runtimeClass,
i5.fromCatalyst(expr) :: Nil,
ObjectType(i6.runtimeClass))
},
toCatalyst = { expr: Expression =>
i5.toCatalyst(Invoke(expr, fieldName, jvmr))
}
)
}
}
private[frameless] sealed trait RecordFieldEncoderLowPriority {
implicit def apply[T](implicit e: TypedEncoder[T]): RecordFieldEncoder[T] = new RecordFieldEncoder[T](e, e.jvmRepr, e.fromCatalyst, e.toCatalyst)
}