Search in sources :

Example 6 with ConsumerFunctionContext

use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.

the class FilterToOperationConverterTest method testSpecifyPropertyFilters.

@Test
public void testSpecifyPropertyFilters() throws OperationException {
    final Schema schema = getSchema();
    final SQLContext sqlContext = getSqlContext("testSpecifyPropertyFilters");
    final Filter[] filters = new Filter[1];
    // GreaterThan
    filters[0] = new GreaterThan("property1", 5);
    FiltersToOperationConverter converter = new FiltersToOperationConverter(sqlContext, getViewFromSchema(schema), schema, filters);
    AbstractGetRDD<?> operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfAllElements);
    View opView = operation.getView();
    List<ConsumerFunctionContext<String, FilterFunction>> entityPostAggFilters = opView.getEntity(ENTITY_GROUP).getPostAggregationFilterFunctions();
    assertEquals(1, entityPostAggFilters.size());
    assertEquals(new ArrayList<>(Collections.singleton("property1")), entityPostAggFilters.get(0).getSelection());
    assertEquals(new IsMoreThan(5, false), entityPostAggFilters.get(0).getFunction());
    for (final String edgeGroup : EDGE_GROUPS) {
        final List<ConsumerFunctionContext<String, FilterFunction>> edgePostAggFilters = opView.getEdge(edgeGroup).getPostAggregationFilterFunctions();
        assertEquals(1, edgePostAggFilters.size());
        assertEquals(new ArrayList<>(Collections.singleton("property1")), edgePostAggFilters.get(0).getSelection());
        assertEquals(new IsMoreThan(5, false), edgePostAggFilters.get(0).getFunction());
    }
    // LessThan
    filters[0] = new LessThan("property4", 8L);
    converter = new FiltersToOperationConverter(sqlContext, getViewFromSchema(schema), schema, filters);
    operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfAllElements);
    // Only groups ENTITY_GROUP and EDGE_GROUP should be in the view as only they have property4
    opView = operation.getView();
    entityPostAggFilters = opView.getEntity(ENTITY_GROUP).getPostAggregationFilterFunctions();
    assertEquals(1, entityPostAggFilters.size());
    assertEquals(new ArrayList<>(Collections.singleton("property4")), entityPostAggFilters.get(0).getSelection());
    assertEquals(new IsLessThan(8L, false), entityPostAggFilters.get(0).getFunction());
    List<ConsumerFunctionContext<String, FilterFunction>> edgePostAggFilters = opView.getEdge(EDGE_GROUP).getPostAggregationFilterFunctions();
    assertEquals(1, edgePostAggFilters.size());
    assertEquals(new ArrayList<>(Collections.singleton("property4")), edgePostAggFilters.get(0).getSelection());
    assertEquals(new IsLessThan(8L, false), edgePostAggFilters.get(0).getFunction());
    // And
    final Filter left = new GreaterThan("property1", 5);
    final Filter right = new GreaterThan("property4", 8L);
    filters[0] = new And(left, right);
    converter = new FiltersToOperationConverter(sqlContext, getViewFromSchema(schema), schema, filters);
    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
    opView = operation.getView();
    entityPostAggFilters = opView.getEntity(ENTITY_GROUP).getPostAggregationFilterFunctions();
    assertEquals(2, entityPostAggFilters.size());
    final ArrayList<String> expectedProperties = new ArrayList<>();
    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));
    final ArrayList<FilterFunction> expectedFunctions = new ArrayList<>();
    expectedFunctions.add(new IsMoreThan(5, false));
    expectedFunctions.add(new IsMoreThan(8L, false));
    assertEquals(expectedFunctions.get(0), entityPostAggFilters.get(0).getFunction());
    assertEquals(expectedFunctions.get(1), entityPostAggFilters.get(1).getFunction());
    edgePostAggFilters = opView.getEdge(EDGE_GROUP).getPostAggregationFilterFunctions();
    assertEquals(2, edgePostAggFilters.size());
    assertEquals(1, edgePostAggFilters.get(0).getSelection().size());
    assertEquals(expectedProperties.get(0), edgePostAggFilters.get(0).getSelection().get(0));
    assertEquals(1, edgePostAggFilters.get(1).getSelection().size());
    assertEquals(expectedProperties.get(1), edgePostAggFilters.get(1).getSelection().get(0));
    sqlContext.sparkContext().stop();
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ArrayList(java.util.ArrayList) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) LessThan(org.apache.spark.sql.sources.LessThan) IsLessThan(uk.gov.gchq.gaffer.function.filter.IsLessThan) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) IsLessThan(uk.gov.gchq.gaffer.function.filter.IsLessThan) Filter(org.apache.spark.sql.sources.Filter) GreaterThan(org.apache.spark.sql.sources.GreaterThan) GetRDDOfAllElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfAllElements) And(org.apache.spark.sql.sources.And) IsMoreThan(uk.gov.gchq.gaffer.function.filter.IsMoreThan) SQLContext(org.apache.spark.sql.SQLContext) Test(org.junit.Test)

