use of org.eclipse.rdf4j.query.algebra.LeftJoin 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.LeftJoin in project rdf4j by eclipse.
the class BasicGroup method expr.
private TupleExpr expr(boolean filterExpr) {
TupleExpr aExpr = null;
if (mExpressions.isEmpty() && mFilters.isEmpty()) {
if (mChildren.isEmpty()) {
return null;
}
} else if (mExpressions.isEmpty() && !mFilters.isEmpty()) {
if (mChildren.isEmpty()) {
aExpr = new Filter(new EmptySet(), filtersAsAnd());
}
} else {
aExpr = asJoin(mExpressions);
if (filterExpr) {
aExpr = filteredTuple(aExpr);
}
}
if (!mChildren.isEmpty()) {
for (Group aGroup : mChildren) {
if (aExpr == null) {
if (mExpressions.isEmpty() && !mFilters.isEmpty()) {
aExpr = new Filter(aGroup.expr(), filtersAsAnd());
} else {
aExpr = aGroup.expr();
}
} else {
BinaryTupleOperator aJoin = aGroup.isOptional() ? new LeftJoin() : new Join();
aJoin.setLeftArg(aExpr);
if (aGroup.isOptional() && aJoin instanceof LeftJoin && aGroup instanceof BasicGroup && !((BasicGroup) aGroup).mFilters.isEmpty()) {
BasicGroup aBasicGroup = (BasicGroup) aGroup;
aJoin.setRightArg(aBasicGroup.expr(false));
((LeftJoin) aJoin).setCondition(aBasicGroup.filtersAsAnd());
} else {
aJoin.setRightArg(aGroup.expr());
}
aExpr = aJoin;
}
}
}
return aExpr;
}
use of org.eclipse.rdf4j.query.algebra.LeftJoin in project rdf4j by eclipse.
the class AbstractQueryBuilder method groupAsJoin.
private TupleExpr groupAsJoin(List<Group> theList) {
BinaryTupleOperator aJoin = new Join();
Filter aFilter = null;
for (Group aGroup : theList) {
TupleExpr aExpr = aGroup.expr();
if (aExpr == null) {
continue;
}
if (aExpr instanceof Filter && (((Filter) aExpr).getArg() == null || ((Filter) aExpr).getArg() instanceof EmptySet)) {
if (aFilter == null) {
aFilter = (Filter) aExpr;
} else {
// if we already have a filter w/ an empty arg, let's And the
// conditions together.
aFilter.setCondition(new And(aFilter.getCondition(), ((Filter) aExpr).getCondition()));
}
continue;
}
if (aFilter != null) {
aFilter.setArg(aExpr);
aExpr = aFilter;
aFilter = null;
}
if (aGroup.isOptional()) {
LeftJoin lj = new LeftJoin();
TupleExpr aLeft = joinOrExpr(aJoin);
if (aLeft != null) {
lj.setLeftArg(aLeft);
lj.setRightArg(aExpr);
aJoin = lj;
continue;
}
}
if (aJoin.getLeftArg() == null) {
aJoin.setLeftArg(aExpr);
} else if (aJoin.getRightArg() == null) {
aJoin.setRightArg(aExpr);
} else {
Join aNewJoin = new Join();
aNewJoin.setLeftArg(aJoin);
aNewJoin.setRightArg(aExpr);
aJoin = aNewJoin;
}
}
TupleExpr aExpr = joinOrExpr(aJoin);
if (aFilter != null) {
aFilter.setArg(aExpr);
aExpr = aFilter;
}
return aExpr;
}
Aggregations