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);
}
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");
}
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));
}
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");
}
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));
}
Aggregations