org.eclipse.rdf4j.query.algebra.Slice Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.Slice. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the limit of the current variable bindings before any further projection.
 */
private static long getLimit(QueryModelNode node) {
    long offset = 0;
    if (node instanceof Slice) {
        Slice slice = (Slice) node;
        if (slice.hasOffset() && slice.hasLimit()) {
            return slice.getOffset() + slice.getLimit();
        } else if (slice.hasLimit()) {
            return slice.getLimit();
        } else if (slice.hasOffset()) {
            offset = slice.getOffset();
        }
    }
    QueryModelNode parent = node.getParentNode();
    if (parent instanceof Distinct || parent instanceof Reduced || parent instanceof Slice) {
        long limit = getLimit(parent);
        if (offset > 0L && limit < Long.MAX_VALUE) {
            return offset + limit;
        } else {
            return limit;
        }
    }
    return Long.MAX_VALUE;
}
 
Example #2
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate {@link Slice} query model nodes.
 * @param parent
 * @param slice
 * @param bindings
 */
private void evaluateSlice(BindingSetPipe parent, Slice slice, BindingSet bindings) {
    final long offset = slice.hasOffset() ? slice.getOffset() : 0;
    final long limit = slice.hasLimit() ? offset + slice.getLimit() : Long.MAX_VALUE;
    evaluateTupleExpr(new BindingSetPipe(parent) {
        private final AtomicLong ll = new AtomicLong(0);
        @Override
        public boolean push(BindingSet bs) throws InterruptedException {
            long l = ll.incrementAndGet();
            if (l > limit+1) {
                return false;
            }
            if (bs == null) return parent.push(null);
            if (l <= offset) {
                return true;
            } else if (l <= limit) {
                return parent.push(bs);
            } else {
                return parent.push(null);
            }
        }
    }, slice.getArg(), bindings);
}
 
Example #3
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSES1927UnequalLiteralValueConstants2() throws Exception {

	StringBuilder qb = new StringBuilder();
	qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"^^<foo:bar> .} ");

	ParsedQuery q = parser.parseQuery(qb.toString(), null);
	TupleExpr te = q.getTupleExpr();

	assertNotNull(te);

	assertTrue(te instanceof Slice);
	Slice s = (Slice) te;
	assertTrue(s.getArg() instanceof Join);
	Join j = (Join) s.getArg();

	assertTrue(j.getLeftArg() instanceof StatementPattern);
	assertTrue(j.getRightArg() instanceof StatementPattern);
	StatementPattern leftArg = (StatementPattern) j.getLeftArg();
	StatementPattern rightArg = (StatementPattern) j.getRightArg();

	assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar()));
	assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName());
}
 
Example #4
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns the limit of the current variable bindings before any further projection.
 */
protected long getLimit(QueryModelNode node) {
	long offset = 0;
	if (node instanceof Slice) {
		Slice slice = (Slice) node;
		if (slice.hasOffset() && slice.hasLimit()) {
			return slice.getOffset() + slice.getLimit();
		} else if (slice.hasLimit()) {
			return slice.getLimit();
		} else if (slice.hasOffset()) {
			offset = slice.getOffset();
		}
	}
	QueryModelNode parent = node.getParentNode();
	if (parent instanceof Distinct || parent instanceof Reduced || parent instanceof Slice) {
		long limit = getLimit(parent);
		if (offset > 0L && limit < Long.MAX_VALUE) {
			return offset + limit;
		} else {
			return limit;
		}
	}
	return Long.MAX_VALUE;
}
 
