Search in sources :

Example 36 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class SubCollectionFetchQueryBuilderTest method testSubCollectionFetchWithJoinFilter.

@Test
public void testSubCollectionFetchWithJoinFilter() {
    Author author = new Author();
    author.setId(1L);
    Book book = new Book();
    book.setId(2);
    List<Path.PathElement> publisherNamePath = Arrays.asList(new Path.PathElement(Book.class, Publisher.class, PUBLISHER), new Path.PathElement(Publisher.class, String.class, NAME));
    FilterPredicate publisherNamePredicate = new InPredicate(new Path(publisherNamePath), PUB1);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).filterExpression(publisherNamePredicate).build();
    Relationship relationshipProjection = Relationship.builder().name(BOOKS).projection(entityProjection).build();
    RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
    SubCollectionFetchQueryBuilder builder = new SubCollectionFetchQueryBuilder(relationship, dictionary, new TestSessionWrapper());
    TestQueryWrapper query = (TestQueryWrapper) builder.build();
    String expected = "SELECT example_Book FROM example.Author example_Author__fetch " + "JOIN example_Author__fetch.books example_Book " + "LEFT JOIN example_Book.publisher example_Book_publisher  " + "WHERE example_Book_publisher.name IN (:books_publisher_name_XXX) AND example_Author__fetch=:example_Author__fetch ";
    String actual = query.getQueryText();
    actual = actual.replaceFirst(":publisher_name_\\w+_\\w+", ":books_publisher_name_XXX");
    assertEquals(expected, actual);
}
Also used : Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) SubCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder) Publisher(example.Publisher) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Test(org.junit.jupiter.api.Test)

Example 37 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class FilterTranslatorTest method testHQLQueryVisitor.

@Test
public void testHQLQueryVisitor() throws Exception {
    List<Path.PathElement> p0Path = Arrays.asList(new Path.PathElement(Book.class, Author.class, "authors"));
    FilterPredicate p0 = new NotEmptyPredicate(new Path(p0Path));
    List<Path.PathElement> p1Path = Arrays.asList(new Path.PathElement(Book.class, Author.class, "authors"), new Path.PathElement(Author.class, String.class, "name"));
    FilterPredicate p1 = new InPredicate(new Path(p1Path), "foo", "bar");
    List<Path.PathElement> p2Path = Arrays.asList(new Path.PathElement(Book.class, String.class, "name"));
    FilterPredicate p2 = new InPredicate(new Path(p2Path), "blah");
    List<Path.PathElement> p3Path = Arrays.asList(new Path.PathElement(Book.class, String.class, "genre"));
    FilterPredicate p3 = new InPredicate(new Path(p3Path), "scifi");
    OrFilterExpression or = new OrFilterExpression(p2, p3);
    AndFilterExpression and1 = new AndFilterExpression(p0, p1);
    AndFilterExpression and2 = new AndFilterExpression(or, and1);
    NotFilterExpression not = new NotFilterExpression(and2);
    FilterTranslator filterOp = new FilterTranslator(dictionary);
    String query = filterOp.apply(not, false);
    query = query.trim().replaceAll(" +", " ");
    String p1Params = p1.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
    String p2Params = p2.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
    String p3Params = p3.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
    String expected = "NOT (((name IN (" + p2Params + ") OR genre IN (" + p3Params + ")) " + "AND (authors IS NOT EMPTY AND authors.name IN (" + p1Params + "))))";
    assertEquals(expected, query);
}
Also used : Path(com.yahoo.elide.core.Path) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) Book(example.Book) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) NotEmptyPredicate(com.yahoo.elide.core.filter.predicates.NotEmptyPredicate) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Test(org.junit.jupiter.api.Test)

Example 38 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class SearchDataTransaction method canSort.

/**
 * Returns whether or not Lucene can be used to sort the query.
 * @param sorting The elide sorting clause
 * @param entityClass The entity being sorted.
 * @return true if Lucene can sort.  False otherwise.
 */
