Search in sources :

Example 26 with IsMoreThan

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());
}
Also used : ParquetStore(uk.gov.gchq.gaffer.parquetstore.ParquetStore) Path(org.apache.hadoop.fs.Path) ArrayList(java.util.ArrayList) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) SchemaUtils(uk.gov.gchq.gaffer.parquetstore.utils.SchemaUtils) ParquetStoreProperties(uk.gov.gchq.gaffer.parquetstore.ParquetStoreProperties) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) ArrayList(java.util.ArrayList) List(java.util.List) FilterPredicate(org.apache.parquet.filter2.predicate.FilterPredicate) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) CalculatePartitionerTest(uk.gov.gchq.gaffer.parquetstore.operation.handler.utilities.CalculatePartitionerTest) LongVertexOperationsTest(uk.gov.gchq.gaffer.parquetstore.operation.handler.LongVertexOperationsTest) Test(org.junit.jupiter.api.Test)

Example 27 with IsMoreThan

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));
}
Also used : ViewParameterDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Test(org.junit.jupiter.api.Test)

Example 28 with IsMoreThan

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);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Element(uk.gov.gchq.gaffer.data.element.Element) Store(uk.gov.gchq.gaffer.store.Store) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) While(uk.gov.gchq.gaffer.operation.impl.While) Predicate(java.util.function.Predicate) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 29 with IsMoreThan

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);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Element(uk.gov.gchq.gaffer.data.element.Element) Store(uk.gov.gchq.gaffer.store.Store) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) While(uk.gov.gchq.gaffer.operation.impl.While) Predicate(java.util.function.Predicate) ExtractProperty(uk.gov.gchq.gaffer.data.element.function.ExtractProperty) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 30 with IsMoreThan

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:");
}
Also used : Filter(uk.gov.gchq.gaffer.operation.impl.function.Filter) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)72 Test (org.junit.jupiter.api.Test)42 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)40 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)34 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)24 Element (uk.gov.gchq.gaffer.data.element.Element)22 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)22 Edge (uk.gov.gchq.gaffer.data.element.Edge)20 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)20 User (uk.gov.gchq.gaffer.user.User)20 Graph (uk.gov.gchq.gaffer.graph.Graph)19 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)19 ArrayList (java.util.ArrayList)18 HashSet (java.util.HashSet)16 Entity (uk.gov.gchq.gaffer.data.element.Entity)16 IsEqual (uk.gov.gchq.koryphe.impl.predicate.IsEqual)13 IsLessThan (uk.gov.gchq.koryphe.impl.predicate.IsLessThan)13 Set (java.util.Set)12 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)12 OperationException (uk.gov.gchq.gaffer.operation.OperationException)12