From 9257b636b3c452c2d8e9b741c9b94e7f744303fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20=C3=89pardaud?= Date: Fri, 18 Oct 2024 15:58:19 +0200 Subject: [PATCH] Use original query case for fast count queries --- .../panache/hibernate/common/runtime/PanacheJpaUtil.java | 7 ++++--- .../panache/hibernate/common/runtime/CountTest.java | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java b/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java index 6cfa4b97582c2..7b9719d4df1fe 100644 --- a/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java +++ b/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java @@ -52,11 +52,12 @@ public static String getFastCountQuery(String query) { Matcher selectMatcher = SELECT_PATTERN.matcher(query); if (selectMatcher.matches()) { // this one cannot be null - String firstSelection = selectMatcher.group(1).trim().toLowerCase(Locale.ROOT); - if (firstSelection.startsWith("distinct")) { + String firstSelection = selectMatcher.group(1).trim(); + String firstSelectionForMatching = firstSelection.toLowerCase(Locale.ROOT); + if (firstSelectionForMatching.startsWith("distinct")) { // if firstSelection matched distinct only, we have something wrong in our selection list, probably functions/parens // so bail out - if (firstSelection.length() == 8) { + if (firstSelectionForMatching.length() == 8) { return getCountQueryUsingParser(query); } // this one can be null diff --git a/extensions/panache/panache-hibernate-common/runtime/src/test/java/io/quarkus/panache/hibernate/common/runtime/CountTest.java b/extensions/panache/panache-hibernate-common/runtime/src/test/java/io/quarkus/panache/hibernate/common/runtime/CountTest.java index a9727e2b4cd6b..4c92a8a4a468a 100644 --- a/extensions/panache/panache-hibernate-common/runtime/src/test/java/io/quarkus/panache/hibernate/common/runtime/CountTest.java +++ b/extensions/panache/panache-hibernate-common/runtime/src/test/java/io/quarkus/panache/hibernate/common/runtime/CountTest.java @@ -47,6 +47,8 @@ public void testFastVersion() { assertFastCountQuery("SELECT COUNT(*) from bar", "select foo,gee from bar"); // one column distinct assertFastCountQuery("SELECT COUNT(distinct foo) from bar", "select distinct foo from bar"); + // with case preserved + assertFastCountQuery("SELECT COUNT(distinct fOO) from bar", "select distinct fOO from bar"); // two columns distinct Assertions.assertThrows(RuntimeException.class, () -> assertFastCountQuery("XX", "select distinct foo,gee from bar")); // nested order by not touched