use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.
the class JpaDataStoreTransactionTest method testGetRelationDelegation.
@ParameterizedTest
@MethodSource("getTestArguments")
public void testGetRelationDelegation(boolean delegateToInMemory, int numberOfAuthors, FilterExpression filter, boolean usesInMemory) throws Exception {
AbstractJpaTransaction tx = new AbstractJpaTransaction(entityManager, (unused) -> {
}, DEFAULT_LOGGER, delegateToInMemory, false) {
@Override
public boolean isOpen() {
return false;
}
@Override
public void begin() {
}
@Override
protected Predicate<Collection<?>> isPersistentCollection() {
return (unused) -> true;
}
};
EntityProjection projection = EntityProjection.builder().type(Author.class).build();
List<Author> authors = new ArrayList<>();
Author author1 = mock(Author.class);
authors.add(author1);
for (int idx = 1; idx < numberOfAuthors; idx++) {
authors.add(mock(Author.class));
}
when(query.getResultList()).thenReturn(authors);
DataStoreIterable<Author> loadedAuthors = tx.loadObjects(projection, scope);
assertFalse(loadedAuthors.needsInMemoryPagination());
assertFalse(loadedAuthors.needsInMemorySort());
assertFalse(loadedAuthors.needsInMemoryFilter());
Relationship relationship = Relationship.builder().name("books").projection(EntityProjection.builder().type(Book.class).filterExpression(filter).build()).build();
PersistentSet returnCollection = mock(PersistentSet.class);
when(author1.getBooks()).thenReturn(returnCollection);
DataStoreIterable<Book> loadedBooks = tx.getToManyRelation(tx, author1, relationship, scope);
assertEquals(usesInMemory, loadedBooks.needsInMemoryFilter());
assertEquals(usesInMemory, loadedBooks.needsInMemorySort());
assertEquals(usesInMemory, loadedBooks.needsInMemoryPagination());
}
use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.
the class RootCollectionFetchQueryBuilder method build.
/**
* Constructs a query that fetches a root collection.
*
* @return the constructed query
*/
@Override
public Query build() {
Type<?> entityClass = this.entityProjection.getType();
String entityName = entityClass.getCanonicalName();
String entityAlias = getTypeAlias(entityClass);
Query query;
FilterExpression filterExpression = entityProjection.getFilterExpression();
if (filterExpression != null) {
PredicateExtractionVisitor extractor = new PredicateExtractionVisitor();
Collection<FilterPredicate> predicates = filterExpression.accept(extractor);
// Build the WHERE clause
String filterClause = WHERE + new FilterTranslator(dictionary).apply(filterExpression, USE_ALIAS);
// Build the JOIN clause
String joinClause = getJoinClauseFromFilters(filterExpression) + getJoinClauseFromSort(entityProjection.getSorting()) + extractToOneMergeJoins(entityClass, entityAlias);
boolean requiresDistinct = entityProjection.getPagination() != null && containsOneToMany(filterExpression);
boolean sortOverRelationship = entityProjection.getSorting() != null && entityProjection.getSorting().getSortingPaths().keySet().stream().anyMatch(path -> path.getPathElements().size() > 1);
if (requiresDistinct && sortOverRelationship) {
// SQL does not support distinct and order by on columns which are not selected
throw new InvalidValueException("Combination of pagination, sorting over relationship and" + " filtering over toMany relationships unsupported");
}
query = session.createQuery(SELECT + (requiresDistinct ? DISTINCT : "") + entityAlias + FROM + entityName + AS + entityAlias + SPACE + joinClause + SPACE + filterClause + SPACE + getSortClause(entityProjection.getSorting()));
// Fill in the query parameters
supplyFilterQueryParameters(query, predicates);
} else {
query = session.createQuery(SELECT + entityAlias + FROM + entityName + AS + entityAlias + SPACE + getJoinClauseFromSort(entityProjection.getSorting()) + extractToOneMergeJoins(entityClass, entityAlias) + SPACE + getSortClause(entityProjection.getSorting()));
}
addPaginationToQuery(query);
return query;
}
use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.
the class PersistentResourceTest method testFilterExpressionByType.
@Test
public void testFilterExpressionByType() {
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
queryParams.add("filter[author.name][infix]", "Hemingway");
RequestScope scope = buildRequestScope("/", mock(DataStoreTransaction.class), new TestUser("1"), queryParams);
Optional<FilterExpression> filter = scope.getLoadFilterExpression(ClassType.of(Author.class));
FilterPredicate predicate = (FilterPredicate) filter.get();
assertEquals("name", predicate.getField());
assertEquals("name", predicate.getFieldPath());
assertEquals(Operator.INFIX, predicate.getOperator());
assertEquals(Arrays.asList("Hemingway"), predicate.getValues());
assertEquals("[Author].name", predicate.getPath().toString());
}
use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.
the class FilteredIteratorTest method testEmptyResult.
@Test
public void testEmptyResult() throws Exception {
EntityDictionary dictionary = EntityDictionary.builder().build();
dictionary.bindEntity(Book.class);
List<Book> books = List.of();
RSQLFilterDialect filterDialect = RSQLFilterDialect.builder().dictionary(dictionary).build();
FilterExpression expression = filterDialect.parse(ClassType.of(Book.class), new HashSet<>(), "title==*bar", NO_VERSION);
RequestScope scope = new TestRequestScope(null, null, dictionary);
Iterator<Book> bookIterator = new FilteredIterator<>(expression, scope, books.iterator());
assertFalse(bookIterator.hasNext());
assertThrows(NoSuchElementException.class, () -> bookIterator.next());
}
use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.
the class InMemoryStoreTransactionTest method testFilterPredicateInMemoryOnComplexAttribute.
@Test
public void testFilterPredicateInMemoryOnComplexAttribute() {
FilterExpression expression = new InPredicate(new Path(Author.class, dictionary, "homeAddress.street1"), "Foo");
EntityProjection projection = EntityProjection.builder().type(Author.class).filterExpression(expression).build();
DataStoreIterable filterInMemory = new DataStoreIterableBuilder(Arrays.asList(author1, author2)).filterInMemory(true).build();
when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(filterInMemory);
Collection<Object> loaded = ImmutableList.copyOf(inMemoryStoreTransaction.loadObjects(projection, scope));
assertEquals(1, loaded.size());
assertTrue(loaded.contains(author1));
}
Aggregations