Search in sources :

Example 31 with IsMoreThan

use of uk.gov.gchq.koryphe.impl.predicate.IsMoreThan in project Gaffer by gchq.

the class FilterHandlerTest method shouldReturnNoResultsWhenGlobalElementsFails.

@Test
public void shouldReturnNoResultsWhenGlobalElementsFails() throws OperationException {
    // Given
    given(store.getSchema()).willReturn(SCHEMA);
    final Edge edge = new Edge.Builder().group(TestGroups.EDGE).source("junctionA").dest("junctionB").directed(true).property(TestPropertyNames.COUNT, 2L).build();
    final Edge edge1 = new Edge.Builder().group(TestGroups.EDGE_2).source("junctionA").dest("junctionB").directed(true).property(TestPropertyNames.COUNT, 1L).build();
    final Edge edge2 = new Edge.Builder().group(TestGroups.EDGE).source("junctionB").dest("junctionA").directed(true).property(TestPropertyNames.COUNT, 4L).build();
    final Entity entity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.COUNT, 3L).build();
    final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY_2).property(TestPropertyNames.COUNT, 4L).build();
    final Entity entity2 = new Entity.Builder().group(TestGroups.ENTITY_3).property(TestPropertyNames.COUNT, 6L).build();
    input.add(edge);
    input.add(edge1);
    input.add(edge2);
    input.add(entity);
    input.add(entity1);
    input.add(entity2);
    final Filter filter = new Filter.Builder().input(input).globalElements(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsMoreThan(10L)).build()).globalEdges(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsMoreThan(2L)).build()).build();
    // When
    final Iterable<? extends Element> results = handler.doOperation(filter, context, store);
    // Then
    final List<Element> resultsList = Lists.newArrayList(results);
    assertEquals(expected, resultsList);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Filter(uk.gov.gchq.gaffer.operation.impl.function.Filter) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Element(uk.gov.gchq.gaffer.data.element.Element) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 32 with IsMoreThan

use of uk.gov.gchq.koryphe.impl.predicate.IsMoreThan in project Gaffer by gchq.

the class FilterHandlerTest method shouldApplyGlobalFilterAndReturnOnlySpecifiedEdges.

@Test
public void shouldApplyGlobalFilterAndReturnOnlySpecifiedEdges() throws OperationException {
    // Given
    given(store.getSchema()).willReturn(SCHEMA);
    final Edge edge = new Edge.Builder().group(TestGroups.EDGE).source("junctionA").dest("junctionB").directed(true).property(TestPropertyNames.COUNT, 2L).build();
    final Edge edge1 = new Edge.Builder().group(TestGroups.EDGE_2).source("junctionA").dest("junctionB").directed(true).property(TestPropertyNames.COUNT, 1L).build();
    final Edge edge2 = new Edge.Builder().group(TestGroups.EDGE_2).source("junctionB").dest("junctionA").directed(true).property(TestPropertyNames.COUNT, 4L).build();
    final Entity entity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.COUNT, 3L).build();
    final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY_2).property(TestPropertyNames.COUNT, 4L).build();
    input.add(edge);
    input.add(edge1);
    input.add(edge2);
    input.add(entity);
    input.add(entity1);
    expected.add(edge2);
    final Filter filter = new Filter.Builder().input(input).globalElements(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsMoreThan(2L)).build()).edge(TestGroups.EDGE_2).build();
    // When
    final Iterable<? extends Element> results = handler.doOperation(filter, context, store);
    // Then
    final List<Element> resultsList = Lists.newArrayList(results);
    assertEquals(expected, resultsList);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Filter(uk.gov.gchq.gaffer.operation.impl.function.Filter) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Element(uk.gov.gchq.gaffer.data.element.Element) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 33 with IsMoreThan

use of uk.gov.gchq.koryphe.impl.predicate.IsMoreThan in project Gaffer by gchq.

the class FilterHandlerTest method shouldFilterInputBasedOnGroupAndCount.

