Bad Request: Unknown exception: Cannot build plan for query

The following query runs fine on hive, but i am having difficulty running on Druid.

SELECT
COUNT(DISTINCT users),
source,
CASE WHEN ametric >= 10 THEN 5 WHEN ametric >= 7 THEN 4 ELSE 1 END AS metric_bucket
FROM (SELECT users, source, SUM(CAST(ametric AS integer)) ametric FROM “test-data” GROUP BY users, source) tbl
GROUP by source, CASE WHEN ametric >= 10 THEN 5 WHEN ametric >= 7 THEN 4 ELSE 1 END

``

Druid says, “Bad Request: Unknown exception: Cannot build plan for query”.
Appreciate any guidance on how to fix the query to make it work with Druid. In essence, i am trying to get a distinct users count (hyper unique column) by a metric bucket.

Thanks!

Rewriting the query in the following way worked.

SELECT
CASE WHEN ametric >= 10 THEN 5 WHEN ametric >= 7 THEN 4 ELSE 1 END AS metric_bucket,
source,
COUNT(DISTINCT users)
FROM “test-data”
GROUP BY
source,
CASE WHEN ametric >= 10 THEN 5 WHEN ametric >= 7 THEN 4 ELSE 1 END
ORDER BY metric_bucket, source

``