private boolean canSort(Sorting sorting, Type<?> entityClass) {
    for (Map.Entry<Path, Sorting.SortOrder> entry : sorting.getSortingPaths().entrySet()) {
        Path path = entry.getKey();
        if (path.getPathElements().size() != 1) {
            return false;
        }
        Path.PathElement last = path.lastElement().get();
        String fieldName = last.getFieldName();
        if (dictionary.getAttributeOrRelationAnnotation(entityClass, SortableField.class, fieldName) == null) {
            return false;
        }
    }
    return true;
}
Also used : Path(com.yahoo.elide.core.Path) SortableField(org.hibernate.search.annotations.SortableField) Map(java.util.Map)

Example 39 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class QueryTranslator method extractOrderBy.

/**
 * Given a list of columns to sort on, constructs an ORDER BY clause in SQL.
 * @param sortClauses The list of sort columns and their sort order (ascending or descending).
 * @return A SQL expression
 */
private String extractOrderBy(Map<Path, Sorting.SortOrder> sortClauses, Query query) {
    if (sortClauses.isEmpty()) {
        return "";
    }
    return " ORDER BY " + sortClauses.entrySet().stream().map((entry) -> {
        Path path = entry.getKey();
        Sorting.SortOrder order = entry.getValue();
        Path.PathElement last = path.lastElement().get();
        SQLColumnProjection projection = fieldToColumnProjection(query, last.getAlias());
        String orderByClause = (query.getColumnProjections().contains(projection) && dialect.useAliasForOrderByClause()) ? applyQuotes(projection.getSafeAlias()) : projection.toSQL(query, metaDataStore);
        return orderByClause + (order.equals(Sorting.SortOrder.desc) ? " DESC" : " ASC");
    }).collect(Collectors.joining(","));
}
Also used : Path(com.yahoo.elide.core.Path) Sorting(com.yahoo.elide.core.request.Sorting)

Example 40 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class HasMemberJPQLGenerator method generate.

@Override
public String generate(FilterPredicate predicate, Function<Path, String> aliasGenerator) {
    Preconditions.checkArgument(predicate.getParameters().size() == 1);
    String notMember = negated ? "NOT " : "";
    if (!FilterPredicate.toManyInPath(dictionary, predicate.getPath())) {
        return String.format("%s %sMEMBER OF %s", predicate.getParameters().get(0).getPlaceholder(), notMember, aliasGenerator.apply(predicate.getPath()));
    }
    Path path = predicate.getPath();
    Preconditions.checkArgument(path.lastElement().isPresent());
    Preconditions.checkArgument(!(path.lastElement().get().getType() instanceof Collection));
    // _INNER_example_Book_authors.name = :authors_name_7c02839c_0)
    return String.format("%sEXISTS (SELECT 1 FROM %s WHERE %s = %s AND %s = %s)", notMember, getFromClause(path), getInnerQueryIdField(path), getOuterQueryIdField(path, aliasGenerator), getInnerFilterFieldReference(path), predicate.getParameters().get(0).getPlaceholder());
}
Also used : Path(com.yahoo.elide.core.Path) Collection(java.util.Collection)

Aggregations

Path (com.yahoo.elide.core.Path)88 Test (org.junit.jupiter.api.Test)67 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)51 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)41 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)35 Query (com.yahoo.elide.datastores.aggregation.query.Query)35 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)34 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)33 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)31 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)29 Book (example.Book)29 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)22 Date (java.util.Date)19 Argument (com.yahoo.elide.core.request.Argument)14 EntityProjection (com.yahoo.elide.core.request.EntityProjection)14 GameRevenue (example.GameRevenue)14 HashSet (java.util.HashSet)14 Author (example.Author)12 Publisher (example.Publisher)9 Sorting (com.yahoo.elide.core.request.Sorting)8