Remove code duplication in NestedSelectRewriter
Implemented NH-3333

Very big kudos go to Dmitry Naumov for his help