Search in sources :

Example 21 with AndFilterExpression

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

the class AggregateBeforeJoinOptimizerTest method testWhereOnMultiReferenceTimeDimensionInProjection.

@Test
public void testWhereOnMultiReferenceTimeDimensionInProjection() {
    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, "lastDate"), Operator.IN, Arrays.asList(new Day(new Date()))));
    Query query = Query.builder().source(gameRevenueTable).metricProjection(gameRevenueTable.getMetricProjection("revenue")).timeDimensionProjection(gameRevenueTable.getTimeDimensionProjection("lastDate")).whereFilter(where).build();
    String expected = "SELECT MAX(`example_GameRevenue_XXX`.`INNER_AGG_XXX`) AS `revenue`," + "PARSEDATETIME(FORMATDATETIME(CASE WHEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` > `example_GameRevenue_XXX`.`saleDate` THEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` ELSE `example_GameRevenue_XXX`.`saleDate` END, 'yyyy-MM-dd'), 'yyyy-MM-dd') AS `lastDate` " + "FROM (SELECT MAX(`example_GameRevenue`.`revenue`) AS `INNER_AGG_XXX`," + "`example_GameRevenue`.`player_stats_id` AS `player_stats_id`," + "`example_GameRevenue`.`saleDate` AS `saleDate`," + "`example_GameRevenue`.`country_id` AS `country_id` " + "FROM `gameRevenue` AS `example_GameRevenue` " + "GROUP BY `example_GameRevenue`.`player_stats_id`, " + "`example_GameRevenue`.`saleDate`, " + "`example_GameRevenue`.`country_id` ) " + "AS `example_GameRevenue_XXX` " + "LEFT OUTER JOIN `playerStats` AS `example_GameRevenue_XXX_playerStats_XXX` " + "ON `example_GameRevenue_XXX`.`player_stats_id` = `example_GameRevenue_XXX_playerStats_XXX`.`id` " + "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 PARSEDATETIME(FORMATDATETIME(CASE WHEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` > `example_GameRevenue_XXX`.`saleDate` THEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` ELSE `example_GameRevenue_XXX`.`saleDate` END, 'yyyy-MM-dd'), 'yyyy-MM-dd') IN (:XXX)) " + "GROUP BY PARSEDATETIME(FORMATDATETIME(CASE WHEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` > `example_GameRevenue_XXX`.`saleDate` THEN `example_GameRevenue_XXX_playerStats_XXX`.`recordedDate` ELSE `example_GameRevenue_XXX`.`saleDate` END, 'yyyy-MM-dd'), 'yyyy-MM-dd')";
    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) Day(com.yahoo.elide.datastores.aggregation.timegrains.Day) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Date(java.util.Date) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 22 with AndFilterExpression

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

the class SplitFilterExpressionVisitor method visitAndExpression.

@Override
public FilterConstraints visitAndExpression(final AndFilterExpression expression) {
    /*
         * Definition:
         *     C = condition
         *     pure-W = WHERE C
         *     pure-H = HAVING C
         *     mix-HW = WHERE C HAVING C'
         *
         * Given that L and R operands of an AndFilterExpression can only be one of "pure-H", "pure-W", or "mix-HW",
         * then:
         *
         *     pure-W1 AND pure-W2 = WHERE C1 AND WHERE C2 = WHERE (C1 AND C2)    = pure-W
         *     pure-H1 AND pure-H2 = HAVING C1 AND HAVING C2 = HAVING (C1 AND C2) = pure-H
         *
         *     pure-H1 AND pureW2 = HAVING C1 AND WHERE C2 = WHERE C2 HAVING C1   = mix-HW
         *     pure-W1 AND pureH2                          = WHERE C1 HAVING C2   = mix-HW
         *
         *     mix-HW1 AND pure-W2 = WHERE C1 HAVING C1' AND WHERE C2 = WHERE (C1 & C2) HAVING C1'  = mix-HW
         *     mix-HW1 AND pure-H2 = WHERE C1 HAVING C1' AND HAVING C2 = WHERE C1 HAVING (C1' & C2) = mix-HW
         *
         *     mix-HW1 AND mim-HW2 = WHERE C1 HAVING C1' AND WHERE C2 HAVING C2' = WHERE (C1 & C2) HAVING (C1' & C2')
         *                         = mix-HW
         */
    FilterConstraints left = expression.getLeft().accept(this);
    FilterConstraints right = expression.getRight().accept(this);
    if (left.isPureWhere() && right.isPureWhere()) {
        // pure-W1 AND pure-W2 = WHERE (C1 & C2) = pure-W
        return FilterConstraints.pureWhere(new AndFilterExpression(left.getWhereExpression(), right.getWhereExpression()));
    }
    if (left.isPureHaving() && right.isPureHaving()) {
        // pure-H1 AND pure-H2 = HAVING (C1 AND C2) = pure-H
        return FilterConstraints.pureHaving(new AndFilterExpression(left.getHavingExpression(), right.getHavingExpression()));
    }
    // all of the rests are mix-HW
    return FilterConstraints.withWhereAndHaving(AndFilterExpression.fromPair(left.getWhereExpression(), right.getWhereExpression()), AndFilterExpression.fromPair(left.getHavingExpression(), right.getHavingExpression()));
}
Also used : AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Example 23 with AndFilterExpression

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

