The combination of the below code:
protected static void replaceDistinctWithGroupBy(StringBuilder sql) { int distinctIndex = sql.indexOf(DISTINCT); if (distinctIndex > 0) { sql.delete(distinctIndex, distinctIndex + DISTINCT.length() + 1); sql.append(" group by").append(getSelectFieldsWithoutAliases(sql)); } }
and
protected static CharSequence getSelectFieldsWithoutAliases(StringBuilder sql) { String select = sql.substring(sql.indexOf(SELECT) + SELECT.length(), sql.indexOf(FROM)); // Strip the as clauses return stripAliases(select); }
may corrupt queries where a column contains the keyword 'from'. For example:
select distinct g.id, g.validFrom from SOME_TABLE g
A possible quick fix would be to not match against the word "from" but rather " from" or similar.