When charting, do two searches if the query isn't distinct

If a search has joins which result in a non-distinct ticket result, the
SUM() aggregator is incorrect.  Notably, if UseSQLForACLChecks is
enabled, and a global role right exists, it causes a LEFT JOIN to ticket
role groups -- which cause all maching tickets to show up four times,
multiplying SUM(TimeWorked) (for instance) by four.

As we can determine if the query will be non-distinct before making it,
split it into a SELECT DISTINCT search that returns ticket ids, followed
by a search over those ticket ids, grouped appropriately.  This comes at
a small speed penalty for such searches, but provides the only clear
route for correctness in SUM() metrics.
Merge branch '4.2/charting-distinct' into 4.2-trunk