Search in sources :

Example 6 with Aggregate

use of uk.gov.gchq.gaffer.operation.impl.function.Aggregate 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)

Example 7 with Aggregate

use of uk.gov.gchq.gaffer.operation.impl.function.Aggregate 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 8 with Aggregate

use of uk.gov.gchq.gaffer.operation.impl.function.Aggregate in project gaffer-doc by gchq.

the class AggregateExample method simpleAggregateElementsExample.

public void simpleAggregateElementsExample() {
    // ---------------------------------------------------------
    final Aggregate aggregate = new Aggregate();
    // ---------------------------------------------------------
    showExample(aggregate, null);
}
Also used : Aggregate(uk.gov.gchq.gaffer.operation.impl.function.Aggregate)

Example 9 with Aggregate

use of uk.gov.gchq.gaffer.operation.impl.function.Aggregate 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 10 with Aggregate

use of uk.gov.gchq.gaffer.operation.impl.function.Aggregate 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)

Aggregations

Aggregate (uk.gov.gchq.gaffer.operation.impl.function.Aggregate)13 Test (org.junit.jupiter.api.Test)10 Schema (uk.gov.gchq.gaffer.store.schema.Schema)10 AggregatePair (uk.gov.gchq.gaffer.operation.util.AggregatePair)9 Element (uk.gov.gchq.gaffer.data.element.Element)6 SchemaEdgeDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition)5 Sum (uk.gov.gchq.koryphe.impl.binaryoperator.Sum)5 ArrayList (java.util.ArrayList)3 Edge (uk.gov.gchq.gaffer.data.element.Edge)3 Entity (uk.gov.gchq.gaffer.data.element.Entity)3 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)3 Context (uk.gov.gchq.gaffer.store.Context)2 SchemaEntityDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition)2 Max (uk.gov.gchq.koryphe.impl.binaryoperator.Max)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)1 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)1 FederatedStore (uk.gov.gchq.gaffer.federatedstore.FederatedStore)1 FederatedStoreProperties (uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties)1