Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use original query case for fast count queries #43963

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading