Skip to content

Commit

Permalink
Merge branch 'main' into apachegh-1474
Browse files Browse the repository at this point in the history
  • Loading branch information
Aklakan committed Aug 11, 2022
2 parents 51784a1 + 425b99b commit c09d112
Show file tree
Hide file tree
Showing 116 changed files with 12,227 additions and 402 deletions.
324 changes: 185 additions & 139 deletions jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpVars.java

Large diffs are not rendered by default.

97 changes: 47 additions & 50 deletions jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

package org.apache.jena.sparql.algebra;

import java.util.Iterator ;
import java.util.Iterator;

import org.apache.jena.sparql.algebra.op.* ;
import org.apache.jena.sparql.algebra.op.*;

/** Apply a visitor to the whole structure of Ops, recursively.
* Visit sub Op before the current level
Expand All @@ -30,121 +30,118 @@ public class OpWalker
{
// Predates org.apache.jena.sparql.algebra.walker.Walker
// Need to convert but OpVars depends on OpWalkerVisitorFixed, OpWalkerVisitorVisible

/* OpVars::
public static void visibleVars(Op op, Set<Var> acc) {
OpVarsPattern visitor = new OpVarsPattern(acc, true) ;
OpVarsPattern visitor = new OpVarsPattern(acc, true);
// Does not work.
//new WalkerVisitorVisible(visitor, null, null, null).walk(op);
//OpWalker.walk(new OpWalkerVisitorVisible(visitor, acc), op) ;
//OpWalker.walk(new OpWalkerVisitorVisible(visitor, acc), op);
}
*/

