[SPARK-4468][SQL] Backports #3334 to branch-1.1