Search in sources :

Example 11 with VarExprList

use of org.apache.jena.sparql.core.VarExprList in project jena by apache.

the class TransformFilterInequality method safeToTransform.

private static boolean safeToTransform(Collection<Var> varsEquality, Op op) {
    // Structure as a visitor?
    if (op instanceof OpBGP || op instanceof OpQuadPattern)
        return true;
    if (op instanceof OpFilter) {
        OpFilter opf = (OpFilter) op;
        // Expressions are always safe transform by substitution.
        return safeToTransform(varsEquality, opf.getSubOp());
    }
    // are very rarely so deep that it matters.
    if (op instanceof OpSequence) {
        OpN opN = (OpN) op;
        for (Op subOp : opN.getElements()) {
            if (!safeToTransform(varsEquality, subOp))
                return false;
        }
        return true;
    }
    if (op instanceof OpJoin || op instanceof OpUnion) {
        Op2 op2 = (Op2) op;
        return safeToTransform(varsEquality, op2.getLeft()) && safeToTransform(varsEquality, op2.getRight());
    }
    // Not safe unless filter variables are mentioned on the LHS.
    if (op instanceof OpConditional || op instanceof OpLeftJoin) {
        Op2 opleftjoin = (Op2) op;
        if (!safeToTransform(varsEquality, opleftjoin.getLeft()) || !safeToTransform(varsEquality, opleftjoin.getRight()))
            return false;
        // Not only must the left and right be safe to transform,
        // but the equality variable must be known to be always set.
        // If the varsLeft are disjoint from assigned vars,
        // we may be able to push assign down right
        // (this generalises the unit table case specialcase1)
        // Needs more investigation.
        Op opLeft = opleftjoin.getLeft();
        Set<Var> varsLeft = OpVars.visibleVars(opLeft);
        if (varsLeft.containsAll(varsEquality))
            return true;
        return false;
    }
    if (op instanceof OpGraph) {
        OpGraph opg = (OpGraph) op;
        return safeToTransform(varsEquality, opg.getSubOp());
    }
    // Subquery - assume scope rewriting has already been applied.
    if (op instanceof OpModifier) {
        // ORDER BY?
        OpModifier opMod = (OpModifier) op;
        if (opMod instanceof OpProject) {
            OpProject opProject = (OpProject) op;
            // AS-ification.
            for (Var v : opProject.getVars()) {
                if (varsEquality.contains(v))
                    return false;
            }
        }
        return safeToTransform(varsEquality, opMod.getSubOp());
    }
    if (op instanceof OpGroup) {
        OpGroup opGroup = (OpGroup) op;
        VarExprList varExprList = opGroup.getGroupVars();
        return safeToTransform(varsEquality, varExprList) && safeToTransform(varsEquality, opGroup.getSubOp());
    }
    if (op instanceof OpTable) {
        OpTable opTable = (OpTable) op;
        if (opTable.isJoinIdentity())
            return true;
    }
    return false;
}
Also used : Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList)

Example 12 with VarExprList

use of org.apache.jena.sparql.core.VarExprList in project jena by apache.

the class SelectHandler method addAll.

/**
	 * Add all the variables from the select handler variable.
	 * 
	 * @param selectHandler
	 *            The select handler to copy the variables from.
	 */
public void addAll(SelectHandler selectHandler) {
    setReduced(selectHandler.query.isReduced());
    setDistinct(selectHandler.query.isDistinct());
    query.setQueryResultStar(selectHandler.query.isQueryResultStar());
    VarExprList shProjectVars = selectHandler.query.getProject();
    VarExprList qProjectVars = query.getProject();
    for (Var var : shProjectVars.getVars()) {
        // make sure there are no duplicates
        if (!qProjectVars.contains(var)) {
            qProjectVars.add(var, shProjectVars.getExpr(var));
        }
    }
    aggHandler.addAll(selectHandler.aggHandler);
}
Also used : Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList)

Example 13 with VarExprList

use of org.apache.jena.sparql.core.VarExprList in project jena by apache.

the class TransformEliminateAssignments method processVarExprList.

private VarExprList processVarExprList(VarExprList exprs, ExprTransform transform) {
    VarExprList newExprs = new VarExprList();
    for (Var v : exprs.getVars()) {
        Expr e = exprs.getExpr(v);
        Expr e2 = ExprTransformer.transform(transform, e);
        newExprs.add(v, e2);
    }
    return newExprs;
}
Also used : Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList)

Example 14 with VarExprList

use of org.apache.jena.sparql.core.VarExprList in project jena by apache.

the class TransformEliminateAssignments method transform.

