Search in sources :

Example 41 with Query

use of com.yahoo.elide.datastores.aggregation.query.Query in project elide by yahoo.

the class QueryEngineTest method testMultipleTimeGrainsFilteredByDayAlias.

@Test
public void testMultipleTimeGrainsFilteredByDayAlias() 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("highScore", Sorting.SortOrder.asc);
    FilterPredicate predicate = new FilterPredicate(new Path(ClassType.of(PlayerStats.class), dictionary, "recordedDate", "byDay", new HashSet<>(dayArguments.values())), Operator.IN, Lists.newArrayList(new Day(Date.valueOf("2019-07-11"))));
    Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("highScore")).whereFilter(predicate).timeDimensionProjection(playerStatsTable.getTimeDimensionProjection("recordedDate", "byDay", dayArguments)).timeDimensionProjection(playerStatsTable.getTimeDimensionProjection("recordedDate", "byMonth", monthArguments)).sorting(new SortingImpl(sortMap, PlayerStats.class, dictionary)).build();
    List<PlayerStats> results = toList(engine.executeQuery(query, transaction).getData());
    assertEquals(1, 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));
}
Also used : Path(com.yahoo.elide.core.Path) Argument(com.yahoo.elide.core.request.Argument) Query(com.yahoo.elide.datastores.aggregation.query.Query) HashMap(java.util.HashMap) PlayerStats(example.PlayerStats) TreeMap(java.util.TreeMap) Month(com.yahoo.elide.datastores.aggregation.timegrains.Month) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) 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 42 with Query

use of com.yahoo.elide.datastores.aggregation.query.Query in project elide by yahoo.

the class SubselectTest method testJoinToFilter.

/**
 * Test grouping by a dimension with a JoinTo annotation.
 *
 * @throws Exception exception
 */
@Test
public void testJoinToFilter() throws Exception {
    Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("highScore")).dimensionProjection(playerStatsTable.getDimensionProjection("overallRating")).whereFilter(filterParser.parseFilterExpression("subCountryIsoCode==USA", playerStatsType, false)).build();
    List<Object> results = toList(engine.executeQuery(query, transaction).getData());
    PlayerStats stats1 = new PlayerStats();
    stats1.setId("0");
    stats1.setOverallRating("Good");
    stats1.setHighScore(1234);
    PlayerStats stats2 = new PlayerStats();
    stats2.setId("1");
    stats2.setOverallRating("Great");
    stats2.setHighScore(2412);
    assertEquals(2, results.size());
    assertEquals(stats1, results.get(0));
    assertEquals(stats2, results.get(1));
}
Also used : Query(com.yahoo.elide.datastores.aggregation.query.Query) PlayerStats(example.PlayerStats) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 43 with Query

use of com.yahoo.elide.datastores.aggregation.query.Query in project elide by yahoo.

the class UseASBeforeTableAliasExplainQueryTest method testInnerJoin.

@Test
public void testInnerJoin() throws Exception {
    Query query = TestQuery.INNER_JOIN.getQuery();
    String expectedQueryStr = "SELECT DISTINCT `example_VideoGame_playerInnerJoin_XXX`.`name` AS `playerNameInnerJoin` FROM `videoGames` `example_VideoGame`" + " INNER JOIN `players` `example_VideoGame_playerInnerJoin_XXX` ON `example_VideoGame`.`player_id`" + " = `example_VideoGame_playerInnerJoin_XXX`.`id`";
    compareQueryLists(expectedQueryStr, engine.explain(query));
}
Also used : Query(com.yahoo.elide.datastores.aggregation.query.Query) Test(org.junit.jupiter.api.Test) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)

Example 44 with Query

use of com.yahoo.elide.datastores.aggregation.query.Query 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 45 with Query

use of com.yahoo.elide.datastores.aggregation.query.Query 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)

Aggregations

Query (com.yahoo.elide.datastores.aggregation.query.Query)242 Test (org.junit.jupiter.api.Test)229 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)214 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)51 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)46 Argument (com.yahoo.elide.core.request.Argument)43 ArrayList (java.util.ArrayList)42 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)37 Path (com.yahoo.elide.core.Path)35 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)31 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)29 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)29 PlayerStats (example.PlayerStats)29 TreeMap (java.util.TreeMap)29 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)28 HashMap (java.util.HashMap)26 Date (java.util.Date)19 HashSet (java.util.HashSet)17 ToString (lombok.ToString)16 SQLDimensionProjection (com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLDimensionProjection)14