Modified version of https://issues.apache.org/jira/secure/attachment/12578838/HIVE-3129.2.patch