@@ -1165,9 +1165,18 @@ class TypedDataset[T] protected[frameless](val dataset: Dataset[T])(implicit val
11651165 }
11661166
11671167 /**
1168- * Explodes (flattens) a single column at a time. It only compiles if the type of column supports this operation.
1168+ * Explodes a single column at a time. It only compiles if the type of column supports this operation.
11691169 *
1170- * @param column the column we wish to explode/flatten
1170+ * @example
1171+ *
1172+ * {{{
1173+ * case class X(i: Int, j: Array[Int])
1174+ * case class Y(i: Int, j: Int)
1175+ *
1176+ * val f: TypedDataset[X] = ???
1177+ * val fNew: TypedDataset[Y] = f.explode('j).as[Y]
1178+ * }}}
1179+ * @param column the column we wish to explode
11711180 */
11721181 def explode [A , TRep <: HList , V [_], OutMod <: HList , OutModValues <: HList , Out ]
11731182 (column : Witness .Lt [Symbol ])
@@ -1189,6 +1198,40 @@ class TypedDataset[T] protected[frameless](val dataset: Dataset[T])(implicit val
11891198 sparkExplode(df(column.value.name))).as[Out ](TypedExpressionEncoder [Out ])
11901199 TypedDataset .create[Out ](trans)
11911200 }
1201+
1202+ /**
1203+ * Flattens a column of type Option[A]. Compiles only if the selected column is of type Option[A].
1204+ *
1205+ *
1206+ * @example
1207+ *
1208+ * {{{
1209+ * case class X(i: Int, j: Option[Int])
1210+ * case class Y(i: Int, j: Int)
1211+ *
1212+ * val f: TypedDataset[X] = ???
1213+ * val fNew: TypedDataset[Y] = f.flattenOption('j).as[Y]
1214+ * }}}
1215+ *
1216+ * @param column the column we wish to flatten
1217+ */
1218+ def flattenOption [A , TRep <: HList , V [_], OutMod <: HList , OutModValues <: HList , Out ]
1219+ (column : Witness .Lt [Symbol ])
1220+ (implicit
1221+ i0 : TypedColumn .Exists [T , column.T , V [A ]],
1222+ i1 : TypedEncoder [A ],
1223+ i2 : V [A ] =:= Option [A ],
1224+ i3 : LabelledGeneric .Aux [T , TRep ],
1225+ i4 : Modifier .Aux [TRep , column.T , V [A ], A , OutMod ],
1226+ i5 : Values .Aux [OutMod , OutModValues ],
1227+ i6 : Tupler .Aux [OutModValues , Out ],
1228+ i7 : TypedEncoder [Out ]
1229+ ): TypedDataset [Out ] = {
1230+ val df = dataset.toDF()
1231+ val trans =
1232+ df.filter(df(column.value.name).isNotNull).as[Out ](TypedExpressionEncoder [Out ])
1233+ TypedDataset .create[Out ](trans)
1234+ }
11921235}
11931236
11941237object TypedDataset {
0 commit comments