use of org.apache.jena.sparql.core.VarExprList in project jena by apache.
the class TestOptimizer method combine_extend_01.
@Test
public void combine_extend_01() {
Op extend = OpExtend.create(OpTable.unit(), new VarExprList(Var.alloc("x"), new NodeValueInteger(1)));
extend = OpExtend.create(extend, new VarExprList(Var.alloc("y"), new NodeValueInteger(2)));
String opExpectedString = StrUtils.strjoinNL("(extend ((?x 1) (?y 2))", " (table unit))");
check(extend, new TransformExtendCombine(), opExpectedString);
}
use of org.apache.jena.sparql.core.VarExprList in project jena by apache.
the class TestOptimizer method combine_assign_03.
@Test
public void combine_assign_03() {
Op assign = OpAssign.create(OpTable.unit(), new VarExprList(Var.alloc("x"), new NodeValueInteger(1)));
assign = OpAssign.create(assign, new VarExprList(Var.alloc("x"), new NodeValueInteger(2)));
String opExpectedString = StrUtils.strjoinNL("(assign ((?x 2))", " (assign ((?x 1))", " (table unit)))");
check(assign, new TransformExtendCombine(), opExpectedString);
}
use of org.apache.jena.sparql.core.VarExprList in project jena by apache.
the class TransformFilterEquality method safeToTransform.
private static boolean safeToTransform(Collection<Var> varsEquality, Op op) {
// Structure as a visitor?
if (op instanceof OpBGP || op instanceof OpQuadPattern)
return true;
if (op instanceof OpPath)
return true;
if (op instanceof OpFilter) {
OpFilter opf = (OpFilter) op;
// Expressions are always safe transform by substitution.
return safeToTransform(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(varsEquality, subOp))
return false;
}
return true;
}
if (op instanceof OpJoin || op instanceof OpUnion) {
Op2 op2 = (Op2) op;
return safeToTransform(varsEquality, op2.getLeft()) && safeToTransform(varsEquality, op2.getRight());
}
// Not safe unless filter variables are mentioned on the LHS.
if (op instanceof OpConditional || op instanceof OpLeftJoin) {
Op2 opleftjoin = (Op2) op;
if (!safeToTransform(varsEquality, opleftjoin.getLeft()) || !safeToTransform(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(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(varsEquality, opMod.getSubOp());
}
if (op instanceof OpGroup) {
OpGroup opGroup = (OpGroup) op;
VarExprList varExprList = opGroup.getGroupVars();
return safeToTransform(varsEquality, varExprList) && safeToTransform(varsEquality, opGroup.getSubOp());
}
if (op instanceof OpTable) {
OpTable opTable = (OpTable) op;
if (opTable.isJoinIdentity())
return true;
}
return false;
}
use of org.apache.jena.sparql.core.VarExprList in project jena by apache.
the class TransformRemoveAssignment method processAssignments.
private VarExprList processAssignments(OpExtendAssign opAssign) {
VarExprList orig = opAssign.getVarExprList();
if (!orig.contains(this.var))
return null;
if (!orig.getExpr(this.var).equals(this.expr))
return null;
VarExprList modified = new VarExprList();
for (Var v : orig.getVars()) {
if (!v.equals(this.var)) {
modified.add(v, orig.getExpr(v));
}
}
if (modified.size() > 0 && modified.size() == orig.size())
return null;
return modified;
}
use of org.apache.jena.sparql.core.VarExprList in project jena by apache.
the class TransformFilterImplicitJoin 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;
}
Aggregations