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,39 @@ public void testArrayContainsCriteria() {
assertThat(people).containsExactly(TEST_PERSON);
}

@Test
public void testIsNotNullCriteriaCaseSensitive() {
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 testStartsWithCriteriaCaseSensitive() {
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 testIsEqualCriteriaCaseSensitive() {
Criteria nameStartsWith = Criteria.getInstance(CriteriaType.IS_EQUAL, "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 @@ -19,6 +19,7 @@
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.*;
import java.util.stream.Collectors;

import static com.azure.spring.data.cosmos.core.convert.MappingCosmosConverter.toCosmosDbValue;
Expand Down Expand Up @@ -64,7 +65,13 @@ 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 if(criteria.getType() == CriteriaType.IS_EQUAL &&
ignoreCase != Part.IgnoreCaseType.NEVER &&
subjectValue.getClass().getTypeName() == "java.lang.String") {
return getFunctionCondition(ignoreCase, CriteriaType.STRING_EQUAL.getSqlKeyword(),
FabianMeiswinkel marked this conversation as resolved.
Show resolved Hide resolved
subject, parameter, true);
} else {
return getCondition(ignoreCase, sqlKeyword, subject, parameter);
}
Expand Down Expand Up @@ -95,14 +102,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 @@ -25,6 +25,11 @@ public enum CriteriaType {
*/
IS_EQUAL("="),

/**
* String equals
*/
STRING_EQUAL("STRINGEQUALS"),

/**
* Or
*/
Expand Down Expand Up @@ -274,6 +279,24 @@ 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:
case STRING_EQUAL:
return true;
default:
return false;
}
}

/**
* Check if CriteriaType operation is unary, with format of (ops A -&gt; B).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
import org.junit.Test;
import org.mockito.Mock;
import org.springframework.data.repository.query.parser.Part;
import org.springframework.data.util.*;
import org.springframework.lang.*;

import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

public class AbstractQueryGeneratorTest {
Expand Down Expand Up @@ -48,6 +49,54 @@ public void binaryOperatorPriorityPreserved() {
"(", parameterNames.get(1), CriteriaType.OR.getSqlKeyword(), parameterNames.get(2), ")"));
}

@Test
public void generateBinaryQueryWithStartsWithDoesNotUseUpper() {
Criteria nameStartsWith = Criteria.getInstance(CriteriaType.STARTS_WITH, "firstName",
Collections.singletonList("TREVOR"),
Part.IgnoreCaseType.ALWAYS);
CosmosQuery query = new CosmosQuery(nameStartsWith);

SqlQuerySpec result = queryGenerator.generateCosmos(query);

Assert.assertEquals(result.getQueryText(), " WHERE STARTSWITH(r.firstName, @firstName0, true) ");
}

@Test
public void generateBinaryQueryWithArrayContainsUsesUpper() {
Criteria hasLastName = Criteria.getInstance(CriteriaType.ARRAY_CONTAINS, "lastName",
Collections.singletonList("ANDERSON"),
Part.IgnoreCaseType.ALWAYS);
CosmosQuery query = new CosmosQuery(hasLastName);

SqlQuerySpec result = queryGenerator.generateCosmos(query);

Assert.assertEquals(result.getQueryText(), " WHERE ARRAY_CONTAINS(UPPER(r.lastName), UPPER(@lastName0)) ");
}

@Test
public void generateBinaryQueryWithIsEqualIntUsesUpper() {
Criteria isEqualInt = Criteria.getInstance(CriteriaType.IS_EQUAL, "zipcode",
Collections.singletonList(20180),
Part.IgnoreCaseType.ALWAYS);
CosmosQuery query = new CosmosQuery(isEqualInt);

SqlQuerySpec result = queryGenerator.generateCosmos(query);

Assert.assertEquals(result.getQueryText(), " WHERE UPPER(r.zipcode) = UPPER(@zipcode0) ");
FabianMeiswinkel marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void generateBinaryQueryWithIsEqualStringDoesNotUseUpper() {
Criteria isEqualString = Criteria.getInstance(CriteriaType.IS_EQUAL, "firstName",
Collections.singletonList("TREVOR"),
Part.IgnoreCaseType.ALWAYS);
CosmosQuery query = new CosmosQuery(isEqualString);

SqlQuerySpec result = queryGenerator.generateCosmos(query);

Assert.assertEquals(result.getQueryText(), " WHERE STRINGEQUALS(r.firstName, @firstName0, true) ");
}

private static class EmptyQueryGenerator extends AbstractQueryGenerator implements QuerySpecGenerator {

@Override
Expand Down