use of uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate in project Gaffer by gchq.
the class FiltersToOperationConverter method getFunctionsFromFilter.
/**
* Converts a Spark {@link Filter} to a map from group to a list of Gaffer {@link TupleAdaptedPredicate}s.
* <p>
* Note that Spark also applies all the filters provided to the {@code buildScan(String[], Filter[])} method
* so not implementing some of the provided {@link Filter}s in Gaffer will not cause errors. However, as many as
* possible should be implemented so that as much filtering as possible happens in iterators running in Accumulo's
* tablet servers (this avoids unnecessary data transfer from Accumulo to Spark).
*
* @param filter The {@link Filter} to transform.
* @return A map from {@link String} to {@link TupleAdaptedPredicate}s implementing the provided {@link Filter}.
*/
private Map<String, List<TupleAdaptedPredicate<String, ?>>> getFunctionsFromFilter(final Filter filter) {
final Map<String, List<TupleAdaptedPredicate<String, ?>>> map = new HashMap<>();
if (filter instanceof EqualTo) {
// Not dealt with as requires a Predicate<?> that returns null if either the controlValue or the
// test value is null - the API of Predicate<?> doesn't permit this.
} else if (filter instanceof EqualNullSafe) {
final EqualNullSafe equalNullSafe = (EqualNullSafe) filter;
final Predicate<?> isEqual = new IsEqual(equalNullSafe.value());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (null != relevantGroups) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<>());
}
map.get(group).add(new TupleAdaptedPredicate<>(isEqual, new String[] { equalNullSafe.attribute() }));
}
}
LOGGER.debug("Converted {} to IsEqual ({})", filter, equalNullSafe.attribute());
} else if (filter instanceof GreaterThan) {
final GreaterThan greaterThan = (GreaterThan) filter;
final Predicate<?> isMoreThan = new IsMoreThan((Comparable<?>) greaterThan.value(), false);
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (null != relevantGroups) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<>());
}
map.get(group).add(new TupleAdaptedPredicate<>(isMoreThan, new String[] { greaterThan.attribute() }));
}
}
LOGGER.debug("Converted {} to isMoreThan ({})", filter, greaterThan.attribute());
} else if (filter instanceof GreaterThanOrEqual) {
final GreaterThanOrEqual greaterThan = (GreaterThanOrEqual) filter;
final Predicate<?> isMoreThan = new IsMoreThan((Comparable<?>) greaterThan.value(), true);
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (null != relevantGroups) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<>());
}
map.get(group).add(new TupleAdaptedPredicate<>(isMoreThan, new String[] { greaterThan.attribute() }));
}
}
LOGGER.debug("Converted {} to IsMoreThan ({})", filter, greaterThan.attribute());
} else if (filter instanceof LessThan) {
final LessThan lessThan = (LessThan) filter;
final Predicate<?> isLessThan = new IsLessThan((Comparable<?>) lessThan.value(), false);
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (null != relevantGroups) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<>());
}
map.get(group).add(new TupleAdaptedPredicate<>(isLessThan, new String[] { lessThan.attribute() }));
}
}
LOGGER.debug("Converted {} to IsLessThan ({})", filter, lessThan.attribute());
} else if (filter instanceof LessThanOrEqual) {
final LessThanOrEqual lessThan = (LessThanOrEqual) filter;
final Predicate<?> isLessThan = new IsLessThan((Comparable<?>) lessThan.value(), true);
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (null != relevantGroups) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<>());
}
map.get(group).add(new TupleAdaptedPredicate<>(isLessThan, new String[] { lessThan.attribute() }));
}
}
LOGGER.debug("Converted {} to LessThanOrEqual ({})", filter, lessThan.attribute());
} else if (filter instanceof In) {
final In in = (In) filter;
final Predicate<?> isIn = new IsIn(new HashSet<>(Arrays.asList(in.values())));
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (null != relevantGroups) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<>());
}
map.get(group).add(new TupleAdaptedPredicate<>(isIn, new String[] { in.attribute() }));
}
}
LOGGER.debug("Converted {} to IsIn ({})", filter, in.attribute());
} else if (filter instanceof IsNull) {
final IsNull isNull = (IsNull) filter;
final Predicate<?> doesntExist = new Not<>(new Exists());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (null != relevantGroups) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<>());
}
map.get(group).add(new TupleAdaptedPredicate<>(doesntExist, new String[] { isNull.attribute() }));
}
}
LOGGER.debug("Converted {} to Not(Exists) ({})", filter, isNull.attribute());
} else if (filter instanceof IsNotNull) {
final IsNotNull isNotNull = (IsNotNull) filter;
final Predicate<?> exists = new Exists();
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (null != relevantGroups) {
for (final String group : relevantGroups) {
if (!map.containsKey(group)) {
map.put(group, new ArrayList<>());
}
map.get(group).add(new TupleAdaptedPredicate<>(exists, new String[] { isNotNull.attribute() }));
}
}
LOGGER.debug("Converted {} to Exists ({})", filter, isNotNull.attribute());
} else if (filter instanceof And) {
final And and = (And) filter;
final Map<String, List<TupleAdaptedPredicate<String, ?>>> left = getFunctionsFromFilter(and.left());
final Map<String, List<TupleAdaptedPredicate<String, ?>>> right = getFunctionsFromFilter(and.right());
final Set<String> relevantGroups = getGroupsFromFilter(filter);
if (null != relevantGroups) {
for (final String group : relevantGroups) {
final List<TupleAdaptedPredicate<String, ?>> concatFilters = new ArrayList<>();
if (null != left.get(group)) {
concatFilters.addAll(left.get(group));
}
if (null != right.get(group)) {
concatFilters.addAll(right.get(group));
}
if (!map.containsKey(group)) {
map.put(group, new ArrayList<>());
}
map.get(group).addAll(concatFilters);
}
}
LOGGER.debug("Converted {} to list of filters ({})", filter, StringUtils.join(map.entrySet(), ','));
}
return map;
}
use of uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate in project Gaffer by gchq.
the class QueryGenerator method getPredicateFromView.
// TODO raise issue saying that could optimise so that only the filters that have not been fully applied
// are reapplied, and it should be able to return the fact that all filters have been applied
// Either the result is:
// - a filter and that fully applies the view
// - a filter and that doesn't fully apply the view
// - no filter and that doesn't fully apply the view
// - no filter and that fully applies the view
// Boolean indicates whether logic was fully applied
private Pair<FilterPredicate, Boolean> getPredicateFromView(final View view, final String group, final boolean isEntityGroup) throws SerialisationException, OperationException {
if (null == view) {
return new Pair<>(null, true);
}
final ViewElementDefinition ved = view.getElement(group);
FilterPredicate filterPredicate = null;
boolean fullyAppliedInAll = true;
if (null != ved) {
List<TupleAdaptedPredicate<String, ?>> preAggFilterFunctions = ved.getPreAggregationFilterFunctions();
if (null != preAggFilterFunctions) {
for (final TupleAdaptedPredicate<String, ?> filterFunctionContext : preAggFilterFunctions) {
final JavaPredicateToParquetPredicate predicateConverter = new JavaPredicateToParquetPredicate(schemaUtils, filterFunctionContext.getPredicate(), filterFunctionContext.getSelection(), group);
filterPredicate = FilterPredicateUtils.and(filterPredicate, predicateConverter.getParquetPredicate());
if (!predicateConverter.isFullyApplied()) {
fullyAppliedInAll = false;
}
}
}
}
return new Pair<>(filterPredicate, fullyAppliedInAll);
}
use of uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate in project Gaffer by gchq.
the class JavaPredicateToParquetPredicate method getOrFilter.
public FilterPredicate getOrFilter(final List<Predicate> predicateList, final String[] selection, final String group, final SchemaUtils schemaUtils) throws SerialisationException {
FilterPredicate combinedFilter = null;
for (final Predicate predicate : predicateList) {
final Predicate filterFunction;
final String[] newSelection;
if (predicate instanceof TupleAdaptedPredicate) {
filterFunction = ((TupleAdaptedPredicate) predicate).getPredicate();
// Build new selections
final Integer[] ints = (Integer[]) ((TupleAdaptedPredicate) predicate).getSelection();
newSelection = new String[ints.length];
for (int x = 0; x < ints.length; x++) {
newSelection[x] = selection[ints[x]];
}
} else {
filterFunction = predicate;
newSelection = selection;
}
final JavaPredicateToParquetPredicate predicateConverter = new JavaPredicateToParquetPredicate(schemaUtils, filterFunction, newSelection, group);
final FilterPredicate parquetPredicate = predicateConverter.getParquetPredicate();
if (!predicateConverter.fullyApplied) {
fullyApplied = false;
}
combinedFilter = FilterPredicateUtils.or(combinedFilter, parquetPredicate);
}
return combinedFilter;
}
use of uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate in project Gaffer by gchq.
the class JavaPredicateToParquetPredicate method getAndFilter.
public FilterPredicate getAndFilter(final List<Predicate> predicateList, final String[] selection, final String group, final SchemaUtils schemaUtils) throws SerialisationException {
FilterPredicate combinedFilter = null;
for (final Predicate predicate : predicateList) {
final Predicate filterFunction;
final String[] newSelection;
if (predicate instanceof TupleAdaptedPredicate) {
filterFunction = ((TupleAdaptedPredicate) predicate).getPredicate();
// Build new selections
final Integer[] ints = (Integer[]) ((TupleAdaptedPredicate) predicate).getSelection();
newSelection = new String[ints.length];
for (int x = 0; x < ints.length; x++) {
newSelection[x] = selection[ints[x]];
}
} else {
filterFunction = predicate;
newSelection = selection;
}
final JavaPredicateToParquetPredicate predicateConverter = new JavaPredicateToParquetPredicate(schemaUtils, filterFunction, newSelection, group);
final FilterPredicate parquetPredicate = predicateConverter.getParquetPredicate();
if (!predicateConverter.fullyApplied) {
fullyApplied = false;
}
combinedFilter = FilterPredicateUtils.and(combinedFilter, parquetPredicate);
}
return combinedFilter;
}
use of uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate in project Gaffer by gchq.
the class ViewElementDefinitionTest method shouldJsonSerialiseAndDeserialise.
@Test
public void shouldJsonSerialiseAndDeserialise() throws SerialisationException {
// Given
final ViewElementDefinition elementDef = new ViewElementDefinition.Builder().transientProperty(TestPropertyNames.PROP_1, String.class).transientProperty(TestPropertyNames.PROP_2, String.class).properties(TestPropertyNames.COUNT, TestPropertyNames.DATE).preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsMoreThan(5)).build()).aggregator(new ElementAggregator.Builder().select(TestPropertyNames.COUNT).execute(new Max()).build()).postAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsLessThan(10)).build()).transformer(new ElementTransformer.Builder().select(TestPropertyNames.COUNT).execute(new TestTransform()).project(TestPropertyNames.PROP_1).build()).postTransformFilter(new ElementFilter.Builder().select(TestPropertyNames.PROP_1).execute(new IsEqual("9")).build()).build();
// When
final byte[] json = JSONSerialiser.serialise(elementDef, true);
final ViewElementDefinition deserialisedElementDef = JSONSerialiser.deserialise(json, ViewElementDefinition.class);
assertEquals(Sets.newHashSet(TestPropertyNames.COUNT, TestPropertyNames.DATE), deserialisedElementDef.getProperties());
assertNull(deserialisedElementDef.getExcludeProperties());
final List<TupleAdaptedPredicate<String, ?>> preFilterComponents = deserialisedElementDef.getPreAggregationFilter().getComponents();
assertThat(preFilterComponents).hasSize(1);
assertArrayEquals(new String[] { TestPropertyNames.COUNT }, preFilterComponents.get(0).getSelection());
assertEquals(new IsMoreThan(5), preFilterComponents.get(0).getPredicate());
final List<TupleAdaptedBinaryOperator<String, ?>> aggComponents = deserialisedElementDef.getAggregator().getComponents();
assertThat(aggComponents).hasSize(1);
assertArrayEquals(new String[] { TestPropertyNames.COUNT }, aggComponents.get(0).getSelection());
assertEquals(new Max(), aggComponents.get(0).getBinaryOperator());
final List<TupleAdaptedPredicate<String, ?>> postFilterComponents = deserialisedElementDef.getPostAggregationFilter().getComponents();
assertThat(postFilterComponents).hasSize(1);
assertArrayEquals(new String[] { TestPropertyNames.COUNT }, postFilterComponents.get(0).getSelection());
assertEquals(new IsLessThan(10), postFilterComponents.get(0).getPredicate());
final List<TupleAdaptedFunction<String, ?, ?>> transformComponents = deserialisedElementDef.getTransformer().getComponents();
assertThat(transformComponents).hasSize(1);
assertArrayEquals(new String[] { TestPropertyNames.COUNT }, transformComponents.get(0).getSelection());
assertEquals(new TestTransform(), transformComponents.get(0).getFunction());
assertArrayEquals(new String[] { TestPropertyNames.PROP_1 }, transformComponents.get(0).getProjection());
final List<TupleAdaptedPredicate<String, ?>> postTransformFilterComponents = deserialisedElementDef.getPostTransformFilter().getComponents();
assertThat(postTransformFilterComponents).hasSize(1);
assertArrayEquals(new String[] { TestPropertyNames.PROP_1 }, postTransformFilterComponents.get(0).getSelection());
assertEquals(new IsEqual("9"), postTransformFilterComponents.get(0).getPredicate());
}
Aggregations