Search in sources :

Example 1 with SortingImpl

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

the class InMemoryStoreTransactionTest method testSortOnComplexAttribute.

@Test
public void testSortOnComplexAttribute() {
    Map<String, Sorting.SortOrder> sortOrder = new HashMap<>();
    sortOrder.put("homeAddress.street1", Sorting.SortOrder.asc);
    Sorting sorting = new SortingImpl(sortOrder, Author.class, dictionary);
    EntityProjection projection = EntityProjection.builder().type(Author.class).sorting(sorting).build();
    DataStoreIterable sortInMemory = new DataStoreIterableBuilder(Arrays.asList(author1, author2)).sortInMemory(true).build();
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(sortInMemory);
    Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
    assertEquals(2, loaded.size());
    Object[] sorted = loaded.toArray();
    assertEquals(author2, sorted[0]);
    assertEquals(author1, sorted[1]);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) HashMap(java.util.HashMap) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Sorting(com.yahoo.elide.core.request.Sorting) Test(org.junit.jupiter.api.Test)

Example 2 with SortingImpl

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

the class InMemoryStoreTransactionTest method testSortingRequiresInMemoryPagination.

@Test
public void testSortingRequiresInMemoryPagination() {
    PaginationImpl pagination = new PaginationImpl(ClassType.of(Book.class), 0, 3, 10, 10, true, false);
    Map<String, Sorting.SortOrder> sortOrder = new HashMap<>();
    sortOrder.put("title", Sorting.SortOrder.desc);
    Sorting sorting = new SortingImpl(sortOrder, Book.class, dictionary);
    EntityProjection projection = EntityProjection.builder().type(Book.class).sorting(sorting).pagination(pagination).build();
    DataStoreIterable sortInMemory = new DataStoreIterableBuilder(books).sortInMemory(true).build();
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(sortInMemory);
    Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
    verify(wrappedTransaction, times(1)).loadObjects(any(EntityProjection.class), eq(scope));
    assertEquals(3, loaded.size());
    List<String> bookTitles = loaded.stream().map((o) -> ((Book) o).getTitle()).collect(Collectors.toList());
    assertEquals(Lists.newArrayList("Book 3", "Book 2", "Book 1"), bookTitles);
    assertEquals(3, pagination.getPageTotals());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Path(com.yahoo.elide.core.Path) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Publisher(example.Publisher) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) ClassType(com.yahoo.elide.core.type.ClassType) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Relationship(com.yahoo.elide.core.request.Relationship) RequestScope(com.yahoo.elide.core.RequestScope) ImmutableSet(com.google.common.collect.ImmutableSet) DefaultClassScanner(com.yahoo.elide.core.utils.DefaultClassScanner) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) HashMap(java.util.HashMap) Author(example.Author) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Editor(example.Editor) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) ElideSettings(com.yahoo.elide.ElideSettings) Price(example.Price) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Sorting(com.yahoo.elide.core.request.Sorting) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Mockito.times(org.mockito.Mockito.times) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Address(example.Address) Mockito.reset(org.mockito.Mockito.reset) EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) HashMap(java.util.HashMap) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Sorting(com.yahoo.elide.core.request.Sorting) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) Book(example.Book) Test(org.junit.jupiter.api.Test)

Example 3 with SortingImpl

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

the class AggregateBeforeJoinOptimizerTest method testSortAndPaginate.

