use of org.apache.jena.atlas.lib.Pair in project jena by apache.
the class DumpOps method dumpNodeTable.
public static void dumpNodeTable(NodeTable nodeTable, Set<NodeTable> dumpedNodeTables) {
if (dumpedNodeTables.contains(nodeTable))
return;
Iterator<Pair<NodeId, Node>> iter = nodeTable.all();
for (; iter.hasNext(); ) {
Pair<NodeId, Node> pair = iter.next();
NodeId nid = pair.car();
Node n = pair.cdr();
String x = NodeFmtLib.displayStr(n);
System.out.printf("%016X %s\n", nid.getId(), x);
}
dumpedNodeTables.add(nodeTable);
}
use of org.apache.jena.atlas.lib.Pair in project jena by apache.
the class TransformFilterEquality method apply.
private static Op apply(ExprList exprs, Op subOp) {
// ---- Find and extract any equality filters.
Pair<List<Pair<Var, NodeValue>>, ExprList> p = preprocessFilterEquality(exprs);
if (p == null || p.getLeft().size() == 0)
return null;
List<Pair<Var, NodeValue>> equalities = p.getLeft();
Collection<Var> varsMentioned = varsMentionedInEqualityFilters(equalities);
ExprList remaining = p.getRight();
// condition is duplicated
if (varsMentioned.size() < equalities.size())
return null;
// hence eliminate all rows. Return the empty table.
if (testSpecialCaseUnused(subOp, equalities, remaining))
// JENA-1184 woraround. Return unchanged.
return null;
// { OPTIONAL{P1} OPTIONAL{P2} ... FILTER(?x = :x) }
if (testSpecialCase1(subOp, equalities, remaining)) {
// Find backbone of ops
List<Op> ops = extractOptionals(subOp);
ops = processSpecialCase1(ops, equalities);
// Put back together
Op op = rebuild((Op2) subOp, ops);
// Put all filters - either we optimized, or we left alone.
// Either way, the complete set of filter expressions.
op = OpFilter.filterBy(exprs, op);
return op;
}
// ---- Transform
Op op = subOp;
if (!safeToTransform(varsMentioned, op))
return null;
for (Pair<Var, NodeValue> equalityTest : equalities) op = processFilterWorker(op, equalityTest.getLeft(), equalityTest.getRight());
// ---- Place any filter expressions around the processed sub op.
if (remaining.size() > 0)
op = OpFilter.filterBy(remaining, op);
return op;
}
use of org.apache.jena.atlas.lib.Pair in project jena by apache.
the class TransformImplicitLeftJoin method apply.
private static Op apply(OpLeftJoin opLeftJoin, Op left, Op right) {
// This handles arbitrarily nested && conditions
ExprList orig = ExprList.splitConjunction(opLeftJoin.getExprs());
// Extract optimizable conditions?
Pair<List<Pair<Var, Var>>, ExprList> p = preprocessFilterImplicitJoin(left, right, orig);
// Were there any optimizable conditions?
if (p == null || p.getLeft().size() == 0)
return null;
List<Pair<Var, Var>> joins = p.getLeft();
Collection<Var> varsMentioned = varsMentionedInImplictJoins(joins);
ExprList remaining = p.getRight();
// ---- Check if the subOp is the right shape to transform.
Op op = right;
if (!safeToTransform(joins, varsMentioned, op))
return null;
// We apply substitution only to the RHS
// This is because applying to both sides would change the join
// semantics of the Left Join
Collection<Var> lhsVars = OpVars.visibleVars(left);
Collection<Var> rhsVars = OpVars.visibleVars(right);
for (Pair<Var, Var> implicitJoin : joins) {
// Which variable do we want to substitute out?
// We don't need to deal with the case of neither variable being on
// the RHS
Var lVar = implicitJoin.getLeft();
Var rVar = implicitJoin.getRight();
if (lhsVars.contains(lVar) && lhsVars.contains(rVar)) {
if (rhsVars.contains(lVar) && rhsVars.contains(rVar)) {
// Both vars are also on RHS
// Order of substitution doesn't matter
op = processFilterWorker(op, lVar, rVar);
} else if (rhsVars.contains(lVar)) {
// Substitute left variable for right variable
op = processFilterWorker(op, lVar, rVar);
} else if (rhsVars.contains(rVar)) {
// Substitute right variable for left variable
op = processFilterWorker(op, rVar, lVar);
} else {
// substitutions
return null;
}
} else if (lhsVars.contains(lVar)) {
if (rhsVars.contains(rVar)) {
// Substitute right variable for left variable
op = processFilterWorker(op, rVar, lVar);
} else {
// May be hit if trying to apply a sequence of substitutions
return null;
}
} else if (lhsVars.contains(rVar)) {
if (rhsVars.contains(lVar)) {
// Substitute left variable for right variable
op = processFilterWorker(op, lVar, rVar);
} else {
// May be hit if trying to apply a sequence of substitutions
return null;
}
} else {
if (rhsVars.contains(lVar) && rhsVars.contains(rVar)) {
// Both variables are on RHS so can substitute one for the
// other
op = processFilterWorker(op, lVar, rVar);
} else {
// May be hit if trying to apply a sequence of substitutions
return null;
}
}
// Re-compute visible RHS vars after each substitution as it may
// affect subsequent substitutions
rhsVars = OpVars.visibleVars(op);
}
if (remaining.size() > 0) {
return OpLeftJoin.create(left, op, remaining);
} else {
return OpLeftJoin.create(left, op, (ExprList) null);
}
}
use of org.apache.jena.atlas.lib.Pair in project jena by apache.
the class TransformImplicitLeftJoin method preprocessFilterImplicitJoin.
private static Pair<List<Pair<Var, Var>>, ExprList> preprocessFilterImplicitJoin(Op left, Op right, ExprList exprs) {
List<Pair<Var, Var>> exprsJoins = new ArrayList<>();
ExprList exprsOther = new ExprList();
for (Expr e : exprs.getList()) {
Pair<Var, Var> p = preprocess(left, right, e);
if (p != null) {
exprsJoins.add(p);
} else {
exprsOther.add(e);
}
}
if (exprsJoins.size() == 0)
return null;
return Pair.create(exprsJoins, exprsOther);
}
use of org.apache.jena.atlas.lib.Pair in project jena by apache.
the class TransformFilterImplicitJoin method preprocessFilterImplicitJoin.
// --- find and extract
private static Pair<List<Pair<Var, Var>>, ExprList> preprocessFilterImplicitJoin(Op subOp, ExprList exprs) {
List<Pair<Var, Var>> exprsJoins = new ArrayList<>();
ExprList exprsOther = new ExprList();
for (Expr e : exprs.getList()) {
Pair<Var, Var> p = preprocess(subOp, e);
if (p != null) {
exprsJoins.add(p);
} else {
exprsOther.add(e);
}
}
if (exprsJoins.size() == 0)
return null;
return Pair.create(exprsJoins, exprsOther);
}
Aggregations