Search in sources :

Example 31 with Var

use of org.apache.jena.sparql.core.Var 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 32 with Var

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

the class ElementRewriter method visit.

@Override
public void visit(ElementData el) {
    ElementData retval = new ElementData();
    Iterator<Var> vars = el.getVars().iterator();
    Iterator<Binding> bindings = el.getRows().iterator();
    while (vars.hasNext()) {
        Var v = vars.next();
        if (values.containsKey(v)) {
            // skip the binding
            bindings.next();
        } else {
            retval.add(v);
            retval.add(rewrite(bindings.next()));
        }
    }
    push(retval);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var)

Example 33 with Var

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

the class TransformSDB method transform.

// Now done in QueryCompilerMain at a later stage.
//    @Override
//    public Op transform(OpSlice opSlice, Op subOp)
//    {
//        if ( ! request.LimitOffsetTranslation )
//            return super.transform(opSlice, subOp) ;
//
//        // Not a slice of SQL
//        if ( ! SDB_QC.isOpSQL(subOp) )
//            return super.transform(opSlice, subOp) ;
//        
//        // Two cases are currently handled:
//        // (slice (project (sql expression)))
//        // (slice (sql expression))
//
//        if ( isProject(opSlice.getSubOp()) )
//            // This should not happen because the pre-transform done in QueryEngineMain
//            // rewrites the case into the equivalent (project (slice ...))
//            return transformSliceProject(opSlice, (OpSQL)subOp, ((OpSQL)subOp).getSqlNode()) ;
//
//        // (slice (sql expression))
//        return transformSlice(opSlice, ((OpSQL)subOp).getSqlNode()) ;
//    }
//        
//    private Op transformSlice(OpSlice opSlice, SqlNode sqlSubOp)
//    {
//        SqlNode n = SqlSelectBlock.slice(request, sqlSubOp, opSlice.getStart(), opSlice.getLength()) ;
//        OpSQL x = new OpSQL(n, opSlice, request) ;
//        return x ;
//    }
//    
//    private Op transformSliceProject(OpSlice opSlice, OpSQL subOp, SqlNode sqlOp)
//    {
//        // This put the LIMIT outside all the projects left joins, which is a bit of a pity.
//        // Could improve by rewriting (slice (project...)) into (project (slice...)) in QueryCompilerMain 
//        OpSQL x = (OpSQL)transformSlice(opSlice, sqlOp) ;
//        SQLBridge bridge = subOp.getBridge() ;
//        return x ;
//    }
//    // ----
//    
//    private boolean translateConstraints = true ;
//    
//    private SDBConstraint transformFilter(OpFilter opFilter)
//    {
//        if ( ! translateConstraints )
//            return null ;
//        
//        ExprList exprs = opFilter.getExprs() ;
//        List<Expr> x = exprs.getList() ;
//        for ( Expr  expr : x )
//        {
//            ConditionCompiler cc = new RegexCompiler() ;
//            SDBConstraint psc = cc.recognize(expr) ;
//            if ( psc != null )
//                return psc ; 
//        }
//        return null ;
//    }
//
//    private Set<Var> getVarsInFilter(Expr expr)
//    {
//        Set<Var> vars = expr.getVarsMentioned() ;
//        return vars ;
//    }
@Override
public Op transform(OpDatasetNames opDatasetNames) {
    if (false)
        return super.transform(opDatasetNames);
    // Basically, an implementation of "GRAPH ?g {}" 
    Node g = opDatasetNames.getGraphNode();
    if (!Var.isVar(g))
        throw new SDBInternalError("OpDatasetNames - not a variable: " + g);
    Var v = Var.alloc(g);
    // Inner SELECT SQL: (SELECT DISTINCT g FROM Quads)
    TableDescQuads quads = request.getStore().getQuadTableDesc();
    SqlTable sqlTableQ = new SqlTable(quads.getTableName());
    sqlTableQ.setIdColumnForVar(v, new SqlColumn(sqlTableQ, quads.getGraphColName()));
    SqlNode sqlNodeQ = SqlSelectBlock.distinct(request, sqlTableQ);
    // Will have the value left join added later. 
    return new OpSQL(sqlNodeQ, opDatasetNames, request);
}
Also used : TableDescQuads(org.apache.jena.sdb.layout2.TableDescQuads) Var(org.apache.jena.sparql.core.Var) SqlNode(org.apache.jena.sdb.core.sqlnode.SqlNode) Node(org.apache.jena.graph.Node) SDBInternalError(org.apache.jena.sdb.shared.SDBInternalError) SqlTable(org.apache.jena.sdb.core.sqlnode.SqlTable) SqlColumn(org.apache.jena.sdb.core.sqlexpr.SqlColumn) SqlNode(org.apache.jena.sdb.core.sqlnode.SqlNode)

Example 34 with Var

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

the class TransformSDB method transform.

