Search in sources :

Example 1 with ImmutablePagination

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

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

the class QueryEngineTest method testPaginationWithFilter.

/**
 * Nested Queries with filter - Pagination
 * @throws Exception
 */
@Test
public void testPaginationWithFilter() throws Exception {
    Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("dailyAverageScorePerPeriod")).dimensionProjection(playerStatsTable.getDimensionProjection("overallRating")).whereFilter(filterParser.parseFilterExpression("overallRating==Great", playerStatsType, false)).timeDimensionProjection(playerStatsTable.getTimeDimensionProjection("recordedDate")).pagination(new ImmutablePagination(0, 1, false, true)).build();
    QueryResult result = engine.executeQuery(query, transaction);
    List<Object> data = toList(result.getData());
    // Jon Doe,1234,72,Good,840,2019-07-12 00:00:00
    PlayerStats stats1 = new PlayerStats();
    stats1.setId("0");
    stats1.setDailyAverageScorePerPeriod(2412.0f);
    stats1.setOverallRating("Great");
    stats1.setRecordedDate(new Day(Date.valueOf("2019-07-11")));
    assertEquals(ImmutableList.of(stats1), data, "Returned record does not match");
    assertEquals(1, result.getPageTotals(), "Page totals does not match");
}
Also used : QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) Query(com.yahoo.elide.datastores.aggregation.query.Query) ImmutablePagination(com.yahoo.elide.datastores.aggregation.query.ImmutablePagination) PlayerStats(example.PlayerStats) Day(com.yahoo.elide.datastores.aggregation.timegrains.Day) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 3 with ImmutablePagination

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

the class QueryKeyExtractorTest method testDuplicateFullQuery.

@Test
public void testDuplicateFullQuery() throws Exception {
    // Build 1st Table
    NamespaceConfig testNamespace = NamespaceConfig.builder().name("namespace1").build();
    NamespacePackage testNamespacePackage = new NamespacePackage(testNamespace);
    Table testTable = Table.builder().cardinality("medium").description("A test table").friendlyName("foo").table("table1").name("Table").schema("db1").category("category1").readAccess("Admin").dbConnectionName("dbConn").isFact(true).filterTemplate("a==b").namespace("namespace1").dimension(Dimension.builder().name("dim1").definition("{{dim1}}").type(Type.BOOLEAN).values(Collections.EMPTY_SET).tags(Collections.EMPTY_SET).build()).build();
    TableType testType = new TableType(testTable, testNamespacePackage);
    dictionary.bindEntity(testType);
    SQLTable testSqlTable = new SQLTable(new Namespace(testNamespacePackage), testType, dictionary);
    // Build 2nd Table
    NamespaceConfig anotherTestNamespace = NamespaceConfig.builder().name("namespace2").build();
    NamespacePackage anotherTestNamespacePackage = new NamespacePackage(anotherTestNamespace);
    Table anotherTestTable = // Exactly same as testTable but different namespace
    Table.builder().cardinality("medium").description("A test table").friendlyName("foo").table("table1").name("Table").schema("db1").category("category1").readAccess("Admin").dbConnectionName("dbConn").isFact(true).filterTemplate("a==b").namespace("namespace2").dimension(Dimension.builder().name("dim1").definition("{{dim1}}").type(Type.BOOLEAN).values(Collections.EMPTY_SET).tags(Collections.EMPTY_SET).build()).build();
    TableType anotherTestType = new TableType(anotherTestTable, anotherTestNamespacePackage);
    dictionary.bindEntity(anotherTestType);
    SQLTable anotherTestSqlTable = new SQLTable(new Namespace(anotherTestNamespacePackage), anotherTestType, dictionary);
    // Build Query and Test
    Query query = Query.builder().source(testSqlTable).dimensionProjection(testSqlTable.getDimensionProjection("dim1")).pagination(new ImmutablePagination(0, 2, false, true)).build();
    assertEquals(// table name
    "namespace1_Table;" + // metrics
    "{}" + // Group by
    "{dim1;{}}" + // time dimensions
    "{}" + // where
    ";" + // having
    ";" + // sort
    ";" + // pagination
    "{0;2;1;}", QueryKeyExtractor.extractKey(query));
    Query anotherQuery = Query.builder().source(anotherTestSqlTable).dimensionProjection(anotherTestSqlTable.getDimensionProjection("dim1")).pagination(new ImmutablePagination(0, 2, false, true)).build();
    assertEquals(// table name
    "namespace2_Table;" + // metrics
    "{}" + // Group by
    "{dim1;{}}" + // time dimensions
    "{}" + // where
    ";" + // having
    ";" + // sort
    ";" + // pagination
    "{0;2;1;}", QueryKeyExtractor.extractKey(anotherQuery));
    assertNotEquals(QueryKeyExtractor.extractKey(anotherQuery), QueryKeyExtractor.extractKey(query));
}
Also used : NamespaceConfig(com.yahoo.elide.modelconfig.model.NamespaceConfig) Table(com.yahoo.elide.modelconfig.model.Table) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) TableType(com.yahoo.elide.datastores.aggregation.dynamic.TableType) Query(com.yahoo.elide.datastores.aggregation.query.Query) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) ImmutablePagination(com.yahoo.elide.datastores.aggregation.query.ImmutablePagination) NamespacePackage(com.yahoo.elide.datastores.aggregation.dynamic.NamespacePackage) Namespace(com.yahoo.elide.datastores.aggregation.metadata.models.Namespace) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 4 with ImmutablePagination

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

