You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Derived count query for SELECT DISTINCT mangles select expressions into invalid JPQL #4341
Deriving a count query from a SELECT DISTINCT query whose selection contains expressions produces invalid JPQL in QueryTransformers.CountSelectionTokenStream:
Flattening the selection tokens drops all whitespace of multi-token expressions.
AS (plus the following token) is stripped at any parenthesis nesting level, breaking cast(x as string).
withoutConstructorExpression() removes all parentheses rather than just the constructor wrapper, breaking function calls inside constructor arguments.
Examples on main (also affects JPQL and EQL; HQL is affected for the constructor case):
select distinct coalesce(u.name, u.lastname) from User u where u.foo = ?1
→ count query: select count(distinct coalescename, lastname) from User where foo = ?1
select distinct case when u.age > 18 then 'adult' else 'minor' end from User u
→ count query: select count(distinct casewhenu.age>18then'adult'else'minor'end) from User u
Both derived count queries are rejected by the project's own query parsers (BadJpqlGrammarException: no viable alternative), so any paged repository query of this shape fails at count-query execution.
Expected: the derived count query preserves the expression's spacing, keeps AS inside function calls, and retains nested parentheses when unwrapping a constructor expression. I will submit a PR with a fix and tests for all three dialects.
Activity
christophstrobl commentedon Sep 22, 2026
@harrisleesh thanks for reporting!
Revisit count query rendering.
Refactor to CountSelection delegate.
Refactor to CountSelection delegate.
Revisit count query rendering.
Refactor to CountSelection delegate.
Revisit count query rendering.