Repository: quarkusio/quarkus
Issue: Unable to perform a count operation on a named query

This is probably less of a bug and more of a gap in my Hibernate/HQL knowledge.  Basically my named query works with `find` but throws when I add `count`.
```Java
@Entity
@DiscriminatorValue("RN")
@NamedQuery(name="Nurse.available", 
            query="select n from Nurse n, Availability av where n.id = av.teamMember and av.onShift = true")
public class Nurse extends TeamMember {

    public static List<Nurse> onShift() {
        return find("#Nurse.available"); //works fine
    }

    public static long onShiftCount() {
        return find("#Nurse.available").count(); //throws exception
    }
}
```
```
org.jboss.resteasy.spi.UnhandledException: io.quarkus.panache.common.exception.PanacheQueryException: Unable to perform a count operation on a named query
```
The immediate workaround to use the query directly:
```Java
public static long onShiftCount() {
    return find("select n from Nurse n, Availability av where n.id = av.teamMember and av.onShift = true").count(); //this works!
}
```

Java 14
Quarkus 1.5.2

Changed files:
extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java
integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java

diff --git a/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java
index 4a5cd99105a..c0fa321b1d5 100644
--- a/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java
+++ b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java
@@ -206,12 +206,14 @@ public void withHint(String hintName, Object value) {
 
     @SuppressWarnings("unchecked")
     public long count() {
-        if (AbstractJpaOperations.isNamedQuery(query)) {
-            throw new PanacheQueryException("Unable to perform a count operation on a named query");
-        }
-
         if (count == null) {
-            Query countQuery = em.createQuery(countQuery());
+            String selectQuery = query;
+            if (AbstractJpaOperations.isNamedQuery(query)) {
+                org.hibernate.query.Query q = (org.hibernate.query.Query) em.createNamedQuery(query.substring(1));
+                selectQuery = q.getQueryString();
+            }
+
+            Query countQuery = em.createQuery(countQuery(selectQuery));
             if (paramsArrayOrMap instanceof Map)
                 AbstractJpaOperations.bindParameters(countQuery, (Map<String, Object>) paramsArrayOrMap);
             else
@@ -223,13 +225,13 @@ public long count() {
         return count;
     }
 
-    private String countQuery() {
+    private String countQuery(String selectQuery) {
         if (countQuery != null) {
             return countQuery;
         }
 
         // try to generate a good count query from the existing query
-        Matcher selectMatcher = SELECT_PATTERN.matcher(query);
+        Matcher selectMatcher = SELECT_PATTERN.matcher(selectQuery);
         String countQuery;
         if (selectMatcher.matches()) {
             // this one cannot be null
@@ -239,17 +241,17 @@ private String countQuery() {
                 String secondSelection = selectMatcher.group(2);
                 // we can only count distinct single columns
                 if (secondSelection != null && !secondSelection.trim().isEmpty()) {
-                    throw new PanacheQueryException("Count query not supported for select query: " + query);
+                    throw new PanacheQueryException("Count query not supported for select query: " + selectQuery);
                 }
                 countQuery = "SELECT COUNT(" + firstSelection + ") " + selectMatcher.group(3);
             } else {
                 // it's not distinct, forget the column list
                 countQuery = "SELECT COUNT(*) " + selectMatcher.group(3);
             }
-        } else if (FROM_PATTERN.matcher(query).matches()) {
-            countQuery = "SELECT COUNT(*) " + query;
+        } else if (FROM_PATTERN.matcher(selectQuery).matches()) {
+            countQuery = "SELECT COUNT(*) " + selectQuery;
         } else {
-            throw new PanacheQueryException("Count query not supported for select query: " + query);
+            throw new PanacheQueryException("Count query not supported for select query: " + selectQuery);
         }
 
         // remove the order by clause
diff --git a/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java b/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java
index 2f5ecc52d0d..971a987404c 100644
--- a/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java
+++ b/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java
@@ -159,6 +159,7 @@ public String testModel() {
         persons = Person.find("#Person.getByName", Parameters.with("name", "stef")).list();
         Assertions.assertEquals(1, persons.size());
         Assertions.assertEquals(person, persons.get(0));
+        Assertions.assertEquals(1, Person.find("#Person.getByName", Parameters.with("name", "stef")).count());
         Assertions.assertThrows(PanacheQueryException.class, () -> Person.find("#Person.namedQueryNotFound").list());
         NamedQueryEntity.find("#NamedQueryMappedSuperClass.getAll").list();
         NamedQueryEntity.find("#NamedQueryEntity.getAll").list();