Search in sources :

Example 1 with SqlTable

use of org.apache.jena.sdb.core.sqlnode.SqlTable in project jena by apache.

the class SlotCompilerIndex method insertConstantAccesses.

protected SqlNode insertConstantAccesses(Collection<Node> constants) {
    SqlNode sqlNode = null;
    for (Node n : constants) {
        long hash = NodeLayout2.hash(n);
        SqlConstant hashValue = new SqlConstant(hash);
        // Access nodes table.
        SqlTable nTable = new SqlTable(getRequest().genId(NodeConstBase), nodeTableDesc.getTableName());
        nTable.addNote("Const: " + FmtUtils.stringForNode(n, getRequest().getPrefixMapping()));
        SqlColumn cHash = new SqlColumn(nTable, nodeTableDesc.getHashColName());
        // Record 
        constantCols.put(n, new SqlColumn(nTable, nodeTableDesc.getIdColName()));
        SqlExpr c = new S_Equal(cHash, hashValue);
        sqlNode = SqlBuilder.innerJoin(getRequest(), sqlNode, nTable);
        sqlNode = SqlBuilder.restrict(getRequest(), sqlNode, c);
    }
    return sqlNode;
}
Also used : SqlNode(org.apache.jena.sdb.core.sqlnode.SqlNode) Node(org.apache.jena.graph.Node) SqlTable(org.apache.jena.sdb.core.sqlnode.SqlTable) SqlNode(org.apache.jena.sdb.core.sqlnode.SqlNode)

Example 2 with SqlTable

use of org.apache.jena.sdb.core.sqlnode.SqlTable in project jena by apache.

the class SqlStageBasicQuad method build.

// There are three special graph names:
//   Quad.defaultGraph - the quad marker for a query pattern for the default graph.
//   Quad.defaultGraphName - the explicit internal name for the default graph
//   Quad.unionDefaultGraph - the internal name for the union of all named graphs (excluding the degault graph).
//
// If SDB.unionDefaultGraph is true in the context, then patterns on the 
// default graph are made against the union graph.
//
// If the explicit names (defaultGraphName, unionDefaultGraph) are used in GRAPH, 
// then the default or union beahviour is avilable regardless of the setting of
//  SDB.unionDefaultGraph.
@Override
public SqlNode build(SDBRequest request, SlotCompiler slotCompiler) {
    SqlExprList conditions = new SqlExprList();
    // ARQ 2.8.4 quad.isDefaultGraph() ;
    boolean defaultGraph = Quad.isDefaultGraph(quad.getGraph());
    boolean unionGraph = quad.isUnionGraph();
    // ---- Choose the mode of access.
    //quad.isDefaultGraph() ;
    boolean accessStoredDefaultGraph = Quad.isDefaultGraph(quad.getGraph());
    boolean accessUnionGraph = false;
    if (accessStoredDefaultGraph && request.getContext().isTrue(SDB.unionDefaultGraph)) {
        // Treat the default graph as the union of all triples in named graphs.
        defaultGraph = false;
        unionGraph = true;
        accessStoredDefaultGraph = false;
        accessUnionGraph = true;
    }
    // GRAPH <name of default graph> { } 
    if (quad.isDefaultGraphExplicit()) {
        // "named" access to the default graph
        accessStoredDefaultGraph = true;
        accessUnionGraph = false;
    }
    if (quad.isUnionGraph()) {
        // "named" access to the union of the named graphs
        accessStoredDefaultGraph = false;
        accessUnionGraph = true;
    // Add DISTINCT over the join of SqlStageBasicQuad for RDF merge 
    }
    // ---- Choose the table to access
    // The default graph table may be a specialized table (e.g. triples, not quads).
    TableDescQuads tableDesc = null;
    String alias = null;
    if (accessStoredDefaultGraph) {
        tableDesc = request.getStore().getTripleTableDesc();
        alias = request.genId(AliasesSql.TriplesTableBase);
    } else {
        tableDesc = request.getStore().getQuadTableDesc();
        alias = request.genId(AliasesSql.QuadTableBase);
    }
    SqlTable table = new SqlTable(alias, tableDesc.getTableName());
    if (accessStoredDefaultGraph)
        table.addNote(FmtUtils.stringForTriple(quad.asTriple(), request.getPrefixMapping()));
    else
        table.addNote(FmtUtils.stringForQuad(quad, request.getPrefixMapping()));
    if (!accessStoredDefaultGraph && !accessUnionGraph)
        slotCompiler.processSlot(request, table, conditions, quad.getGraph(), tableDesc.getGraphColName());
    slotCompiler.processSlot(request, table, conditions, quad.getSubject(), tableDesc.getSubjectColName());
    slotCompiler.processSlot(request, table, conditions, quad.getPredicate(), tableDesc.getPredicateColName());
    slotCompiler.processSlot(request, table, conditions, quad.getObject(), tableDesc.getObjectColName());
    return SqlBuilder.restrict(request, table, conditions);
}
Also used : TableDescQuads(org.apache.jena.sdb.layout2.TableDescQuads) SqlExprList(org.apache.jena.sdb.core.sqlexpr.SqlExprList) SqlTable(org.apache.jena.sdb.core.sqlnode.SqlTable)

Example 3 with SqlTable