Example #5
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSES1927UnequalLiteralValueConstants1() throws Exception {

	StringBuilder qb = new StringBuilder();
	qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"@en .} ");

	ParsedQuery q = parser.parseQuery(qb.toString(), null);
	TupleExpr te = q.getTupleExpr();

	assertNotNull(te);

	assertTrue(te instanceof Slice);
	Slice s = (Slice) te;
	assertTrue(s.getArg() instanceof Join);
	Join j = (Join) s.getArg();

	assertTrue(j.getLeftArg() instanceof StatementPattern);
	assertTrue(j.getRightArg() instanceof StatementPattern);
	StatementPattern leftArg = (StatementPattern) j.getLeftArg();
	StatementPattern rightArg = (StatementPattern) j.getRightArg();

	assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar()));
	assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName());
}
 
Example #6
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSES1922PathSequenceWithValueConstant() throws Exception {

	StringBuilder qb = new StringBuilder();
	qb.append("ASK {?A (<foo:bar>)/<foo:foo> <foo:objValue>} ");

	ParsedQuery q = parser.parseQuery(qb.toString(), null);
	TupleExpr te = q.getTupleExpr();

	assertNotNull(te);

	assertTrue(te instanceof Slice);
	Slice s = (Slice) te;
	assertTrue(s.getArg() instanceof Join);
	Join j = (Join) s.getArg();

	assertTrue(j.getLeftArg() instanceof StatementPattern);
	assertTrue(j.getRightArg() instanceof StatementPattern);
	StatementPattern leftArg = (StatementPattern) j.getLeftArg();
	StatementPattern rightArg = (StatementPattern) j.getRightArg();

	assertTrue(leftArg.getObjectVar().equals(rightArg.getSubjectVar()));
	assertEquals(leftArg.getObjectVar().getName(), rightArg.getSubjectVar().getName());
}
 
Example #7
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testParsedBooleanQueryRootNode() throws Exception {
	StringBuilder qb = new StringBuilder();
	qb.append("ASK {?a <foo:bar> \"test\"}");

	ParsedBooleanQuery q = (ParsedBooleanQuery) parser.parseQuery(qb.toString(), null);
	TupleExpr te = q.getTupleExpr();

	assertNotNull(te);
	assertTrue(te instanceof Slice);
	assertNull(te.getParentNode());
}
 
Example #8
Source File: SparqlToPigTransformVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(Slice node) throws RuntimeException {
    super.meet(node);
    long limit = node.getLimit();
    //PROJ = LIMIT PROJ 10;
    pigScriptBuilder.append("PROJ = LIMIT PROJ ").append(limit).append(";\n");
}
 
Example #9
Source File: ParallelEvaluationStrategyImpl.java    From rya with Apache License 2.0 5 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Slice slice, BindingSet bindings)
        throws QueryEvaluationException {
    CloseableIteration<BindingSet, QueryEvaluationException> result = evaluate(slice.getArg(), bindings);
    if (slice.hasOffset()) {
        result = new OffsetIteration<BindingSet, QueryEvaluationException>(result, slice.getOffset());
    }
    if (slice.hasLimit()) {
        result = new LimitIteration<BindingSet, QueryEvaluationException>(result, slice.getLimit());
    }
    return result;
}
 
Example #10
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the parent of the node is an instance of {@link Distinct} or {@link Reduced}.
 * @param node the {@link QueryModelNode} to test
 * @return {@code true} if the parent is and instance of {@link Distinct} or {@link Reduced} and {@code false} otherwise. If the parent is
 * an instance of {@link Slice} then the parent is considered to be the first non-{@code Slice} node up the tree.
 */
private static boolean isReducedOrDistinct(QueryModelNode node) {
    QueryModelNode parent = node.getParentNode();
    if (parent instanceof Slice) {
        return isReducedOrDistinct(parent);
    }
    return parent instanceof Distinct || parent instanceof Reduced;
}
 
Example #11
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Switch logic for evaluation of any instance of a {@link UnaryTupleOperator} query model node
 * @param parent
 * @param expr
 * @param bindings
 */
