Skip to content

Commit

Permalink
[SPARK-6326][SQL] Improve castStruct to be faster
Browse files Browse the repository at this point in the history
Current `castStruct` should be very slow. This pr slightly improves it.

Author: Liang-Chi Hsieh <[email protected]>

Closes apache#5017 from viirya/faster_caststruct and squashes the following commits:

385d5b0 [Liang-Chi Hsieh] Further improved.
746fcfb [Liang-Chi Hsieh] Make castStruct faster.
  • Loading branch information
viirya authored and marmbrus committed Mar 26, 2015
1 parent e6d1406 commit 73d5775
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,17 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
val casts = from.fields.zip(to.fields).map {
case (fromField, toField) => cast(fromField.dataType, toField.dataType)
}
// TODO: This is very slow!
buildCast[Row](_, row => Row(row.toSeq.zip(casts).map {
case (v, cast) => if (v == null) null else cast(v)
}: _*))
// TODO: Could be faster?
val newRow = new GenericMutableRow(from.fields.size)
buildCast[Row](_, row => {
var i = 0
while (i < row.length) {
val v = row(i)
newRow.update(i, if (v == null) null else casts(i)(v))
i += 1
}
newRow.copy()
})
}

private[this] def cast(from: DataType, to: DataType): Any => Any = to match {
Expand Down

0 comments on commit 73d5775

Please sign in to comment.