Example 7 with ConsumerFunctionContext

use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.

the class FilterToOperationConverterTest method testSpecifySourceOrDestinationAndPropertyFilter.

@Test
public void testSpecifySourceOrDestinationAndPropertyFilter() throws OperationException {
    final Schema schema = getSchema();
    final SQLContext sqlContext = getSqlContext("testSpecifyVertexAndPropertyFilter");
    // 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(sqlContext, getViewFromSchema(schema), schema, filters);
    AbstractGetRDD<?> operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfElements);
    assertEquals(0, operation.getView().getEntityGroups().size());
    assertEquals(2, 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();
    for (final String edgeGroup : EDGE_GROUPS) {
        final List<ConsumerFunctionContext<String, FilterFunction>> edgePostAggFilters = opView.getEdge(edgeGroup).getPostAggregationFilterFunctions();
        assertEquals(1, edgePostAggFilters.size());
        assertEquals(new ArrayList<>(Collections.singleton("property1")), edgePostAggFilters.get(0).getSelection());
        assertEquals(new IsMoreThan(5, false), edgePostAggFilters.get(0).getFunction());
    }
    // 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(sqlContext, getViewFromSchema(schema), schema, filters);
    operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfElements);
    assertEquals(0, operation.getView().getEntityGroups().size());
    assertEquals(1, 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();
    final List<ConsumerFunctionContext<String, FilterFunction>> entityPostAggFilters = opView.getEdge(EDGE_GROUP).getPostAggregationFilterFunctions();
    assertEquals(2, entityPostAggFilters.size());
    final List<String> expectedProperties = new ArrayList<>();
    expectedProperties.add("property1");
    expectedProperties.add("property4");
    assertEquals(1, entityPostAggFilters.get(0).getSelection().size());
    assertEquals(expectedProperties.get(0), entityPostAggFilters.get(0).getSelection().get(0));
    assertEquals(new IsMoreThan(5, false), entityPostAggFilters.get(0).getFunction());
    assertEquals(1, entityPostAggFilters.get(1).getSelection().size());
    assertEquals(expectedProperties.get(1), entityPostAggFilters.get(1).getSelection().get(0));
    assertEquals(new IsLessThan(8, false), entityPostAggFilters.get(1).getFunction());
    sqlContext.sparkContext().stop();
}
Also used : Schema(uk.gov.gchq.gaffer.store.schema.Schema) ArrayList(java.util.ArrayList) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) EqualTo(org.apache.spark.sql.sources.EqualTo) LessThan(org.apache.spark.sql.sources.LessThan) IsLessThan(uk.gov.gchq.gaffer.function.filter.IsLessThan) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) IsLessThan(uk.gov.gchq.gaffer.function.filter.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.gaffer.function.filter.IsMoreThan) SQLContext(org.apache.spark.sql.SQLContext) GetRDDOfElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfElements) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with ConsumerFunctionContext

use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.

the class FilterToOperationConverterTest method testSpecifyMultiplePropertyFilters.

@Test
public void testSpecifyMultiplePropertyFilters() throws OperationException {
    final Schema schema = getSchema();
    final SQLContext sqlContext = getSqlContext("testSpecifyMultiplePropertyFilters");
    final Filter[] filters = new Filter[2];
    filters[0] = new GreaterThan("property1", 5);
    filters[1] = new LessThan("property4", 8L);
    FiltersToOperationConverter converter = new FiltersToOperationConverter(sqlContext, getViewFromSchema(schema), schema, filters);
    AbstractGetRDD<?> 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 = operation.getView();
    List<ConsumerFunctionContext<String, FilterFunction>> entityPostAggFilters = opView.getEntity(ENTITY_GROUP).getPostAggregationFilterFunctions();
    assertEquals(2, entityPostAggFilters.size());
    final ArrayList<String> expectedProperties = new ArrayList<>();
    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));
    final ArrayList<FilterFunction> expectedFunctions = new ArrayList<>();
    expectedFunctions.add(new IsMoreThan(5, false));
    expectedFunctions.add(new IsLessThan(8L, false));
    assertEquals(expectedFunctions.get(0), entityPostAggFilters.get(0).getFunction());
    assertEquals(expectedFunctions.get(1), entityPostAggFilters.get(1).getFunction());
    final List<ConsumerFunctionContext<String, FilterFunction>> edgePostAggFilters = opView.getEdge(EDGE_GROUP).getPostAggregationFilterFunctions();
    assertEquals(2, edgePostAggFilters.size());
    assertEquals(1, edgePostAggFilters.get(0).getSelection().size());
    assertEquals(expectedProperties.get(0), edgePostAggFilters.get(0).getSelection().get(0));
    assertEquals(1, edgePostAggFilters.get(1).getSelection().size());
    assertEquals(expectedProperties.get(1), edgePostAggFilters.get(1).getSelection().get(0));
    sqlContext.sparkContext().stop();
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ArrayList(java.util.ArrayList) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) LessThan(org.apache.spark.sql.sources.LessThan) IsLessThan(uk.gov.gchq.gaffer.function.filter.IsLessThan) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) IsLessThan(uk.gov.gchq.gaffer.function.filter.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.gaffer.function.filter.IsMoreThan) SQLContext(org.apache.spark.sql.SQLContext) Test(org.junit.Test)

