Search in sources :

Example 41 with VarExprList

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

the class SyntaxVarScope method check.

// Other check (not scoping at this level) of a query
private static void check(Query query, Collection<Var> vars) {
    // Check any expressions are assigned to fresh variables.
    checkExprListAssignment(vars, query.getProject());
    // Legal in ARQ, not in SPARQL 1.1
    if (!Syntax.syntaxARQ.equals(query.getSyntax())) {
        if (query.isQueryResultStar() && query.hasGroupBy())
            throw new QueryParseException("SELECT * not legal with GROUP BY", -1, -1);
    }
    // Check any variable in an expression is in scope (if GROUP BY) 
    checkExprVarUse(query);
    // ENABLE
    if (false && query.hasGroupBy()) {
        VarExprList exprList2 = query.getGroupBy();
        checkExprListAssignment(vars, exprList2);
    // CHECK 
    }
}
Also used : VarExprList(org.apache.jena.sparql.core.VarExprList) QueryParseException(org.apache.jena.query.QueryParseException)

Example 42 with VarExprList

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

the class SyntaxVarScope method checkExprVarUse.

private static void checkExprVarUse(Query query) {
    if (query.hasGroupBy()) {
        VarExprList groupKey = query.getGroupBy();
        // Copy - we need to add variables
        // SELECT (count(*) AS ?C)  (?C+1 as ?D) 
        List<Var> inScopeVars = new ArrayList<>(groupKey.getVars());
        VarExprList exprList = query.getProject();
        for (Var v : exprList.getVars()) {
            // In scope?
            Expr e = exprList.getExpr(v);
            if (e == null) {
                if (!inScopeVars.contains(v)) {
                    throw new QueryParseException("Non-group key variable in SELECT: " + v, -1, -1);
                }
            } else {
                Set<Var> eVars = e.getVarsMentioned();
                for (Var v2 : eVars) {
                    if (!inScopeVars.contains(v2)) {
                        throw new QueryParseException("Non-group key variable in SELECT: " + v2 + " in expression " + e, -1, -1);
                    }
                }
            }
            inScopeVars.add(v);
        }
    }
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList) QueryParseException(org.apache.jena.query.QueryParseException)

Example 43 with VarExprList

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

the class NodeTransformOp method transform.

@Override
public Op transform(OpGroup opGroup, Op subOp) {
    VarExprList groupVars = opGroup.getGroupVars();
    VarExprList groupVars2 = NodeTransformLib.transformVars(transform, groupVars);
    if (groupVars2 == groupVars)
        return super.transform(opGroup, subOp);
    return OpGroup.create(subOp, groupVars2, opGroup.getAggregators());
}
Also used : VarExprList(org.apache.jena.sparql.core.VarExprList)

Example 44 with VarExprList

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

the class SelectClauseTest method testAddVarVar.

@ContractTest
public void testAddVarVar() throws Exception {
    Var v = Var.alloc("one");
    SelectClause<?> selectClause = getProducer().newInstance();
    Query query = getQuery(selectClause.addVar(v));
    VarExprList expr = query.getProject();
    assertEquals(1, expr.size());
    assertTrue(expr.contains(v));
}
Also used : Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList) ContractTest(org.xenei.junit.contract.ContractTest)

Example 45 with VarExprList

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

the class TransformImplicitLeftJoin method safeToTransform.

private static boolean safeToTransform(List<Pair<Var, Var>> joins, 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;
        return safeToTransform(joins, 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(joins, varsEquality, subOp))
                return false;
        }
        return true;
    }
    if (op instanceof OpJoin) {
        Op2 op2 = (Op2) op;
        return safeToTransform(joins, varsEquality, op2.getLeft()) && safeToTransform(joins, varsEquality, op2.getRight());
    }
    if (op instanceof OpUnion) {
        // True only if for any pairs that affect the pattern both variables occur
        Set<Var> fixedVars = OpVars.fixedVars(op);
        for (Pair<Var, Var> pair : joins) {
            if (fixedVars.contains(pair.getLeft()) && !fixedVars.contains(pair.getRight()))
                return false;
            if (!fixedVars.contains(pair.getLeft()) && fixedVars.contains(pair.getRight()))
                return false;
        }
        return true;
    }
    // Not safe unless filter variables are mentioned on the LHS.
    if (op instanceof OpConditional || op instanceof OpLeftJoin) {
        Op2 opleftjoin = (Op2) op;
        if (!safeToTransform(joins, varsEquality, opleftjoin.getLeft()) || !safeToTransform(joins, 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(joins, 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(joins, varsEquality, opMod.getSubOp());
    }
    if (op instanceof OpGroup) {
        OpGroup opGroup = (OpGroup) op;
        VarExprList varExprList = opGroup.getGroupVars();
        return safeToTransform(varsEquality, varExprList) && safeToTransform(joins, varsEquality, opGroup.getSubOp());
    }
    if (op instanceof OpTable) {
        OpTable opTable = (OpTable) op;
        if (opTable.isJoinIdentity())
            return true;
    }
    return false;
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Var(org.apache.jena.sparql.core.Var) 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