Search in sources :

Example 6 with GraphFilters

use of uk.gov.gchq.gaffer.operation.graph.GraphFilters in project Gaffer by gchq.

the class FilterToOperationConverterTest method testSpecifySourceOrDestinationAndPropertyFilter.

@Test
public void testSpecifySourceOrDestinationAndPropertyFilter() {
    final Schema schema = getSchema();
    final SparkSession sparkSession = SparkSessionProvider.getSparkSession();
    // Specify src and a filter on property1
    Filter[] filters = new Filter[2];
    filters[0] = new GreaterThan("property1", 5);
    filters[1] = new EqualTo(SchemaToStructTypeConverter.SRC_COL_NAME, "0");
    FiltersToOperationConverter converter = new FiltersToOperationConverter(getViewFromSchema(schema), schema, filters);
    Operation operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfElements);
    assertEquals(0, ((GraphFilters) operation).getView().getEntityGroups().size());
    assertEquals(2, ((GraphFilters) operation).getView().getEdgeGroups().size());
    final Set<EntityId> seeds = new HashSet<>();
    for (final Object seed : ((GetRDDOfElements) operation).getInput()) {
        seeds.add((EntitySeed) seed);
    }
    assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
    View opView = ((GraphFilters) operation).getView();
    for (final String edgeGroup : EDGE_GROUPS) {
        final List<TupleAdaptedPredicate<String, ?>> edgePostAggFilters = opView.getEdge(edgeGroup).getPostAggregationFilterFunctions();
        assertThat(edgePostAggFilters).hasSize(1);
        assertArrayEquals(new String[] { "property1" }, edgePostAggFilters.get(0).getSelection());
        assertEquals(new IsMoreThan(5, false), edgePostAggFilters.get(0).getPredicate());
    }
    // Specify src and filters on property1 and property4
    filters = new Filter[3];
    filters[0] = new GreaterThan("property1", 5);
    filters[1] = new EqualTo(SchemaToStructTypeConverter.SRC_COL_NAME, "0");
    filters[2] = new LessThan("property4", 8);
    converter = new FiltersToOperationConverter(getViewFromSchema(schema), schema, filters);
    operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfElements);
    assertEquals(0, ((GraphFilters) operation).getView().getEntityGroups().size());
    assertEquals(1, ((GraphFilters) operation).getView().getEdgeGroups().size());
    seeds.clear();
    for (final Object seed : ((GetRDDOfElements) operation).getInput()) {
        seeds.add((EntitySeed) seed);
    }
    assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
    opView = ((GraphFilters) operation).getView();
    final List<TupleAdaptedPredicate<String, ?>> entityPostAggFilters = opView.getEdge(EDGE_GROUP).getPostAggregationFilterFunctions();
    assertThat(entityPostAggFilters).hasSize(2);
    final List<String> expectedProperties = new ArrayList<>();
    expectedProperties.add("property1");
    expectedProperties.add("property4");
    assertThat(entityPostAggFilters.get(0).getSelection()).hasSize(1);
    assertEquals(expectedProperties.get(0), entityPostAggFilters.get(0).getSelection()[0]);
    assertEquals(new IsMoreThan(5, false), entityPostAggFilters.get(0).getPredicate());
    assertThat(entityPostAggFilters.get(1).getSelection()).hasSize(1);
    assertEquals(expectedProperties.get(1), entityPostAggFilters.get(1).getSelection()[0]);
    assertEquals(new IsLessThan(8, false), entityPostAggFilters.get(1).getPredicate());
}
Also used : TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) SparkSession(org.apache.spark.sql.SparkSession) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ArrayList(java.util.ArrayList) GraphFilters(uk.gov.gchq.gaffer.operation.graph.GraphFilters) Operation(uk.gov.gchq.gaffer.operation.Operation) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) EqualTo(org.apache.spark.sql.sources.EqualTo) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) LessThan(org.apache.spark.sql.sources.LessThan) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) Filter(org.apache.spark.sql.sources.Filter) GreaterThan(org.apache.spark.sql.sources.GreaterThan) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) GetRDDOfElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfElements) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 7 with GraphFilters

use of uk.gov.gchq.gaffer.operation.graph.GraphFilters in project Gaffer by gchq.

the class FilterToOperationConverterTest method testSpecifyMultiplePropertyFilters.

