Search in sources :

Example 66 with Query

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

the class CurrencyRate method testSqlHelper.

@Test
public void testSqlHelper() {
    Map<String, Argument> revenueArg = new HashMap<>();
    revenueArg.put("format", Argument.builder().name("format").value("11D00").build());
    SQLMetricProjection revenueUsingSqlHelper = (SQLMetricProjection) revenueFactTable.getMetricProjection("revenueUsingSqlHelper", "revenueUsingSqlHelper", revenueArg);
    SQLMetricProjection impressionsPerUSD = (SQLMetricProjection) revenueFactTable.getMetricProjection("impressionsPerUSD");
    Map<String, Argument> impressionsPerUSDArg = new HashMap<>();
    impressionsPerUSDArg.put("format", Argument.builder().name("format").value("11D00").build());
    SQLMetricProjection impressionsPerUSDWithArg = (SQLMetricProjection) revenueFactTable.getMetricProjection("impressionsPerUSD", "impressionsPerUSDWithArg", impressionsPerUSDArg);
    // impressionsPerUSD2 invokes 'revenueUsingSqlHelper' instead of 'revenue'.
    SQLMetricProjection impressionsPerUSD2 = (SQLMetricProjection) revenueFactTable.getMetricProjection("impressionsPerUSD2");
    Query query = Query.builder().source(revenueFactTable).metricProjection(revenueUsingSqlHelper).metricProjection(impressionsPerUSD).metricProjection(impressionsPerUSDWithArg).metricProjection(impressionsPerUSD2).arguments(queryArgs).build();
    Query.QueryBuilder builder = Query.builder().source(query.getSource()).metricProjections(query.getMetricProjections()).arguments(query.getArguments());
    Query expandedQuery = addHiddenProjections(metaDataStore, builder, query).build();
    // definition: TO_CHAR(SUM({{$revenue}}) * {{sql from='rate' column='conversionRate[format:9999D0000]'}}, {{$$column.args.format}})
    // -> value of 'format' argument is passed in the query for "revenueUsingSqlHelper" column and same is used for
    // resolving this column.
    // -> pinned value (9999D0000) of 'format' argument in SQL helper is used while resolving referenced column "rate.conversionRate".
    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`, 9999D0000), 11D00)", revenueUsingSqlHelper.toSQL(expandedQuery, metaDataStore));
    // definition: TO_CHAR({{sql column='impressions[aggregation:SUM]'}} / {{sql column='revenue[format:99999D00000]'}}, {{$$table.args.format}})
    // -> {{$$table.args.format}} is resolved using query argument 'format' (999999D000000).
    // -> pinned value (SUM) of 'aggregation' argument in SQL helper is used while resolving invoked column "impressions".
    // -> pinned value (99999D00000) of 'format' argument in SQL helper is used while resolving invoked column "revenue".
    // -> revenue definition is : TO_CHAR(SUM({{$revenue}}) * {{rate.conversionRate}}, {{$$column.args.format}}),
    // Available 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`.`impressions`)" + " / TO_CHAR(SUM(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact`.`revenue`)" + " * TO_CHAR(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact_rate_221749474`.`conversion_rate`, 99999D00000), 99999D00000), 999999D000000)", impressionsPerUSD.toSQL(expandedQuery, metaDataStore));
    // -> Even 'format' is passed in query column args, pinned value (9999D0000) of 'format' argument in SQL helper is used while
    // resolving "revenue" column and same is passed to joined table's "conversionRate" column.
    assertEquals("TO_CHAR(SUM(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact`.`impressions`)" + " / TO_CHAR(SUM(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact`.`revenue`)" + " * TO_CHAR(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact_rate_221749474`.`conversion_rate`, 99999D00000), 99999D00000), 999999D000000)", impressionsPerUSDWithArg.toSQL(expandedQuery, metaDataStore));
    // definition: TO_CHAR({{sql column='impressions[aggregation:SUM]'}} / {{sql column='revenueUsingSqlHelper[format:99999D00000]'}}, {{$$table.args.format}})
    // -> As "rate.conversionRate" is invoked using SQL helper from "revenue" column, this uses the fixed value(9999D0000) of
    // 'format' argument provided in definition of "revenueUsingSqlHelper" column.
    assertEquals("TO_CHAR(SUM(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact`.`impressions`)" + " / TO_CHAR(SUM(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact`.`revenue`)" + " * TO_CHAR(`com_yahoo_elide_datastores_aggregation_metadata_RevenueFact_rate_221749474`.`conversion_rate`, 9999D0000), 99999D00000), 999999D000000)", impressionsPerUSD2.toSQL(expandedQuery, metaDataStore));
}
Also used : Argument(com.yahoo.elide.core.request.Argument) Query(com.yahoo.elide.datastores.aggregation.query.Query) HashMap(java.util.HashMap) ToString(lombok.ToString) SQLMetricProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLMetricProjection) Test(org.junit.jupiter.api.Test)

