Search in sources :

Example 11 with FilterExpression

use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.

the class PermissionToFilterExpressionVisitorTest method filterExpressionForPermissions.

private FilterExpression filterExpressionForPermissions(String permission) {
    Function<Check, Expression> checkFn = (check) -> new CheckExpression(check, null, requestScope, null, cache);
    ParseTree expression = EntityPermissions.parseExpression(permission);
    PermissionToFilterExpressionVisitor fev = new PermissionToFilterExpressionVisitor(dictionary, requestScope, null);
    return expression.accept(new PermissionExpressionVisitor(dictionary, checkFn)).accept(NORMALIZATION_VISITOR).accept(fev);
}
Also used : CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression) BeforeEach(org.junit.jupiter.api.BeforeEach) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Arrays(java.util.Arrays) Path(com.yahoo.elide.core.Path) Role(com.yahoo.elide.core.security.checks.prefab.Role) User(com.yahoo.elide.core.security.User) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) Map(java.util.Map) PermissionExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionExpressionVisitor) NO_EVALUATION_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.NO_EVALUATION_EXPRESSION) RequestScope(com.yahoo.elide.core.RequestScope) OperationCheck(com.yahoo.elide.core.security.checks.OperationCheck) MethodSource(org.junit.jupiter.params.provider.MethodSource) PermissionToFilterExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor) UUID(java.util.UUID) Arguments(org.junit.jupiter.params.provider.Arguments) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) ExpressionResultCache(com.yahoo.elide.core.security.permissions.ExpressionResultCache) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Optional(java.util.Optional) TRUE_USER_CHECK_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.TRUE_USER_CHECK_EXPRESSION) PermissionExpressionNormalizationVisitor(com.yahoo.elide.core.security.visitors.PermissionExpressionNormalizationVisitor) TestDictionary(com.yahoo.elide.core.dictionary.TestDictionary) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) HashMap(java.util.HashMap) Author(example.Author) Function(java.util.function.Function) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) FilterExpressionCheck(com.yahoo.elide.core.security.checks.FilterExpressionCheck) ElideSettings(com.yahoo.elide.ElideSettings) TestUser(com.yahoo.elide.core.security.TestUser) ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) Check(com.yahoo.elide.core.security.checks.Check) Book(example.Book) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EntityPermissions(com.yahoo.elide.core.dictionary.EntityPermissions) Type(com.yahoo.elide.core.type.Type) Operator(com.yahoo.elide.core.filter.Operator) Collections(java.util.Collections) FALSE_USER_CHECK_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.FALSE_USER_CHECK_EXPRESSION) CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) OperationCheck(com.yahoo.elide.core.security.checks.OperationCheck) FilterExpressionCheck(com.yahoo.elide.core.security.checks.FilterExpressionCheck) Check(com.yahoo.elide.core.security.checks.Check) PermissionToFilterExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor) PermissionExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionExpressionVisitor) ParseTree(org.antlr.v4.runtime.tree.ParseTree) CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression)

Example 12 with FilterExpression

use of com.yahoo.elide.core.filter.expression.FilterExpression 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 13 with FilterExpression

use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.

the class EntityProjectionMakerTest method testRootCollectionWithGlobalFilter.

