use of uk.gov.gchq.koryphe.impl.predicate.IsMoreThan in project Gaffer by gchq.
the class QueryGeneratorTest method testQueryGeneratorForGetElementsWithEntitySeeds.
@Test
public void testQueryGeneratorForGetElementsWithEntitySeeds(@TempDir java.nio.file.Path tempDir) throws IOException, OperationException {
// Given
// - Create snapshot folder
final String folder = String.format("file:///%s", tempDir.toString());
final String snapshotFolder = folder + "/" + ParquetStore.getSnapshotPath(1000L);
// - Write out Parquet files so know the partitioning
CalculatePartitionerTest.writeData(snapshotFolder, new SchemaUtils(schema));
// - Initialise store
final ParquetStoreProperties storeProperties = new ParquetStoreProperties();
storeProperties.setDataDir(folder);
storeProperties.setTempFilesDir(folder + "/tmpdata");
final ParquetStore store = (ParquetStore) ParquetStore.createStore("graphId", schema, storeProperties);
// When 1 - no view, query for vertex 0
GetElements getElements = new GetElements.Builder().input(new EntitySeed(0L)).seedMatching(SeedMatching.SeedMatchingType.RELATED).build();
ParquetQuery query = new QueryGenerator(store).getParquetQuery(getElements);
// Then 1
final List expected = new ArrayList<>();
final FilterPredicate vertex0 = eq(FilterApi.longColumn(ParquetStore.VERTEX), 0L);
final FilterPredicate source0 = eq(FilterApi.longColumn(ParquetStore.SOURCE), 0L);
final FilterPredicate destination0 = eq(FilterApi.longColumn(ParquetStore.DESTINATION), 0L);
for (final String group : Arrays.asList(TestGroups.ENTITY, TestGroups.ENTITY_2)) {
final Path groupFolderPath = new Path(snapshotFolder, ParquetStore.getGroupSubDir(group, false));
final Path pathForPartitionFile = new Path(groupFolderPath, ParquetStore.getFile(0));
expected.add(new ParquetFileQuery(pathForPartitionFile, vertex0, true));
}
for (final String group : Arrays.asList(TestGroups.EDGE, TestGroups.EDGE_2)) {
final Path groupFolderPath = new Path(snapshotFolder, ParquetStore.getGroupSubDir(group, false));
final Path pathForPartitionFile = new Path(groupFolderPath, ParquetStore.getFile(0));
expected.add(new ParquetFileQuery(pathForPartitionFile, source0, true));
final Path reversedGroupFolderPath = new Path(snapshotFolder, ParquetStore.getGroupSubDir(group, true));
final Path pathForReversedPartitionFile = new Path(reversedGroupFolderPath, ParquetStore.getFile(0));
expected.add(new ParquetFileQuery(pathForReversedPartitionFile, destination0, true));
}
assertThat(expected).containsOnly(query.getAllParquetFileQueries().toArray());
// When 2 - no view, query for vertices 0 and 1000000
getElements = new GetElements.Builder().input(new EntitySeed(0L), new EntitySeed(1000000L)).seedMatching(SeedMatching.SeedMatchingType.RELATED).build();
query = new QueryGenerator(store).getParquetQuery(getElements);
// Then 2
expected.clear();
final FilterPredicate vertex1000000 = eq(FilterApi.longColumn(ParquetStore.VERTEX), 1000000L);
final FilterPredicate source1000000 = eq(FilterApi.longColumn(ParquetStore.SOURCE), 1000000L);
final FilterPredicate destination1000000 = eq(FilterApi.longColumn(ParquetStore.DESTINATION), 1000000L);
for (final String group : Arrays.asList(TestGroups.ENTITY, TestGroups.ENTITY_2)) {
final Path groupFolderPath = new Path(snapshotFolder, ParquetStore.getGroupSubDir(group, false));
final Path pathForPartitionFile1 = new Path(groupFolderPath, ParquetStore.getFile(0));
expected.add(new ParquetFileQuery(pathForPartitionFile1, vertex0, true));
final Path pathForPartitionFile2 = new Path(groupFolderPath, ParquetStore.getFile(9));
expected.add(new ParquetFileQuery(pathForPartitionFile2, vertex1000000, true));
}
for (final String group : Arrays.asList(TestGroups.EDGE, TestGroups.EDGE_2)) {
final Path groupFolderPath = new Path(snapshotFolder, ParquetStore.getGroupSubDir(group, false));
final Path reversedGroupFolderPath = new Path(snapshotFolder, ParquetStore.getGroupSubDir(group, true));
// Partition 0, vertex 0L
final Path pathForPartitionFile1 = new Path(groupFolderPath, ParquetStore.getFile(0));
expected.add(new ParquetFileQuery(pathForPartitionFile1, source0, true));
// Partition 9, vertex 1000000L
final Path pathForPartitionFile2 = new Path(groupFolderPath, ParquetStore.getFile(9));
expected.add(new ParquetFileQuery(pathForPartitionFile2, source1000000, true));
// Partition 0 of reversed, vertex 0L
final Path pathForPartitionFile3 = new Path(reversedGroupFolderPath, ParquetStore.getFile(0));
expected.add(new ParquetFileQuery(pathForPartitionFile3, destination0, true));
// Partition 9 of reversed, vertex 1000000L
final Path pathForPartitionFile4 = new Path(reversedGroupFolderPath, ParquetStore.getFile(9));
expected.add(new ParquetFileQuery(pathForPartitionFile4, destination1000000, true));
}
assertThat(expected).containsOnly(query.getAllParquetFileQueries().toArray());
// When 3 - view with filter that can be pushed down to Parquet, query for vertices 0 and 1000000
getElements = new GetElements.Builder().input(new EntitySeed(0L), new EntitySeed(1000000L)).seedMatching(SeedMatching.SeedMatchingType.RELATED).view(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(10)).build()).build()).build()).build();
query = new QueryGenerator(store).getParquetQuery(getElements);
// Then 3
expected.clear();
final FilterPredicate source0AndCount = and(gt(FilterApi.intColumn("count"), 10), eq(FilterApi.longColumn(ParquetStore.SOURCE), 0L));
final FilterPredicate source1000000AndCount = and(gt(FilterApi.intColumn("count"), 10), eq(FilterApi.longColumn(ParquetStore.SOURCE), 1000000L));
final FilterPredicate destination0AndCount = and(gt(FilterApi.intColumn("count"), 10), eq(FilterApi.longColumn(ParquetStore.DESTINATION), 0L));
final FilterPredicate destination1000000AndCount = and(gt(FilterApi.intColumn("count"), 10), eq(FilterApi.longColumn(ParquetStore.DESTINATION), 1000000L));
final Path groupFolderPath = new Path(snapshotFolder, ParquetStore.getGroupSubDir(TestGroups.EDGE, false));
final Path reversedGroupFolderPath = new Path(snapshotFolder, ParquetStore.getGroupSubDir(TestGroups.EDGE, true));
// Partition 0, vertex 0L
final Path pathForPartitionFile1 = new Path(groupFolderPath, ParquetStore.getFile(0));
expected.add(new ParquetFileQuery(pathForPartitionFile1, source0AndCount, true));
// Partition 9, vertex 1000000L
final Path pathForPartitionFile2 = new Path(groupFolderPath, ParquetStore.getFile(9));
expected.add(new ParquetFileQuery(pathForPartitionFile2, source1000000AndCount, true));
// Partition 0 of reversed, vertex 0L
final Path pathForPartitionFile3 = new Path(reversedGroupFolderPath, ParquetStore.getFile(0));
expected.add(new ParquetFileQuery(pathForPartitionFile3, destination0AndCount, true));
// Partition 9 of reversed, vertex 1000000L
final Path pathForPartitionFile4 = new Path(reversedGroupFolderPath, ParquetStore.getFile(9));
expected.add(new ParquetFileQuery(pathForPartitionFile4, destination1000000AndCount, true));
assertThat(expected).containsOnly(query.getAllParquetFileQueries().toArray());
// When 4 - view with filter that can't be pushed down to Parquet, query for vertices 0 and 1000000
getElements = new GetElements.Builder().input(new EntitySeed(0L), new EntitySeed(1000000L)).seedMatching(SeedMatching.SeedMatchingType.RELATED).view(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsEvenFilter()).build()).build()).build()).build();
query = new QueryGenerator(store).getParquetQuery(getElements);
// Then 4
expected.clear();
// Partition 0, vertex 0L
expected.add(new ParquetFileQuery(pathForPartitionFile1, source0, false));
// Partition 9, vertex 1000000L
expected.add(new ParquetFileQuery(pathForPartitionFile2, source1000000, false));
// Partition 0 of reversed, vertex 0L
expected.add(new ParquetFileQuery(pathForPartitionFile3, destination0, false));
// Partition 9 of reversed, vertex 1000000L
expected.add(new ParquetFileQuery(pathForPartitionFile4, destination1000000, false));
assertThat(expected).containsOnly(query.getAllParquetFileQueries().toArray());
}
use of uk.gov.gchq.koryphe.impl.predicate.IsMoreThan in project Gaffer by gchq.
the class NamedViewResolverTest method shouldResolveNamedViewWithParametersToMakeCompleteFilter.
@Test
public void shouldResolveNamedViewWithParametersToMakeCompleteFilter() throws CacheOperationFailedException {
Map<String, Object> paramMap = Maps.newHashMap();
paramMap.put(IS_MORE_THAN_X_PARAM_KEY, 7L);
ViewParameterDetail param = new ViewParameterDetail.Builder().defaultValue(2L).description("more than filter").valueClass(Long.class).build();
Map<String, ViewParameterDetail> paramDetailMap = Maps.newHashMap();
paramDetailMap.put(IS_MORE_THAN_X_PARAM_KEY, param);
// Make a real View with a parameter
final View extendedView = new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(IdentifierType.VERTEX.name()).execute(new IsMoreThan("${" + IS_MORE_THAN_X_PARAM_KEY + "}")).build()).build()).build();
final NamedViewDetail extendedNamedViewDetail = new NamedViewDetail.Builder().name(NAMED_VIEW_NAME).view(extendedView).parameters(paramDetailMap).build();
given(CACHE.getNamedView(NAMED_VIEW_NAME, CONTEXT.getUser())).willReturn(extendedNamedViewDetail);
final OperationChain<?> opChain = new OperationChain.Builder().first(new GetElements.Builder().view(new NamedView.Builder().name(NAMED_VIEW_NAME).build()).build()).build();
// When
RESOLVER.preExecute(opChain, CONTEXT);
GetElements getElements = (GetElements) opChain.getOperations().get(0);
// Then
assertTrue(new String(getElements.getView().toCompactJson()).contains(VALUE_JSON_STRING + 2));
final OperationChain<?> opChain1 = new OperationChain.Builder().first(new GetElements.Builder().view(new NamedView.Builder().name(NAMED_VIEW_NAME).parameters(paramMap).build()).build()).build();
// When
RESOLVER.preExecute(opChain1, CONTEXT);
GetElements getElements1 = (GetElements) opChain1.getOperations().get(0);
// Then
assertTrue(new String(getElements1.getView().toCompactJson()).contains(VALUE_JSON_STRING + 7));
}
use of uk.gov.gchq.koryphe.impl.predicate.IsMoreThan in project Gaffer by gchq.
the class WhileHandlerTest method shouldUpdateTransformInputAndTestAgainstPredicate.
@Test
public void shouldUpdateTransformInputAndTestAgainstPredicate() throws OperationException {
final Edge input = new Edge.Builder().group("testEdge").source("src").dest("dest").directed(true).property("count", 3).build();
final Map<Element, Object> transform = mock(Map.class);
final Map<Element, Object> transformClone = mock(Map.class);
given(transform.shallowClone()).willReturn(transformClone);
final Predicate predicate = new IsMoreThan(2);
final Conditional conditional = new Conditional(predicate, transform);
final Context context = mock(Context.class);
final Store store = mock(Store.class);
final GetElements getElements = mock(GetElements.class);
final While operation = new While.Builder<>().input(input).maxRepeats(1).conditional(conditional).operation(getElements).build();
final WhileHandler handler = new WhileHandler();
// When
handler.doOperation(operation, context, store);
// Then
verify(transformClone).setInput(input);
verify(store).execute(transformClone, context);
}
use of uk.gov.gchq.koryphe.impl.predicate.IsMoreThan in project Gaffer by gchq.
the class WhileHandlerTest method shouldFailPredicateTestAndNotExecuteDelegateOperation.
@Test
public void shouldFailPredicateTestAndNotExecuteDelegateOperation() throws OperationException {
// Given
final Edge input = new Edge.Builder().group("testEdge").source("src").dest("dest").directed(true).property("count", 3).build();
final Map<Element, Object> transform = new Map.Builder<Element>().first(new ExtractProperty("count")).build();
final Predicate predicate = new IsMoreThan(5);
final Conditional conditional = new Conditional(predicate, transform);
final Context context = mock(Context.class);
final Store store = mock(Store.class);
final GetElements getElements = mock(GetElements.class);
final While operation = new While.Builder<>().input(input).maxRepeats(1).conditional(conditional).operation(getElements).build();
final WhileHandler handler = new WhileHandler();
// When
handler.doOperation(operation, context, store);
// Then
verify(store, never()).execute((Output) getElements, context);
}
use of uk.gov.gchq.koryphe.impl.predicate.IsMoreThan in project Gaffer by gchq.
the class FilterHandlerTest method shouldFailValidationWhenTypeArgumentOfPredicateIsIncorrect.
@Test
public void shouldFailValidationWhenTypeArgumentOfPredicateIsIncorrect() {
// Given
final Schema schema = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().source("junctionA").destination("junctionB").property(TestPropertyNames.COUNT, "count.long").build()).edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().source("junctionA").destination("junctionB").property(TestPropertyNames.COUNT, "count.long").build()).type("count.long", new TypeDefinition(Long.class)).build();
given(store.getSchema()).willReturn(schema);
final Edge edge = new Edge.Builder().group(TestGroups.EDGE).source("junctionA").dest("junctionB").property(TestPropertyNames.COUNT, 2L).build();
final Edge edge1 = new Edge.Builder().group(TestGroups.EDGE).source("junctionA").dest("junctionB").property(TestPropertyNames.COUNT, 1L).build();
input.add(edge);
input.add(edge1);
final Filter filter = new Filter.Builder().input(input).globalEdges(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsMoreThan("abcd")).build()).build();
assertThatExceptionOfType(OperationException.class).isThrownBy(() -> handler.doOperation(filter, context, store)).withMessageContaining("is not compatible with the input type:");
}
Aggregations