Example 67 with Query

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

the class DefaultQueryValidatorTest method testInvalidColumnArgument.

@Test
public void testInvalidColumnArgument() {
    SQLTable source = (SQLTable) metaDataStore.getTable("playerStatsView", NO_VERSION);
    Map<String, Argument> argumentMap = new HashMap<>();
    argumentMap.put("format", Argument.builder().name("format").value(";").build());
    Query query = Query.builder().source(source).metricProjection(source.getMetricProjection("highScore")).dimensionProjection(source.getDimensionProjection("countryName", "countryName", argumentMap)).build();
    validateQuery(query, "Invalid operation: Argument 'format' for column 'countryName' must match one of these values: [lower, upper]");
}
Also used : Argument(com.yahoo.elide.core.request.Argument) Query(com.yahoo.elide.datastores.aggregation.query.Query) HashMap(java.util.HashMap) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 68 with Query

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

the class DefaultQueryValidatorTest method testSortingOnNotQueriedDimension.

@Test
public void testSortingOnNotQueriedDimension() {
    Map<String, Sorting.SortOrder> sortMap = new TreeMap<>();
    sortMap.put("countryIsoCode", Sorting.SortOrder.asc);
    Query query = Query.builder().source(playerStatsTable).metricProjection(playerStatsTable.getMetricProjection("lowScore")).dimensionProjection(playerStatsTable.getDimensionProjection("overallRating")).sorting(new SortingImpl(sortMap, PlayerStats.class, dictionary)).build();
    validateQuery(query, "Invalid operation: Can not sort on countryIsoCode as it is not present in query");
}
Also used : 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 69 with Query

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

the class DefaultQueryValidatorTest method testInvalidTableArgument.

@Test
public void testInvalidTableArgument() {
    SQLTable source = (SQLTable) metaDataStore.getTable("playerStatsView", NO_VERSION);
    Map<String, Argument> tableArguments = new HashMap<>();
    tableArguments.put("rating", Argument.builder().name("rating").value("SELECT * FROM FOO;").build());
    Map<String, Argument> columnArguments = new HashMap<>();
    columnArguments.put("format", Argument.builder().name("format").value("lower").build());
    Query query = Query.builder().source(source).arguments(tableArguments).metricProjection(source.getMetricProjection("highScore")).dimensionProjection(source.getDimensionProjection("countryName", columnArguments)).build();
    validateQuery(query, "Invalid operation: Argument 'rating' for table 'playerStatsView' has an invalid value: SELECT * FROM FOO;");
}
Also used : Argument(com.yahoo.elide.core.request.Argument) Query(com.yahoo.elide.datastores.aggregation.query.Query) HashMap(java.util.HashMap) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 70 with Query

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

the class EntityProjectionTranslatorTest method testWherePromotion.

@Test
public void testWherePromotion() throws ParseException {
    FilterExpression originalFilter = filterParser.parseFilterExpression("overallRating==Good,lowScore[foo:bar]<45", playerStatsType, false);
    EntityProjection projection = basicProjection.copyOf().filterExpression(originalFilter).build();
    EntityProjectionTranslator translator = new EntityProjectionTranslator(engine, playerStatsTable, projection, scope, true);
    Query query = translator.getQuery();
    SplitFilterExpressionVisitor visitor = new SplitFilterExpressionVisitor(playerStatsTable);
    FilterConstraints constraints = originalFilter.accept(visitor);
    FilterExpression whereFilter = constraints.getWhereExpression();
    FilterExpression havingFilter = constraints.getHavingExpression();
    assertEquals(whereFilter, query.getWhereFilter());
    assertEquals(havingFilter, query.getHavingFilter());
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) Query(com.yahoo.elide.datastores.aggregation.query.Query) SplitFilterExpressionVisitor(com.yahoo.elide.datastores.aggregation.filter.visitor.SplitFilterExpressionVisitor) FilterConstraints(com.yahoo.elide.datastores.aggregation.filter.visitor.FilterConstraints) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) 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