use of uk.gov.gchq.gaffer.function.filter.IsEqual in project Gaffer by gchq.
the class FiltersToOperationConverter method getFunctionsFromFilter.
/**
* Converts a Spark {@link Filter} to a map from group to a list of Gaffer {@link ConsumerFunctionContext}s.
* <p>
* Note that Spark also applies all the filters provided to the <code>buildScan(String[], Filter[])</code> method
* so not implementing some of the provided {@link Filter}s in Gaffer will not cause errors. However, as many as
* possible should be implemented so that as much filtering as possible happens in iterators running in Accumulo's
* tablet servers (this avoids unnecessary data transfer from Accumulo to Spark).
*
* @param filter The {@link Filter} to transform.
* @return A map from {@link String} to {@link ConsumerFunctionContext}s implementing the provided {@link Filter}.
*/
private Map<String, List<ConsumerFunctionContext<String, FilterFunction>>> getFunctionsFromFilter(final Filter filter) {
final Map<String, List<ConsumerFunctionContext<String, FilterFunction>>> map = new HashMap<>();
if (filter instanceof EqualTo) {
// Not dealt with as requires a FilterFunction that returns null if either the controlValue or the
// test value is null - the API of FilterFunction doesn't permit this.
} else if (filter instanceof EqualNullSafe) {
final EqualNullSafe equalNullSafe = (EqualNullSafe) filter;
final FilterFunction isEqual = new IsEqual(equalNullSafe.value());
final List<String> properties = Collections.singletonList(equalNullSafe.attribute());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (relevantGroups != null) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
}
map.get(group).add(new ConsumerFunctionContext<>(isEqual, properties));
}
}
LOGGER.debug("Converted {} to IsEqual ({})", filter, properties.get(0));
} else if (filter instanceof GreaterThan) {
final GreaterThan greaterThan = (GreaterThan) filter;
final FilterFunction isMoreThan = new IsMoreThan((Comparable<?>) greaterThan.value(), false);
final List<String> properties = Collections.singletonList(greaterThan.attribute());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (relevantGroups != null) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
}
map.get(group).add(new ConsumerFunctionContext<>(isMoreThan, properties));
}
}
LOGGER.debug("Converted {} to isMoreThan ({})", filter, properties.get(0));
} else if (filter instanceof GreaterThanOrEqual) {
final GreaterThanOrEqual greaterThan = (GreaterThanOrEqual) filter;
final FilterFunction isMoreThan = new IsMoreThan((Comparable<?>) greaterThan.value(), true);
final List<String> properties = Collections.singletonList(greaterThan.attribute());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (relevantGroups != null) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
}
map.get(group).add(new ConsumerFunctionContext<>(isMoreThan, properties));
}
}
LOGGER.debug("Converted {} to IsMoreThan ({})", filter, properties.get(0));
} else if (filter instanceof LessThan) {
final LessThan lessThan = (LessThan) filter;
final FilterFunction isLessThan = new IsLessThan((Comparable<?>) lessThan.value(), false);
final List<String> properties = Collections.singletonList(lessThan.attribute());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (relevantGroups != null) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
}
map.get(group).add(new ConsumerFunctionContext<>(isLessThan, properties));
}
}
LOGGER.debug("Converted {} to IsLessThan ({})", filter, properties.get(0));
} else if (filter instanceof LessThanOrEqual) {
final LessThanOrEqual lessThan = (LessThanOrEqual) filter;
final FilterFunction isLessThan = new IsLessThan((Comparable<?>) lessThan.value(), true);
final List<String> properties = Collections.singletonList(lessThan.attribute());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (relevantGroups != null) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
}
map.get(group).add(new ConsumerFunctionContext<>(isLessThan, properties));
}
}
LOGGER.debug("Converted {} to LessThanOrEqual ({})", filter, properties.get(0));
} else if (filter instanceof In) {
final In in = (In) filter;
final FilterFunction isIn = new IsIn(new HashSet<>(Arrays.asList(in.values())));
final List<String> properties = Collections.singletonList(in.attribute());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (relevantGroups != null) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
}
map.get(group).add(new ConsumerFunctionContext<>(isIn, properties));
}
}
LOGGER.debug("Converted {} to IsIn ({})", filter, properties.get(0));
} else if (filter instanceof IsNull) {
final IsNull isNull = (IsNull) filter;
final FilterFunction doesntExist = new Not(new Exists());
final List<String> properties = Collections.singletonList(isNull.attribute());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (relevantGroups != null) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
}
map.get(group).add(new ConsumerFunctionContext<>(doesntExist, properties));
}
}
LOGGER.debug("Converted {} to Not(Exists) ({})", filter, properties.get(0));
} else if (filter instanceof IsNotNull) {
final IsNotNull isNotNull = (IsNotNull) filter;
final FilterFunction exists = new Exists();
final List<String> properties = Collections.singletonList(isNotNull.attribute());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (relevantGroups != null) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
}
map.get(group).add(new ConsumerFunctionContext<>(exists, properties));
}
}
LOGGER.debug("Converted {} to Exists ({})", filter, properties.get(0));
} else if (filter instanceof And) {
final And and = (And) filter;
final Map<String, List<ConsumerFunctionContext<String, FilterFunction>>> left = getFunctionsFromFilter(and.left());
final Map<String, List<ConsumerFunctionContext<String, FilterFunction>>> right = getFunctionsFromFilter(and.right());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (relevantGroups != null) {
for (final String group : relevantGroups) {
final List<ConsumerFunctionContext<String, FilterFunction>> concatFilters = new ArrayList<>();
if (left.get(group) != null) {
concatFilters.addAll(left.get(group));
}
if (right.get(group) != null) {
concatFilters.addAll(right.get(group));
}
if (!map.containsKey(group)) {
map.put(group, new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
}
map.get(group).addAll(concatFilters);
}
}
LOGGER.debug("Converted {} to list of filters ({})", filter, StringUtils.join(map.entrySet(), ','));
}
return map;
}
use of uk.gov.gchq.gaffer.function.filter.IsEqual in project Gaffer by gchq.
the class FilteringIT method testFilteringProperties.
@Test
@TraitRequirement(StoreTrait.PRE_AGGREGATION_FILTERING)
public void testFilteringProperties() throws OperationException {
// Given
final List<ElementSeed> seeds = Arrays.asList(new EntitySeed("A3"), new EdgeSeed("A5", "B5", false));
final GetElements<ElementSeed, Element> getElementsWithoutFiltering = new GetElements.Builder<>().seeds(seeds).build();
final GetElements<ElementSeed, Element> getElementsWithFiltering = new GetElements.Builder<>().seeds(seeds).view(new View.Builder().entity(TestGroups.ENTITY, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(IdentifierType.VERTEX.name()).execute(new IsEqual("A5")).build()).build()).edge(TestGroups.EDGE, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.INT).execute(new IsLessThan(2)).build()).build()).build()).build();
// When - without filtering
final List<Element> resultsWithoutFiltering = Lists.newArrayList(graph.execute(getElementsWithoutFiltering, getUser()));
// When - with filtering
final List<Element> resultsWithFiltering = Lists.newArrayList(graph.execute(getElementsWithFiltering, getUser()));
// Then - without filtering
assertNotNull(resultsWithoutFiltering);
assertEquals(8, resultsWithoutFiltering.size());
assertThat(resultsWithoutFiltering, IsCollectionContaining.hasItems(getEdge("A3", "A3", false), getEdge("A3", "B3", false), getEdge("A3", "C3", false), getEdge("A3", "D3", false), getEdge("A5", "B5", false), getEntity("A5"), getEntity("B5")));
// Then - with filtering
assertNotNull(resultsWithFiltering);
assertEquals(6, resultsWithFiltering.size());
assertThat(resultsWithFiltering, IsCollectionContaining.hasItems(getEdge("A3", "A3", false), getEdge("A3", "B3", false), getEdge("A5", "B5", false), getEdge("A3", "D3", false), getEdge("A3", "C3", false), getEntity("A5")));
}
use of uk.gov.gchq.gaffer.function.filter.IsEqual in project Gaffer by gchq.
the class LoadAndQuery method run.
/**
* Finds average reviews (from other users) of all films viewed by user02.
* <ul>
* <li>Starts from a seed of user02.</li>
* <li>Finds all filmIds connected to user02 (adjacent entity seeds)</li>
* <li>Then finds all reviews that have those filmIds.</li>
* <li>Then filters out all reviews from user02.</li>
* <li>Then aggregates the reviews together.</li>
* <li>Then transforms the rating from a percent to a 5 star rating and stores the value in a transient property called starRating</li>
* <li>Then returns the reviews (Entities)</li>
* </ul>
* This query can be written in JSON and executed over a rest service - see
* resources/example/films/json/load.json and resources/example/films/json/query.json
*
* @return the review entities
* @throws OperationException if operation chain fails to be executed on the graph
*/
public CloseableIterable<Entity> run() throws OperationException {
// Setup graph
final Graph graph = new Graph.Builder().storeProperties(StreamUtil.openStream(getClass(), "/example/films/mockaccumulostore.properties", true)).addSchemas(StreamUtil.openStreams(getClass(), "/example/films/schema", true)).build();
// Populate the graph with some example data
// Create an operation chain. The output from the first operation is passed in as the input the second operation.
// So the chain operation will generate elements from the domain objects then add these elements to the graph.
final OperationChain<Void> populateChain = new OperationChain.Builder().first(new GenerateElements.Builder<>().objects(new SampleData().generate()).generator(new DataGenerator()).build()).then(new AddElements.Builder().build()).build();
// Execute the populate operation chain on the graph
graph.execute(populateChain, USER);
// Run a query on the graph to fetch average star ratings for all films user02 has watched.
// Create an operation chain.
// So the chain operation will get the adjacent review entity seeds then get the review entities.
final OperationChain<CloseableIterable<Entity>> queryChain = new OperationChain.Builder().first(new GetAdjacentEntitySeeds.Builder().view(new View.Builder().edge(Group.VIEWING).build()).addSeed(new EntitySeed("user02")).build()).then(new GetEntities.Builder().view(new View.Builder().entity(Group.REVIEW, new ViewElementDefinition.Builder().transientProperty(TransientProperty.FIVE_STAR_RATING, Float.class).preAggregationFilter(new ElementFilter.Builder().select(Property.USER_ID).execute(new Not(new IsEqual("user02"))).build()).groupBy().transformer(new ElementTransformer.Builder().select(Property.RATING, Property.COUNT).project(TransientProperty.FIVE_STAR_RATING).execute(new StarRatingTransform()).build()).build()).build()).build()).build();
// Execute the query operation chain on the graph.
return graph.execute(queryChain, USER);
}
use of uk.gov.gchq.gaffer.function.filter.IsEqual in project Gaffer by gchq.
the class IsEqualExample method isEqualTo5.
public void isEqualTo5() {
// ---------------------------------------------------------
final IsEqual function = new IsEqual(5);
// ---------------------------------------------------------
runExample(function, 5, 5L, "5", '5');
}
use of uk.gov.gchq.gaffer.function.filter.IsEqual in project Gaffer by gchq.
the class IsEqualExample method isEqualToString5.
public void isEqualToString5() {
// ---------------------------------------------------------
final IsEqual function = new IsEqual("5");
// ---------------------------------------------------------
runExample(function, 5, 5L, "5", '5');
}
Aggregations