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

Performance improvement for case insensitive queries #29597

Merged
merged 11 commits into from
Jun 24, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,28 @@ public void testArrayContainsCriteria() {
assertThat(people).containsExactly(TEST_PERSON);
}

@Test
public void testIsNotNullCriteriaNotCaseSensitive() {
Criteria hasLastName = Criteria.getInstance(CriteriaType.IS_NOT_NULL, "lastName",
Collections.emptyList(),
Part.IgnoreCaseType.ALWAYS);
List<Person> people = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(hasLastName), Person.class,
containerName));

assertThat(people).containsExactly(TEST_PERSON);
}

@Test
public void testStartsWithCriteriaNotCaseSensitive() {
Criteria nameStartsWith = Criteria.getInstance(CriteriaType.STARTS_WITH, "firstName",
Collections.singletonList(TEST_PERSON.getFirstName().toUpperCase()),
Part.IgnoreCaseType.ALWAYS);
List<Person> people = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(nameStartsWith), Person.class,
containerName));

assertThat(people).containsExactly(TEST_PERSON);
}

@Test
public void testBetweenCriteria() {
Criteria ageBetween = Criteria.getInstance(CriteriaType.BETWEEN, "age", Arrays.asList(AGE - 1, AGE + 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ private String generateBinaryQuery(@NonNull Criteria criteria, @NonNull List<Pai
parameters.add(Pair.of(parameter, subjectValue));

if (CriteriaType.isFunction(criteria.getType())) {
return getFunctionCondition(ignoreCase, sqlKeyword, subject, parameter);
return getFunctionCondition(ignoreCase, sqlKeyword, subject, parameter,
CriteriaType.isFunctionWithCaseSensitiveSupport(criteria.getType()));
} else {
return getCondition(ignoreCase, sqlKeyword, subject, parameter);
}
Expand Down Expand Up @@ -95,14 +96,19 @@ private String getCondition(final Part.IgnoreCaseType ignoreCase, final String s
* @param sqlKeyword sql key word, operation name
* @param subject sql column name
* @param parameter sql filter value
* @param takesCaseSensitiveParam if the function type can take the third boolean param
* @return condition string
*/
private String getFunctionCondition(final Part.IgnoreCaseType ignoreCase, final String sqlKeyword,
final String subject, final String parameter) {
final String subject, final String parameter, final boolean takesCaseSensitiveParam) {
if (Part.IgnoreCaseType.NEVER == ignoreCase) {
return String.format("%s(r.%s, @%s)", sqlKeyword, subject, parameter);
} else {
return String.format("%s(UPPER(r.%s), UPPER(@%s))", sqlKeyword, subject, parameter);
if (takesCaseSensitiveParam) {
return String.format("%s(r.%s, @%s, true)", sqlKeyword, subject, parameter);
} else {
return String.format("%s(UPPER(r.%s), UPPER(@%s))", sqlKeyword, subject, parameter);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,23 @@ public static boolean isFunction(CriteriaType type) {
}
}

/**
* Check if CriteriaType operation is a function.
*
* @param type CriteriaType
* @return True if match, or false.
*/
public static boolean isFunctionWithCaseSensitiveSupport(CriteriaType type) {
switch (type) {
case CONTAINING:
case ENDS_WITH:
case STARTS_WITH:
return true;
default:
return false;
}
}

/**
* Check if CriteriaType operation is unary, with format of (ops A -&gt; B).
*
Expand Down