@Test
public void testSortAndPaginate() {
    Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
    sortMap.put("highScore", Sorting.SortOrder.desc);
    // WHERE filter
    FilterPredicate predicate = new FilterPredicate(new Path(PlayerStats.class, dictionary, "highScore"), Operator.GT, Arrays.asList(9000));
    Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("highScore")).dimensionProjection(playerStatsTable.getDimensionProjection("overallRating")).timeDimensionProjection(playerStatsTable.getTimeDimensionProjection("recordedDate")).pagination(new ImmutablePagination(10, 5, false, true)).sorting(new SortingImpl(sortMap, PlayerStats.class, dictionary)).havingFilter(predicate).whereFilter(parseFilterExpression("countryIsoCode==USA", playerStatsType, false)).build();
    String expectedQueryStr1 = "SELECT COUNT(*) FROM " + "(SELECT `example_PlayerStats_XXX`.`overallRating`, " + "`example_PlayerStats_XXX`.`recordedDate` " + "FROM (SELECT MAX(`example_PlayerStats`.`highScore`) AS `INNER_AGG_XXX`," + "`example_PlayerStats`.`overallRating` AS `overallRating`," + "`example_PlayerStats`.`country_id` AS `country_id`," + "PARSEDATETIME(FORMATDATETIME(`example_PlayerStats`.`recordedDate`, 'yyyy-MM-dd'), 'yyyy-MM-dd') AS `recordedDate` " + "FROM `playerStats` AS `example_PlayerStats` " + "GROUP BY `example_PlayerStats`.`overallRating`, " + "`example_PlayerStats`.`country_id`, " + "PARSEDATETIME(FORMATDATETIME(`example_PlayerStats`.`recordedDate`, 'yyyy-MM-dd'), 'yyyy-MM-dd') ) " + "AS `example_PlayerStats_XXX` " + "LEFT OUTER JOIN `countries` AS `example_PlayerStats_XXX_country_XXX` " + "ON `example_PlayerStats_XXX`.`country_id` = `example_PlayerStats_XXX_country_XXX`.`id` " + "WHERE `example_PlayerStats_XXX_country_XXX`.`iso_code` IN (:XXX) " + "GROUP BY `example_PlayerStats_XXX`.`overallRating`, " + "`example_PlayerStats_XXX`.`recordedDate` " + "HAVING MAX(`example_PlayerStats_XXX`.`INNER_AGG_XXX`) > :XXX ) AS `pagination_subquery`\n";
    String expectedQueryStr2 = "SELECT MAX(`example_PlayerStats_XXX`.`INNER_AGG_XXX`) AS `highScore`," + "`example_PlayerStats_XXX`.`overallRating` AS `overallRating`," + "`example_PlayerStats_XXX`.`recordedDate` AS `recordedDate` " + "FROM (SELECT MAX(`example_PlayerStats`.`highScore`) AS `INNER_AGG_XXX`," + "`example_PlayerStats`.`overallRating` AS `overallRating`," + "`example_PlayerStats`.`country_id` AS `country_id`," + "PARSEDATETIME(FORMATDATETIME(`example_PlayerStats`.`recordedDate`, 'yyyy-MM-dd'), 'yyyy-MM-dd') AS `recordedDate` " + "FROM `playerStats` AS `example_PlayerStats` " + "GROUP BY `example_PlayerStats`.`overallRating`, " + "`example_PlayerStats`.`country_id`, " + "PARSEDATETIME(FORMATDATETIME(`example_PlayerStats`.`recordedDate`, 'yyyy-MM-dd'), 'yyyy-MM-dd') ) " + "AS `example_PlayerStats_XXX` " + "LEFT OUTER JOIN `countries` AS `example_PlayerStats_XXX_country_XXX` " + "ON `example_PlayerStats_XXX`.`country_id` = `example_PlayerStats_XXX_country_XXX`.`id` " + "WHERE `example_PlayerStats_XXX_country_XXX`.`iso_code` IN (:XXX) " + "GROUP BY `example_PlayerStats_XXX`.`overallRating`, " + "`example_PlayerStats_XXX`.`recordedDate` " + "HAVING MAX(`example_PlayerStats_XXX`.`INNER_AGG_XXX`) > :XXX " + "ORDER BY MAX(`example_PlayerStats_XXX`.`INNER_AGG_XXX`) " + "DESC LIMIT 5 OFFSET 10\n";
    List<String> expectedQueryList = new ArrayList<>();
    expectedQueryList.add(expectedQueryStr1);
    expectedQueryList.add(expectedQueryStr2);
    compareQueryLists(expectedQueryList, engine.explain(query));
    testQueryExecution(query);
}
Also used : Path(com.yahoo.elide.core.Path) Query(com.yahoo.elide.datastores.aggregation.query.Query) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) ArrayList(java.util.ArrayList) ImmutablePagination(com.yahoo.elide.datastores.aggregation.query.ImmutablePagination) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) PlayerStats(example.PlayerStats) TreeMap(java.util.TreeMap) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 4 with SortingImpl

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

the class AggregateBeforeJoinOptimizerTest method testSortOnMultiReferenceTimeDimensionInProjection.

