use of org.apache.jena.sdb.core.sqlnode.SqlNode 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;
}
use of org.apache.jena.sdb.core.sqlnode.SqlNode in project jena by apache.
the class TransformSDB method doBridge.
// See QueryCompilerMain.SqlNodesFinisher.visit(OpExt op)
// Be careful about being done twice.
// XXX SHARE CODE!
private static OpSQL doBridge(SDBRequest request, OpSQL opSQL, List<Var> projectVars, Op original) {
SqlNode sqlNode = opSQL.getSqlNode();
SQLBridgeFactory f = request.getStore().getSQLBridgeFactory();
SQLBridge bridge = f.create(request, sqlNode, projectVars);
bridge.build();
sqlNode = bridge.getSqlNode();
opSQL = new OpSQL(sqlNode, original, request);
opSQL.setBridge(bridge);
// New is better?
opSQL.resetSqlNode(sqlNode);
return opSQL;
}
use of org.apache.jena.sdb.core.sqlnode.SqlNode in project jena by apache.
the class TransformSDB method transform.
// Modifiers: the structure is:
// slice
// distinct/reduced
// project
// order
// having
// group
// [toList]
// modifier : having
// modifier : group
// ---- Modifiers
@Override
public Op transform(OpDistinct opDistinct, Op subOp) {
if (!SDB_QC.isOpSQL(subOp))
return super.transform(opDistinct, subOp);
// Derby does not support DISTINCT on CLOBS
if (!request.DistinctTranslation)
return super.transform(opDistinct, subOp);
OpSQL opSubSQL = (OpSQL) subOp;
SqlNode sqlSubOp = opSubSQL.getSqlNode();
SqlNode n = SqlSelectBlock.distinct(request, sqlSubOp);
OpSQL opSQL = new OpSQL(n, opDistinct, request);
// Pull up bridge, if any
opSQL.setBridge(opSubSQL.getBridge());
return opSQL;
}
use of org.apache.jena.sdb.core.sqlnode.SqlNode 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);
}
use of org.apache.jena.sdb.core.sqlnode.SqlNode 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);
}
Aggregations