Search in sources :

Example 6 with EqualTo

use of org.apache.spark.sql.sources.EqualTo in project Gaffer by gchq.

the class FilterToOperationConverterTest method testSpecifyVertex.

@Test
public void testSpecifyVertex() throws OperationException {
    final Schema schema = getSchema();
    final SQLContext sqlContext = getSqlContext("testSpecifyVertex");
    final Filter[] filters = new Filter[1];
    filters[0] = new EqualTo(SchemaToStructTypeConverter.VERTEX_COL_NAME, "0");
    final FiltersToOperationConverter converter = new FiltersToOperationConverter(sqlContext, getViewFromSchema(schema), schema, filters);
    final AbstractGetRDD<?> operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfElements);
    assertEquals(Collections.singleton(ENTITY_GROUP), operation.getView().getEntityGroups());
    assertEquals(0, operation.getView().getEdgeGroups().size());
    final Set<EntitySeed> seeds = new HashSet<>();
    for (final Object seed : ((GetRDDOfElements) operation).getSeeds()) {
        seeds.add((EntitySeed) seed);
    }
    assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
    sqlContext.sparkContext().stop();
}
Also used : Filter(org.apache.spark.sql.sources.Filter) Schema(uk.gov.gchq.gaffer.store.schema.Schema) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) SQLContext(org.apache.spark.sql.SQLContext) EqualTo(org.apache.spark.sql.sources.EqualTo) GetRDDOfElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfElements) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with EqualTo

use of org.apache.spark.sql.sources.EqualTo in project Gaffer by gchq.

the class FiltersToOperationConverter method applyVertexSourceDestinationFilters.

private AbstractGetRDD<?> applyVertexSourceDestinationFilters(final View view) {
    View clonedView = view.clone();
    AbstractGetRDD<?> operation = null;
    for (final Filter filter : filters) {
        if (filter instanceof EqualTo) {
            final EqualTo equalTo = (EqualTo) filter;
            final String attribute = equalTo.attribute();
            if (attribute.equals(SchemaToStructTypeConverter.VERTEX_COL_NAME)) {
                // Only entities are relevant, so remove any edge groups from the view
                LOGGER.info("Found EqualTo filter with attribute {}, setting views to only contain entity groups", attribute);
                View.Builder viewBuilder = new View.Builder();
                for (final String entityGroup : view.getEntityGroups()) {
                    viewBuilder = viewBuilder.entity(entityGroup);
                }
                clonedView = viewBuilder.build();
                LOGGER.info("Setting operation to GetRDDOfElements");
                operation = new GetRDDOfElements<>(sqlContext.sparkContext(), new EntitySeed(equalTo.value()));
                operation.setView(clonedView);
                break;
            } else if (attribute.equals(SchemaToStructTypeConverter.SRC_COL_NAME) || attribute.equals(SchemaToStructTypeConverter.DST_COL_NAME)) {
                // Only edges are relevant, so remove any entity groups from the view
                LOGGER.info("Found EqualTo filter with attribute {}, setting views to only contain edge groups", attribute);
                View.Builder viewBuilder = new View.Builder();
                for (final String edgeGroup : view.getEdgeGroups()) {
                    viewBuilder = viewBuilder.edge(edgeGroup);
                }
                clonedView = viewBuilder.build();
                LOGGER.info("Setting operation to GetRDDOfElements");
                operation = new GetRDDOfElements<>(sqlContext.sparkContext(), new EntitySeed(equalTo.value()));
                operation.setView(clonedView);
                break;
            }
        }
    }
    if (operation == null) {
        LOGGER.debug("Setting operation to GetRDDOfAllElements");
        operation = new GetRDDOfAllElements(sqlContext.sparkContext());
        operation.setView(clonedView);
    }
    return operation;
}
Also used : Filter(org.apache.spark.sql.sources.Filter) GetRDDOfAllElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfAllElements) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) EqualTo(org.apache.spark.sql.sources.EqualTo) GetRDDOfElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfElements)

Example 8 with EqualTo

use of org.apache.spark.sql.sources.EqualTo in project Gaffer by gchq.

the class FiltersToOperationConverter method checkForGroups.

/**
     * Returns the set of all groups in the filter, if the filter specifies that the group must be equal to a certain
     * value.
     *
     * @param filter The {@link Filter} that will be checked for groups.
     * @return A set of strings containing the required groups, <code>null</code> if no groups are specified in the
     * filter.
     */