@Test
public void testRootCollectionWithGlobalFilter() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.add("filter", "genre=='Science Fiction'");
    String path = "/book";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Science Fiction");
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    EntityProjection expected = EntityProjection.builder().type(Book.class).attribute(Attribute.builder().name("language").type(String.class).build()).attribute(Attribute.builder().name("genre").type(String.class).build()).attribute(Attribute.builder().name("title").type(String.class).build()).attribute(Attribute.builder().name("awards").type(Collection.class).build()).attribute(Attribute.builder().name("publishDate").type(long.class).build()).attribute(Attribute.builder().name("authorTypes").type(Collection.class).build()).attribute(Attribute.builder().name("price").type(Price.class).build()).filterExpression(expression).relationship("publisher", EntityProjection.builder().type(Publisher.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).relationship("authors", EntityProjection.builder().type(Author.class).build()).pagination(PaginationImpl.getDefaultPagination(ClassType.of(Book.class))).build();
    EntityProjection actual = maker.parsePath(path);
    projectionEquals(expected, actual);
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Book(example.Book) Collection(java.util.Collection) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Editor(example.Editor) Test(org.junit.jupiter.api.Test)

Example 14 with FilterExpression

use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.

the class MatchesTemplateVisitor method isValid.

/**
 * Determines if a client filter matches or contains a subexpression that matches a template filter.
 * @param templateFilter A templated filter expression
 * @param clientFilter The client provided filter expression.
 * @param arguments If the client filter matches, extract any table arguments.
 * @return True if the client filter matches.  False otherwise.
 */
public static boolean isValid(FilterExpression templateFilter, FilterExpression clientFilter, Map<String, Argument> arguments) {
    Preconditions.checkNotNull(templateFilter);
    if (clientFilter == null) {
        return false;
    }
    // First we normalize the filters so any NOT clauses are pushed down immediately in front of a predicate.
    // This lets us treat logical AND and OR without regard for any preceding NOT clauses.
    FilterExpression normalizedTemplateFilter = templateFilter.accept(new FilterExpressionNormalizationVisitor());
    FilterExpression normalizedClientFilter = clientFilter.accept(new FilterExpressionNormalizationVisitor());
    MatchesTemplateVisitor templateVisitor = new MatchesTemplateVisitor(normalizedTemplateFilter);
    boolean matches = normalizedClientFilter.accept(templateVisitor);
    if (matches) {
        arguments.putAll(templateVisitor.arguments);
    }
    return matches;
}
Also used : FilterExpressionNormalizationVisitor(com.yahoo.elide.core.filter.visitors.FilterExpressionNormalizationVisitor) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression)

Example 15 with FilterExpression

use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.

the class DefaultQueryValidator method validateHavingClause.

@Override
public void validateHavingClause(Query query) {
    FilterExpression havingClause = query.getHavingFilter();
    if (havingClause == null) {
        return;
    }
    havingClause.accept(new PredicateExtractionVisitor()).forEach(predicate -> {
        Path path = predicate.getPath();
        if (path.getPathElements().size() > 1) {
            throw new InvalidOperationException("Relationship traversal not supported for analytic queries.");
        }
        validatePredicate(query, predicate);
        extractFilterProjections(query, havingClause).stream().forEach(projection -> {
            Predicate<ColumnProjection> filterByNameAndArgs = (column) -> (column.getAlias().equals(projection.getAlias()) || column.getName().equals(projection.getName())) && column.getArguments().equals(projection.getArguments());
            // Query by (alias or name) and arguments.   The filter may or may not be using the alias.
            if (query.getColumnProjection(filterByNameAndArgs) == null) {
                Predicate<ColumnProjection> filterByName = (column) -> (column.getAlias().equals(projection.getAlias()) || column.getName().equals(projection.getName()));
                // The column wasn't projected at all.
                if (query.getColumnProjection(filterByName) == null) {
                    throw new InvalidOperationException(String.format("Post aggregation filtering on '%s' requires the field to be projected in the response", projection.getAlias()));
                // The column was projected but arguments didn't match.
                } else {
                    throw new InvalidOperationException(String.format("Post aggregation filtering on '%s' requires the field to be projected " + "in the response with matching arguments", projection.getAlias()));
                }
            }
        });
    });
}
Also used : Path(com.yahoo.elide.core.Path) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Path(com.yahoo.elide.core.Path) Queryable.extractFilterProjections(com.yahoo.elide.datastores.aggregation.query.Queryable.extractFilterProjections) Argument(com.yahoo.elide.core.request.Argument) HashSet(java.util.HashSet) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) Map(java.util.Map) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) Sorting(com.yahoo.elide.core.request.Sorting) Predicate(java.util.function.Predicate) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) ArgumentDefinition(com.yahoo.elide.datastores.aggregation.metadata.models.ArgumentDefinition) Set(java.util.Set) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) Stream(java.util.stream.Stream) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) Type(com.yahoo.elide.core.type.Type) Operator(com.yahoo.elide.core.filter.Operator) Optional(java.util.Optional) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression)

Aggregations

FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)214 Test (org.junit.jupiter.api.Test)161 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)91 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)72 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)49 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)46 Path (com.yahoo.elide.core.Path)44 Query (com.yahoo.elide.datastores.aggregation.query.Query)42 Argument (com.yahoo.elide.core.request.Argument)39 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)39 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)34 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)34 HashMap (java.util.HashMap)29 Book (example.Book)28 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)24 EntityProjection (com.yahoo.elide.core.request.EntityProjection)22 Date (java.util.Date)21 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)20 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)19 HashSet (java.util.HashSet)18