use of org.apache.jena.sdb.core.sqlnode.SqlTable 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 4 with SqlTable

use of org.apache.jena.sdb.core.sqlnode.SqlTable in project jena by apache.

the class SQLBridge2 method insertValueGetter.

private SqlNode insertValueGetter(SDBRequest request, SqlNode sqlNode, Var var) {
    ScopeEntry e1 = sqlNode.getIdScope().findScopeForVar(var);
    if (e1 == null) {
        // Debug.
        Scope scope = sqlNode.getIdScope();
        // Variable not actually in results.
        return sqlNode;
    }
    // Already in scope (from a condition)?
    ScopeEntry e2 = sqlNode.getNodeScope().findScopeForVar(var);
    if (e2 != null)
        // Already there
        return sqlNode;
    SqlColumn c1 = e1.getColumn();
    // Not in scope -- add a table to get it
    TableDescNodes nodeTableDesc = request.getStore().getNodeTableDesc();
    String tableAlias = request.genId(NodeBase);
    SqlTable nTable = new SqlTable(tableAlias, nodeTableDesc.getTableName());
    String nodeKeyColName = nodeTableDesc.getNodeRefColName();
    SqlColumn c2 = new SqlColumn(nTable, nodeKeyColName);
    nTable.setValueColumnForVar(var, c2);
    // Condition for value: triple table column = node table id/hash 
    nTable.addNote("Var: " + var);
    SqlExpr cond = new S_Equal(c1, c2);
    SqlNode n = SqlBuilder.leftJoin(request, sqlNode, nTable, cond);
    return n;
}
Also used : ScopeEntry(org.apache.jena.sdb.core.ScopeEntry) Scope(org.apache.jena.sdb.core.Scope) SqlExpr(org.apache.jena.sdb.core.sqlexpr.SqlExpr) SqlTable(org.apache.jena.sdb.core.sqlnode.SqlTable) S_Equal(org.apache.jena.sdb.core.sqlexpr.S_Equal) SqlColumn(org.apache.jena.sdb.core.sqlexpr.SqlColumn) SqlNode(org.apache.jena.sdb.core.sqlnode.SqlNode)

Example 5 with SqlTable

use of org.apache.jena.sdb.core.sqlnode.SqlTable in project jena by apache.

the class SQLBridge2 method buildProject.

@Override
protected void buildProject() {
    for (Var v : getProject()) {
        if (!v.isNamedVar())
            continue;
        ScopeEntry e = getSqlNode().getNodeScope().findScopeForVar(v);
        if (e == null) {
            // Should be a column mentioned in the SELECT which is not mentioned in this block
            continue;
        }
        SqlColumn vCol = e.getColumn();
        SqlTable table = vCol.getTable();
        String sqlVarName = allocSqlName(v);
        // Need to allocate aliases because otherwise we need to access
        // "table.column" as a label and "." is illegal in a label
        String vLex = SQLUtils.gen(sqlVarName, "lex");
        // Call overrideable helper method for lex column (Oracle)
        SqlColumn cLex = getLexSqlColumn(table);
        String vLexNChar = SQLUtils.gen(sqlVarName, "lexNChar");
        // Call overrideable helper method for lex column (Oracle)
        SqlColumn cLexNChar = getLexNCharSqlColumn(table);
        String vDatatype = SQLUtils.gen(sqlVarName, "datatype");
        SqlColumn cDatatype = new SqlColumn(table, "datatype");
        String vLang = SQLUtils.gen(sqlVarName, "lang");
        SqlColumn cLang = new SqlColumn(table, "lang");
        String vType = SQLUtils.gen(sqlVarName, "type");
        SqlColumn cType = new SqlColumn(table, "type");
        addProject(cLex, vLex);
        // Oracle NCLOB support
        if (cLexNChar != null) {
            addProject(cLexNChar, vLexNChar);
        }
        addProject(cDatatype, vDatatype);
        addProject(cLang, vLang);
        addProject(cType, vType);
        addAnnotation(sqlVarName + "=" + v.toString());
    }
    setAnnotation();
}
Also used : ScopeEntry(org.apache.jena.sdb.core.ScopeEntry) Var(org.apache.jena.sparql.core.Var) SqlTable(org.apache.jena.sdb.core.sqlnode.SqlTable) SqlColumn(org.apache.jena.sdb.core.sqlexpr.SqlColumn)

Aggregations

SqlTable (org.apache.jena.sdb.core.sqlnode.SqlTable)5 SqlColumn (org.apache.jena.sdb.core.sqlexpr.SqlColumn)3 SqlNode (org.apache.jena.sdb.core.sqlnode.SqlNode)3 Node (org.apache.jena.graph.Node)2 ScopeEntry (org.apache.jena.sdb.core.ScopeEntry)2 TableDescQuads (org.apache.jena.sdb.layout2.TableDescQuads)2 Var (org.apache.jena.sparql.core.Var)2 Scope (org.apache.jena.sdb.core.Scope)1 S_Equal (org.apache.jena.sdb.core.sqlexpr.S_Equal)1 SqlExpr (org.apache.jena.sdb.core.sqlexpr.SqlExpr)1 SqlExprList (org.apache.jena.sdb.core.sqlexpr.SqlExprList)1 SDBInternalError (org.apache.jena.sdb.shared.SDBInternalError)1