Ported SPARK-1870 from 1.0 branch to 0.9 branch