use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.
the class ViewIT method shouldDeserialiseJsonView.
@Test
public void shouldDeserialiseJsonView() throws IOException {
// Given
// When
View view = loadView();
// Then
final ViewElementDefinition edge = view.getEdge(TestGroups.EDGE);
final ElementTransformer transformer = edge.getTransformer();
assertNotNull(transformer);
final List<ConsumerProducerFunctionContext<String, TransformFunction>> contexts = transformer.getFunctions();
assertEquals(1, contexts.size());
final List<String> selection = contexts.get(0).getSelection();
assertEquals(2, selection.size());
assertEquals(TestPropertyNames.PROP_1, selection.get(0));
assertEquals(IdentifierType.SOURCE.name(), selection.get(1));
final List<String> projection = contexts.get(0).getProjection();
assertEquals(1, projection.size());
assertEquals(TestPropertyNames.TRANSIENT_1, projection.get(0));
assertTrue(contexts.get(0).getFunction() instanceof ExampleTransformFunction);
final ElementFilter postFilter = edge.getPostTransformFilter();
assertNotNull(postFilter);
final List<ConsumerFunctionContext<String, FilterFunction>> filterContexts = postFilter.getFunctions();
assertEquals(1, contexts.size());
final List<String> postFilterSelection = filterContexts.get(0).getSelection();
assertEquals(1, postFilterSelection.size());
assertEquals(TestPropertyNames.TRANSIENT_1, postFilterSelection.get(0));
assertTrue(filterContexts.get(0).getFunction() instanceof ExampleFilterFunction);
}
use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.
the class OrTest method shouldJsonSerialiseAndDeserialise.
@Test
public void shouldJsonSerialiseAndDeserialise() throws SerialisationException {
// Given
final Or filter = new Or(Collections.singletonList(new ConsumerFunctionContext<Integer, FilterFunction>(new Or(), Arrays.asList(0, 1, 2))));
// When
final String json = new String(new JSONSerialiser().serialise(filter, true));
// Then
JsonUtil.assertEquals(String.format("{%n" + " \"class\" : \"uk.gov.gchq.gaffer.function.filter.Or\",%n" + " \"functions\" : [ {%n" + " \"function\" : {%n" + " \"class\" : \"uk.gov.gchq.gaffer.function.filter.Or\",%n" + " \"functions\" : [ ]%n" + " },%n" + " \"selection\" : [ 0, 1, 2 ]%n" + " } ]%n" + "}"), json);
// When 2
final Or deserialisedFilter = new JSONSerialiser().deserialise(json.getBytes(), Or.class);
// Then 2
assertNotNull(deserialisedFilter);
}
use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.
the class FiltersToOperationConverter method applyPropertyFilters.
private AbstractGetRDD<?> applyPropertyFilters(final View derivedView, final AbstractGetRDD<?> operation) {
final List<Set<String>> groupsRelatedToFilters = new ArrayList<>();
for (final Filter filter : filters) {
final Set<String> groupsRelatedToFilter = getGroupsFromFilter(filter);
if (groupsRelatedToFilter != null && !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<ConsumerFunctionContext<String, FilterFunction>>> groupToFunctions = new HashMap<>();
for (final Filter filter : filters) {
final Map<String, List<ConsumerFunctionContext<String, FilterFunction>>> map = getFunctionsFromFilter(filter);
for (final Entry<String, List<ConsumerFunctionContext<String, FilterFunction>>> entry : map.entrySet()) {
if (!groupToFunctions.containsKey(entry.getKey())) {
groupToFunctions.put(entry.getKey(), new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
}
groupToFunctions.get(entry.getKey()).addAll(entry.getValue());
}
}
LOGGER.info("The following functions will be applied for the given group:");
for (final Entry<String, List<ConsumerFunctionContext<String, FilterFunction>>> entry : groupToFunctions.entrySet()) {
LOGGER.info("Group = {}: ", entry.getKey());
for (final ConsumerFunctionContext<String, FilterFunction> cfc : entry.getValue()) {
LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getFunction());
}
}
boolean updated = false;
View.Builder builder = new View.Builder();
for (final String group : derivedView.getEntityGroups()) {
if (intersection.contains(group)) {
if (groupToFunctions.get(group) != null) {
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 ConsumerFunctionContext<String, FilterFunction> cfc : groupToFunctions.get(group)) {
LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getFunction());
}
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 (groupToFunctions.get(group) != null) {
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 ConsumerFunctionContext<String, FilterFunction> cfc : groupToFunctions.get(group)) {
LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getFunction());
}
builder = builder.edge(group, ved);
updated = true;
} else {
LOGGER.info("Not adding any filter functions to the view for group {}", group);
}
}
}
if (updated) {
operation.setView(builder.build());
} else {
operation.setView(derivedView);
}
return operation;
}
use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext 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.context.ConsumerFunctionContext in project Gaffer by gchq.
the class FilterToOperationConverterTest method testSpecifyVertexAndPropertyFilter.
@Test
public void testSpecifyVertexAndPropertyFilter() throws OperationException {
final Schema schema = getSchema();
final SQLContext sqlContext = getSqlContext("testSpecifyVertexAndPropertyFilter");
// Specify vertex and a filter on property1
Filter[] filters = new Filter[2];
filters[0] = new GreaterThan("property1", 5);
filters[1] = new EqualTo(SchemaToStructTypeConverter.VERTEX_COL_NAME, "0");
FiltersToOperationConverter converter = new FiltersToOperationConverter(sqlContext, getViewFromSchema(schema), schema, filters);
AbstractGetRDD<?> operation = converter.getOperation();
assertTrue(operation instanceof GetRDDOfElements);
assertEquals(1, operation.getView().getEntityGroups().size());
assertEquals(0, operation.getView().getEdgeGroups().size());
final Set<EntitySeed> seeds = new HashSet<>();
for (final Object seed : ((GetRDDOfElements) operation).getSeeds()) {
seeds.add((EntitySeed) seed);
}
assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
View opView = operation.getView();
List<ConsumerFunctionContext<String, FilterFunction>> entityPostAggFilters = opView.getEntity(ENTITY_GROUP).getPostAggregationFilterFunctions();
assertEquals(1, entityPostAggFilters.size());
final ArrayList<String> expectedProperties = new ArrayList<>();
expectedProperties.add("property1");
assertEquals(1, entityPostAggFilters.get(0).getSelection().size());
assertEquals(expectedProperties.get(0), entityPostAggFilters.get(0).getSelection().get(0));
final ArrayList<FilterFunction> expectedFunctions = new ArrayList<>();
expectedFunctions.add(new IsMoreThan(5, false));
assertEquals(expectedFunctions.get(0), entityPostAggFilters.get(0).getFunction());
// Specify vertex and filters on properties property1 and property4
filters = new Filter[3];
filters[0] = new GreaterThan("property1", 5);
filters[1] = new EqualTo(SchemaToStructTypeConverter.VERTEX_COL_NAME, "0");
filters[2] = new LessThan("property4", 8);
converter = new FiltersToOperationConverter(sqlContext, getViewFromSchema(schema), schema, filters);
operation = converter.getOperation();
assertTrue(operation instanceof GetRDDOfElements);
assertEquals(1, operation.getView().getEntityGroups().size());
assertEquals(0, operation.getView().getEdgeGroups().size());
seeds.clear();
for (final Object seed : ((GetRDDOfElements) operation).getSeeds()) {
seeds.add((EntitySeed) seed);
}
assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
opView = operation.getView();
entityPostAggFilters = opView.getEntity(ENTITY_GROUP).getPostAggregationFilterFunctions();
assertEquals(2, entityPostAggFilters.size());
expectedProperties.clear();
expectedProperties.add("property1");
expectedProperties.add("property4");
assertEquals(1, entityPostAggFilters.get(0).getSelection().size());
assertEquals(expectedProperties.get(0), entityPostAggFilters.get(0).getSelection().get(0));
assertEquals(1, entityPostAggFilters.get(1).getSelection().size());
assertEquals(expectedProperties.get(1), entityPostAggFilters.get(1).getSelection().get(0));
expectedFunctions.clear();
expectedFunctions.add(new IsMoreThan(5, false));
expectedFunctions.add(new IsLessThan(8, false));
assertEquals(expectedFunctions.get(0), entityPostAggFilters.get(0).getFunction());
assertEquals(expectedFunctions.get(1), entityPostAggFilters.get(1).getFunction());
sqlContext.sparkContext().stop();
}
Aggregations