@Test
public void testSpecifyMultiplePropertyFilters() {
    final Schema schema = getSchema();
    final SparkSession sparkSession = SparkSessionProvider.getSparkSession();
    final Filter[] filters = new Filter[2];
    filters[0] = new GreaterThan("property1", 5);
    filters[1] = new LessThan("property4", 8L);
    FiltersToOperationConverter converter = new FiltersToOperationConverter(getViewFromSchema(schema), schema, filters);
    Operation operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfAllElements);
    // Only groups ENTITY_GROUP and EDGE_GROUP should be in the view as only they have property1 and property4
    View opView = ((GraphFilters) operation).getView();
    List<TupleAdaptedPredicate<String, ?>> entityPostAggFilters = opView.getEntity(ENTITY_GROUP).getPostAggregationFilterFunctions();
    assertThat(entityPostAggFilters).hasSize(2);
    final ArrayList<String> expectedProperties = new ArrayList<>();
    expectedProperties.add("property1");
    expectedProperties.add("property4");
    assertThat(entityPostAggFilters.get(0).getSelection()).hasSize(1);
    assertEquals(expectedProperties.get(0), entityPostAggFilters.get(0).getSelection()[0]);
    assertThat(entityPostAggFilters.get(1).getSelection()).hasSize(1);
    assertEquals(expectedProperties.get(1), entityPostAggFilters.get(1).getSelection()[0]);
    final ArrayList<Predicate> expectedFunctions = new ArrayList<>();
    expectedFunctions.add(new IsMoreThan(5, false));
    expectedFunctions.add(new IsLessThan(8L, false));
    assertEquals(expectedFunctions.get(0), entityPostAggFilters.get(0).getPredicate());
    assertEquals(expectedFunctions.get(1), entityPostAggFilters.get(1).getPredicate());
    final List<TupleAdaptedPredicate<String, ?>> edgePostAggFilters = opView.getEdge(EDGE_GROUP).getPostAggregationFilterFunctions();
    assertThat(edgePostAggFilters).hasSize(2);
    assertThat(edgePostAggFilters.get(0).getSelection()).hasSize(1);
    assertEquals(expectedProperties.get(0), edgePostAggFilters.get(0).getSelection()[0]);
    assertThat(edgePostAggFilters.get(1).getSelection()).hasSize(1);
    assertEquals(expectedProperties.get(1), edgePostAggFilters.get(1).getSelection()[0]);
}
Also used : TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) SparkSession(org.apache.spark.sql.SparkSession) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ArrayList(java.util.ArrayList) GraphFilters(uk.gov.gchq.gaffer.operation.graph.GraphFilters) Operation(uk.gov.gchq.gaffer.operation.Operation) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) 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) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) Filter(org.apache.spark.sql.sources.Filter) GreaterThan(org.apache.spark.sql.sources.GreaterThan) GetRDDOfAllElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfAllElements) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Test(org.junit.jupiter.api.Test)

Example 8 with GraphFilters

use of uk.gov.gchq.gaffer.operation.graph.GraphFilters in project Gaffer by gchq.

the class AccumuloStore method updateConfiguration.

/**
 * Updates a Hadoop {@link Configuration} with information needed to connect to the Accumulo store. It adds
 * iterators to apply the provided {@link View}. This method will be used by operations that run MapReduce
 * or Spark jobs against the Accumulo store.
 *
 * @param conf         A {@link Configuration} to be updated.
 * @param graphFilters The operation {@link GraphFilters} to be applied.
 * @param user         The {@link User} to be used.
 * @throws StoreException If there is a failure to connect to Accumulo or a problem setting the iterators.
 */
