use of org.apache.jena.sdb.core.ScopeEntry 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);
}
use of org.apache.jena.sdb.core.ScopeEntry in project jena by apache.
the class SlotCompiler method processSlot.
public final void processSlot(SDBRequest request, SqlTable table, SqlExprList conditions, Node node, String colName) {
SqlColumn thisCol = new SqlColumn(table, colName);
if (!node.isVariable()) {
constantSlot(request, node, thisCol, conditions);
return;
}
Var var = Var.alloc(node);
if (table.getIdScope().hasColumnForVar(var)) {
ScopeEntry e = table.getIdScope().findScopeForVar(var);
SqlColumn otherCol = e.getColumn();
SqlExpr c = new S_Equal(otherCol, thisCol);
conditions.add(c);
c.addNote("processVar: " + node);
return;
}
table.setIdColumnForVar(var, thisCol);
}
use of org.apache.jena.sdb.core.ScopeEntry 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;
}
use of org.apache.jena.sdb.core.ScopeEntry in project jena by apache.
the class SqlBuilder method join.
// private static String sqlNodeName(SqlNode sNode)
// {
// if ( sNode == null ) return "<null>" ;
// if ( sNode.isProject() ) return "Project" ;
// if ( sNode.isRestrict() ) return "Restrict/"+sqlNodeName(sNode.asRestrict().getSubNode()) ;
// if ( sNode.isTable() ) return "Table" ;
// if ( sNode.isInnerJoin() ) return "JoinInner" ;
// if ( sNode.isLeftJoin() ) return "Joinleft" ;
// if ( sNode.isCoalesce() ) return "Coalesce" ;
// return "<unknown>" ;
// }
// Join/LeftJoin two subexpressions, calculating the join conditions in the process
// If a coalesce (LeftJoin) then don't equate left and right vars of the same name.
// A SqlCoalesce is a special case of LeftJoin where ignoreVars!=null
private static SqlJoin join(SDBRequest request, JoinType joinType, SqlNode left, SqlNode right, Set<Var> ignoreVars) {
SqlExprList conditions = new SqlExprList();
if (joinType == INNER)
// Put any left filter into the join conditions.
// Does not apply to LEFT because the LHS filter does not apply to the right in the same way.
left = extractRestrict(left, conditions);
right = extractRestrict(right, conditions);
for (Var v : left.getIdScope().getVars()) {
if (right.getIdScope().hasColumnForVar(v)) {
ScopeEntry sLeft = left.getIdScope().findScopeForVar(v);
ScopeEntry sRight = right.getIdScope().findScopeForVar(v);
SqlExpr c = joinCondition(joinType, sLeft, sRight);
conditions.add(c);
c.addNote("Join var: " + v);
}
}
SqlJoin join = SqlJoin.create(joinType, left, right);
join.addConditions(conditions);
return join;
}
use of org.apache.jena.sdb.core.ScopeEntry in project jena by apache.
the class SQLBridge1 method buildProject.
@Override
protected void buildProject() {
for (Var v : getProject()) {
if (!v.isNamedVar())
continue;
// Value scope == IdScope for layout1
// CHECK
ScopeEntry e = getSqlExprNode().getIdScope().findScopeForVar(v);
if (e == null)
continue;
SqlColumn c = e.getColumn();
String sqlVarName = allocSqlName(v);
addProject(c, sqlVarName);
addAnnotation(sqlVarName + "=" + v.toString());
}
setAnnotation();
}
Aggregations