Skip to content

Commit

Permalink
Merge pull request #43963 from FroMage/42064
Browse files Browse the repository at this point in the history
Use original query case for fast count queries
  • Loading branch information
FroMage authored Oct 18, 2024
2 parents 5e4a69e + 9257b63 commit 476e1ba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 476e1ba

Please sign in to comment.