Search in sources :

Example 16 with Expr

use of org.apache.jena.sparql.expr.Expr in project jena by apache.

the class TransformFilterPlacement method transform.

@Override
public Op transform(OpFilter opFilter, Op x) {
    ExprList exprs = opFilter.getExprs();
    // Extract any expressions with "nasty" cases (RAND, UUID, STRUUID and BNODE)
    // which are not true functions (they return a different value every call so
    // number of calls matters.  NOW is safe (returns a fixed time point for the whole
    // query.
    // Phase one - check to see if work needed. 
    ExprList exprs2 = null;
    for (Expr expr : exprs) {
        if (!ExprLib.isStable(expr)) {
            if (exprs2 == null)
                exprs2 = new ExprList();
            exprs2.add(expr);
        }
    }
    // Phase 2 - if needed, split.
    if (exprs2 != null) {
        ExprList exprs1 = new ExprList();
        for (Expr expr : exprs) {
            // We are assuming fixup is rare. 
            if (ExprLib.isStable(expr))
                exprs1.add(expr);
        }
        exprs = exprs1;
    }
    Placement placement = transform(exprs, x);
    if (isNoChange(placement))
        // Didn't do anything.
        return super.transform(opFilter, x);
    Op op = buildFilter(placement);
    if (exprs2 != null)
        // Add back the non-deterministic expressions
        op = OpFilter.filterBy(exprs2, op);
    return op;
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Expr(org.apache.jena.sparql.expr.Expr) ExprList(org.apache.jena.sparql.expr.ExprList)

Example 17 with Expr

use of org.apache.jena.sparql.expr.Expr in project jena by apache.

the class TransformFilterPlacement method insertAnyFilter$.

/** For any expression now in scope, wrap the op with a filter. */
private static Op insertAnyFilter$(ExprList unplacedExprs, Collection<Var> patternVarsScope, Op op) {
    for (Iterator<Expr> iter = unplacedExprs.iterator(); iter.hasNext(); ) {
        Expr expr = iter.next();
        // Cache
        Set<Var> exprVars = expr.getVarsMentioned();
        if (patternVarsScope.containsAll(exprVars)) {
            if (op == null)
                op = OpTable.unit();
            op = OpFilter.filter(expr, op);
            iter.remove();
        }
    }
    return op;
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) Var(org.apache.jena.sparql.core.Var)

Example 18 with Expr

use of org.apache.jena.sparql.expr.Expr in project jena by apache.

the class TransformFilterPlacement method placePropertyFunctionProcedure.

private Placement placePropertyFunctionProcedure(ExprList exprsIn, Set<Var> varScope, Op1 op) {
    ExprList exprListPlaceable = new ExprList();
    ExprList exprListRetain = new ExprList();
    for (Expr expr : exprsIn) {
        Set<Var> mentioned = expr.getVarsMentioned();
        if (Collections.disjoint(varScope, mentioned))
            exprListPlaceable.add(expr);
        else
            exprListRetain.add(expr);
    }
    if (!exprListPlaceable.isEmpty()) {
        Placement p = transform(exprListPlaceable, op.getSubOp());
        if (isNoChange(p))
            return resultNoChange(op);
        Op newOp = op.copy(p.op);
        p.unplaced.addAll(exprListRetain);
        return result(newOp, p.unplaced);
    }
    return resultNoChange(op);
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Expr(org.apache.jena.sparql.expr.Expr) ExprList(org.apache.jena.sparql.expr.ExprList) Var(org.apache.jena.sparql.core.Var)

Example 19 with Expr

use of org.apache.jena.sparql.expr.Expr in project jena by apache.

the class TransformFilterPlacement method placeProject.

private Placement placeProject(ExprList exprs, OpProject input) {
    Collection<Var> varsProject = input.getVars();
    ExprList pushed = new ExprList();
    ExprList unpushed = new ExprList();
    for (Expr expr : exprs) {
        Set<Var> exprVars = expr.getVarsMentioned();
        if (varsProject.containsAll(exprVars))
            pushed.add(expr);
        else
            unpushed.add(expr);
    }
    if (pushed.isEmpty())
        return resultNoChange(input);
    // (filter (project ...)) ===> (project (filter ...)) 
    return processSubOp1(pushed, unpushed, input);
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) ExprList(org.apache.jena.sparql.expr.ExprList) Var(org.apache.jena.sparql.core.Var)

Example 20 with Expr

use of org.apache.jena.sparql.expr.Expr in project jena by apache.

the class TransformFilterPlacement method placeUnion.

private Placement placeUnion(ExprList exprs, OpUnion input) {
    if (false) {
        // Push into both sides without thinking.
        // Left as a safety fallback.
        Op left = input.getLeft();
        Placement pLeft = transform(exprs, left);
        Op right = input.getRight();
        Placement pRight = transform(exprs, right);
        if (pLeft != null && !pLeft.unplaced.isEmpty())
            return noChangePlacement;
        if (pRight != null && !pRight.unplaced.isEmpty())
            return noChangePlacement;
        // Must be guarded by the above.
        left = transformOpAlways(exprs, left);
        right = transformOpAlways(exprs, right);
        Op op2 = OpUnion.create(left, right);
        return result(op2, emptyList);
    }
    Op left = input.getLeft();
    Placement pLeft = transform(exprs, left);
    Op right = input.getRight();
    Placement pRight = transform(exprs, right);
    // If it's placed in neither arm it should be passed back out for placement.
    //
    // If it's done in both arms, then expression can be left pushed in
    // and not passed back out for placement.
    // If it is done in one arm and not the other, then it can be left pushed
    // in but needs to be redone for the other arm as if it were no placed at all.
    // A filter applied twice is safe.
    ExprList exprs2 = null;
    for (Expr expr : exprs) {
        boolean unplacedLeft = (isNoChange(pLeft) || pLeft.unplaced.getList().contains(expr));
        boolean unplacedRight = (isNoChange(pRight) || pRight.unplaced.getList().contains(expr));
        //            if ( unplacedLeft && unplacedRight ) {
        //                System.out.println("Unplaced:     "+expr) ;
        //            } else if ( unplacedLeft ) {
        //                System.out.println("Unplaced(L):  "+expr) ;
        //            } else if ( unplacedRight ) {
        //                System.out.println("Unplaced(R):  "+expr) ;
        //            } else
        //                System.out.println("Placed(L+R):  "+expr) ;
        boolean placed = !unplacedLeft && !unplacedRight;
        if (placed)
            // Went into both arms - expression has been handled completely.
            continue;
        if (exprs2 == null)
            exprs2 = new ExprList();
        exprs2.add(expr);
    }
    Op newLeft = (pLeft == null) ? left : pLeft.op;
    Op newRight = (pRight == null) ? right : pRight.op;
    if (exprs2 == null)
        exprs2 = emptyList;
    //Op op2 = OpUnion.create(newLeft, newRight) ;
    Op op2 = input.copy(newLeft, newRight);
    return result(op2, exprs2);
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Expr(org.apache.jena.sparql.expr.Expr) ExprList(org.apache.jena.sparql.expr.ExprList)

Aggregations

Expr (org.apache.jena.sparql.expr.Expr)82 Var (org.apache.jena.sparql.core.Var)28 ExprList (org.apache.jena.sparql.expr.ExprList)15 NodeValue (org.apache.jena.sparql.expr.NodeValue)15 Op (org.apache.jena.sparql.algebra.Op)13 ExprVar (org.apache.jena.sparql.expr.ExprVar)12 FunctionEnvBase (org.apache.jena.sparql.function.FunctionEnvBase)9 Test (org.junit.Test)9 VarExprList (org.apache.jena.sparql.core.VarExprList)8 Node (org.apache.jena.graph.Node)7 E_Random (org.apache.jena.sparql.expr.E_Random)7 ArrayList (java.util.ArrayList)5 Triple (org.apache.jena.graph.Triple)4 E_Multiply (org.apache.jena.sparql.expr.E_Multiply)4 E_Regex (org.apache.jena.sparql.expr.E_Regex)4 ContractTest (org.xenei.junit.contract.ContractTest)4 IndentedLineBuffer (org.apache.jena.atlas.io.IndentedLineBuffer)3 ExprAggregator (org.apache.jena.sparql.expr.ExprAggregator)3 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)2 Query (org.apache.jena.query.Query)2