@Test
public void shouldFilterInputBasedOnGroupAndCount() throws OperationException {
    // Given
    given(store.getSchema()).willReturn(SCHEMA);
    final Edge edge = new Edge.Builder().group(TestGroups.EDGE).source("junctionA").dest("junctionB").directed(true).property(TestPropertyNames.COUNT, 2L).build();
    final Edge edge1 = new Edge.Builder().group(TestGroups.EDGE).source("junctionA").dest("junctionB").directed(true).property(TestPropertyNames.COUNT, 1L).build();
    final Edge edge2 = new Edge.Builder().group(TestGroups.EDGE).source("junctionB").dest("junctionA").directed(true).property(TestPropertyNames.COUNT, 4L).build();
    final Edge edge3 = new Edge.Builder().group(TestGroups.EDGE_2).source("junctionC").dest("junctionD").directed(true).property(TestPropertyNames.COUNT, 3L).build();
    input.add(edge);
    input.add(edge1);
    input.add(edge2);
    input.add(edge3);
    expected.add(edge);
    expected.add(edge2);
    final Filter filter = new Filter.Builder().input(input).edge(TestGroups.EDGE, new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsMoreThan(1L)).build()).build();
    // When
    final Iterable<? extends Element> result = handler.doOperation(filter, context, store);
    // Then
    final List<Element> resultList = Streams.toStream(result).collect(Collectors.toList());
    assertEquals(expected, resultList);
}
Also used : Filter(uk.gov.gchq.gaffer.operation.impl.function.Filter) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Element(uk.gov.gchq.gaffer.data.element.Element) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 34 with IsMoreThan

use of uk.gov.gchq.koryphe.impl.predicate.IsMoreThan 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 TupleAdaptedPredicate}s.
 * <p>
 * Note that Spark also applies all the filters provided to the {@code buildScan(String[], Filter[])} 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 TupleAdaptedPredicate}s implementing the provided {@link Filter}.
 */
