|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +// Some portions generated by Codex |
| 12 | +package org.eclipse.rdf4j.query.algebra.evaluation.optimizer; |
| 13 | + |
| 14 | +import java.util.Collection; |
| 15 | +import java.util.Set; |
| 16 | + |
| 17 | +import org.eclipse.rdf4j.query.BindingSet; |
| 18 | +import org.eclipse.rdf4j.query.Dataset; |
| 19 | +import org.eclipse.rdf4j.query.algebra.BindingSetAssignment; |
| 20 | +import org.eclipse.rdf4j.query.algebra.Join; |
| 21 | +import org.eclipse.rdf4j.query.algebra.StatementPattern; |
| 22 | +import org.eclipse.rdf4j.query.algebra.TupleExpr; |
| 23 | +import org.eclipse.rdf4j.query.algebra.Var; |
| 24 | +import org.eclipse.rdf4j.query.algebra.evaluation.QueryOptimizer; |
| 25 | +import org.eclipse.rdf4j.query.algebra.helpers.AbstractSimpleQueryModelVisitor; |
| 26 | + |
| 27 | +/** |
| 28 | + * Reorders joins to avoid an early cartesian product between two {@link BindingSetAssignment}s (VALUES) when the result |
| 29 | + * is immediately joined with a {@link StatementPattern} that can be evaluated efficiently with a single bound side. |
| 30 | + */ |
| 31 | +public class BindingSetAssignmentJoinOrderOptimizer implements QueryOptimizer { |
| 32 | + |
| 33 | + @Override |
| 34 | + public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings) { |
| 35 | + if (UnorderedSliceDetector.hasUnorderedSlice(tupleExpr)) { |
| 36 | + return; |
| 37 | + } |
| 38 | + tupleExpr.visit(new BindingSetAssignmentJoinOrderVisitor()); |
| 39 | + } |
| 40 | + |
| 41 | + private static final class BindingSetAssignmentJoinOrderVisitor |
| 42 | + extends AbstractSimpleQueryModelVisitor<RuntimeException> { |
| 43 | + |
| 44 | + @Override |
| 45 | + public void meet(Join join) throws RuntimeException { |
| 46 | + super.meet(join); |
| 47 | + |
| 48 | + StatementPattern statementPattern; |
| 49 | + Join bindingSetJoin; |
| 50 | + if (join.getLeftArg() instanceof Join && join.getRightArg() instanceof StatementPattern) { |
| 51 | + bindingSetJoin = (Join) join.getLeftArg(); |
| 52 | + statementPattern = (StatementPattern) join.getRightArg(); |
| 53 | + } else if (join.getRightArg() instanceof Join && join.getLeftArg() instanceof StatementPattern) { |
| 54 | + bindingSetJoin = (Join) join.getRightArg(); |
| 55 | + statementPattern = (StatementPattern) join.getLeftArg(); |
| 56 | + } else { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (!(bindingSetJoin.getLeftArg() instanceof BindingSetAssignment) |
| 61 | + || !(bindingSetJoin.getRightArg() instanceof BindingSetAssignment)) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + BindingSetAssignment leftValues = (BindingSetAssignment) bindingSetJoin.getLeftArg(); |
| 66 | + BindingSetAssignment rightValues = (BindingSetAssignment) bindingSetJoin.getRightArg(); |
| 67 | + if (!areDisjoint(leftValues.getBindingNames(), rightValues.getBindingNames())) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (!statementPatternUsesAny(statementPattern, leftValues.getBindingNames()) |
| 72 | + || !statementPatternUsesAny(statementPattern, rightValues.getBindingNames())) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + BindingSetAssignment firstValues = chooseFirst(statementPattern, leftValues, rightValues); |
| 77 | + BindingSetAssignment secondValues = firstValues == leftValues ? rightValues : leftValues; |
| 78 | + |
| 79 | + StatementPattern statementPatternClone = statementPattern.clone(); |
| 80 | + Join inner = new Join((TupleExpr) firstValues.clone(), statementPatternClone); |
| 81 | + Join outer = new Join(inner, (TupleExpr) secondValues.clone()); |
| 82 | + |
| 83 | + join.replaceWith(outer); |
| 84 | + outer.visit(this); |
| 85 | + } |
| 86 | + |
| 87 | + private static boolean areDisjoint(Set<String> left, Set<String> right) { |
| 88 | + for (String name : left) { |
| 89 | + if (right.contains(name)) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + } |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + private static boolean statementPatternUsesAny(StatementPattern statementPattern, Set<String> bindingNames) { |
| 97 | + for (Var var : new Var[] { statementPattern.getSubjectVar(), statementPattern.getPredicateVar(), |
| 98 | + statementPattern.getObjectVar(), statementPattern.getContextVar() }) { |
| 99 | + if (var == null || var.hasValue()) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + if (bindingNames.contains(var.getName())) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + } |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + private static BindingSetAssignment chooseFirst(StatementPattern statementPattern, BindingSetAssignment left, |
| 110 | + BindingSetAssignment right) { |
| 111 | + int leftScore = positionScore(statementPattern, left.getBindingNames()); |
| 112 | + int rightScore = positionScore(statementPattern, right.getBindingNames()); |
| 113 | + if (leftScore != rightScore) { |
| 114 | + return leftScore < rightScore ? left : right; |
| 115 | + } |
| 116 | + |
| 117 | + int leftSize = estimateSize(left); |
| 118 | + int rightSize = estimateSize(right); |
| 119 | + if (leftSize != rightSize) { |
| 120 | + return leftSize <= rightSize ? left : right; |
| 121 | + } |
| 122 | + |
| 123 | + return left; |
| 124 | + } |
| 125 | + |
| 126 | + private static int positionScore(StatementPattern statementPattern, Set<String> names) { |
| 127 | + Var subject = statementPattern.getSubjectVar(); |
| 128 | + if (subject != null && !subject.hasValue() && names.contains(subject.getName())) { |
| 129 | + return 0; |
| 130 | + } |
| 131 | + Var object = statementPattern.getObjectVar(); |
| 132 | + if (object != null && !object.hasValue() && names.contains(object.getName())) { |
| 133 | + return 1; |
| 134 | + } |
| 135 | + Var predicate = statementPattern.getPredicateVar(); |
| 136 | + if (predicate != null && !predicate.hasValue() && names.contains(predicate.getName())) { |
| 137 | + return 2; |
| 138 | + } |
| 139 | + Var context = statementPattern.getContextVar(); |
| 140 | + if (context != null && !context.hasValue() && names.contains(context.getName())) { |
| 141 | + return 3; |
| 142 | + } |
| 143 | + return Integer.MAX_VALUE; |
| 144 | + } |
| 145 | + |
| 146 | + private static int estimateSize(BindingSetAssignment assignment) { |
| 147 | + Iterable<BindingSet> bindingSets = assignment.getBindingSets(); |
| 148 | + if (bindingSets instanceof Collection<?>) { |
| 149 | + return ((Collection<?>) bindingSets).size(); |
| 150 | + } |
| 151 | + return Integer.MAX_VALUE; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments