TEIID-2967 correcting several aspects of sort optimization