Search in sources :

Example 1 with AndFilterExpression

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

the class FilterExpressionNormalizationVisitor method visitNotExpression.

@Override
public FilterExpression visitNotExpression(NotFilterExpression fe) {
    FilterExpression inner = fe.getNegated();
    if (inner instanceof AndFilterExpression) {
        AndFilterExpression and = (AndFilterExpression) inner;
        FilterExpression left = new NotFilterExpression(and.getLeft()).accept(this);
        FilterExpression right = new NotFilterExpression(and.getRight()).accept(this);
        return new OrFilterExpression(left, right);
    }
    if (inner instanceof OrFilterExpression) {
        OrFilterExpression or = (OrFilterExpression) inner;
        FilterExpression left = new NotFilterExpression(or.getLeft()).accept(this);
        FilterExpression right = new NotFilterExpression(or.getRight()).accept(this);
        return new AndFilterExpression(left, right);
    }
    if (inner instanceof NotFilterExpression) {
        NotFilterExpression not = (NotFilterExpression) inner;
        return (not.getNegated()).accept(this);
    }
    if (inner instanceof FilterPredicate) {
        FilterPredicate filter = (FilterPredicate) inner;
        return filter.negate();
    }
    return inner;
}
Also used : OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) 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) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Example 2 with AndFilterExpression

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

the class FilterExpressionCheckEvaluationVisitor method visitAndExpression.

@Override
public Boolean visitAndExpression(AndFilterExpression expression) {
    FilterExpression left = expression.getLeft();
    FilterExpression right = expression.getRight();
    return left.accept(this) && right.accept(this);
}
Also used : 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 3 with AndFilterExpression

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

the class MatchesTemplateVisitor method matches.

private boolean matches(FilterExpression a, FilterExpression b) {
    if (!a.getClass().equals(b.getClass())) {
        return false;
    }
    if (a instanceof AndFilterExpression) {
        AndFilterExpression andA = (AndFilterExpression) a;
        AndFilterExpression andB = (AndFilterExpression) b;
        return matches(andA.getLeft(), andB.getLeft()) && matches(andA.getRight(), andB.getRight());
    }
    if (a instanceof OrFilterExpression) {
        OrFilterExpression orA = (OrFilterExpression) a;
        OrFilterExpression orB = (OrFilterExpression) b;
        return matches(orA.getLeft(), orB.getLeft()) && matches(orA.getRight(), orB.getRight());
    }
    if (a instanceof NotFilterExpression) {
        NotFilterExpression notA = (NotFilterExpression) a;
        NotFilterExpression notB = (NotFilterExpression) b;
        return matches(notA.getNegated(), notB.getNegated());
    }
    FilterPredicate predicateA = (FilterPredicate) a;
    FilterPredicate predicateB = (FilterPredicate) b;
    boolean valueMatches = predicateA.getValues().equals(predicateB.getValues());
    boolean operatorMatches = predicateA.getOperator().equals(predicateB.getOperator());
    boolean pathMatches = pathMatches(predicateA.getPath(), predicateB.getPath());
    boolean usingTemplate = false;
    if (predicateA.getValues().size() == 1) {
        String value = predicateA.getValues().get(0).toString();
        Matcher matcher = TEMPLATE_PATTERN.matcher(value);
        usingTemplate = matcher.matches();
        if (usingTemplate && pathMatches & operatorMatches) {
            String argumentName = matcher.group(1);
            arguments.put(argumentName, Argument.builder().name(argumentName).value(predicateB.getValues().size() == 1 ? predicateB.getValues().get(0) : predicateB.getValues()).build());
        }
    }
    return (operatorMatches && pathMatches && (valueMatches || usingTemplate));
}
Also used : OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) Matcher(java.util.regex.Matcher) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Example 4 with AndFilterExpression

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

the class VerifyFieldAccessFilterExpressionVisitorTest method testReject.