// **** Replace with org.apache.jena.sparql.algebra.walker.Walker
public static void walk(Op op, OpVisitor visitor)
{
public static void walk(Op op, OpVisitor visitor) {
// new walker :: this works
// Walker.walk(op, visitor);
walk(new OpWalkerVisitor(visitor, null, null), op) ;
walk(new OpWalkerVisitor(visitor, null, null), op);
}

// public static void walk(Op op, OpVisitor visitor, OpVisitor beforeVisitor, OpVisitor afterVisitor)
// {
// walk(new OpWalkerVisitor(visitor, beforeVisitor, afterVisitor), op) ;

// public static void walk(Op op, OpVisitor visitor, OpVisitor beforeVisitor, OpVisitor afterVisitor) {
// walk(new OpWalkerVisitor(visitor, beforeVisitor, afterVisitor), op);
// }

public static void walk(OpWalkerVisitor walkerVisitor, Op op)
{
// Stil needed for OpWalkerVisitorVisible, OpWalkerVisitorFixed
op.visit(walkerVisitor) ;

public static void walk(OpWalkerVisitor walkerVisitor, Op op) {
// Still needed for OpWalkerVisitorVisible, OpWalkerVisitorFixed
op.visit(walkerVisitor);
}

protected static class OpWalkerVisitor extends OpVisitorByType {
private final OpVisitor beforeVisitor ;
private final OpVisitor afterVisitor ;
protected final OpVisitor visitor ;
private final OpVisitor beforeVisitor;
private final OpVisitor afterVisitor;
protected final OpVisitor visitor;

public OpWalkerVisitor(OpVisitor visitor, OpVisitor beforeVisitor, OpVisitor afterVisitor) {
this.visitor = visitor ;
this.beforeVisitor = beforeVisitor ;
this.afterVisitor = afterVisitor ;
this.visitor = visitor;
this.beforeVisitor = beforeVisitor;
this.afterVisitor = afterVisitor;
}

public OpWalkerVisitor(OpVisitor visitor) {
this(visitor, null, null) ;
this(visitor, null, null);
}

protected final void before(Op op) {
if ( beforeVisitor != null )
op.visit(beforeVisitor) ;
op.visit(beforeVisitor);
}

protected final void after(Op op) {
if ( afterVisitor != null )
op.visit(afterVisitor) ;
op.visit(afterVisitor);
}

@Override
protected void visit0(Op0 op) {
before(op) ;
before(op);
if ( visitor != null )
op.visit(visitor) ;
after(op) ;
op.visit(visitor);
after(op);
}

@Override
protected void visit1(Op1 op) {
before(op) ;
before(op);
if ( op.getSubOp() != null )
op.getSubOp().visit(this) ;
op.getSubOp().visit(this);
if ( visitor != null )
op.visit(visitor) ;
after(op) ;
op.visit(visitor);
after(op);
}

@Override
protected void visitFilter(OpFilter op) {
visit1(op) ;
visit1(op);
}

@Override
protected void visitLeftJoin(OpLeftJoin op) {
visit2(op) ;
visit2(op);
}

@Override
protected void visit2(Op2 op) {
before(op) ;
before(op);
if ( op.getLeft() != null )
op.getLeft().visit(this) ;
op.getLeft().visit(this);
if ( op.getRight() != null )
op.getRight().visit(this) ;
op.getRight().visit(this);
if ( visitor != null )
op.visit(visitor) ;
after(op) ;
op.visit(visitor);
after(op);
}

@Override
protected void visitN(OpN op) {
before(op) ;
before(op);
for (Iterator<Op> iter = op.iterator(); iter.hasNext();) {
Op sub = iter.next() ;
sub.visit(this) ;
Op sub = iter.next();
sub.visit(this);
}
if ( visitor != null )
op.visit(visitor) ;
after(op) ;
op.visit(visitor);
after(op);
}

@Override
protected void visitExt(OpExt op) {
before(op) ;
before(op);
if ( op.effectiveOp() != null )
// Walk the effective op, if present.
op.effectiveOp().visit(this);
if ( visitor != null )
op.visit(visitor) ;
after(op) ;
op.visit(visitor);
after(op);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* Optimizer for transforming implicit joins. These covers queries like the
* following:
* </p>
*
*
* <pre>
* SELECT *
* WHERE
Expand All @@ -57,7 +57,7 @@
* would otherwise be required to evaluate. The optimization where applicable
* results in a query of the following form:
* </p>
*
*
* <pre>
* SELECT *
* WHERE
Expand Down Expand Up @@ -139,13 +139,13 @@ private static Op apply(ExprList exprs, Op subOp) {
op = OpFilter.filterBy(exprs, op);
return op;
}

// Special case : filter is over a union where one/both sides are always false
if (testSpecialCaseUnion(subOp, joins)) {
// This will attempt to eliminate the sides that are always false
op = processSpecialCaseUnion(subOp, joins);
// In the case where both sides were invalid we'll have a table empty

// In the case where both sides were invalid we'll have a table empty
// operator at this point and can return immediately
if (op instanceof OpTable) return op;
}
Expand Down Expand Up @@ -286,7 +286,7 @@ private static boolean safeToTransform(List<Pair<Var, Var>> joins, Collection<Va
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);
Expand Down Expand Up @@ -392,13 +392,13 @@ private static boolean testSpecialCaseOptional(Op op, List<Pair<Var, Var>> joins
}
return isTableUnit(op);
}

private static boolean testSpecialCaseUnion(Op op, List<Pair<Var, Var>> joins) {
if (op instanceof OpUnion) {
OpUnion union = (OpUnion) op;
Set<Var> leftVars = OpVars.visibleVars(union.getLeft());
Set<Var> rightVars = OpVars.visibleVars(union.getRight());

// Is a special case if there is any implicit join where only one of the variables mentioned in an
// implicit join is present on one side of the union
for (Pair<Var, Var> p : joins) {
Expand Down Expand Up @@ -449,22 +449,22 @@ private static boolean isTableUnit(Op op) {
}
return false;
}

private static Op processSpecialCaseUnion(Op op, List<Pair<Var, Var>> joins) {
if (op instanceof OpUnion) {
OpUnion union = (OpUnion) op;

Set<Var> leftVars = OpVars.visibleVars(union.getLeft());
Set<Var> rightVars = OpVars.visibleVars(union.getRight());

// Is a special case if there is any implicit join where only one of the variables mentioned in an
// implicit join is present on one side of the union
boolean leftEmpty = false, rightEmpty = false;
for (Pair<Var, Var> p : joins) {
if (leftEmpty || !leftVars.contains(p.getLeft()) || !leftVars.contains(p.getRight())) leftEmpty = true;
if (rightEmpty || !rightVars.contains(p.getLeft()) || !rightVars.contains(p.getRight())) rightEmpty = true;
}

// If both sides of the union guarantee to produce errors then just replace the whole thing with table empty
if (leftEmpty && rightEmpty) return OpTable.empty();
if (leftEmpty) return union.getRight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* Optimizer for transforming implicit joins. These covers queries like the
* following:
* </p>
*
*
* <pre>
* SELECT *
* WHERE
Expand All @@ -60,7 +60,7 @@
* would otherwise be required to evaluate. The optimization where applicable
* results in a query of the following form:
* </p>
*
*
* <pre>
* SELECT *
* WHERE
Expand Down Expand Up @@ -107,10 +107,10 @@ public Op transform(OpLeftJoin opLeftJoin, Op left, Op right) {
private static Op apply(OpLeftJoin opLeftJoin, Op left, Op right) {
// This handles arbitrarily nested && conditions
ExprList orig = ExprList.splitConjunction(opLeftJoin.getExprs());

// Extract optimizable conditions?
Pair<List<Pair<Var, Var>>, ExprList> p = preprocessFilterImplicitJoin(left, right, orig);

// Were there any optimizable conditions?
if (p == null || p.getLeft().size() == 0)
return null;
Expand Down Expand Up @@ -245,7 +245,6 @@ private static Pair<Var, Var> preprocess(Op opLeft, Op opRight, Expr e) {
if (e instanceof E_Equals) {
// Is a safe equals for this optimization?
Tuple<Set<Var>> varsByPosition = OpVars.mentionedVarsByPosition(opLeft, opRight);

if (!isSafeEquals(varsByPosition, left.asVar(), right.asVar()))
return null;
}
Expand Down Expand Up @@ -330,7 +329,7 @@ private static boolean safeToTransform(List<Pair<Var, Var>> joins, Collection<Va
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);
Expand Down
Loading

0 comments on commit c09d112

Please sign in to comment.