private void evaluateUnaryTupleOperator(BindingSetPipe parent, UnaryTupleOperator expr, BindingSet bindings) {
    if (expr instanceof Projection) {
        evaluateProjection(parent, (Projection) expr, bindings);
    } else if (expr instanceof MultiProjection) {
        evaluateMultiProjection(parent, (MultiProjection) expr, bindings);
    } else if (expr instanceof Filter) {
        evaluateFilter(parent, (Filter) expr, bindings);
    } else if (expr instanceof Service) {
        evaluateService(parent, (Service) expr, bindings);
    } else if (expr instanceof Slice) {
        evaluateSlice(parent, (Slice) expr, bindings);
    } else if (expr instanceof Extension) {
        evaluateExtension(parent, (Extension) expr, bindings);
    } else if (expr instanceof Distinct) {
        evaluateDistinct(parent, (Distinct) expr, bindings);
    } else if (expr instanceof Reduced) {
        evaluateReduced(parent, (Reduced) expr, bindings);
    } else if (expr instanceof Group) {
        evaluateGroup(parent, (Group) expr, bindings);
    } else if (expr instanceof Order) {
        evaluateOrder(parent, (Order) expr, bindings);
    } else if (expr instanceof QueryRoot) {
        parentStrategy.sharedValueOfNow = null;
        evaluateTupleExpr(parent, ((QueryRoot) expr).getArg(), bindings);
    } else if (expr instanceof DescribeOperator) {
        evaluateDescribeOperator(parent, (DescribeOperator) expr, bindings);
    } else if (expr == null) {
        parent.handleException(new IllegalArgumentException("expr must not be null"));
    } else {
        parent.handleException(new QueryEvaluationException("Unknown unary tuple operator type: " + expr.getClass()));
    }
}
 
Example #12
Source File: TupleExprBuilderTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNegatedPathWithFixedObject() {
	String query = "ASK WHERE { ?s !<http://example.org/p> <http://example.org/o> . }";

	try {
		TupleExprBuilder builder = new TupleExprBuilder(SimpleValueFactory.getInstance());
		ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(query);
		TupleExpr result = builder.visit(qc, null);

		assertTrue(result instanceof Slice);
	} catch (Exception e) {
		e.printStackTrace();
		fail("should parse ask query with negated property path");
	}
}
 
Example #13
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testWildCardPathComplexSubjectHandling() {

	String query = "PREFIX : <http://example.org/>\n ASK { ?a (:comment/^(:subClassOf|(:type/:label))/:type)* ?b } ";

	ParsedQuery parsedQuery = parser.parseQuery(query, null);
	TupleExpr tupleExpr = parsedQuery.getTupleExpr();

	Slice slice = (Slice) tupleExpr;

	ArbitraryLengthPath path = (ArbitraryLengthPath) slice.getArg();
	Var pathStart = path.getSubjectVar();
	Var pathEnd = path.getObjectVar();

	assertThat(pathStart.getName()).isEqualTo("a");
	assertThat(pathEnd.getName()).isEqualTo("b");

	Join pathSequence = (Join) path.getPathExpression();
	Join innerJoin = (Join) pathSequence.getLeftArg();
	Var commentObjectVar = ((StatementPattern) innerJoin.getLeftArg()).getObjectVar();

	Union union = (Union) innerJoin.getRightArg();
	Var subClassOfSubjectVar = ((StatementPattern) union.getLeftArg()).getSubjectVar();
	assertThat(subClassOfSubjectVar).isNotEqualTo(commentObjectVar);

	Var subClassOfObjectVar = ((StatementPattern) union.getLeftArg()).getObjectVar();

	assertThat(subClassOfObjectVar).isEqualTo(commentObjectVar);
}
 
Example #14
Source File: LimitOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Slice node) throws OptimizationException {
	if (!node.hasOffset()) {
		applicableLimitInScope = node.getLimit();
	}
	super.meet(node);
	applicableLimitInScope = -1;

}
 
Example #15
Source File: BaseTupleExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(final Slice theSlice) throws Exception {
	if (theSlice.hasOffset()) {
		mOffset = theSlice.getOffset();
	}

	if (theSlice.hasLimit()) {
		mLimit = theSlice.getLimit();
	}

	theSlice.visitChildren(this);
}
 
