use of com.yahoo.elide.core.filter.expression.AndFilterExpression in project elide by yahoo.
the class AggregateBeforeJoinOptimizerTest method testWhereOnDimensionNotInProjectionNotRequiringJoin.
@Test
public void testWhereOnDimensionNotInProjectionNotRequiringJoin() {
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, "category"), Operator.IN, Arrays.asList("foo")));
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` " + "FROM `gameRevenue` AS `example_GameRevenue` " + "WHERE `example_GameRevenue`.`category` IN (:XXX) " + "GROUP BY `example_GameRevenue`.`country_id` ) " + "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)\n";
compareQueryLists(expected, engine.explain(query));
testQueryExecution(query);
}
use of com.yahoo.elide.core.filter.expression.AndFilterExpression in project elide by yahoo.
the class AggregateBeforeJoinOptimizerTest method testWhereOnTimeDimensionNotInProjectionNotRequiringJoin.
@Test
public void testWhereOnTimeDimensionNotInProjectionNotRequiringJoin() {
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, "saleDate"), Operator.IN, Arrays.asList(new Day(new Date()))));
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` " + "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` ) " + "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)\n";
compareQueryLists(expected, engine.explain(query));
testQueryExecution(query);
}
use of com.yahoo.elide.core.filter.expression.AndFilterExpression in project elide by yahoo.
the class FilterTranslatorTest method testHQLQueryVisitor.
@Test
public void testHQLQueryVisitor() throws Exception {
List<Path.PathElement> p0Path = Arrays.asList(new Path.PathElement(Book.class, Author.class, "authors"));
FilterPredicate p0 = new NotEmptyPredicate(new Path(p0Path));
List<Path.PathElement> p1Path = Arrays.asList(new Path.PathElement(Book.class, Author.class, "authors"), new Path.PathElement(Author.class, String.class, "name"));
FilterPredicate p1 = new InPredicate(new Path(p1Path), "foo", "bar");
List<Path.PathElement> p2Path = Arrays.asList(new Path.PathElement(Book.class, String.class, "name"));
FilterPredicate p2 = new InPredicate(new Path(p2Path), "blah");
List<Path.PathElement> p3Path = Arrays.asList(new Path.PathElement(Book.class, String.class, "genre"));
FilterPredicate p3 = new InPredicate(new Path(p3Path), "scifi");
OrFilterExpression or = new OrFilterExpression(p2, p3);
AndFilterExpression and1 = new AndFilterExpression(p0, p1);
AndFilterExpression and2 = new AndFilterExpression(or, and1);
NotFilterExpression not = new NotFilterExpression(and2);
FilterTranslator filterOp = new FilterTranslator(dictionary);
String query = filterOp.apply(not, false);
query = query.trim().replaceAll(" +", " ");
String p1Params = p1.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
String p2Params = p2.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
String p3Params = p3.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
String expected = "NOT (((name IN (" + p2Params + ") OR genre IN (" + p3Params + ")) " + "AND (authors IS NOT EMPTY AND authors.name IN (" + p1Params + "))))";
assertEquals(expected, query);
}
use of com.yahoo.elide.core.filter.expression.AndFilterExpression in project elide by yahoo.
the class PersistentResource method loadRecords.
/**
* Load a collection from the datastore.
*
* @param projection the projection to load
* @param requestScope the request scope
* @param ids a list of object identifiers to optionally load. Can be empty.
* @return a filtered collection of resources loaded from the datastore.
*/
public static Observable<PersistentResource> loadRecords(EntityProjection projection, List<String> ids, RequestScope requestScope) {
Type<?> loadClass = projection.getType();
Pagination pagination = projection.getPagination();
Sorting sorting = projection.getSorting();
FilterExpression filterExpression = projection.getFilterExpression();
EntityDictionary dictionary = requestScope.getDictionary();
DataStoreTransaction tx = requestScope.getTransaction();
if (shouldSkipCollection(loadClass, ReadPermission.class, requestScope, projection.getRequestedFields())) {
if (ids.isEmpty()) {
return Observable.empty();
}
throw new InvalidObjectIdentifierException(ids.toString(), dictionary.getJsonAliasFor(loadClass));
}
Set<String> requestedFields = projection.getRequestedFields();
if (pagination != null && !pagination.isDefaultInstance() && !CanPaginateVisitor.canPaginate(loadClass, dictionary, requestScope, requestedFields)) {
throw new BadRequestException(String.format("Cannot paginate %s", dictionary.getJsonAliasFor(loadClass)));
}
Set<PersistentResource> newResources = new LinkedHashSet<>();
if (!ids.isEmpty()) {
String typeAlias = dictionary.getJsonAliasFor(loadClass);
newResources = requestScope.getNewPersistentResources().stream().filter(resource -> typeAlias.equals(resource.getTypeName()) && ids.contains(resource.getUUID().orElse(""))).collect(Collectors.toSet());
FilterExpression idExpression = buildIdFilterExpression(ids, loadClass, dictionary, requestScope);
// Combine filters if necessary
filterExpression = Optional.ofNullable(filterExpression).map(fe -> (FilterExpression) new AndFilterExpression(idExpression, fe)).orElse(idExpression);
}
Optional<FilterExpression> permissionFilter = getPermissionFilterExpression(loadClass, requestScope, requestedFields);
if (permissionFilter.isPresent()) {
if (filterExpression != null) {
filterExpression = new AndFilterExpression(filterExpression, permissionFilter.get());
} else {
filterExpression = permissionFilter.get();
}
}
EntityProjection modifiedProjection = projection.copyOf().filterExpression(filterExpression).sorting(sorting).pagination(pagination).build();
Observable<PersistentResource> existingResources = filter(ReadPermission.class, Optional.ofNullable(modifiedProjection.getFilterExpression()), projection.getRequestedFields(), Observable.fromIterable(new PersistentResourceSet(tx.loadObjects(modifiedProjection, requestScope), requestScope)));
// TODO: Sort again in memory now that two sets are glommed together?
Observable<PersistentResource> allResources = Observable.fromIterable(newResources).mergeWith(existingResources);
Set<String> foundIds = new HashSet<>();
allResources = allResources.doOnNext((resource) -> {
String id = (String) resource.getUUID().orElseGet(resource::getId);
if (ids.contains(id)) {
foundIds.add(id);
}
});
allResources = allResources.doOnComplete(() -> {
Set<String> missedIds = Sets.difference(new HashSet<>(ids), foundIds);
if (!missedIds.isEmpty()) {
throw new InvalidObjectIdentifierException(missedIds.toString(), dictionary.getJsonAliasFor(loadClass));
}
});
return allResources;
}
use of com.yahoo.elide.core.filter.expression.AndFilterExpression in project elide by yahoo.
the class SplitFilterExpressionVisitorTest method testVisitAndExpression.
@Test
public void testVisitAndExpression() {
// pure-W AND pure-W
AndFilterExpression filterExpression = new AndFilterExpression(WHERE_PREDICATE, WHERE_PREDICATE);
assertEquals("(playerStats.overallRating IN [foo] AND playerStats.overallRating IN [foo])", splitFilterExpressionVisitor.visitAndExpression(filterExpression).getWhereExpression().toString());
assertNull(splitFilterExpressionVisitor.visitAndExpression(filterExpression).getHavingExpression());
// pure-H AND pure-W
filterExpression = new AndFilterExpression(HAVING_PREDICATE, WHERE_PREDICATE);
assertEquals("playerStats.overallRating IN [foo]", splitFilterExpressionVisitor.visitAndExpression(filterExpression).getWhereExpression().toString());
assertEquals("playerStats.highScore GT [99]", splitFilterExpressionVisitor.visitAndExpression(filterExpression).getHavingExpression().toString());
// pure-W AND pure-H
filterExpression = new AndFilterExpression(WHERE_PREDICATE, HAVING_PREDICATE);
assertEquals("playerStats.overallRating IN [foo]", splitFilterExpressionVisitor.visitAndExpression(filterExpression).getWhereExpression().toString());
assertEquals("playerStats.highScore GT [99]", splitFilterExpressionVisitor.visitAndExpression(filterExpression).getHavingExpression().toString());
// non-pure case - H1 AND W1 AND H2
AndFilterExpression and1 = new AndFilterExpression(HAVING_PREDICATE, WHERE_PREDICATE);
AndFilterExpression and2 = new AndFilterExpression(and1, HAVING_PREDICATE);
assertEquals("playerStats.overallRating IN [foo]", splitFilterExpressionVisitor.visitAndExpression(and2).getWhereExpression().toString());
assertEquals("(playerStats.highScore GT [99] AND playerStats.highScore GT [99])", splitFilterExpressionVisitor.visitAndExpression(and2).getHavingExpression().toString());
// non-pure case - (H1 OR H2) AND W1
OrFilterExpression or = new OrFilterExpression(HAVING_PREDICATE, HAVING_PREDICATE);
AndFilterExpression and = new AndFilterExpression(or, WHERE_PREDICATE);
assertEquals("playerStats.overallRating IN [foo]", splitFilterExpressionVisitor.visitAndExpression(and).getWhereExpression().toString());
assertEquals("(playerStats.highScore GT [99] OR playerStats.highScore GT [99])", splitFilterExpressionVisitor.visitAndExpression(and).getHavingExpression().toString());
}
Aggregations