@Test
public void testReject() {
    Path p1Path = new Path(Arrays.asList(new PathElement(Book.class, Author.class, AUTHORS), new PathElement(Author.class, String.class, NAME)));
    FilterPredicate p1 = new InPredicate(p1Path, "foo", "bar");
    Path p2Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, HOME)));
    FilterPredicate p2 = new InPredicate(p2Path, "blah");
    Path p3Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, GENRE)));
    FilterPredicate p3 = new InPredicate(p3Path, SCIFI);
    // P4 is a duplicate of P3
    Path p4Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, GENRE)));
    FilterPredicate p4 = new InPredicate(p4Path, SCIFI);
    OrFilterExpression or = new OrFilterExpression(p2, p3);
    AndFilterExpression and1 = new AndFilterExpression(or, p1);
    AndFilterExpression and2 = new AndFilterExpression(and1, p4);
    NotFilterExpression not = new NotFilterExpression(and2);
    Book book = new Book();
    Author author = new Author();
    book.setAuthors(Collections.singleton(author));
    author.setBooks(Collections.singleton(book));
    PersistentResource<Book> resource = new PersistentResource<>(book, "", scope);
    PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
    when(permissionExecutor.checkSpecificFieldPermissions(resource, null, ReadPermission.class, HOME)).thenThrow(ForbiddenAccessException.class);
    VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
    // restricted HOME field
    assertFalse(not.accept(visitor));
    assertFalse(and1.accept(visitor));
    assertFalse(and2.accept(visitor));
    assertFalse(or.accept(visitor));
    assertFalse(p2.accept(visitor));
    // unrestricted fields
    assertTrue(p1.accept(visitor));
    assertTrue(p3.accept(visitor));
    assertTrue(p4.accept(visitor));
    verify(permissionExecutor, times(8)).evaluateFilterJoinUserChecks(any(), any());
    verify(permissionExecutor, times(5)).checkSpecificFieldPermissions(resource, null, ReadPermission.class, HOME);
    verify(permissionExecutor, times(9)).checkUserPermissions(any(), any(), isA(String.class));
    verify(permissionExecutor, times(5)).handleFilterJoinReject(any(), any(), any());
}
Also used : Path(com.yahoo.elide.core.Path) PersistentResource(com.yahoo.elide.core.PersistentResource) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) PathElement(com.yahoo.elide.core.Path.PathElement) Book(example.Book) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Test(org.junit.jupiter.api.Test)

Example 5 with AndFilterExpression

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

the class AggregateBeforeJoinOptimizerTest method testWhereOnMultiReferenceDimensionNotInProjection.

@Test
public void testWhereOnMultiReferenceDimensionNotInProjection() {
    SQLTable gameRevenueTable = (SQLTable) metaDataStore.getTable("gameRevenue", NO_VERSION);
    FilterExpression where = new AndFilterExpression(new FilterPredicate(new Path(GameRevenue.class, dictionary, "countryIsoCode"), Operator.IN, Arrays.asList("foo")), new FilterPredicate(new Path(GameRevenue.class, dictionary, "countryCategory"), Operator.IN, Arrays.asList("US")));
    Query query = Query.builder().source(gameRevenueTable).metricProjection(gameRevenueTable.getMetricProjection("revenue")).whereFilter(where).build();
    String expected = "SELECT MAX(`example_GameRevenue_XXX`.`INNER_AGG_XXX`) AS `revenue` " + "FROM (SELECT MAX(`example_GameRevenue`.`revenue`) AS `INNER_AGG_XXX`," + "`example_GameRevenue`.`country_id` AS `country_id`," + "`example_GameRevenue`.`category` AS `category` " + "FROM `gameRevenue` AS `example_GameRevenue` " + "GROUP BY `example_GameRevenue`.`country_id`, " + "`example_GameRevenue`.`category` ) " + "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` " + "WHERE (`example_GameRevenue_XXX_country_XXX`.`iso_code` IN (:XXX) " + "AND CASE WHEN `example_GameRevenue_XXX_country_XXX`.`iso_code` = 'US' THEN `example_GameRevenue_XXX`.`category` ELSE 'UNKNONWN' END IN (:XXX))\n";
    compareQueryLists(expected, engine.explain(query));
    testQueryExecution(query);
}
Also used : Path(com.yahoo.elide.core.Path) Query(com.yahoo.elide.datastores.aggregation.query.Query) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) 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) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)34 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)22 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)21 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)20 Test (org.junit.jupiter.api.Test)17 Path (com.yahoo.elide.core.Path)15 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)10 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)10 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)7 Query (com.yahoo.elide.datastores.aggregation.query.Query)7 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)7 Book (example.Book)6 Author (example.Author)5 PathElement (com.yahoo.elide.core.Path.PathElement)4 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)4 Date (java.util.Date)4 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)3 RelationshipType (com.yahoo.elide.core.dictionary.RelationshipType)3 Type (com.yahoo.elide.core.type.Type)3 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)3