Example #16
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected boolean isReducedOrDistinct(QueryModelNode node) {
	QueryModelNode parent = node.getParentNode();
	if (parent instanceof Slice) {
		return isReducedOrDistinct(parent);
	}
	return parent instanceof Distinct || parent instanceof Reduced;
}
 
Example #17
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Slice slice, BindingSet bindings)
		throws QueryEvaluationException {
	CloseableIteration<BindingSet, QueryEvaluationException> result = evaluate(slice.getArg(), bindings);

	if (slice.hasOffset()) {
		result = new OffsetIteration<>(result, slice.getOffset());
	}

	if (slice.hasLimit()) {
		result = new LimitIteration<>(result, slice.getLimit());
	}

	return result;
}
 
Example #18
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(UnaryTupleOperator expr,
		BindingSet bindings) throws QueryEvaluationException {
	if (expr instanceof Projection) {
		return evaluate((Projection) expr, bindings);
	} else if (expr instanceof MultiProjection) {
		return evaluate((MultiProjection) expr, bindings);
	} else if (expr instanceof Filter) {
		return evaluate((Filter) expr, bindings);
	} else if (expr instanceof Service) {
		return evaluate((Service) expr, bindings);
	} else if (expr instanceof Slice) {
		return evaluate((Slice) expr, bindings);
	} else if (expr instanceof Extension) {
		return evaluate((Extension) expr, bindings);
	} else if (expr instanceof Distinct) {
		return evaluate((Distinct) expr, bindings);
	} else if (expr instanceof Reduced) {
		return evaluate((Reduced) expr, bindings);
	} else if (expr instanceof Group) {
		return evaluate((Group) expr, bindings);
	} else if (expr instanceof Order) {
		return evaluate((Order) expr, bindings);
	} else if (expr instanceof QueryRoot) {
		// new query, reset shared return value for successive calls of
		// NOW()
		this.sharedValueOfNow = null;
		return evaluate(expr.getArg(), bindings);
	} else if (expr instanceof DescribeOperator) {
		return evaluate((DescribeOperator) expr, bindings);
	} else if (expr == null) {
		throw new IllegalArgumentException("expr must not be null");
	} else {
		throw new QueryEvaluationException("Unknown unary tuple operator type: " + expr.getClass());
	}
}
 
Example #19
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Slice node) throws RDFHandlerException {
	node.getArg().visit(this);
	if (node.hasLimit()) {
		handler.handleStatement(valueFactory.createStatement(subject, SP.LIMIT_PROPERTY,
				valueFactory.createLiteral(Long.toString(node.getLimit()), XMLSchema.INTEGER)));
	}
	if (node.hasOffset()) {
		handler.handleStatement(valueFactory.createStatement(subject, SP.OFFSET_PROPERTY,
				valueFactory.createLiteral(Long.toString(node.getOffset()), XMLSchema.INTEGER)));
	}
}
 
Example #20
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Slice node) throws RDFHandlerException {
	if (!isSubQuery) { // ignore root slice
		node.getArg().visit(this);
	} else {
		super.meet(node);
	}
}
 
Example #21
Source File: GenericInfoOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Slice node) throws OptimizationException {
	// remember the limit of the main query (i.e. outside of a projection)
	if (!seenProjection) {
		limit = node.getLimit();
	}
	super.meet(node);
}
 
Example #22
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Slice node) throws X {
	meetUnaryTupleOperator(node);
}
 
Example #23
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testWildCardPathPushNegation() {

	String query = "PREFIX : <http://example.org/>\n ASK {:IBM ^(:|!:) ?jane.} ";

	ParsedQuery parsedQuery = parser.parseQuery(query, null);
	TupleExpr tupleExpr = parsedQuery.getTupleExpr();

	Slice slice = (Slice) tupleExpr;
	Union union = (Union) slice.getArg();

	Var leftSubjectVar = ((StatementPattern) union.getLeftArg()).getSubjectVar();
	Var rightSubjectVar = ((StatementPattern) ((Filter) union.getRightArg()).getArg()).getSubjectVar();

	assertEquals(leftSubjectVar, rightSubjectVar);

}
 
