Search in sources :

Example 1 with Not

use of uk.gov.gchq.gaffer.function.filter.Not 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;
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) 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) IsIn(uk.gov.gchq.gaffer.function.filter.IsIn) In(org.apache.spark.sql.sources.In) ArrayList(java.util.ArrayList) GreaterThanOrEqual(org.apache.spark.sql.sources.GreaterThanOrEqual) LessThan(org.apache.spark.sql.sources.LessThan) IsLessThan(uk.gov.gchq.gaffer.function.filter.IsLessThan) IsNotNull(org.apache.spark.sql.sources.IsNotNull) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) IsLessThan(uk.gov.gchq.gaffer.function.filter.IsLessThan) GreaterThan(org.apache.spark.sql.sources.GreaterThan) ArrayList(java.util.ArrayList) List(java.util.List) IsIn(uk.gov.gchq.gaffer.function.filter.IsIn) EqualTo(org.apache.spark.sql.sources.EqualTo) IsEqual(uk.gov.gchq.gaffer.function.filter.IsEqual) Not(uk.gov.gchq.gaffer.function.filter.Not) Exists(uk.gov.gchq.gaffer.function.filter.Exists) And(org.apache.spark.sql.sources.And) IsNull(org.apache.spark.sql.sources.IsNull) IsMoreThan(uk.gov.gchq.gaffer.function.filter.IsMoreThan) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Not

use of uk.gov.gchq.gaffer.function.filter.Not 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);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) StarRatingTransform(uk.gov.gchq.gaffer.example.films.function.transform.StarRatingTransform) SampleData(uk.gov.gchq.gaffer.example.films.data.SampleData) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) IsEqual(uk.gov.gchq.gaffer.function.filter.IsEqual) Not(uk.gov.gchq.gaffer.function.filter.Not) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) DataGenerator(uk.gov.gchq.gaffer.example.films.generator.DataGenerator) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 3 with Not

use of uk.gov.gchq.gaffer.function.filter.Not in project Gaffer by gchq.

the class NotExample method doesNotExist.

public void doesNotExist() {
    // ---------------------------------------------------------
    final Not function = new Not(new Exists());
    // ---------------------------------------------------------
    runExample(function, new Object[] { 1 }, new Object[] { null }, new Object[] { "" }, new Object[] { "abc" });
}
Also used : Not(uk.gov.gchq.gaffer.function.filter.Not) Exists(uk.gov.gchq.gaffer.function.filter.Exists)

Example 4 with Not

use of uk.gov.gchq.gaffer.function.filter.Not in project Gaffer by gchq.

the class NotExample method areNotEqual.

public void areNotEqual() {
    // ---------------------------------------------------------
    final Not function = new Not(new AreEqual());
    // ---------------------------------------------------------
    runExample(function, new Object[] { 1, 1.0 }, new Object[] { 1, 2 }, new Object[] { 2.5, 2.5 }, new Object[] { "", null }, new Object[] { "abc", "abc" });
}
Also used : Not(uk.gov.gchq.gaffer.function.filter.Not) AreEqual(uk.gov.gchq.gaffer.function.filter.AreEqual)

Aggregations

Not (uk.gov.gchq.gaffer.function.filter.Not)4 Exists (uk.gov.gchq.gaffer.function.filter.Exists)2 IsEqual (uk.gov.gchq.gaffer.function.filter.IsEqual)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 And (org.apache.spark.sql.sources.And)1 EqualNullSafe (org.apache.spark.sql.sources.EqualNullSafe)1 EqualTo (org.apache.spark.sql.sources.EqualTo)1 GreaterThan (org.apache.spark.sql.sources.GreaterThan)1 GreaterThanOrEqual (org.apache.spark.sql.sources.GreaterThanOrEqual)1 In (org.apache.spark.sql.sources.In)1 IsNotNull (org.apache.spark.sql.sources.IsNotNull)1 IsNull (org.apache.spark.sql.sources.IsNull)1 LessThan (org.apache.spark.sql.sources.LessThan)1 LessThanOrEqual (org.apache.spark.sql.sources.LessThanOrEqual)1 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)1