We're updating the issue view to help you get more done. 

SQLServer2005Dialect corrupts distinct queries if table data includes the keyword "from"

Description

The combination of the below code:

1 2 3 4 5 6 7 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

1 2 3 4 5 6 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.

Environment

None

Status

Assignee

Unassigned

Reporter

Alix Warnke

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

Affirmative

Pull Request

None

backportDecision

None

Components

Affects versions

3.6.10

Priority

Major