@Test
public void testSortOnMultiReferenceTimeDimensionInProjection() {
    SQLTable gameRevenueTable = (SQLTable) metaDataStore.getTable("gameRevenue", NO_VERSION);
    Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
    sortMap.put("lastDate", Sorting.SortOrder.desc);
    Query query = Query.builder().source(gameRevenueTable).metricProjection(gameRevenueTable.getMetricProjection("revenue")).timeDimensionProjection(gameRevenueTable.getTimeDimensionProjection("lastDate")).sorting(new SortingImpl(sortMap, GameRevenue.class, dictionary)).build();
    String expected = "SELECT MAX(`example_GameRevenue_XXX`.`INNER_AGG_XXX`) AS `revenue`," + "PARSEDATETIME(FORMATDATETIME(CASE WHEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` > `example_GameRevenue_XXX`.`saleDate` THEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` ELSE `example_GameRevenue_XXX`.`saleDate` END, 'yyyy-MM-dd'), 'yyyy-MM-dd') AS `lastDate` " + "FROM (SELECT MAX(`example_GameRevenue`.`revenue`) AS `INNER_AGG_XXX`," + "`example_GameRevenue`.`player_stats_id` AS `player_stats_id`," + "`example_GameRevenue`.`saleDate` AS `saleDate` " + "FROM `gameRevenue` AS `example_GameRevenue` " + "GROUP BY `example_GameRevenue`.`player_stats_id`, " + "`example_GameRevenue`.`saleDate` ) AS `example_GameRevenue_XXX` " + "LEFT OUTER JOIN `playerStats` AS `example_GameRevenue_XXX_playerStats_XXX` " + "ON `example_GameRevenue_XXX`.`player_stats_id` = `example_GameRevenue_XXX_playerStats_XXX`.`id` " + "GROUP BY PARSEDATETIME(FORMATDATETIME(CASE WHEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` > `example_GameRevenue_XXX`.`saleDate` THEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` ELSE `example_GameRevenue_XXX`.`saleDate` END, 'yyyy-MM-dd'), 'yyyy-MM-dd') " + "ORDER BY PARSEDATETIME(FORMATDATETIME(CASE WHEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` > `example_GameRevenue_XXX`.`saleDate` THEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` ELSE `example_GameRevenue_XXX`.`saleDate` END, 'yyyy-MM-dd'), 'yyyy-MM-dd') DESC\n";
    compareQueryLists(expected, engine.explain(query));
    testQueryExecution(query);
}
Also used : Query(com.yahoo.elide.datastores.aggregation.query.Query) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) TreeMap(java.util.TreeMap) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 5 with SortingImpl

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

the class AggregateBeforeJoinOptimizerTest method testSortOnDimensionInProjectionNotRequiringJoin.

@Test
public void testSortOnDimensionInProjectionNotRequiringJoin() {
    SQLTable gameRevenueTable = (SQLTable) metaDataStore.getTable("gameRevenue", NO_VERSION);
    Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
    sortMap.put("category", Sorting.SortOrder.desc);
    Query query = Query.builder().source(gameRevenueTable).dimensionProjection(gameRevenueTable.getDimensionProjection("countryIsoCode")).dimensionProjection(gameRevenueTable.getDimensionProjection("category")).metricProjection(gameRevenueTable.getMetricProjection("revenue")).sorting(new SortingImpl(sortMap, GameRevenue.class, dictionary)).build();
    String expected = "SELECT MAX(`example_GameRevenue_XXX`.`INNER_AGG_XXX`) AS `revenue`," + "`example_GameRevenue_XXX_country_XXX`.`iso_code` AS `countryIsoCode`," + "`example_GameRevenue_XXX`.`category` AS `category` " + "FROM (SELECT MAX(`example_GameRevenue`.`revenue`) AS `INNER_AGG_XXX`," + "`example_GameRevenue`.`country_id` AS `country_id`," + "`example_GameRevenue`.`category` AS `category` " + "FROM `gameRevenue` AS `example_GameRevenue` " + "GROUP BY `example_GameRevenue`.`country_id`, " + "`example_GameRevenue`.`category` ) " + "AS `example_GameRevenue_XXX` " + "LEFT OUTER JOIN `countries` AS `example_GameRevenue_XXX_country_XXX` " + "ON `example_GameRevenue_XXX`.`country_id` = `example_GameRevenue_XXX_country_XXX`.`id` " + "GROUP BY `example_GameRevenue_XXX_country_XXX`.`iso_code`, " + "`example_GameRevenue_XXX`.`category` " + "ORDER BY `example_GameRevenue_XXX`.`category` DESC\n";
    compareQueryLists(expected, engine.explain(query));
    testQueryExecution(query);
}
Also used : Query(com.yahoo.elide.datastores.aggregation.query.Query) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) TreeMap(java.util.TreeMap) 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