use of org.openrdf.query.algebra.Compare in project incubator-rya by apache.
the class AggregationPipelineQueryNodeTest method testFilter.
@Test
public void testFilter() {
final AggregationPipelineQueryNode base = new AggregationPipelineQueryNode(collection, new LinkedList<>(), Sets.newHashSet("x", "y"), Sets.newHashSet("x", "y", "opt"), HashBiMap.create());
// Extend with a supported filter
AggregationPipelineQueryNode node = base.clone();
boolean success = node.filter(new Compare(new Var("x"), new Var("y"), Compare.CompareOp.EQ));
Assert.assertTrue(success);
Assert.assertEquals(Sets.newHashSet("x", "y", "opt"), node.getBindingNames());
Assert.assertEquals(Sets.newHashSet("x", "y"), node.getAssuredBindingNames());
Assert.assertEquals(3, node.getPipeline().size());
// Extend with an unsupported filter
node = base.clone();
success = node.filter(new IsLiteral(new Var("opt")));
Assert.assertFalse(success);
Assert.assertEquals(Sets.newHashSet("x", "y", "opt"), node.getBindingNames());
Assert.assertEquals(Sets.newHashSet("x", "y"), node.getAssuredBindingNames());
Assert.assertEquals(0, node.getPipeline().size());
}
use of org.openrdf.query.algebra.Compare in project incubator-rya by apache.
the class AggregationPipelineQueryNode method filter.
/**
* Add a SPARQL filter to the pipeline, if possible. A filter eliminates
* results that don't satisfy a given condition. Not all conditional
* expressions are supported. If unsupported expressions are used in the
* filter, the pipeline will remain unchanged and this method will return
* false. Currently only supports binary {@link Compare} conditions among
* variables and/or literals.
* @param condition The filter condition
* @return True if the filter was successfully converted into a pipeline
* step, false otherwise.
*/
public boolean filter(ValueExpr condition) {
if (condition instanceof Compare) {
Compare compare = (Compare) condition;
Compare.CompareOp operator = compare.getOperator();
Object leftArg = valueFieldExpr(compare.getLeftArg());
Object rightArg = valueFieldExpr(compare.getRightArg());
if (leftArg == null || rightArg == null) {
// unsupported value expression, can't convert filter
return false;
}
final String opFunc;
switch(operator) {
case EQ:
opFunc = "$eq";
break;
case NE:
opFunc = "$ne";
break;
case LT:
opFunc = "$lt";
break;
case LE:
opFunc = "$le";
break;
case GT:
opFunc = "$gt";
break;
case GE:
opFunc = "$ge";
break;
default:
// unrecognized comparison operator, can't convert filter
return false;
}
Document compareDoc = new Document(opFunc, Arrays.asList(leftArg, rightArg));
pipeline.add(Aggregates.project(Projections.fields(Projections.computed("FILTER", compareDoc), Projections.include(VALUES, HASHES, TYPES, LEVEL, TIMESTAMP))));
pipeline.add(Aggregates.match(new Document("FILTER", true)));
pipeline.add(Aggregates.project(Projections.fields(Projections.include(VALUES, HASHES, TYPES, LEVEL, TIMESTAMP))));
return true;
}
return false;
}
use of org.openrdf.query.algebra.Compare in project backstage by zepheira.
the class ListFacet method createRestrictionClause.
protected ValueExpr createRestrictionClause(TupleQueryBuilder builder, String valueAsString, String valueType, ValueExpr input, ValueExpr previousClauses) {
Value value = stringToValue(valueAsString);
Compare compare = new Compare(input, builder.makeVar("v", value), CompareOp.EQ);
return previousClauses == null ? compare : new Or(previousClauses, compare);
}
Aggregations