the class QueryEngineTest method testPagination.

/**
 * Test pagination.
 *
 * @throws Exception exception
 */
@Test
public void testPagination() throws Exception {
    Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("lowScore")).dimensionProjection(playerStatsTable.getDimensionProjection("overallRating")).timeDimensionProjection(playerStatsTable.getTimeDimensionProjection("recordedDate")).pagination(new ImmutablePagination(0, 1, false, true)).build();
    QueryResult result = engine.executeQuery(query, transaction);
    List<Object> data = toList(result.getData());
    // Jon Doe,1234,72,Good,840,2019-07-12 00:00:00
    PlayerStats stats1 = new PlayerStats();
    stats1.setId("0");
    stats1.setLowScore(35);
    stats1.setOverallRating("Good");
    stats1.setRecordedDate(new Day(Date.valueOf("2019-07-12")));
    assertEquals(ImmutableList.of(stats1), data, "Returned record does not match");
    assertEquals(3, result.getPageTotals(), "Page totals does not match");
}
Also used : QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) Query(com.yahoo.elide.datastores.aggregation.query.Query) ImmutablePagination(com.yahoo.elide.datastores.aggregation.query.ImmutablePagination) PlayerStats(example.PlayerStats) Day(com.yahoo.elide.datastores.aggregation.timegrains.Day) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 5 with ImmutablePagination

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

SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)5 ImmutablePagination (com.yahoo.elide.datastores.aggregation.query.ImmutablePagination)5 Query (com.yahoo.elide.datastores.aggregation.query.Query)5 Test (org.junit.jupiter.api.Test)5 PlayerStats (example.PlayerStats)4 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)2 QueryResult (com.yahoo.elide.datastores.aggregation.query.QueryResult)2 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)2 TreeMap (java.util.TreeMap)2 Path (com.yahoo.elide.core.Path)1 RSQLFilterDialect (com.yahoo.elide.core.filter.dialect.RSQLFilterDialect)1 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)1 NamespacePackage (com.yahoo.elide.datastores.aggregation.dynamic.NamespacePackage)1 TableType (com.yahoo.elide.datastores.aggregation.dynamic.TableType)1 Namespace (com.yahoo.elide.datastores.aggregation.metadata.models.Namespace)1 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)1 NamespaceConfig (com.yahoo.elide.modelconfig.model.NamespaceConfig)1 Table (com.yahoo.elide.modelconfig.model.Table)1 ArrayList (java.util.ArrayList)1