use of org.eclipse.rdf4j.query.algebra.Join in project rdf4j by eclipse.
the class TupleExprBuilder method visit.
@Override
public TupleExpr visit(ASTSelectQuery node, Object data) throws VisitorException {
GraphPattern parentGP = graphPattern;
// Start with building the graph pattern
graphPattern = new GraphPattern(parentGP);
node.getWhereClause().jjtAccept(this, null);
TupleExpr tupleExpr = graphPattern.buildTupleExpr();
// Apply grouping
Group group = null;
ASTGroupClause groupNode = node.getGroupClause();
if (groupNode != null) {
tupleExpr = (TupleExpr) groupNode.jjtAccept(this, tupleExpr);
group = (Group) tupleExpr;
}
final ASTHavingClause havingClause = node.getHavingClause();
if (havingClause != null) {
if (group == null) {
// create implicit group
group = new Group(tupleExpr);
}
// Apply HAVING group filter condition
tupleExpr = processHavingClause(havingClause, tupleExpr, group);
}
// process bindings clause
final ASTBindingsClause bindingsClause = node.getBindingsClause();
if (bindingsClause != null) {
tupleExpr = new Join((BindingSetAssignment) bindingsClause.jjtAccept(this, null), tupleExpr);
}
final ASTOrderClause orderClause = node.getOrderClause();
if (orderClause != null) {
if (group == null) {
// create implicit group
group = new Group(tupleExpr);
}
// Apply result ordering
tupleExpr = processOrderClause(node.getOrderClause(), tupleExpr, group);
}
// Apply projection
tupleExpr = (TupleExpr) node.getSelect().jjtAccept(this, tupleExpr);
// Process limit and offset clauses
ASTLimit limitNode = node.getLimit();
long limit = -1L;
if (limitNode != null) {
limit = (Long) limitNode.jjtAccept(this, null);
}
ASTOffset offsetNode = node.getOffset();
long offset = -1L;
if (offsetNode != null) {
offset = (Long) offsetNode.jjtAccept(this, null);
}
if (offset >= 1L || limit >= 0L) {
tupleExpr = new Slice(tupleExpr, offset, limit);
}
if (parentGP != null) {
parentGP.addRequiredTE(tupleExpr);
graphPattern = parentGP;
}
return tupleExpr;
}
use of org.eclipse.rdf4j.query.algebra.Join in project rdf4j by eclipse.
the class TupleExprBuilder method visit.
@Override
public TupleExpr visit(ASTConstructQuery node, Object data) throws VisitorException {
// Start with building the graph pattern
graphPattern = new GraphPattern();
node.getWhereClause().jjtAccept(this, null);
TupleExpr tupleExpr = graphPattern.buildTupleExpr();
// Apply grouping
ASTGroupClause groupNode = node.getGroupClause();
if (groupNode != null) {
tupleExpr = (TupleExpr) groupNode.jjtAccept(this, tupleExpr);
}
Group group = null;
if (tupleExpr instanceof Group) {
group = (Group) tupleExpr;
} else {
// create a new implicit group. Note that this group will only
// actually
// be used in the query model if the query has HAVING or ORDER BY
// clause
group = new Group(tupleExpr);
}
// Apply HAVING group filter condition
tupleExpr = processHavingClause(node.getHavingClause(), tupleExpr, group);
// process bindings clause
ASTBindingsClause bindingsClause = node.getBindingsClause();
if (bindingsClause != null) {
tupleExpr = new Join((BindingSetAssignment) bindingsClause.jjtAccept(this, null), tupleExpr);
}
// Apply result ordering
tupleExpr = processOrderClause(node.getOrderClause(), tupleExpr, null);
// Process construct clause
ASTConstruct constructNode = node.getConstruct();
if (!constructNode.isWildcard()) {
tupleExpr = (TupleExpr) constructNode.jjtAccept(this, tupleExpr);
} else {
// create construct clause from graph pattern.
ConstructorBuilder cb = new ConstructorBuilder();
// possible future use.
try {
tupleExpr = cb.buildConstructor(tupleExpr, false, false);
} catch (MalformedQueryException e) {
throw new VisitorException(e.getMessage());
}
}
// process limit and offset clauses
ASTLimit limitNode = node.getLimit();
long limit = -1L;
if (limitNode != null) {
limit = (Long) limitNode.jjtAccept(this, null);
}
ASTOffset offsetNode = node.getOffset();
long offset = -1;
if (offsetNode != null) {
offset = (Long) offsetNode.jjtAccept(this, null);
}
if (offset >= 1 || limit >= 0) {
tupleExpr = new Slice(tupleExpr, offset, limit);
}
return tupleExpr;
}
use of org.eclipse.rdf4j.query.algebra.Join in project rdf4j by eclipse.
the class SPARQLParserTest method testSES1927UnequalLiteralValueConstants2.
@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());
}
use of org.eclipse.rdf4j.query.algebra.Join in project rdf4j by eclipse.
the class GraphPattern method buildTupleExpr.
/**
* Builds a combined tuple expression from the tuple expressions and constraints in this graph pattern.
*
* @return A tuple expression for this graph pattern.
*/
public TupleExpr buildTupleExpr() {
TupleExpr result;
if (requiredTEs.isEmpty()) {
result = new SingletonSet();
} else {
result = requiredTEs.get(0);
for (int i = 1; i < requiredTEs.size(); i++) {
TupleExpr te = requiredTEs.get(i);
// if (containsProjection(te) || containsProjection(result))
// {
// result = new BottomUpJoin(result, te);
// }
// else {
result = new Join(result, te);
// }
}
}
for (Map.Entry<TupleExpr, List<ValueExpr>> entry : optionalTEs) {
List<ValueExpr> constraints = entry.getValue();
if (constraints != null && !constraints.isEmpty()) {
ValueExpr condition = constraints.get(0);
for (int i = 1; i < constraints.size(); i++) {
condition = new And(condition, constraints.get(i));
}
result = new LeftJoin(result, entry.getKey(), condition);
} else {
result = new LeftJoin(result, entry.getKey());
}
}
for (ValueExpr constraint : constraints) {
result = new Filter(result, constraint);
}
return result;
}
use of org.eclipse.rdf4j.query.algebra.Join in project rdf4j by eclipse.
the class TupleExprBuilder method createTupleExprForNegatedPropertySet.
private TupleExpr createTupleExprForNegatedPropertySet(NegatedPropertySet nps, int index) {
Var subjVar = nps.getSubjectVar();
Var predVar = createAnonVar();
ValueExpr filterCondition = null;
ValueExpr filterConditionInverse = null;
// build (inverted) filter conditions for each negated path element.
for (PropertySetElem elem : nps.getPropertySetElems()) {
ValueConstant predicate = elem.getPredicate();
if (elem.isInverse()) {
Compare compare = new Compare(predVar, predicate, CompareOp.NE);
if (filterConditionInverse == null) {
filterConditionInverse = compare;
} else {
filterConditionInverse = new And(compare, filterConditionInverse);
}
} else {
Compare compare = new Compare(predVar, predicate, CompareOp.NE);
if (filterCondition == null) {
filterCondition = compare;
} else {
filterCondition = new And(compare, filterCondition);
}
}
}
TupleExpr patternMatch = null;
// one item)
if (filterCondition != null) {
for (ValueExpr objVar : nps.getObjectList()) {
if (patternMatch == null) {
patternMatch = new StatementPattern(nps.getScope(), subjVar, predVar, (Var) objVar, nps.getContextVar());
} else {
patternMatch = new Join(new StatementPattern(nps.getScope(), subjVar, predVar, (Var) objVar, nps.getContextVar()), patternMatch);
}
}
}
TupleExpr patternMatchInverse = null;
// one item):
if (filterConditionInverse != null) {
for (ValueExpr objVar : nps.getObjectList()) {
if (patternMatchInverse == null) {
patternMatchInverse = new StatementPattern(nps.getScope(), (Var) objVar, predVar, subjVar, nps.getContextVar());
} else {
patternMatchInverse = new Join(new StatementPattern(nps.getScope(), (Var) objVar, predVar, subjVar, nps.getContextVar()), patternMatchInverse);
}
}
}
TupleExpr completeMatch = null;
if (patternMatch != null) {
completeMatch = new Filter(patternMatch, filterCondition);
}
if (patternMatchInverse != null) {
if (completeMatch == null) {
completeMatch = new Filter(patternMatchInverse, filterConditionInverse);
} else {
completeMatch = new Union(new Filter(patternMatchInverse, filterConditionInverse), completeMatch);
}
}
return completeMatch;
}
Aggregations