Cherry pick from amirhhz's branch. Included other fixes and CDH 4.6 changes.