Search in sources :

Example 6 with AggregatePair

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

the class AggregateHandlerTest method shouldFailValidationWhenTypeArgumentOfBinaryOperatorInFunctionIsIncorrect.

@Test
public void shouldFailValidationWhenTypeArgumentOfBinaryOperatorInFunctionIsIncorrect() {
    // Given
    final Schema schema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().property(TestPropertyNames.TIMESTAMP, "timestamp.long").build()).type("timestamp.long", new TypeDefinition(Long.class)).build();
    given(store.getSchema()).willReturn(schema);
    input.add(entity);
    input.add(entity1);
    input.add(entity2);
    entities.put(TestGroups.ENTITY, new AggregatePair(new ElementAggregator.Builder().select(TestPropertyNames.TIMESTAMP).execute(new Or()).build()));
    final Aggregate aggregate = new Aggregate.Builder().input(input).entities(entities).build();
    // When / Then
    assertThatExceptionOfType(OperationException.class).isThrownBy(() -> handler.doOperation(aggregate, context, store)).withMessageContaining("Incompatible types.");
}
Also used : Or(uk.gov.gchq.koryphe.impl.binaryoperator.Or) Schema(uk.gov.gchq.gaffer.store.schema.Schema) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) Aggregate(uk.gov.gchq.gaffer.operation.impl.function.Aggregate) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.jupiter.api.Test)

Example 7 with AggregatePair

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

the class AggregateValidator method validateOperation.

@Override
protected ValidationResult validateOperation(final Aggregate operation, final Schema schema) {
    final ValidationResult result = new ValidationResult();
    final Map<String, ?> entities = null != operation.getEntities() ? operation.getEntities() : new HashMap<>();
    final Map<String, ?> edges = null != operation.getEdges() ? operation.getEdges() : new HashMap<>();
    for (final Map.Entry<String, ?> entry : edges.entrySet()) {
        result.add(validateEdge(entry, schema));
        result.add(validateElementAggregator(entry, schema));
        result.add(validateAggregatePropertyClasses(schema.getEdge(entry.getKey()), (AggregatePair) entry.getValue()));
    }
    for (final Map.Entry<String, ?> entry : entities.entrySet()) {
        result.add(validateEntity(entry, schema));
        result.add(validateElementAggregator(entry, schema));
        result.add(validateAggregatePropertyClasses(schema.getEntity(entry.getKey()), (AggregatePair) entry.getValue()));
    }
    return result;
}
Also used : AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with AggregatePair

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

the class AggregateHandlerTest method shouldFailValidationWhenElementAggregatorOperationIsNull.

@Test
public void shouldFailValidationWhenElementAggregatorOperationIsNull() {
    // Given
    final Schema schema = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().aggregator(new ElementAggregator.Builder().select("count").execute(null).build()).groupBy("timestamp").build()).build();
    given(store.getSchema()).willReturn(schema);
    input.add(edge);
    input.add(edge1);
    input.add(edge2);
    edges.put(TestGroups.EDGE, new AggregatePair());
    final Aggregate aggregate = new Aggregate.Builder().input(input).edges(edges).build();
    // When / Then
    assertThatExceptionOfType(OperationException.class).isThrownBy(() -> handler.doOperation(aggregate, context, store)).withMessageContaining("Schema contains an ElementAggregator with a null function.");
}
Also used : Schema(uk.gov.gchq.gaffer.store.schema.Schema) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) Aggregate(uk.gov.gchq.gaffer.operation.impl.function.Aggregate) Test(org.junit.jupiter.api.Test)

Example 9 with AggregatePair

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

the class AggregateHandlerTest method shouldAggregateTheSameFromSchemaOrOperation.

@Test
public void shouldAggregateTheSameFromSchemaOrOperation() throws OperationException {
    // Given
    final Schema schema = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().build()).build();
    final Store store1 = mock(Store.class);
    final List<Element> input1 = new ArrayList<>();
    final Set<Element> expected1 = new HashSet<>();
    final Map<String, AggregatePair> edges1 = new HashMap<>();
    final Schema schema1 = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().aggregator(new ElementAggregator.Builder().select("count").execute(new Sum()).select("turns").execute(new Sum()).build()).groupBy("timestamp").build()).build();
    given(store.getSchema()).willReturn(schema);
    given(store1.getSchema()).willReturn(schema1);
    input.add(edge);
    input.add(edge1);
    input.add(edge2);
    final Edge localEdge = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 2L).property("turns", 6).property("count", 2L).build();
    final Edge localEdge1 = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 1L).property("turns", 9).property("count", 5L).build();
    final Edge localEdge2 = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 2L).property("turns", 4).property("count", 1L).build();
    input1.add(localEdge);
    input1.add(localEdge1);
    input1.add(localEdge2);
    final AggregatePair pair = new AggregatePair(new String[] { "timestamp" }, new ElementAggregator.Builder().select("count").execute(new Sum()).select("turns").execute(new Sum()).build());
    final AggregatePair otherPair = new AggregatePair();
    edges.put(TestGroups.EDGE, pair);
    edges1.put(TestGroups.EDGE, otherPair);
    final Edge expectedEdge = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 2L).property("turns", 10).property("count", 3L).build();
    expected.add(expectedEdge);
    expected.add(edge1);
    expected1.add(expectedEdge);
    expected1.add(edge1);
    final Aggregate aggregate = new Aggregate.Builder().input(input).edges(edges).build();
    final Aggregate aggregate1 = new Aggregate.Builder().input(input1).edges(edges1).build();
    // When
    final Iterable<? extends Element> results = handler.doOperation(aggregate, context, store);
    final Set<Element> resultsSet = Sets.newHashSet(results);
    final Iterable<? extends Element> results1 = handler.doOperation(aggregate1, context, store1);
    final Set<Element> resultsSet1 = Sets.newHashSet(results1);
    // Then
    assertEquals(expected, resultsSet);
    assertEquals(expected1, resultsSet1);
}
Also used : HashMap(java.util.HashMap) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) Store(uk.gov.gchq.gaffer.store.Store) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) Aggregate(uk.gov.gchq.gaffer.operation.impl.function.Aggregate) Edge(uk.gov.gchq.gaffer.data.element.Edge) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 10 with AggregatePair

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

the class AggregateHandlerTest method shouldAggregateElementsWhenAggregatorNotProvidedInSchema.

@Test
public void shouldAggregateElementsWhenAggregatorNotProvidedInSchema() throws OperationException {
    // Given
    final Schema schema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().groupBy("timestamp").build()).build();
    given(store.getSchema()).willReturn(schema);
    input.add(entity);
    input.add(entity1);
    input.add(entity2);
    input.add(entity3);
    final AggregatePair pair = new AggregatePair(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();
    final Entity expectedEntity1 = new Entity.Builder().group(TestGroups.ENTITY).property("timestamp", 2L).property("count", 6).build();
    expected.add(expectedEntity);
    expected.add(expectedEntity1);
    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) 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