Search in sources :

Example 1 with Argument

use of com.yahoo.elide.core.request.Argument in project elide by yahoo.

the class AggregationDataStoreTransaction method addColumnFilterArguments.

@VisibleForTesting
Query addColumnFilterArguments(Table table, Query query, EntityDictionary dictionary) {
    Query.QueryBuilder queryBuilder = Query.builder();
    query.getColumnProjections().stream().forEach(projection -> {
        Column column = table.getColumn(Column.class, projection.getName());
        FilterExpression requiredFilter = column.getRequiredFilter(dictionary);
        if (requiredFilter != null) {
            Map<String, Argument> allArguments = validateRequiredFilter(requiredFilter, query, column);
            if (projection.getArguments() != null) {
                allArguments.putAll(projection.getArguments());
            }
            queryBuilder.column(projection.withArguments(allArguments));
        } else {
            queryBuilder.column(projection);
        }
    });
    return queryBuilder.arguments(query.getArguments()).havingFilter(query.getHavingFilter()).whereFilter(query.getWhereFilter()).sorting(query.getSorting()).pagination(query.getPagination()).bypassingCache(query.isBypassingCache()).source(query.getSource()).scope(query.getScope()).build();
}
Also used : Query(com.yahoo.elide.datastores.aggregation.query.Query) Argument(com.yahoo.elide.core.request.Argument) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) ToString(lombok.ToString) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with Argument

use of com.yahoo.elide.core.request.Argument in project elide by yahoo.

the class DefaultQueryValidator method validateColumnArguments.

protected void validateColumnArguments(Query query, ColumnProjection projection) {
    SQLTable table = (SQLTable) query.getSource();
    Column column = table.getColumn(Column.class, projection.getName());
    column.getArgumentDefinitions().forEach(columnArgument -> {
        Argument clientArgument = projection.getArguments().get(columnArgument.getName());
        validateArgument(Optional.ofNullable(clientArgument), columnArgument, "column '" + projection.getAlias() + "'");
    });
}
Also used : Argument(com.yahoo.elide.core.request.Argument) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)

Example 3 with Argument

use of com.yahoo.elide.core.request.Argument in project elide by yahoo.

the class Queryable method getFilterProjections.

default List<ColumnProjection> getFilterProjections(FilterExpression expression) {
    List<ColumnProjection> results = new ArrayList<>();
    if (expression == null) {
        return results;
    }
    Collection<FilterPredicate> predicates = expression.accept(new PredicateExtractionVisitor());
    predicates.stream().forEach((predicate -> {
        Map<String, Argument> arguments = new HashMap<>();
        predicate.getPath().lastElement().get().getArguments().forEach(argument -> arguments.put(argument.getName(), argument));
        ColumnProjection projection = getSource().getColumnProjection(predicate.getField(), arguments);
        results.add(projection);
    }));
    return results;
}
Also used : PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) HashMap(java.util.HashMap) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Argument(com.yahoo.elide.core.request.Argument) ConnectionDetails(com.yahoo.elide.datastores.aggregation.queryengines.sql.ConnectionDetails) List(java.util.List) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) SQLJoin(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLJoin) Map(java.util.Map) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Argument

use of com.yahoo.elide.core.request.Argument in project elide by yahoo.

the class Queryable method extractFilterProjections.

/**
 * Converts a filter expression into a set of ColumnProjections.
 * @param query The parent query.
 * @param expression The filter expression to extract.
 * @return A set of zero or more column projections with their arguments.
 */
static Set<ColumnProjection> extractFilterProjections(Queryable query, FilterExpression expression) {
    if (expression == null) {
        return new LinkedHashSet<>();
    }
    Collection<FilterPredicate> predicates = expression.accept(new PredicateExtractionVisitor());
    Set<ColumnProjection> filterProjections = new LinkedHashSet<>();
    predicates.stream().forEach((predicate -> {
        Map<String, Argument> arguments = new HashMap<>();
        predicate.getPath().lastElement().get().getArguments().forEach(argument -> arguments.put(argument.getName(), argument));
        ColumnProjection projection = query.getSource().getColumnProjection(predicate.getField(), arguments);
        if (projection != null) {
            filterProjections.add(projection);
        }
    }));
    return filterProjections;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) HashMap(java.util.HashMap) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Argument(com.yahoo.elide.core.request.Argument) ConnectionDetails(com.yahoo.elide.datastores.aggregation.queryengines.sql.ConnectionDetails) List(java.util.List) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) SQLJoin(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLJoin) Map(java.util.Map) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with Argument

