fix bugs handling of DISTINCT qualifier in aggregate functions
jira: MB-9214 and MB-9206