Search in sources :

Example 11 with Pair

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);
}
Also used : Node(org.apache.jena.graph.Node) NodeId(org.apache.jena.tdb.store.NodeId) Pair(org.apache.jena.atlas.lib.Pair)

Example 12 with Pair

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;
}
Also used : Op(org.apache.jena.sparql.algebra.Op) VarExprList(org.apache.jena.sparql.core.VarExprList) Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList) Pair(org.apache.jena.atlas.lib.Pair)

Example 13 with Pair

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);
    }
}
Also used : Op(org.apache.jena.sparql.algebra.Op) VarExprList(org.apache.jena.sparql.core.VarExprList) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) List(java.util.List) VarExprList(org.apache.jena.sparql.core.VarExprList) Pair(org.apache.jena.atlas.lib.Pair)

Example 14 with Pair

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);
}
Also used : VarExprList(org.apache.jena.sparql.core.VarExprList) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) Pair(org.apache.jena.atlas.lib.Pair)

Example 15 with Pair

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);
}
Also used : VarExprList(org.apache.jena.sparql.core.VarExprList) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) Pair(org.apache.jena.atlas.lib.Pair)

Aggregations

Pair (org.apache.jena.atlas.lib.Pair)19 Var (org.apache.jena.sparql.core.Var)9 VarExprList (org.apache.jena.sparql.core.VarExprList)8 ArrayList (java.util.ArrayList)7 Node (org.apache.jena.graph.Node)6 NodeId (org.apache.jena.tdb.store.NodeId)5 Record (org.apache.jena.tdb.base.record.Record)4 List (java.util.List)3 Op (org.apache.jena.sparql.algebra.Op)3 RecordBufferPage (org.apache.jena.tdb.base.recordbuffer.RecordBufferPage)3 ByteBuffer (java.nio.ByteBuffer)2 Iterator (java.util.Iterator)1 Function (java.util.function.Function)1 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)1 Iter (org.apache.jena.atlas.iterator.Iter)1 IteratorDelayedInitialization (org.apache.jena.atlas.iterator.IteratorDelayedInitialization)1 IteratorWithBuffer (org.apache.jena.atlas.iterator.IteratorWithBuffer)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)1 ExprAggregator (org.apache.jena.sparql.expr.ExprAggregator)1