public void updateConfiguration(final Configuration conf, final GraphFilters graphFilters, final User user) throws StoreException {
    try {
        final View view = graphFilters.getView();
        // Table name
        LOGGER.info("Updating configuration with table name of {}", getTableName());
        InputConfigurator.setInputTableName(AccumuloInputFormat.class, conf, getTableName());
        // User
        addUserToConfiguration(conf);
        // Authorizations
        Authorizations authorisations;
        if (null != user && null != user.getDataAuths()) {
            authorisations = new Authorizations(user.getDataAuths().toArray(new String[user.getDataAuths().size()]));
        } else {
            authorisations = new Authorizations();
        }
        InputConfigurator.setScanAuthorizations(AccumuloInputFormat.class, conf, authorisations);
        LOGGER.info("Updating configuration with authorizations of {}", authorisations);
        // Zookeeper
        addZookeeperToConfiguration(conf);
        // Add keypackage, schema and view to conf
        conf.set(ElementInputFormat.KEY_PACKAGE, getProperties().getKeyPackageClass());
        LOGGER.info("Updating configuration with key package of {}", getProperties().getKeyPackageClass());
        conf.set(ElementInputFormat.SCHEMA, new String(getSchema().toCompactJson(), CommonConstants.UTF_8));
        LOGGER.debug("Updating configuration with Schema of {}", getSchema());
        conf.set(ElementInputFormat.VIEW, new String(view.toCompactJson(), CommonConstants.UTF_8));
        LOGGER.debug("Updating configuration with View of {}", view);
        if (view.hasGroups()) {
            // Add the columns to fetch
            final Collection<org.apache.accumulo.core.util.Pair<Text, Text>> columnFamilyColumnQualifierPairs = Stream.concat(view.getEntityGroups().stream(), view.getEdgeGroups().stream()).map(g -> new org.apache.accumulo.core.util.Pair<>(new Text(g), (Text) null)).collect(Collectors.toSet());
            InputConfigurator.fetchColumns(AccumuloInputFormat.class, conf, columnFamilyColumnQualifierPairs);
            LOGGER.info("Updated configuration with column family/qualifiers of {}", StringUtils.join(columnFamilyColumnQualifierPairs, ','));
            // Add iterators that depend on the view
            final IteratorSetting elementPreFilter = getKeyPackage().getIteratorFactory().getElementPreAggregationFilterIteratorSetting(view, this);
            if (null != elementPreFilter) {
                InputConfigurator.addIterator(AccumuloInputFormat.class, conf, elementPreFilter);
                LOGGER.info("Added pre-aggregation filter iterator of {}", elementPreFilter);
            }
            final IteratorSetting elementPostFilter = getKeyPackage().getIteratorFactory().getElementPostAggregationFilterIteratorSetting(view, this);
            if (null != elementPostFilter) {
                InputConfigurator.addIterator(AccumuloInputFormat.class, conf, elementPostFilter);
                LOGGER.info("Added post-aggregation filter iterator of {}", elementPostFilter);
            }
            final IteratorSetting edgeEntityDirFilter = getKeyPackage().getIteratorFactory().getEdgeEntityDirectionFilterIteratorSetting(graphFilters);
            if (null != edgeEntityDirFilter) {
                InputConfigurator.addIterator(AccumuloInputFormat.class, conf, edgeEntityDirFilter);
                LOGGER.info("Added edge direction filter iterator of {}", edgeEntityDirFilter);
            }
        }
    } catch (final AccumuloSecurityException | IteratorSettingException | UnsupportedEncodingException e) {
        throw new StoreException(e);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) VISIBILITY(uk.gov.gchq.gaffer.store.StoreTrait.VISIBILITY) TableUtils(uk.gov.gchq.gaffer.accumulostore.utils.TableUtils) Text(org.apache.hadoop.io.Text) GenerateSplitPointsFromSampleHandler(uk.gov.gchq.gaffer.accumulostore.operation.handler.GenerateSplitPointsFromSampleHandler) Element(uk.gov.gchq.gaffer.data.element.Element) SchemaOptimiser(uk.gov.gchq.gaffer.store.schema.SchemaOptimiser) AddElementsFromHdfsHandler(uk.gov.gchq.gaffer.accumulostore.operation.hdfs.handler.AddElementsFromHdfsHandler) STORE_VALIDATION(uk.gov.gchq.gaffer.store.StoreTrait.STORE_VALIDATION) Configuration(org.apache.hadoop.conf.Configuration) GetElementsBetweenSetsHandler(uk.gov.gchq.gaffer.accumulostore.operation.handler.GetElementsBetweenSetsHandler) InputConfigurator(org.apache.accumulo.core.client.mapreduce.lib.impl.InputConfigurator) SampleElementsForSplitPoints(uk.gov.gchq.gaffer.operation.impl.SampleElementsForSplitPoints) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) AccumuloInputFormat(org.apache.accumulo.core.client.mapreduce.AccumuloInputFormat) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) Set(java.util.Set) PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) GetElementsBetweenSets(uk.gov.gchq.gaffer.accumulostore.operation.impl.GetElementsBetweenSets) Stream(java.util.stream.Stream) AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) StoreTrait(uk.gov.gchq.gaffer.store.StoreTrait) OutputOperationHandler(uk.gov.gchq.gaffer.store.operation.handler.OutputOperationHandler) AddElementsHandler(uk.gov.gchq.gaffer.accumulostore.operation.handler.AddElementsHandler) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException) SplitStore(uk.gov.gchq.gaffer.operation.impl.SplitStore) ImportAccumuloKeyValueFiles(uk.gov.gchq.gaffer.accumulostore.operation.hdfs.operation.ImportAccumuloKeyValueFiles) GetElementsWithinSet(uk.gov.gchq.gaffer.accumulostore.operation.impl.GetElementsWithinSet) Key(org.apache.accumulo.core.data.Key) GetElementsHandler(uk.gov.gchq.gaffer.accumulostore.operation.handler.GetElementsHandler) Status(uk.gov.gchq.gaffer.core.exception.Status) SampleDataForSplitPoints(uk.gov.gchq.gaffer.hdfs.operation.SampleDataForSplitPoints) HdfsSplitStoreFromFileHandler(uk.gov.gchq.gaffer.hdfs.operation.handler.HdfsSplitStoreFromFileHandler) ElementInputFormat(uk.gov.gchq.gaffer.accumulostore.inputformat.ElementInputFormat) IteratorSettingException(uk.gov.gchq.gaffer.accumulostore.key.exception.IteratorSettingException) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) Operation(uk.gov.gchq.gaffer.operation.Operation) SplitStoreFromIterable(uk.gov.gchq.gaffer.operation.impl.SplitStoreFromIterable) Schema(uk.gov.gchq.gaffer.store.schema.Schema) CommonConstants(uk.gov.gchq.gaffer.commonutil.CommonConstants) ChainedIterable(uk.gov.gchq.gaffer.commonutil.iterable.ChainedIterable) OperationHandler(uk.gov.gchq.gaffer.store.operation.handler.OperationHandler) ClientConfiguration(org.apache.accumulo.core.client.ClientConfiguration) GetElementsWithinSetHandler(uk.gov.gchq.gaffer.accumulostore.operation.handler.GetElementsWithinSetHandler) SplitStoreFromFile(uk.gov.gchq.gaffer.operation.impl.SplitStoreFromFile) GetAdjacentIdsHandler(uk.gov.gchq.gaffer.accumulostore.operation.handler.GetAdjacentIdsHandler) SummariseGroupOverRanges(uk.gov.gchq.gaffer.accumulostore.operation.impl.SummariseGroupOverRanges) LoggerFactory(org.slf4j.LoggerFactory) Mutation(org.apache.accumulo.core.data.Mutation) TRANSFORMATION(uk.gov.gchq.gaffer.store.StoreTrait.TRANSFORMATION) SampleElementsForSplitPointsHandler(uk.gov.gchq.gaffer.accumulostore.operation.handler.SampleElementsForSplitPointsHandler) GetAllElementsHandler(uk.gov.gchq.gaffer.accumulostore.operation.handler.GetAllElementsHandler) QUERY_AGGREGATION(uk.gov.gchq.gaffer.store.StoreTrait.QUERY_AGGREGATION) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) POST_TRANSFORMATION_FILTERING(uk.gov.gchq.gaffer.store.StoreTrait.POST_TRANSFORMATION_FILTERING) Value(org.apache.accumulo.core.data.Value) GetElementsInRangesHandler(uk.gov.gchq.gaffer.accumulostore.operation.handler.GetElementsInRangesHandler) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) Pair(uk.gov.gchq.gaffer.commonutil.pair.Pair) Collection(java.util.Collection) SplitStoreHandler(uk.gov.gchq.gaffer.accumulostore.operation.hdfs.handler.SplitStoreHandler) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Store(uk.gov.gchq.gaffer.store.Store) PRE_AGGREGATION_FILTERING(uk.gov.gchq.gaffer.store.StoreTrait.PRE_AGGREGATION_FILTERING) SplitStoreFromIterableHandler(uk.gov.gchq.gaffer.accumulostore.operation.hdfs.handler.SplitStoreFromIterableHandler) List(java.util.List) INGEST_AGGREGATION(uk.gov.gchq.gaffer.store.StoreTrait.INGEST_AGGREGATION) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Entry(java.util.Map.Entry) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) GraphFilters(uk.gov.gchq.gaffer.operation.graph.GraphFilters) StoreException(uk.gov.gchq.gaffer.store.StoreException) Serialiser(uk.gov.gchq.gaffer.serialisation.Serialiser) ORDERED(uk.gov.gchq.gaffer.store.StoreTrait.ORDERED) SummariseGroupOverRangesHandler(uk.gov.gchq.gaffer.accumulostore.operation.handler.SummariseGroupOverRangesHandler) User(uk.gov.gchq.gaffer.user.User) Connector(org.apache.accumulo.core.client.Connector) AccumuloKeyPackage(uk.gov.gchq.gaffer.accumulostore.key.AccumuloKeyPackage) GetElementsInRanges(uk.gov.gchq.gaffer.accumulostore.operation.impl.GetElementsInRanges) POST_AGGREGATION_FILTERING(uk.gov.gchq.gaffer.store.StoreTrait.POST_AGGREGATION_FILTERING) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) Max(uk.gov.gchq.koryphe.impl.binaryoperator.Max) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Logger(org.slf4j.Logger) AccumuloStoreConstants(uk.gov.gchq.gaffer.accumulostore.utils.AccumuloStoreConstants) GenerateSplitPointsFromSample(uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample) GafferRuntimeException(uk.gov.gchq.gaffer.core.exception.GafferRuntimeException) SampleDataForSplitPointsHandler(uk.gov.gchq.gaffer.accumulostore.operation.hdfs.handler.SampleDataForSplitPointsHandler) Authorizations(org.apache.accumulo.core.security.Authorizations) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) MATCHED_VERTEX(uk.gov.gchq.gaffer.store.StoreTrait.MATCHED_VERTEX) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) ImportAccumuloKeyValueFilesHandler(uk.gov.gchq.gaffer.accumulostore.operation.hdfs.handler.ImportAccumuloKeyValueFilesHandler) AddElementsFromHdfs(uk.gov.gchq.gaffer.hdfs.operation.AddElementsFromHdfs) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Collections(java.util.Collections) Authorizations(org.apache.accumulo.core.security.Authorizations) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Text(org.apache.hadoop.io.Text) IteratorSettingException(uk.gov.gchq.gaffer.accumulostore.key.exception.IteratorSettingException) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) StoreException(uk.gov.gchq.gaffer.store.StoreException) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) Pair(uk.gov.gchq.gaffer.commonutil.pair.Pair)