the class PermissionToFilterExpressionVisitorTest method testTestMethods.

@Test
public void testTestMethods() {
    List<FilterExpression> unfilterable = Arrays.asList(FALSE_USER_CHECK_EXPRESSION, TRUE_USER_CHECK_EXPRESSION, NO_EVALUATION_EXPRESSION);
    for (FilterExpression e : unfilterable) {
        assertTrue(isSpecialCase(e), String.format("isSpecialCase(%s)", e));
    }
    assertTrue(containsOnlyFilterableExpressions(new AndFilterExpression(IN_PREDICATE, NOT_IN_PREDICATE)));
    assertTrue(containsOnlyFilterableExpressions(new OrFilterExpression(IN_PREDICATE, NOT_IN_PREDICATE)));
    assertFalse(containsOnlyFilterableExpressions(new AndFilterExpression(IN_PREDICATE, FALSE_USER_CHECK_EXPRESSION)));
    assertFalse(containsOnlyFilterableExpressions(new OrFilterExpression(IN_PREDICATE, FALSE_USER_CHECK_EXPRESSION)));
    assertFalse(containsOnlyFilterableExpressions(new AndFilterExpression(IN_PREDICATE, new AndFilterExpression(IN_PREDICATE, TRUE_USER_CHECK_EXPRESSION))));
    assertFalse(containsOnlyFilterableExpressions(new OrFilterExpression(IN_PREDICATE, new OrFilterExpression(IN_PREDICATE, TRUE_USER_CHECK_EXPRESSION))));
    FilterExpression negated, expected;
    negated = negate(new OrFilterExpression(NOT_IN_PREDICATE, NOT_IN_PREDICATE));
    expected = new AndFilterExpression(IN_PREDICATE, IN_PREDICATE);
    assertEquals(expected, negated);
}
Also used : OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 24 with AndFilterExpression

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

the class PermissionToFilterExpressionVisitorTest method containsOnlyFilterableExpressions.

private static boolean containsOnlyFilterableExpressions(FilterExpression expr) {
    if (isSpecialCase(expr)) {
        return false;
    }
    FilterExpression left, right;
    if (expr instanceof OrFilterExpression) {
        OrFilterExpression or = (OrFilterExpression) expr;
        left = or.getLeft();
        right = or.getRight();
        return containsOnlyFilterableExpressions(left) && containsOnlyFilterableExpressions(right);
    }
    if (expr instanceof AndFilterExpression) {
        AndFilterExpression and = (AndFilterExpression) expr;
        left = and.getLeft();
        right = and.getRight();
        return containsOnlyFilterableExpressions(left) && containsOnlyFilterableExpressions(right);
    }
    return true;
}
Also used : OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Example 25 with AndFilterExpression

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

the class PermissionToFilterExpressionVisitor method visitAndExpression.

@Override
public FilterExpression visitAndExpression(AndExpression andExpression) {
    FilterExpression left = andExpression.getLeft().accept(this);
    FilterExpression right = andExpression.getRight().accept(this);
    // (FALSE_USER_CHECK_EXPRESSION AND NO_EVALUATION_EXPRESSION) => FALSE_USER_CHECK_EXPRESSION
    if (expressionWillFail(left) || expressionWillFail(right)) {
        return FALSE_USER_CHECK_EXPRESSION;
    }
    if (expressionWillNotFilter(left)) {
        return right;
    }
    if (expressionWillNotFilter(right)) {
        return left;
    }
    return new AndFilterExpression(left, right);
}
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) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

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