@Override
public Op transform(OpLeftJoin opJoin, Op left, Op right) {
    if (!request.LeftJoinTranslation)
        return super.transform(opJoin, left, right);
    if (!SDB_QC.isOpSQL(left) || !SDB_QC.isOpSQL(right))
        return super.transform(opJoin, left, right);
    // Condition(s) in the left join.  Punt for now. 
    if (opJoin.getExprs() != null)
        return super.transform(opJoin, left, right);
    SqlNode sqlLeft = ((OpSQL) left).getSqlNode();
    SqlNode sqlRight = ((OpSQL) right).getSqlNode();
    // Check for coalesce.
    // Do optional variables on the right appear only as optional variables on the left?
    Set<ScopeEntry> scopes = sqlLeft.getIdScope().findScopes();
    // Find optional-on-left
    Set<ScopeEntry> scopes2 = toSet(filter(scopes.iterator(), ScopeEntry.OptionalFilter));
    // Vars from left optionals.
    Set<Var> leftOptVars = toSet(map(scopes2.iterator(), ScopeEntry::getVar));
    if (false) {
        Iter<ScopeEntry> iter = Iter.iter(scopes);
        Set<Var> leftOptVars_ = iter.filter(ScopeEntry.OptionalFilter).map(ScopeEntry::getVar).toSet();
    }
    // Find optional-on-right (easier - it's all variables) 
    Set<Var> rightOptVars = sqlRight.getIdScope().getVars();
    // And finally, calculate the intersection of the two.
    // SetUtils extension - one side could be an iterator  
    Set<Var> coalesceVars = intersection(leftOptVars, rightOptVars);
    if (coalesceVars.size() > 0) {
        String alias = request.genId(AliasesSql.CoalesceAliasBase);
        SqlNode sqlNode = SqlBuilder.leftJoinCoalesce(request, alias, sqlLeft, sqlRight, coalesceVars);
        return new OpSQL(sqlNode, opJoin, request);
    // Punt
    //return super.transform(opJoin, left, right) ;
    }
    return new OpSQL(SqlBuilder.leftJoin(request, sqlLeft, sqlRight, null), opJoin, request);
}
Also used : ScopeEntry(org.apache.jena.sdb.core.ScopeEntry) Var(org.apache.jena.sparql.core.Var) SqlNode(org.apache.jena.sdb.core.sqlnode.SqlNode)

Example 35 with Var

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

the class QBR_SubProperty method rewrite.

@Override
public QuadBlock rewrite(SDBRequest request, QuadBlock quadBlock) {
    if (!quadBlock.contains(null, null, rdfType, null))
        return quadBlock;
    quadBlock = new QuadBlock(quadBlock);
    int i = 0;
    // Better/clearer : do as copy over from one block to another. 
    while ((i = quadBlock.findFirst(i, null, null, rdfType, null)) != -1) {
        // { :s rdf:type :C } => { :s rdf:type ?V . ?V rdfs:subClassOf :C } 
        Quad rdfTypeQuad = quadBlock.get(i);
        Var var = request.genVar();
        Quad q1 = new Quad(rdfTypeQuad.getGraph(), rdfTypeQuad.getSubject(), rdfType, var);
        Quad q2 = new Quad(rdfTypeQuad.getGraph(), var, RDFS.subClassOf.asNode(), rdfTypeQuad.getObject());
        // replace rdf:type statement
        quadBlock.set(i, q1);
        // add subClassOf statement
        quadBlock.add(i + 1, q2);
        // Skip the two statements.
        i = i + 2;
    }
    return quadBlock;
}
Also used : QuadBlock(org.apache.jena.sdb.compiler.QuadBlock) Quad(org.apache.jena.sparql.core.Quad) Var(org.apache.jena.sparql.core.Var)

Aggregations

Var (org.apache.jena.sparql.core.Var)264 Node (org.apache.jena.graph.Node)83 ArrayList (java.util.ArrayList)53 Test (org.junit.Test)47 Binding (org.apache.jena.sparql.engine.binding.Binding)33 VarExprList (org.apache.jena.sparql.core.VarExprList)30 Op (org.apache.jena.sparql.algebra.Op)29 Expr (org.apache.jena.sparql.expr.Expr)28 Triple (org.apache.jena.graph.Triple)17 HashMap (java.util.HashMap)15 ContractTest (org.xenei.junit.contract.ContractTest)13 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)12 Query (org.apache.jena.query.Query)11 ExprList (org.apache.jena.sparql.expr.ExprList)11 SortCondition (org.apache.jena.query.SortCondition)10 ExprVar (org.apache.jena.sparql.expr.ExprVar)10 HashSet (java.util.HashSet)9 Pair (org.apache.jena.atlas.lib.Pair)9 SqlColumn (org.apache.jena.sdb.core.sqlexpr.SqlColumn)9 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)9