use of com.yahoo.elide.core.request.Argument in project elide by yahoo.

the class AggregateBeforeJoinOptimizerTest method testWhereOnTimeDimensionInProjectionNotRequiringJoinWithDefaultMatchingArguments.

@Test
public void testWhereOnTimeDimensionInProjectionNotRequiringJoinWithDefaultMatchingArguments() {
    SQLTable gameRevenueTable = (SQLTable) metaDataStore.getTable("gameRevenue", NO_VERSION);
    Set<Argument> arguments = new HashSet<>();
    arguments.add(Argument.builder().name("grain").value("DAY").build());
    FilterExpression having = new FilterPredicate(new Path(GameRevenue.class, dictionary, "revenue"), Operator.GT, Arrays.asList(9000));
    FilterExpression where = new FilterPredicate(new Path(GameRevenue.class, dictionary, "saleDate", "saleDate", arguments), Operator.IN, Arrays.asList(new Day(new Date())));
    Query query = Query.builder().source(gameRevenueTable).metricProjection(gameRevenueTable.getMetricProjection("revenue")).dimensionProjection(gameRevenueTable.getDimensionProjection("countryIsoCode")).timeDimensionProjection(gameRevenueTable.getTimeDimensionProjection("saleDate")).havingFilter(having).whereFilter(where).build();
    String expected = "SELECT MAX(`example_GameRevenue_XXX`.`INNER_AGG_XXX`) AS `revenue`," + "`example_GameRevenue_XXX_country_XXX`.`iso_code` AS `countryIsoCode`," + "`example_GameRevenue_XXX`.`saleDate` AS `saleDate` " + "FROM (SELECT MAX(`example_GameRevenue`.`revenue`) AS `INNER_AGG_XXX`," + "`example_GameRevenue`.`country_id` AS `country_id`," + "PARSEDATETIME(FORMATDATETIME(`example_GameRevenue`.`saleDate`, 'yyyy-MM-dd'), 'yyyy-MM-dd') AS `saleDate` " + "FROM `gameRevenue` AS `example_GameRevenue` " + "WHERE PARSEDATETIME(FORMATDATETIME(`example_GameRevenue`.`saleDate`, 'yyyy-MM-dd'), 'yyyy-MM-dd') IN (:XXX) " + "GROUP BY `example_GameRevenue`.`country_id`, " + "PARSEDATETIME(FORMATDATETIME(`example_GameRevenue`.`saleDate`, 'yyyy-MM-dd'), 'yyyy-MM-dd') ) " + "AS `example_GameRevenue_XXX` " + "LEFT OUTER JOIN `countries` AS `example_GameRevenue_XXX_country_XXX` " + "ON `example_GameRevenue_XXX`.`country_id` = `example_GameRevenue_XXX_country_XXX`.`id` " + "GROUP BY `example_GameRevenue_XXX_country_XXX`.`iso_code`, " + "`example_GameRevenue_XXX`.`saleDate` " + "HAVING MAX(`example_GameRevenue_XXX`.`INNER_AGG_XXX`) > :XXX\n";
    compareQueryLists(expected, engine.explain(query));
    testQueryExecution(query);
}
Also used : Path(com.yahoo.elide.core.Path) Argument(com.yahoo.elide.core.request.Argument) Query(com.yahoo.elide.datastores.aggregation.query.Query) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) GameRevenue(example.GameRevenue) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Day(com.yahoo.elide.datastores.aggregation.timegrains.Day) Date(java.util.Date) HashSet(java.util.HashSet) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

Argument (com.yahoo.elide.core.request.Argument)71 Test (org.junit.jupiter.api.Test)59 HashMap (java.util.HashMap)48 Query (com.yahoo.elide.datastores.aggregation.query.Query)38 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)35 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)25 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)22 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)15 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)14 HashSet (java.util.HashSet)14 Path (com.yahoo.elide.core.Path)12 ToString (lombok.ToString)12 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)11 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)10 Date (java.util.Date)10 SQLDimensionProjection (com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLDimensionProjection)9 GameRevenue (example.GameRevenue)7 PlayerStats (example.PlayerStats)6 SQLMetricProjection (com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLMetricProjection)5 Month (com.yahoo.elide.datastores.aggregation.timegrains.Month)5