private Map<String, List<TupleAdaptedPredicate<String, ?>>> getFunctionsFromFilter(final Filter filter) {
    final Map<String, List<TupleAdaptedPredicate<String, ?>>> map = new HashMap<>();
    if (filter instanceof EqualTo) {
    // Not dealt with as requires a Predicate<?> that returns null if either the controlValue or the
    // test value is null - the API of Predicate<?> doesn't permit this.
    } else if (filter instanceof EqualNullSafe) {
        final EqualNullSafe equalNullSafe = (EqualNullSafe) filter;
        final Predicate<?> isEqual = new IsEqual(equalNullSafe.value());
        final Set<String> relevantGroups = getGroupsFromFilter(filter);
        if (null != relevantGroups) {
            for (final String group : relevantGroups) {
                if (!map.containsKey(group)) {
                    map.put(group, new ArrayList<>());
                }
                map.get(group).add(new TupleAdaptedPredicate<>(isEqual, new String[] { equalNullSafe.attribute() }));
            }
        }
        LOGGER.debug("Converted {} to IsEqual ({})", filter, equalNullSafe.attribute());
    } else if (filter instanceof GreaterThan) {
        final GreaterThan greaterThan = (GreaterThan) filter;
        final Predicate<?> isMoreThan = new IsMoreThan((Comparable<?>) greaterThan.value(), false);
        final Set<String> relevantGroups = getGroupsFromFilter(filter);
        if (null != relevantGroups) {
            for (final String group : relevantGroups) {
                if (!map.containsKey(group)) {
                    map.put(group, new ArrayList<>());
                }
                map.get(group).add(new TupleAdaptedPredicate<>(isMoreThan, new String[] { greaterThan.attribute() }));
            }
        }
        LOGGER.debug("Converted {} to isMoreThan ({})", filter, greaterThan.attribute());
    } else if (filter instanceof GreaterThanOrEqual) {
        final GreaterThanOrEqual greaterThan = (GreaterThanOrEqual) filter;
        final Predicate<?> isMoreThan = new IsMoreThan((Comparable<?>) greaterThan.value(), true);
        final Set<String> relevantGroups = getGroupsFromFilter(filter);
        if (null != relevantGroups) {
            for (final String group : relevantGroups) {
                if (!map.containsKey(group)) {
                    map.put(group, new ArrayList<>());
                }
                map.get(group).add(new TupleAdaptedPredicate<>(isMoreThan, new String[] { greaterThan.attribute() }));
            }
        }
        LOGGER.debug("Converted {} to IsMoreThan ({})", filter, greaterThan.attribute());
    } else if (filter instanceof LessThan) {
        final LessThan lessThan = (LessThan) filter;
        final Predicate<?> isLessThan = new IsLessThan((Comparable<?>) lessThan.value(), false);
        final Set<String> relevantGroups = getGroupsFromFilter(filter);
        if (null != relevantGroups) {
            for (final String group : relevantGroups) {
                if (!map.containsKey(group)) {
                    map.put(group, new ArrayList<>());
                }
                map.get(group).add(new TupleAdaptedPredicate<>(isLessThan, new String[] { lessThan.attribute() }));
            }
        }
        LOGGER.debug("Converted {} to IsLessThan ({})", filter, lessThan.attribute());
    } else if (filter instanceof LessThanOrEqual) {
        final LessThanOrEqual lessThan = (LessThanOrEqual) filter;
        final Predicate<?> isLessThan = new IsLessThan((Comparable<?>) lessThan.value(), true);
        final Set<String> relevantGroups = getGroupsFromFilter(filter);
        if (null != relevantGroups) {
            for (final String group : relevantGroups) {
                if (!map.containsKey(group)) {
                    map.put(group, new ArrayList<>());
                }
                map.get(group).add(new TupleAdaptedPredicate<>(isLessThan, new String[] { lessThan.attribute() }));
            }
        }
        LOGGER.debug("Converted {} to LessThanOrEqual ({})", filter, lessThan.attribute());
    } else if (filter instanceof In) {
        final In in = (In) filter;
        final Predicate<?> isIn = new IsIn(new HashSet<>(Arrays.asList(in.values())));
        final Set<String> relevantGroups = getGroupsFromFilter(filter);
        if (null != relevantGroups) {
            for (final String group : relevantGroups) {
                if (!map.containsKey(group)) {
                    map.put(group, new ArrayList<>());
                }
                map.get(group).add(new TupleAdaptedPredicate<>(isIn, new String[] { in.attribute() }));
            }
        }
        LOGGER.debug("Converted {} to IsIn ({})", filter, in.attribute());
    } else if (filter instanceof IsNull) {
        final IsNull isNull = (IsNull) filter;
        final Predicate<?> doesntExist = new Not<>(new Exists());
        final Set<String> relevantGroups = getGroupsFromFilter(filter);
        if (null != relevantGroups) {
            for (final String group : relevantGroups) {
                if (!map.containsKey(group)) {
                    map.put(group, new ArrayList<>());
                }
                map.get(group).add(new TupleAdaptedPredicate<>(doesntExist, new String[] { isNull.attribute() }));
            }
        }
        LOGGER.debug("Converted {} to Not(Exists) ({})", filter, isNull.attribute());
    } else if (filter instanceof IsNotNull) {
        final IsNotNull isNotNull = (IsNotNull) filter;
        final Predicate<?> exists = new Exists();
        final Set<String> relevantGroups = getGroupsFromFilter(filter);
        if (null != relevantGroups) {
            for (final String group : relevantGroups) {
                if (!map.containsKey(group)) {
                    map.put(group, new ArrayList<>());
                }
                map.get(group).add(new TupleAdaptedPredicate<>(exists, new String[] { isNotNull.attribute() }));
            }
        }
        LOGGER.debug("Converted {} to Exists ({})", filter, isNotNull.attribute());
    } else if (filter instanceof And) {
        final And and = (And) filter;
        final Map<String, List<TupleAdaptedPredicate<String, ?>>> left = getFunctionsFromFilter(and.left());
        final Map<String, List<TupleAdaptedPredicate<String, ?>>> right = getFunctionsFromFilter(and.right());
        final Set<String> relevantGroups = getGroupsFromFilter(filter);
        if (null != relevantGroups) {
            for (final String group : relevantGroups) {
                final List<TupleAdaptedPredicate<String, ?>> concatFilters = new ArrayList<>();
                if (null != left.get(group)) {
                    concatFilters.addAll(left.get(group));
                }
                if (null != right.get(group)) {
                    concatFilters.addAll(right.get(group));
                }
                if (!map.containsKey(group)) {
                    map.put(group, new ArrayList<>());
                }
                map.get(group).addAll(concatFilters);
            }
        }
        LOGGER.debug("Converted {} to list of filters ({})", filter, StringUtils.join(map.entrySet(), ','));
    }
    return map;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) EqualNullSafe(org.apache.spark.sql.sources.EqualNullSafe) LessThanOrEqual(org.apache.spark.sql.sources.LessThanOrEqual) HashMap(java.util.HashMap) In(org.apache.spark.sql.sources.In) IsIn(uk.gov.gchq.koryphe.impl.predicate.IsIn) ArrayList(java.util.ArrayList) GreaterThanOrEqual(org.apache.spark.sql.sources.GreaterThanOrEqual) TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) Predicate(java.util.function.Predicate) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) LessThan(org.apache.spark.sql.sources.LessThan) IsNotNull(org.apache.spark.sql.sources.IsNotNull) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) GreaterThan(org.apache.spark.sql.sources.GreaterThan) ArrayList(java.util.ArrayList) List(java.util.List) TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) IsIn(uk.gov.gchq.koryphe.impl.predicate.IsIn) EqualTo(org.apache.spark.sql.sources.EqualTo) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) Exists(uk.gov.gchq.koryphe.impl.predicate.Exists) And(org.apache.spark.sql.sources.And) IsNull(org.apache.spark.sql.sources.IsNull) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) HashMap(java.util.HashMap) Map(java.util.Map)

