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);
}
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.");
}
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);
}
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.");
}
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);
}
Aggregations