Example 9 with GraphFilters

use of uk.gov.gchq.gaffer.operation.graph.GraphFilters in project Gaffer by gchq.

the class OperationChainValidator method validateViews.

protected void validateViews(final Operation op, final User user, final Store store, final ValidationResult validationResult) {
    if (op instanceof GraphFilters) {
        final Schema schema = getSchema(op, user, store);
        final ValidationResult viewValidationResult = viewValidator.validate(((GraphFilters) op).getView(), schema, getStoreTraits(store));
        if (!viewValidationResult.isValid()) {
            validationResult.addError("View for operation " + op.getClass().getName() + " is not valid. ");
            validationResult.add(viewValidationResult);
        }
    }
}
Also used : Schema(uk.gov.gchq.gaffer.store.schema.Schema) GraphFilters(uk.gov.gchq.gaffer.operation.graph.GraphFilters) ValidationResult(uk.gov.gchq.koryphe.ValidationResult)

Example 10 with GraphFilters

use of uk.gov.gchq.gaffer.operation.graph.GraphFilters in project Gaffer by gchq.

the class FiltersToOperationConverter method applyPropertyFilters.

private Output<RDD<Element>> applyPropertyFilters(final View derivedView, final Output<RDD<Element>> operation) {
    final List<Set<String>> groupsRelatedToFilters = new ArrayList<>();
    for (final Filter filter : filters) {
        final Set<String> groupsRelatedToFilter = getGroupsFromFilter(filter);
        if (null != groupsRelatedToFilter && !groupsRelatedToFilter.isEmpty()) {
            groupsRelatedToFilters.add(groupsRelatedToFilter);
        }
        LOGGER.info("Groups {} are related to filter {}", StringUtils.join(groupsRelatedToFilter, ','), filter);
    }
    LOGGER.info("Groups related to filters are: {}", StringUtils.join(groupsRelatedToFilters, ','));
    // Take the intersection of this list of groups - only these groups can be related to the query
    final Set<String> intersection = new HashSet<>(derivedView.getEntityGroups());
    intersection.addAll(derivedView.getEdgeGroups());
    for (final Set<String> groupsRelatedToFilter : groupsRelatedToFilters) {
        intersection.retainAll(groupsRelatedToFilter);
    }
    LOGGER.info("Groups that can be returned are: {}", StringUtils.join(intersection, ','));
    // Update view with filters and add to operation
    final Map<String, List<TupleAdaptedPredicate<String, ?>>> groupToFunctions = new HashMap<>();
    for (final Filter filter : filters) {
        final Map<String, List<TupleAdaptedPredicate<String, ?>>> map = getFunctionsFromFilter(filter);
        for (final Entry<String, List<TupleAdaptedPredicate<String, ?>>> entry : map.entrySet()) {
            if (!groupToFunctions.containsKey(entry.getKey())) {
                groupToFunctions.put(entry.getKey(), new ArrayList<>());
            }
            groupToFunctions.get(entry.getKey()).addAll(entry.getValue());
        }
    }
    LOGGER.info("The following functions will be applied for the given group:");
    for (final Entry<String, List<TupleAdaptedPredicate<String, ?>>> entry : groupToFunctions.entrySet()) {
        LOGGER.info("Group = {}: ", entry.getKey());
        for (final TupleAdaptedPredicate<String, ?> cfc : entry.getValue()) {
            if (null != cfc.getSelection()) {
                LOGGER.info("\t{} {}", cfc.getSelection(), cfc.getPredicate());
            } else {
                LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getPredicate());
            }
        }
    }
    boolean updated = false;
    View.Builder builder = new View.Builder();
    for (final String group : derivedView.getEntityGroups()) {
        if (intersection.contains(group)) {
            if (null != groupToFunctions.get(group)) {
                final ViewElementDefinition ved = new ViewElementDefinition.Builder().merge(derivedView.getEntity(group)).postAggregationFilterFunctions(groupToFunctions.get(group)).build();
                LOGGER.info("Adding the following filter functions to the view for group {}:", group);
                for (final TupleAdaptedPredicate<String, ?> cfc : groupToFunctions.get(group)) {
                    if (null != cfc.getSelection()) {
                        LOGGER.info("\t{} {}", cfc.getSelection(), cfc.getPredicate());
                    } else {
                        LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getPredicate());
                    }
                }
                builder = builder.entity(group, ved);
                updated = true;
            } else {
                LOGGER.info("Not adding any filter functions to the view for group {}", group);
            }
        }
    }
    for (final String group : derivedView.getEdgeGroups()) {
        if (intersection.contains(group)) {
            if (null != groupToFunctions.get(group)) {
                final ViewElementDefinition ved = new ViewElementDefinition.Builder().merge(derivedView.getEdge(group)).postAggregationFilterFunctions(groupToFunctions.get(group)).build();
                LOGGER.info("Adding the following filter functions to the view for group {}:", group);
                for (final TupleAdaptedPredicate<String, ?> cfc : groupToFunctions.get(group)) {
                    if (null != cfc.getSelection()) {
                        LOGGER.info("\t{} {}", cfc.getSelection(), cfc.getPredicate());
                    } else {
                        LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getPredicate());
                    }
                }
                builder = builder.edge(group, ved);
                updated = true;
            } else {
                LOGGER.info("Not adding any filter functions to the view for group {}", group);
            }
        }
    }
    if (updated) {
        ((GraphFilters) operation).setView(builder.build());
    } else {
        ((GraphFilters) operation).setView(derivedView);
    }
    return operation;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GraphFilters(uk.gov.gchq.gaffer.operation.graph.GraphFilters) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Filter(org.apache.spark.sql.sources.Filter) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

GraphFilters (uk.gov.gchq.gaffer.operation.graph.GraphFilters)13 Schema (uk.gov.gchq.gaffer.store.schema.Schema)11 Filter (org.apache.spark.sql.sources.Filter)10 Operation (uk.gov.gchq.gaffer.operation.Operation)10 SparkSession (org.apache.spark.sql.SparkSession)9 Test (org.junit.jupiter.api.Test)9 EqualTo (org.apache.spark.sql.sources.EqualTo)7 HashSet (java.util.HashSet)6 EntityId (uk.gov.gchq.gaffer.data.element.id.EntityId)6 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)6 ArrayList (java.util.ArrayList)5 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)5 GetRDDOfAllElements (uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfAllElements)5 GetRDDOfElements (uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfElements)5 GreaterThan (org.apache.spark.sql.sources.GreaterThan)4 LessThan (org.apache.spark.sql.sources.LessThan)4 IsLessThan (uk.gov.gchq.koryphe.impl.predicate.IsLessThan)4 IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)4 TupleAdaptedPredicate (uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate)4 Predicate (java.util.function.Predicate)3