Example 9 with ConsumerFunctionContext

use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.

the class SchemaElementDefinitionValidatorTest method shouldValidateFunctionSelectionsAndReturnTrueWhenAllFunctionsAreValid.

@Test
public void shouldValidateFunctionSelectionsAndReturnTrueWhenAllFunctionsAreValid() {
    // Given
    final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
    final Processor<String, ConsumerFunctionContext<String, ConsumerFunction>> processor = mock(Processor.class);
    final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
    final List<ConsumerFunctionContext<String, ConsumerFunction>> functions = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final ConsumerFunctionContext<String, ConsumerFunction> context = mock(ConsumerFunctionContext.class);
        final ConsumerFunction function = mock(ConsumerFunction.class);
        given(function.getInputClasses()).willReturn(new Class<?>[] { CharSequence.class, CharSequence.class });
        given(context.getFunction()).willReturn(function);
        given(context.getSelection()).willReturn(Arrays.asList("key" + i + "a", "key" + i + "b"));
        functions.add(context);
        given(elementDef.getClass(context.getSelection().get(0))).willReturn((Class) CharSequence.class);
        given(elementDef.getClass(context.getSelection().get(1))).willReturn((Class) String.class);
    }
    given(processor.getFunctions()).willReturn(functions);
    // When
    final boolean isValid = validator.validateFunctionArgumentTypes(processor, elementDef);
    // Then
    assertTrue(isValid);
}
Also used : ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) ArrayList(java.util.ArrayList) ConsumerFunction(uk.gov.gchq.gaffer.function.ConsumerFunction) Test(org.junit.Test)

Example 10 with ConsumerFunctionContext

use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.

the class AccumuloStoreRelationTest method testBuildScanRestrictViewByProperty.

@Test
public void testBuildScanRestrictViewByProperty() throws OperationException, StoreException {
    final List<ConsumerFunctionContext<String, FilterFunction>> filters = new ArrayList<>();
    filters.add(new ConsumerFunctionContext<>(new IsMoreThan(5, false), new ArrayList<>(Arrays.asList("property1"))));
    final View view = new View.Builder().edge(GetDataFrameOfElementsHandlerTest.EDGE_GROUP, new ViewElementDefinition.Builder().postAggregationFilterFunctions(filters).build()).build();
    final Predicate<Element> returnElement = (Element element) -> element.getGroup().equals(GetDataFrameOfElementsHandlerTest.EDGE_GROUP) && ((Integer) element.getProperty("property1")) > 5;
    testBuildScanWithView("testBuildScanRestrictViewByProperty", view, returnElement);
}
Also used : ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) IsMoreThan(uk.gov.gchq.gaffer.function.filter.IsMoreThan) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Test(org.junit.Test)

Aggregations

ConsumerFunctionContext (uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext)14 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)7 FilterFunction (uk.gov.gchq.gaffer.function.FilterFunction)7 IsMoreThan (uk.gov.gchq.gaffer.function.filter.IsMoreThan)6 Filter (org.apache.spark.sql.sources.Filter)5 GreaterThan (org.apache.spark.sql.sources.GreaterThan)5 LessThan (org.apache.spark.sql.sources.LessThan)5 IsLessThan (uk.gov.gchq.gaffer.function.filter.IsLessThan)5 HashSet (java.util.HashSet)4 SQLContext (org.apache.spark.sql.SQLContext)4 Schema (uk.gov.gchq.gaffer.store.schema.Schema)4 EqualTo (org.apache.spark.sql.sources.EqualTo)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Set (java.util.Set)2 And (org.apache.spark.sql.sources.And)2 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)2 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)2