Search in sources :

Example 46 with SortingImpl

use of com.yahoo.elide.core.sort.SortingImpl in project elide by yahoo.

the class RootCollectionFetchQueryBuilderTest method testRootFetchWithRelationshipSortingAndFilters.

@Test
public void testRootFetchWithRelationshipSortingAndFilters() {
    Map<String, Sorting.SortOrder> sorting = new HashMap<>();
    sorting.put(PUBLISHER + PERIOD + EDITOR + PERIOD + FIRSTNAME, Sorting.SortOrder.desc);
    Path.PathElement idPath = new Path.PathElement(Book.class, Chapter.class, "id");
    FilterPredicate idPredicate = new InPredicate(idPath, 1);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).sorting(new SortingImpl(sorting, Book.class, dictionary)).filterExpression(idPredicate).build();
    RootCollectionFetchQueryBuilder builder = new RootCollectionFetchQueryBuilder(entityProjection, dictionary, new TestSessionWrapper());
    TestQueryWrapper query = (TestQueryWrapper) builder.build();
    String expected = "SELECT example_Book FROM example.Book AS example_Book" + " LEFT JOIN example_Book.publisher example_Book_publisher" + " LEFT JOIN example_Book_publisher.editor example_Book_publisher_editor" + " WHERE example_Book.id IN (:id_XXX)" + " order by example_Book_publisher_editor.firstName desc";
    String actual = query.getQueryText();
    actual = actual.trim().replaceAll(" +", " ");
    actual = actual.replaceFirst(":id_\\w+", ":id_XXX");
    assertEquals(expected, actual);
}
Also used : Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) HashMap(java.util.HashMap) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) Book(example.Book) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) RootCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.RootCollectionFetchQueryBuilder) Test(org.junit.jupiter.api.Test)

Example 47 with SortingImpl

use of com.yahoo.elide.core.sort.SortingImpl in project elide by yahoo.

the class SubCollectionFetchQueryBuilderTest method testSubCollectionFetchWithSortingAndFilters.

@Test
public void testSubCollectionFetchWithSortingAndFilters() {
    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);
    Map<String, Sorting.SortOrder> sorting = new HashMap<>();
    sorting.put(TITLE, Sorting.SortOrder.asc);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).filterExpression(publisherNamePredicate).sorting(new SortingImpl(sorting, Book.class, dictionary)).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 (:publisher_name_XXX) AND example_Author__fetch=:example_Author__fetch order by example_Book.title asc";
    String actual = query.getQueryText();
    actual = actual.replaceFirst(":publisher_name_\\w+", ":publisher_name_XXX");
    actual = actual.trim().replaceAll(" +", " ");
    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) HashMap(java.util.HashMap) Publisher(example.Publisher) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) 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 48 with SortingImpl

use of com.yahoo.elide.core.sort.SortingImpl in project elide by yahoo.

the class DefaultQueryValidatorTest method testSortingOnNotQueriedMetric.

@Test
public void testSortingOnNotQueriedMetric() {
    Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
    sortMap.put("highScore", Sorting.SortOrder.asc);
    Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("lowScore")).dimensionProjection(playerStatsTable.getDimensionProjection("overallRating")).sorting(new SortingImpl(sortMap, PlayerStats.class, dictionary)).build();
    validateQuery(query, "Invalid operation: Can not sort on highScore as it is not present in query");
}
Also used : Query(com.yahoo.elide.datastores.aggregation.query.Query) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) TreeMap(java.util.TreeMap) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 49 with SortingImpl

use of com.yahoo.elide.core.sort.SortingImpl in project elide by yahoo.

the class DefaultQueryValidatorTest method testSortingOnId.

@Test
public void testSortingOnId() {
    Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
    sortMap.put("id", Sorting.SortOrder.asc);
    Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("lowScore")).metricProjection(playerStatsTable.getMetricProjection("id")).dimensionProjection(playerStatsTable.getDimensionProjection("overallRating")).sorting(new SortingImpl(sortMap, PlayerStats.class, dictionary)).build();
    validateQuery(query, "Invalid operation: Sorting on id field is not permitted");
}
Also used : Query(com.yahoo.elide.datastores.aggregation.query.Query) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) TreeMap(java.util.TreeMap) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 50 with SortingImpl

use of com.yahoo.elide.core.sort.SortingImpl in project elide by yahoo.

the class QueryKeyExtractorTest method testFullQuery.

@Test
public void testFullQuery() throws Exception {
    RSQLFilterDialect filterParser = RSQLFilterDialect.builder().dictionary(dictionary).build();
    Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
    sortMap.put("playerName", Sorting.SortOrder.asc);
    Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("highScore")).dimensionProjection(playerStatsTable.getDimensionProjection("overallRating")).timeDimensionProjection(playerStatsTable.getTimeDimensionProjection("recordedDate")).whereFilter(filterParser.parseFilterExpression("countryNickName=='Uncle Sam'", ClassType.of(PlayerStats.class), false)).havingFilter(filterParser.parseFilterExpression("highScore > 300", ClassType.of(PlayerStats.class), false)).sorting(new SortingImpl(sortMap, PlayerStats.class, dictionary)).pagination(new ImmutablePagination(0, 2, false, true)).build();
    assertEquals(// table name
    "example_PlayerStats;" + // columns
    "{highScore;{}}" + // group by
    "{overallRating;{}}" + // time dimensions
    "{recordedDate;{}}" + // where
    "{P;{{example.PlayerStats;java.lang.String;countryNickName;}}IN;9;Uncle Sam;}" + // having
    "{P;{{example.PlayerStats;long;highScore;}}GT;3;300;}" + // sort
    "{example.PlayerStats;{{example.PlayerStats;java.lang.String;playerName;}}asc;}" + // pagination
    "{0;2;1;}", QueryKeyExtractor.extractKey(query));
}
Also used : Query(com.yahoo.elide.datastores.aggregation.query.Query) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) ImmutablePagination(com.yahoo.elide.datastores.aggregation.query.ImmutablePagination) PlayerStats(example.PlayerStats) TreeMap(java.util.TreeMap) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

SortingImpl (com.yahoo.elide.core.sort.SortingImpl)53 Test (org.junit.jupiter.api.Test)53 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)29 Query (com.yahoo.elide.datastores.aggregation.query.Query)29 TreeMap (java.util.TreeMap)29 HashMap (java.util.HashMap)23 EntityProjection (com.yahoo.elide.core.request.EntityProjection)15 Book (example.Book)15 PlayerStats (example.PlayerStats)12 Sorting (com.yahoo.elide.core.request.Sorting)10 Path (com.yahoo.elide.core.Path)9 Relationship (com.yahoo.elide.core.request.Relationship)8 Author (example.Author)8 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)7 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)7 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)7 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)6 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)6 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)6 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)6