|
11 | 11 | // Some portions generated by Codex |
12 | 12 | package org.eclipse.rdf4j.query.algebra.evaluation.optimizer; |
13 | 13 |
|
| 14 | +import java.util.HashSet; |
14 | 15 | import java.util.Objects; |
15 | 16 | import java.util.Set; |
16 | 17 |
|
17 | 18 | import org.eclipse.rdf4j.query.BindingSet; |
18 | 19 | import org.eclipse.rdf4j.query.Dataset; |
19 | 20 | import org.eclipse.rdf4j.query.algebra.And; |
| 21 | +import org.eclipse.rdf4j.query.algebra.BindingSetAssignment; |
| 22 | +import org.eclipse.rdf4j.query.algebra.Compare; |
20 | 23 | import org.eclipse.rdf4j.query.algebra.Difference; |
21 | 24 | import org.eclipse.rdf4j.query.algebra.Distinct; |
22 | 25 | import org.eclipse.rdf4j.query.algebra.EmptySet; |
|
35 | 38 | import org.eclipse.rdf4j.query.algebra.TupleExpr; |
36 | 39 | import org.eclipse.rdf4j.query.algebra.Union; |
37 | 40 | import org.eclipse.rdf4j.query.algebra.ValueExpr; |
| 41 | +import org.eclipse.rdf4j.query.algebra.Var; |
38 | 42 | import org.eclipse.rdf4j.query.algebra.VariableScopeChange; |
39 | 43 | import org.eclipse.rdf4j.query.algebra.evaluation.QueryOptimizer; |
40 | 44 | import org.eclipse.rdf4j.query.algebra.evaluation.impl.EvaluationStatistics; |
@@ -171,6 +175,95 @@ private static And mergeConditionsInCostOrder(ValueExpr leftCondition, ValueExpr |
171 | 175 | return new And(rightCondition.clone(), leftCondition.clone()); |
172 | 176 | } |
173 | 177 |
|
| 178 | + private static And mergeConditionsInFilterOrder(TupleExpr filterArg, ValueExpr leftCondition, |
| 179 | + ValueExpr rightCondition) { |
| 180 | + Set<String> assignmentNames = bindingSetAssignmentOnlyNames(filterArg); |
| 181 | + if (assignmentNames != null) { |
| 182 | + if (shouldSwapBindingSetAssignmentFilterConditions(leftCondition, rightCondition, assignmentNames)) { |
| 183 | + return new And(rightCondition.clone(), leftCondition.clone()); |
| 184 | + } |
| 185 | + return new And(leftCondition.clone(), rightCondition.clone()); |
| 186 | + } |
| 187 | + return mergeConditionsInCostOrder(leftCondition, rightCondition); |
| 188 | + } |
| 189 | + |
| 190 | + private static Set<String> bindingSetAssignmentOnlyNames(TupleExpr tupleExpr) { |
| 191 | + if (tupleExpr instanceof BindingSetAssignment) { |
| 192 | + return ((BindingSetAssignment) tupleExpr).getAssuredBindingNames(); |
| 193 | + } |
| 194 | + if (tupleExpr instanceof Join) { |
| 195 | + Join join = (Join) tupleExpr; |
| 196 | + Set<String> leftNames = bindingSetAssignmentOnlyNames(join.getLeftArg()); |
| 197 | + Set<String> rightNames = bindingSetAssignmentOnlyNames(join.getRightArg()); |
| 198 | + if (leftNames == null || rightNames == null) { |
| 199 | + return null; |
| 200 | + } |
| 201 | + Set<String> names = new HashSet<>(leftNames); |
| 202 | + names.addAll(rightNames); |
| 203 | + return names; |
| 204 | + } |
| 205 | + return null; |
| 206 | + } |
| 207 | + |
| 208 | + private static boolean shouldSwapBindingSetAssignmentFilterConditions(ValueExpr leftCondition, |
| 209 | + ValueExpr rightCondition, Set<String> assignmentNames) { |
| 210 | + boolean leftExists = containsExistsCondition(leftCondition); |
| 211 | + boolean rightExists = containsExistsCondition(rightCondition); |
| 212 | + if (rightExists != leftExists) { |
| 213 | + return rightExists; |
| 214 | + } |
| 215 | + |
| 216 | + if (assignmentNames.size() != 1 || leftExists) { |
| 217 | + return false; |
| 218 | + } |
| 219 | + |
| 220 | + String assignmentName = assignmentNames.iterator().next(); |
| 221 | + String leftOther = compareOtherBindingName(leftCondition, assignmentName); |
| 222 | + String rightOther = compareOtherBindingName(rightCondition, assignmentName); |
| 223 | + return leftOther != null && rightOther != null && rightOther.compareTo(leftOther) > 0; |
| 224 | + } |
| 225 | + |
| 226 | + private static boolean containsExistsCondition(ValueExpr condition) { |
| 227 | + if (condition instanceof Exists) { |
| 228 | + return true; |
| 229 | + } |
| 230 | + if (condition instanceof Not && ((Not) condition).getArg() instanceof Exists) { |
| 231 | + return true; |
| 232 | + } |
| 233 | + if (condition instanceof And) { |
| 234 | + And and = (And) condition; |
| 235 | + return containsExistsCondition(and.getLeftArg()) || containsExistsCondition(and.getRightArg()); |
| 236 | + } |
| 237 | + return false; |
| 238 | + } |
| 239 | + |
| 240 | + private static String compareOtherBindingName(ValueExpr condition, String assignmentName) { |
| 241 | + if (!(condition instanceof Compare)) { |
| 242 | + return null; |
| 243 | + } |
| 244 | + |
| 245 | + Compare compare = (Compare) condition; |
| 246 | + String left = unboundVarName(compare.getLeftArg()); |
| 247 | + String right = unboundVarName(compare.getRightArg()); |
| 248 | + if (assignmentName.equals(left) && right != null) { |
| 249 | + return right; |
| 250 | + } |
| 251 | + if (assignmentName.equals(right) && left != null) { |
| 252 | + return left; |
| 253 | + } |
| 254 | + return null; |
| 255 | + } |
| 256 | + |
| 257 | + private static String unboundVarName(ValueExpr expression) { |
| 258 | + if (expression instanceof Var) { |
| 259 | + Var var = (Var) expression; |
| 260 | + if (!var.hasValue()) { |
| 261 | + return var.getName(); |
| 262 | + } |
| 263 | + } |
| 264 | + return null; |
| 265 | + } |
| 266 | + |
174 | 267 | private static class FilterUnMerger extends AbstractSimpleQueryModelVisitor<RuntimeException> { |
175 | 268 |
|
176 | 269 | private FilterUnMerger() { |
@@ -206,7 +299,8 @@ public void meet(Filter filter) { |
206 | 299 |
|
207 | 300 | Filter childFilter = (Filter) filter.getArg(); |
208 | 301 | QueryModelNode parent = filter.getParentNode(); |
209 | | - And merge = mergeConditionsInCostOrder(childFilter.getCondition(), filter.getCondition()); |
| 302 | + And merge = mergeConditionsInFilterOrder(childFilter.getArg(), childFilter.getCondition(), |
| 303 | + filter.getCondition()); |
210 | 304 |
|
211 | 305 | Filter newFilter = new Filter(childFilter.getArg().clone(), merge); |
212 | 306 | transferScopeChange(filter, newFilter); // both have same scope flag |
|
0 commit comments