@Override
public Op transform(OpExtend opExtend, Op subOp) {
    // the assigned value is unnecessary or only used once
    if (!this.tracker.insideProjection())
        return super.transform(opExtend, subOp);
    // Track the assignments for future reference
    this.tracker.putAssignments(opExtend.getVarExprList());
    // Eliminate and inline assignments
    VarExprList unusedAssignments = processUnused(opExtend.getVarExprList());
    VarExprList newAssignments = new VarExprList();
    for (Var assignVar : opExtend.getVarExprList().getVars()) {
        // If unused eliminate
        if (unusedAssignments != null && unusedAssignments.contains(assignVar))
            continue;
        Expr currExpr = opExtend.getVarExprList().getExpr(assignVar);
        // See what vars are used in the current expression
        Set<Var> vars = new HashSet<>();
        ExprVars.nonOpVarsMentioned(vars, currExpr);
        // See if we can inline anything
        for (Var var : vars) {
            // Usage count will be 2 if we can eliminate the assignment
            // First usage is when it is introduced by the assignment and
            // the second is when it is used now used in another assignment
            Expr e = getAssignExpr(var);
            if (this.tracker.getUsageCount(var) == 2 && hasAssignment(var) && canInline(e)) {
                // Can go back and eliminate that assignment
                subOp = eliminateAssignment(subOp, var);
                // Replace the variable usage with the expression within
                // expression
                currExpr = ExprTransformer.transform(new ExprTransformSubstitute(var, e), currExpr);
                this.tracker.getAssignments().remove(var);
                // Need to update any assignments we may be tracking that
                // refer to the variable we just inlined
                this.tracker.updateAssignments(var, e);
                // VarExprList we are currently building
                if (newAssignments.contains(var) && newAssignments.getExpr(var).equals(e)) {
                    newAssignments.getVars().remove(var);
                    newAssignments.getExprs().remove(var);
                }
            }
        }
        newAssignments.add(assignVar, currExpr);
    }
    // May be able to eliminate the extend entirely in some cases
    if (newAssignments.size() > 0) {
        return OpExtend.create(subOp, newAssignments);
    } else {
        return subOp;
    }
}
Also used : Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList)

Example 15 with VarExprList

use of org.apache.jena.sparql.core.VarExprList in project jena by apache.

the class TransformExtendCombine method transform.

@Override
public Op transform(OpAssign opAssign, Op subOp) {
    if (subOp instanceof OpAssign) {
        // If a variable is assigned twice, don't do anything.
        // (assign (?x 2)  (assign (?x 1) op)) => leave alone.
        // This is the safest option in a rare case.
        // It would be OK if addAll does a replacement without checking
        // but having it check and complain about duplicates adds robustness.
        // In OpExtend, it's actually illegal.
        OpAssign x = (OpAssign) subOp;
        VarExprList outerVarExprList = opAssign.getVarExprList();
        // Copy
        VarExprList innerVarExprList = new VarExprList(x.getVarExprList());
        Op r = OpAssign.assign(x.getSubOp(), innerVarExprList);
        // This contains an "if already (assign)" test.
        r = OpAssign.assign(r, outerVarExprList);
        return r;
    }
    return super.transform(opAssign, subOp);
}
Also used : Op(org.apache.jena.sparql.algebra.Op) OpAssign(org.apache.jena.sparql.algebra.op.OpAssign) VarExprList(org.apache.jena.sparql.core.VarExprList)

Aggregations

VarExprList (org.apache.jena.sparql.core.VarExprList)49 Var (org.apache.jena.sparql.core.Var)21 Test (org.junit.Test)15 Op (org.apache.jena.sparql.algebra.Op)12 Expr (org.apache.jena.sparql.expr.Expr)8 Query (org.apache.jena.query.Query)7 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)7 ContractTest (org.xenei.junit.contract.ContractTest)6 E_Random (org.apache.jena.sparql.expr.E_Random)4 QueryParseException (org.apache.jena.query.QueryParseException)3 ExprAggregator (org.apache.jena.sparql.expr.ExprAggregator)2 ExprVar (org.apache.jena.sparql.expr.ExprVar)2 StringReader (java.io.StringReader)1 Map (java.util.Map)1 ExprRewriter (org.apache.jena.arq.querybuilder.rewriters.ExprRewriter)1 Node (org.apache.jena.graph.Node)1 SortCondition (org.apache.jena.query.SortCondition)1 OpAssign (org.apache.jena.sparql.algebra.op.OpAssign)1 OpExtend (org.apache.jena.sparql.algebra.op.OpExtend)1 BindingHashMap (org.apache.jena.sparql.engine.binding.BindingHashMap)1