Search in sources :

Example 1 with AggregatePair

use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project gaffer-doc by gchq.

the class AggregateExample method aggregateOnlyEdgesOfTypeEdgeWithATransientPropertyAndProvidedAggregator.

public void aggregateOnlyEdgesOfTypeEdgeWithATransientPropertyAndProvidedAggregator() {
    // ---------------------------------------------------------
    final String[] groupBy = {};
    final Aggregate aggregate = new Aggregate.Builder().edge("edge", new AggregatePair(groupBy, new ElementAggregator.Builder().select("transientProperty1").execute(new StringConcat()).build())).build();
    // ---------------------------------------------------------
    showExample(aggregate, "The groupBy has been set to an empty array. This will override the groupBy value in the schema.");
}
Also used : StringConcat(uk.gov.gchq.koryphe.impl.binaryoperator.StringConcat) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) Aggregate(uk.gov.gchq.gaffer.operation.impl.function.Aggregate)

Example 2 with AggregatePair

use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.

the class AggregateHandler method doOperation.

public Iterable<? extends Element> doOperation(final Aggregate operation, final Schema schema) throws OperationException {
    if (null == operation.getInput()) {
        throw new OperationException("Aggregate operation has null iterable of elements");
    }
    // all elements should be used. This matches the way a View works.
    if (null == operation.getEntities() && null == operation.getEdges()) {
        final Map<String, AggregatePair> entityMap = new HashMap<>();
        schema.getEntityGroups().forEach(e -> entityMap.put(e, new AggregatePair()));
        operation.setEntities(entityMap);
        final Map<String, AggregatePair> edgeMap = new HashMap<>();
        schema.getEdgeGroups().forEach(e -> edgeMap.put(e, new AggregatePair()));
        operation.setEdges(edgeMap);
    }
    final ValidationResult result = validator.validate(operation, schema);
    if (!result.isValid()) {
        throw new OperationException("Aggregate operation is invalid. " + result.getErrorString());
    }
    return AggregatorUtil.queryAggregate(operation.getInput(), schema, buildView(operation));
}
Also used : HashMap(java.util.HashMap) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) OperationException(uk.gov.gchq.gaffer.operation.OperationException)

Example 3 with AggregatePair

use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.

the class AggregateHandler method buildView.

private View buildView(final Aggregate operation) {
    View.Builder builder = new View.Builder();
    if (null != operation.getEntities()) {
        for (final Map.Entry<String, AggregatePair> entry : operation.getEntities().entrySet()) {
            final String group = entry.getKey();
            final AggregatePair pair = entry.getValue();
            builder = builder.entity(group, new ViewElementDefinition.Builder().groupBy(pair.getGroupBy()).aggregator(pair.getElementAggregator()).build());
        }
    }
    if (null != operation.getEdges()) {
        for (final Map.Entry<String, AggregatePair> entry : operation.getEdges().entrySet()) {
            final String group = entry.getKey();
            final AggregatePair pair = entry.getValue();
            builder = builder.edge(group, new ViewElementDefinition.Builder().groupBy(pair.getGroupBy()).aggregator(pair.getElementAggregator()).build());
        }
    }
    return builder.build();
}
Also used : AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with AggregatePair

use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.

the class AggregateHandlerTest method shouldFailValidationWhenSchemaElementDefinitionsAreNull.

@Test
public void shouldFailValidationWhenSchemaElementDefinitionsAreNull() {
    // Given
    given(store.getSchema()).willReturn(new Schema());
    input.add(edge);
    input.add(edge1);
    edges.put(TestGroups.EDGE, new AggregatePair());
    final Aggregate aggregate = new Aggregate.Builder().input(input).edges(edges).build();
    // When / Then
    try {
        final Iterable<? extends Element> results = handler.doOperation(aggregate, context, store);
    } catch (final OperationException e) {
        assertTrue(e.getMessage().contains("Edge group: " + TestGroups.EDGE + " does not exist in the schema."));
    }
}
Also used : Schema(uk.gov.gchq.gaffer.store.schema.Schema) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) Aggregate(uk.gov.gchq.gaffer.operation.impl.function.Aggregate) OperationException(uk.gov.gchq.gaffer.operation.OperationException) Test(org.junit.jupiter.api.Test)

Example 5 with AggregatePair

use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.

the class AggregateHandlerTest method shouldAggregateElementsWhenNoGroupByInSchema.

@Test
public void shouldAggregateElementsWhenNoGroupByInSchema() throws OperationException {
    // Given
    final Schema schema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().build()).build();
    given(store.getSchema()).willReturn(schema);
    input.add(entity);
    input.add(entity1);
    input.add(entity2);
    final AggregatePair pair = new AggregatePair(new String[] { "timestamp" }, new ElementAggregator.Builder().select("count").execute(new Sum()).build());
    entities.put(TestGroups.ENTITY, pair);
    final Entity expectedEntity = new Entity.Builder().group(TestGroups.ENTITY).property("timestamp", 3L).property("count", 5).build();
    expected.add(expectedEntity);
    expected.add(entity1);
    final Aggregate aggregate = new Aggregate.Builder().input(input).entities(entities).build();
    // When
    final Iterable<? extends Element> results = handler.doOperation(aggregate, context, store);
    final Set<Element> resultsSet = Sets.newHashSet(results);
    // Then
    assertEquals(expected, resultsSet);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Element(uk.gov.gchq.gaffer.data.element.Element) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) Aggregate(uk.gov.gchq.gaffer.operation.impl.function.Aggregate) Test(org.junit.jupiter.api.Test)

Aggregations

AggregatePair (uk.gov.gchq.gaffer.operation.util.AggregatePair)13 Aggregate (uk.gov.gchq.gaffer.operation.impl.function.Aggregate)9 Test (org.junit.jupiter.api.Test)8 Schema (uk.gov.gchq.gaffer.store.schema.Schema)8 Element (uk.gov.gchq.gaffer.data.element.Element)5 Sum (uk.gov.gchq.koryphe.impl.binaryoperator.Sum)5 HashMap (java.util.HashMap)4 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)4 SchemaEdgeDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition)4 Edge (uk.gov.gchq.gaffer.data.element.Edge)3 Entity (uk.gov.gchq.gaffer.data.element.Entity)3 ValidationResult (uk.gov.gchq.koryphe.ValidationResult)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 OperationException (uk.gov.gchq.gaffer.operation.OperationException)2 SchemaEntityDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition)2 Max (uk.gov.gchq.koryphe.impl.binaryoperator.Max)2 HashSet (java.util.HashSet)1 BinaryOperator (java.util.function.BinaryOperator)1 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)1