Example 35 with IsMoreThan

use of uk.gov.gchq.koryphe.impl.predicate.IsMoreThan in project Gaffer by gchq.

the class AccumuloStoreTest method testAbleToInsertAndRetrieveEntityQueryingEqualAndRelated.

private void testAbleToInsertAndRetrieveEntityQueryingEqualAndRelated(final AccumuloStore store) throws OperationException {
    final Entity e = new Entity(TestGroups.ENTITY, "1");
    e.putProperty(TestPropertyNames.PROP_1, 1);
    e.putProperty(TestPropertyNames.PROP_2, 2);
    e.putProperty(TestPropertyNames.PROP_3, 3);
    e.putProperty(TestPropertyNames.PROP_4, 4);
    e.putProperty(TestPropertyNames.COUNT, 1);
    final User user = new User();
    final AddElements add = new AddElements.Builder().input(e).build();
    store.execute(add, new Context(user));
    final EntityId entityId1 = new EntitySeed("1");
    final GetElements getBySeed = new GetElements.Builder().view(new View.Builder().entity(TestGroups.ENTITY).build()).input(entityId1).build();
    CloseableIterable<? extends Element> results = null;
    try {
        results = store.execute(getBySeed, new Context(user));
        assertEquals(1, Iterables.size(results));
        assertTrue(Iterables.contains(results, e));
    } finally {
        if (results != null) {
            results.close();
        }
    }
    final GetElements getRelated = new GetElements.Builder().view(new View.Builder().entity(TestGroups.ENTITY).build()).input(entityId1).build();
    CloseableIterable<? extends Element> relatedResults = null;
    try {
        relatedResults = store.execute(getRelated, store.createContext(user));
        assertEquals(1, Iterables.size(relatedResults));
        assertTrue(Iterables.contains(relatedResults, e));
        final GetElements getRelatedWithPostAggregationFilter = new GetElements.Builder().view(new View.Builder().entity(TestGroups.ENTITY, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.PROP_1).execute(new IsMoreThan(0)).build()).postAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsMoreThan(6)).build()).build()).build()).input(entityId1).build();
        relatedResults = store.execute(getRelatedWithPostAggregationFilter, store.createContext(user));
        assertEquals(0, Iterables.size(relatedResults));
    } finally {
        if (relatedResults != null) {
            relatedResults.close();
        }
    }
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Context(uk.gov.gchq.gaffer.store.Context) Entity(uk.gov.gchq.gaffer.data.element.Entity) User(uk.gov.gchq.gaffer.user.User) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)

Aggregations

IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)72 Test (org.junit.jupiter.api.Test)42 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)40 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)34 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)24 Element (uk.gov.gchq.gaffer.data.element.Element)22 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)22 Edge (uk.gov.gchq.gaffer.data.element.Edge)20 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)20 User (uk.gov.gchq.gaffer.user.User)20 Graph (uk.gov.gchq.gaffer.graph.Graph)19 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)19 ArrayList (java.util.ArrayList)18 HashSet (java.util.HashSet)16 Entity (uk.gov.gchq.gaffer.data.element.Entity)16 IsEqual (uk.gov.gchq.koryphe.impl.predicate.IsEqual)13 IsLessThan (uk.gov.gchq.koryphe.impl.predicate.IsLessThan)13 Set (java.util.Set)12 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)12 OperationException (uk.gov.gchq.gaffer.operation.OperationException)12