Search in sources :

Example 11 with SortingImpl

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

the class ViewTest method testViewAttribute.

@Test
public void testViewAttribute() throws Exception {
    Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
    sortMap.put("countryViewIsoCode", Sorting.SortOrder.desc);
    Query query = Query.builder().source(playerStatsWithViewSchema).metricProjection(playerStatsWithViewSchema.getMetricProjection("lowScore")).dimensionProjection(playerStatsWithViewSchema.getDimensionProjection("countryViewIsoCode")).sorting(new SortingImpl(sortMap, PlayerStatsWithView.class, dictionary)).build();
    List<Object> results = toList(engine.executeQuery(query, transaction).getData());
    PlayerStatsWithView usa0 = new PlayerStatsWithView();
    usa0.setId("0");
    usa0.setLowScore(35);
    usa0.setCountryViewIsoCode("USA");
    PlayerStatsWithView hk1 = new PlayerStatsWithView();
    hk1.setId("1");
    hk1.setLowScore(72);
    hk1.setCountryViewIsoCode("HKG");
    assertEquals(2, results.size());
    assertEquals(usa0, results.get(0));
    assertEquals(hk1, results.get(1));
    // the join would not happen for a view join
    PlayerStatsWithView actualStats1 = (PlayerStatsWithView) results.get(0);
    assertNull(actualStats1.getCountry());
}
Also used : Query(com.yahoo.elide.datastores.aggregation.query.Query) PlayerStatsWithView(example.PlayerStatsWithView) 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 12 with SortingImpl

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

the class ViewTest method testSortingViewAttribute.

@Test
public void testSortingViewAttribute() throws Exception {
    Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
    sortMap.put("countryViewIsoCode", Sorting.SortOrder.desc);
    Query query = Query.builder().source(playerStatsWithViewSchema).metricProjection(playerStatsWithViewSchema.getMetricProjection("lowScore")).dimensionProjection(playerStatsWithViewSchema.getDimensionProjection("countryViewViewIsoCode")).sorting(new SortingImpl(sortMap, PlayerStatsWithView.class, dictionary)).build();
    List<Object> results = toList(engine.executeQuery(query, transaction).getData());
    PlayerStatsWithView usa0 = new PlayerStatsWithView();
    usa0.setId("0");
    usa0.setLowScore(35);
    usa0.setCountryViewViewIsoCode("USA");
    PlayerStatsWithView hk1 = new PlayerStatsWithView();
    hk1.setId("1");
    hk1.setLowScore(72);
    hk1.setCountryViewViewIsoCode("HKG");
    assertEquals(2, results.size());
    assertEquals(usa0, results.get(0));
    assertEquals(hk1, results.get(1));
    // the join would not happen for a view join
    PlayerStatsWithView actualStats1 = (PlayerStatsWithView) results.get(0);
    assertNull(actualStats1.getCountry());
}
Also used : Query(com.yahoo.elide.datastores.aggregation.query.Query) PlayerStatsWithView(example.PlayerStatsWithView) 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 13 with SortingImpl

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

the class QueryEngineTest method testAllArgumentQuery.

/**
 * Test group by, having, dimension, metric at the same time.
 *
 * @throws Exception exception
 */
@Test
public void testAllArgumentQuery() throws Exception {
    Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
    sortMap.put("countryName", Sorting.SortOrder.asc);
    Query query = Query.builder().source(playerStatsViewTable).metricProjection(playerStatsViewTable.getMetricProjection("highScore")).dimensionProjection(playerStatsViewTable.getDimensionProjection("countryName")).whereFilter(filterParser.parseFilterExpression("countryName=='United States'", playerStatsViewType, false)).havingFilter(filterParser.parseFilterExpression("highScore > 300", playerStatsViewType, false)).sorting(new SortingImpl(sortMap, PlayerStatsView.class, dictionary)).arguments(playerStatsViewTableArgs).build();
    List<Object> results = toList(engine.executeQuery(query, transaction).getData());
    PlayerStatsView stats2 = new PlayerStatsView();
    stats2.setId("0");
    stats2.setHighScore(2412);
    stats2.setCountryName("United States");
    assertEquals(ImmutableList.of(stats2), results);
}
Also used : PlayerStatsView(example.PlayerStatsView) 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 14 with SortingImpl

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

the class QueryEngineTest method testMultipleTimeGrainsSortedByDayAlias.

@Test
public void testMultipleTimeGrainsSortedByDayAlias() throws Exception {
    Map<String, Argument> dayArguments = new HashMap<>();
    dayArguments.put("grain", Argument.builder().name("grain").value(TimeGrain.DAY).build());
    Map<String, Argument> monthArguments = new HashMap<>();
    monthArguments.put("grain", Argument.builder().name("grain").value(TimeGrain.MONTH).build());
    Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
    sortMap.put("byDay", Sorting.SortOrder.asc);
    Set<Attribute> sortAttributes = new HashSet<>(Arrays.asList(Attribute.builder().type(TIME_TYPE).name("recordedDate").alias("byDay").arguments(dayArguments.values()).build()));
    Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("highScore")).timeDimensionProjection(playerStatsTable.getTimeDimensionProjection("recordedDate", "byDay", dayArguments)).timeDimensionProjection(playerStatsTable.getTimeDimensionProjection("recordedDate", "byMonth", monthArguments)).sorting(new SortingImpl(sortMap, ClassType.of(PlayerStats.class), sortAttributes, dictionary)).build();
    List<PlayerStats> results = toList(engine.executeQuery(query, transaction).getData());
    assertEquals(3, results.size());
    assertEquals(2412, results.get(0).getHighScore());
    assertEquals(new Day(Date.valueOf("2019-07-11")), results.get(0).fetch("byDay", null));
    assertEquals(new Month(Date.valueOf("2019-07-01")), results.get(0).fetch("byMonth", null));
    assertEquals(1234, results.get(1).getHighScore());
    assertEquals(new Day(Date.valueOf("2019-07-12")), results.get(1).fetch("byDay", null));
    assertEquals(new Month(Date.valueOf("2019-07-01")), results.get(1).fetch("byMonth", null));
    assertEquals(1000, results.get(2).getHighScore());
    assertEquals(new Day(Date.valueOf("2019-07-13")), results.get(2).fetch("byDay", null));
    assertEquals(new Month(Date.valueOf("2019-07-01")), results.get(2).fetch("byMonth", null));
}
Also used : Argument(com.yahoo.elide.core.request.Argument) Query(com.yahoo.elide.datastores.aggregation.query.Query) HashMap(java.util.HashMap) Attribute(com.yahoo.elide.core.request.Attribute) PlayerStats(example.PlayerStats) TreeMap(java.util.TreeMap) Month(com.yahoo.elide.datastores.aggregation.timegrains.Month) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) Day(com.yahoo.elide.datastores.aggregation.timegrains.Day) HashSet(java.util.HashSet) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 15 with SortingImpl

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

the class SubCollectionFetchQueryBuilderTest method testSubCollectionFetchWithRelationshipSorting.

@Test
public void testSubCollectionFetchWithRelationshipSorting() {
    Author author = new Author();
    author.setId(1L);
    Book book = new Book();
    book.setId(2);
    Map<String, Sorting.SortOrder> sorting = new HashMap<>();
    sorting.put(PUBLISHER + PERIOD + NAME, Sorting.SortOrder.asc);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).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_Author__fetch=:example_Author__fetch order by example_Book_publisher.name asc";
    String actual = query.getQueryText();
    actual = actual.trim().replaceAll(" +", " ");
    assertEquals(expected, actual);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) SubCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder) HashMap(java.util.HashMap) 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) 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