Incorporate shark/pull/329 into Spark SQL.

Credit to @chiragaggarwal.