private Set<String> checkForGroups(final Filter filter) {
    if (filter instanceof EqualTo) {
        final EqualTo equalTo = (EqualTo) filter;
        if (equalTo.attribute().equals(SchemaToStructTypeConverter.GROUP)) {
            LOGGER.info("Filter {} specifies that {} should be {}", filter, SchemaToStructTypeConverter.GROUP, equalTo.value());
            return Collections.singleton((String) equalTo.value());
        }
    } else if (filter instanceof Or) {
        final Or or = (Or) filter;
        if (or.left() instanceof EqualTo && or.right() instanceof EqualTo && ((EqualTo) or.left()).attribute().equals(SchemaToStructTypeConverter.GROUP) && ((EqualTo) or.right()).attribute().equals(SchemaToStructTypeConverter.GROUP)) {
            final Set<String> groups = new HashSet<>();
            groups.add((String) ((EqualTo) or.left()).value());
            groups.add((String) ((EqualTo) or.right()).value());
            LOGGER.info("Filter {} specifies that {} should be {} or {}", filter, SchemaToStructTypeConverter.GROUP, ((EqualTo) or.left()).value(), ((EqualTo) or.right()).value());
            return groups;
        }
    } else if (filter instanceof In) {
        final In in = (In) filter;
        if (in.attribute().equals(SchemaToStructTypeConverter.GROUP)) {
            final Set<String> groups = new HashSet<>();
            for (final Object o : in.values()) {
                groups.add((String) o);
            }
            LOGGER.info("Filter {} specifies that {} should be in {}", filter, SchemaToStructTypeConverter.GROUP, StringUtils.join(in.values(), ','));
            return groups;
        }
    }
    return null;
}
Also used : Or(org.apache.spark.sql.sources.Or) HashSet(java.util.HashSet) Set(java.util.Set) IsIn(uk.gov.gchq.gaffer.function.filter.IsIn) In(org.apache.spark.sql.sources.In) EqualTo(org.apache.spark.sql.sources.EqualTo) HashSet(java.util.HashSet)

Example 9 with EqualTo

use of org.apache.spark.sql.sources.EqualTo in project Gaffer by gchq.

the class FilterToOperationConverterTest method testSpecifySource.

@Test
public void testSpecifySource() throws OperationException {
    final Schema schema = getSchema();
    final SQLContext sqlContext = getSqlContext("testSpecifySource");
    final Filter[] filters = new Filter[1];
    filters[0] = new EqualTo(SchemaToStructTypeConverter.SRC_COL_NAME, "0");
    FiltersToOperationConverter converter = new FiltersToOperationConverter(sqlContext, getViewFromSchema(schema), schema, filters);
    AbstractGetRDD<?> operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfElements);
    assertEquals(0, operation.getView().getEntityGroups().size());
    assertEquals(EDGE_GROUPS, operation.getView().getEdgeGroups());
    final Set<EntitySeed> seeds = new HashSet<>();
    for (final Object seed : ((GetRDDOfElements) operation).getSeeds()) {
        seeds.add((EntitySeed) seed);
    }
    assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
    sqlContext.sparkContext().stop();
}
Also used : Filter(org.apache.spark.sql.sources.Filter) Schema(uk.gov.gchq.gaffer.store.schema.Schema) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) SQLContext(org.apache.spark.sql.SQLContext) EqualTo(org.apache.spark.sql.sources.EqualTo) GetRDDOfElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfElements) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with EqualTo

use of org.apache.spark.sql.sources.EqualTo in project Gaffer by gchq.

the class FilterToOperationConverterTest method testIncompatibleGroups.

@Test
public void testIncompatibleGroups() throws OperationException {
    final Schema schema = getSchema();
    final SQLContext sqlContext = getSqlContext("testIncompatibleGroups");
    final Filter[] filters = new Filter[2];
    filters[0] = new EqualTo(SchemaToStructTypeConverter.GROUP, "A");
    filters[1] = new EqualTo(SchemaToStructTypeConverter.GROUP, "B");
    final FiltersToOperationConverter converter = new FiltersToOperationConverter(sqlContext, getViewFromSchema(schema), schema, filters);
    final AbstractGetRDD<?> operation = converter.getOperation();
    assertNull(operation);
    sqlContext.sparkContext().stop();
}
Also used : Filter(org.apache.spark.sql.sources.Filter) Schema(uk.gov.gchq.gaffer.store.schema.Schema) SQLContext(org.apache.spark.sql.SQLContext) EqualTo(org.apache.spark.sql.sources.EqualTo) Test(org.junit.Test)

Aggregations

EqualTo (org.apache.spark.sql.sources.EqualTo)12 Filter (org.apache.spark.sql.sources.Filter)10 SQLContext (org.apache.spark.sql.SQLContext)9 Test (org.junit.Test)9 Schema (uk.gov.gchq.gaffer.store.schema.Schema)9 HashSet (java.util.HashSet)7 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)6 GetRDDOfElements (uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfElements)6 ArrayList (java.util.ArrayList)3 GreaterThan (org.apache.spark.sql.sources.GreaterThan)3 LessThan (org.apache.spark.sql.sources.LessThan)3 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)3 ConsumerFunctionContext (uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext)3 IsLessThan (uk.gov.gchq.gaffer.function.filter.IsLessThan)3 IsMoreThan (uk.gov.gchq.gaffer.function.filter.IsMoreThan)3 GetRDDOfAllElements (uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfAllElements)3 Set (java.util.Set)2 In (org.apache.spark.sql.sources.In)2 Or (org.apache.spark.sql.sources.Or)2 FilterFunction (uk.gov.gchq.gaffer.function.FilterFunction)2