Summary
Each having() appends ... HAVING ... to text; reorderSelectClauses groups same-key fragments by joining with a space, so two calls become HAVING COUNT(id) > $1 HAVING SUM(age) < $2 — invalid SQL.
Evidence (repro)
.having(['COUNT(id)','>',3]).having(['SUM(age)','<',100]).toSQL()
-> ... HAVING COUNT(id) > $1 HAVING SUM(age) < $2Location
src/client.ts:~4429-4458.
Suggested fix
Detect an existing HAVING (like orderBy/groupBy do) and append subsequent conditions with AND.
Severity: medium. Found via internal code audit.