Example #24
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testWildCardPathPushNegation2() {

	String query = "PREFIX : <http://example.org/>\n ASK {:IBM ^(:|!:) :Jane.} ";

	ParsedQuery parsedQuery = parser.parseQuery(query, null);
	TupleExpr tupleExpr = parsedQuery.getTupleExpr();

	Slice slice = (Slice) tupleExpr;
	Union union = (Union) slice.getArg();

	Var leftSubjectVar = ((StatementPattern) union.getLeftArg()).getSubjectVar();
	Var rightSubjectVar = ((StatementPattern) ((Filter) union.getRightArg()).getArg()).getSubjectVar();

	assertEquals(leftSubjectVar, rightSubjectVar);

}
 
Example #25
Source File: EvaluationStatistics.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Slice slice) {
	cardinality = 1;
}
 
Example #26
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitAsk(Resource ask) throws RDF4JException {
	TupleExpr whereExpr = visitWhere(ask);
	tupleRoot = new Slice(whereExpr, 0, 1);
}
 
Example #27
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitSelect(Resource select) throws RDF4JException {
	Value resultVars = TripleSources.singleValue(select, SP.RESULT_VARIABLES_PROPERTY, store);
	if (!(resultVars instanceof Resource)) {
		throw new MalformedSpinException(
				String.format("Value of %s is not a resource", SP.RESULT_VARIABLES_PROPERTY));
	}

	Map<String, ProjectionElem> oldProjElems = projElems;
	projElems = new LinkedHashMap<>();
	Projection projection = visitResultVariables((Resource) resultVars, oldProjElems);
	TupleExpr whereExpr = visitWhere(select);
	projection.setArg(whereExpr);

	Value groupBy = TripleSources.singleValue(select, SP.GROUP_BY_PROPERTY, store);
	if (groupBy instanceof Resource) {
		visitGroupBy((Resource) groupBy);
	}
	if (group != null) {
		group.setArg(projection.getArg());
		projection.setArg(group);
	}

	Value having = TripleSources.singleValue(select, SP.HAVING_PROPERTY, store);
	if (having instanceof Resource) {
		TupleExpr havingExpr = visitHaving((Resource) having);
		projection.setArg(havingExpr);
	}

	addSourceExpressions(projection, projElems.values());
	projElems = oldProjElems;

	Value orderby = TripleSources.singleValue(select, SP.ORDER_BY_PROPERTY, store);
	if (orderby instanceof Resource) {
		Order order = visitOrderBy((Resource) orderby);
		order.setArg(projection.getArg());
		projection.setArg(order);
	}

	boolean distinct = TripleSources.booleanValue(select, SP.DISTINCT_PROPERTY, store);
	if (distinct) {
		tupleRoot = new Distinct(tupleRoot);
	}

	long offset = -1L;
	Value offsetValue = TripleSources.singleValue(select, SP.OFFSET_PROPERTY, store);
	if (offsetValue instanceof Literal) {
		offset = ((Literal) offsetValue).longValue();
	}
	long limit = -1L;
	Value limitValue = TripleSources.singleValue(select, SP.LIMIT_PROPERTY, store);
	if (limitValue instanceof Literal) {
		limit = ((Literal) limitValue).longValue();
	}
	if (offset > 0L || limit >= 0L) {
		Slice slice = new Slice(tupleRoot);
		if (offset > 0L) {
			slice.setOffset(offset);
		}
		if (limit >= 0L) {
			slice.setLimit(limit);
		}
		tupleRoot = slice;
	}
}
 
Example #28
Source File: RdfCloudTripleStoreEvaluationStatistics.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void meet(final Slice node) {
    cardinality = node.getLimit();
}