Search in sources :

Example 61 with Query

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

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

the class QueryEngineTest method testFromSubQuery.

/**
 * Test loading records using {@link FromSubquery}.
 *
 * @throws Exception exception
 */
@Test
public void testFromSubQuery() throws Exception {
    Query query = Query.builder().source(playerStatsViewTable.toQueryable()).metricProjection(playerStatsViewTable.getMetricProjection("highScore")).arguments(playerStatsViewTableArgs).build();
    List<Object> results = toList(engine.executeQuery(query, transaction).getData());
    PlayerStatsView stats2 = new PlayerStatsView();
    stats2.setId("0");
    stats2.setHighScore(2412);
    assertEquals(ImmutableList.of(stats2), results);
}
Also used : PlayerStatsView(example.PlayerStatsView) Query(com.yahoo.elide.datastores.aggregation.query.Query) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 63 with Query

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

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

the class QueryEngineTest method testHavingClauseJoin.

/**
 * Test having clause integrates with group by clause and join.
 *
 * @throws Exception exception
 */
@Test
public void testHavingClauseJoin() throws Exception {
    Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("highScore")).dimensionProjection(playerStatsTable.getDimensionProjection("overallRating")).dimensionProjection(playerStatsTable.getDimensionProjection("countryIsoCode")).havingFilter(filterParser.parseFilterExpression("countryIsoCode==USA", playerStatsType, false)).build();
    List<Object> results = toList(engine.executeQuery(query, transaction).getData());
    PlayerStats stats0 = new PlayerStats();
    stats0.setId("1");
    stats0.setOverallRating("Great");
    stats0.setCountryIsoCode("USA");
    stats0.setHighScore(2412);
    PlayerStats stats1 = new PlayerStats();
    stats1.setId("0");
    stats1.setOverallRating("Good");
    stats1.setCountryIsoCode("USA");
    stats1.setHighScore(1234);
    assertEquals(ImmutableList.of(stats1, stats0), results);
}
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 65 with Query

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

the class CurrencyRate method testLogicalReference.

@Test
public void testLogicalReference() {
    Map<String, Argument> impressionsArg = new HashMap<>();
    impressionsArg.put("aggregation", Argument.builder().name("aggregation").value("SUM").build());
    SQLMetricProjection impressions = (SQLMetricProjection) revenueFactTable.getMetricProjection("impressions", "impressions", impressionsArg);
    Map<String, Argument> testImpressionsArg = new HashMap<>();
    testImpressionsArg.put("aggregation", Argument.builder().name("aggregation").value("MIN").build());
    SQLMetricProjection testImpressions = (SQLMetricProjection) revenueFactTable.getMetricProjection("testImpressions", "testImpressions", testImpressionsArg);
    SQLMetricProjection testRevenue = (SQLMetricProjection) revenueFactTable.getMetricProjection("testRevenue");
    SQLMetricProjection testRevenueLogicalRef = (SQLMetricProjection) revenueFactTable.getMetricProjection("testRevenueLogicalRef");
    Map<String, Argument> revenueArg = new HashMap<>();
    revenueArg.put("format", Argument.builder().name("format").value("11D00").build());
    SQLMetricProjection revenueWithArg = (SQLMetricProjection) revenueFactTable.getMetricProjection("revenue", "revenueWithArg", revenueArg);
    SQLDimensionProjection conversionRate = (SQLDimensionProjection) revenueFactTable.getDimensionProjection("conversionRate");
    SQLDimensionProjection rateProvider = (SQLDimensionProjection) revenueFactTable.getDimensionProjection("rateProvider");
    Query query = Query.builder().source(revenueFactTable).metricProjection(impressions).metricProjection(testImpressions).metricProjection(testRevenue).metricProjection(testRevenueLogicalRef).metricProjection(revenueWithArg).dimensionProjection(conversionRate).dimensionProjection(rateProvider).arguments(queryArgs).build();
    Query.QueryBuilder builder = Query.builder().source(query.getSource()).metricProjections(query.getMetricProjections()).dimensionProjections(query.getDimensionProjections()).arguments(query.getArguments());
    Query expandedQuery = addHiddenProjections(metaDataStore, builder, query).build();
    // definition: {{$$column.args.aggregation}}({{$impressions}})
    // -> value of 'aggregation' argument is passed in the query for "impressions" column and same is used while
    // resolving this column.
    assertEquals("SUM(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact`.`impressions`)", impressions.toSQL(expandedQuery, metaDataStore));
    // definition: {{impressions}}) * {{$$table.args.testPercentage}}
    // -> default value of table argument 'testPercentage' is used.
    // -> value of 'aggregation' argument is passed in the query for "testImpressions" column and same is used while
    // resolving referenced column "impressions".
    assertEquals("MIN(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact`.`impressions`)) * 0.1", testImpressions.toSQL(expandedQuery, metaDataStore));
    // definition: {{revenue}}
    // revenue definition: TO_CHAR(SUM({{$revenue}}) * {{rate.conversionRate}}, {{$$column.args.format}})
    // -> default value of 'format' argument in "revenue" column is used while resolving this column.
    // -> default value of 'format' argument in "revenue" column is passed to joined table's "conversionRate" column.
    assertEquals("TO_CHAR(SUM(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact`.`revenue`)" + " * TO_CHAR(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact_rate_221749474`.`conversion_rate`, 99D00), 99D00)", testRevenue.toSQL(expandedQuery, metaDataStore));
    // definition: {{revenueUsingLogicalRef}}
    // revenueUsingLogicalRef's definition: TO_CHAR(SUM({{$revenue}}) * {{conversionRate}}, {{$$column.args.format}})
    // -> default value of 'format' argument in "revenueUsingLogicalRef" column is used while resolving this column.
    // -> This column references "conversionRate" which references "rate.conversionRate". Since conversionRate doesn't have
    // 'format' argument defined, default value of 'format' argument in joined table's "conversionRate" column is used.
    assertEquals("TO_CHAR(SUM(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact`.`revenue`)" + " * TO_CHAR(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact_rate_221749474`.`conversion_rate`, 9D0), 99D00)", testRevenueLogicalRef.toSQL(expandedQuery, metaDataStore));
    // definition: TO_CHAR(SUM({{$revenue}}) * {{rate.conversionRate}}, {{$$column.args.format}})
    // -> value of 'format' argument is passed in the query for "revenue" column and same is used for resolving
    // referenced column "rate.conversionRate" and this column.
    assertEquals("TO_CHAR(SUM(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact`.`revenue`)" + " * TO_CHAR(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact_rate_221749474`.`conversion_rate`, 11D00), 11D00)", revenueWithArg.toSQL(expandedQuery, metaDataStore));
    // definition: {{rate.conversionRate}}
    // -> logical column 'conversionRate' doesn't support arguments.
    // -> default value of 'format' argument in "conversionRate" column of joined table is used while resolving this.
    assertEquals("TO_CHAR(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact_rate_221749474`.`conversion_rate`, 9D0)", conversionRate.toSQL(expandedQuery, metaDataStore));
    // definition: {{rate.$provider}}
    assertEquals("`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact_rate_221749474`.`provider`", rateProvider.toSQL(expandedQuery, metaDataStore));
}
Also used : Argument(com.yahoo.elide.core.request.Argument) Query(com.yahoo.elide.datastores.aggregation.query.Query) HashMap(java.util.HashMap) SQLDimensionProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLDimensionProjection) ToString(lombok.ToString) SQLMetricProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLMetricProjection) 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