|
| 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.HashSet; |
| 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.And; |
| 20 | +import org.eclipse.rdf4j.query.algebra.BinaryValueOperator; |
| 21 | +import org.eclipse.rdf4j.query.algebra.Bound; |
| 22 | +import org.eclipse.rdf4j.query.algebra.Coalesce; |
| 23 | +import org.eclipse.rdf4j.query.algebra.Exists; |
| 24 | +import org.eclipse.rdf4j.query.algebra.Filter; |
| 25 | +import org.eclipse.rdf4j.query.algebra.If; |
| 26 | +import org.eclipse.rdf4j.query.algebra.Join; |
| 27 | +import org.eclipse.rdf4j.query.algebra.LeftJoin; |
| 28 | +import org.eclipse.rdf4j.query.algebra.NAryValueOperator; |
| 29 | +import org.eclipse.rdf4j.query.algebra.Not; |
| 30 | +import org.eclipse.rdf4j.query.algebra.Or; |
| 31 | +import org.eclipse.rdf4j.query.algebra.TupleExpr; |
| 32 | +import org.eclipse.rdf4j.query.algebra.UnaryValueOperator; |
| 33 | +import org.eclipse.rdf4j.query.algebra.ValueConstant; |
| 34 | +import org.eclipse.rdf4j.query.algebra.ValueExpr; |
| 35 | +import org.eclipse.rdf4j.query.algebra.Var; |
| 36 | +import org.eclipse.rdf4j.query.algebra.evaluation.QueryOptimizer; |
| 37 | +import org.eclipse.rdf4j.query.algebra.helpers.AbstractSimpleQueryModelVisitor; |
| 38 | + |
| 39 | +/** |
| 40 | + * Rewrites Filter(LeftJoin(...)) into Filter(Join(...)) when the filter condition depends on optional-only variables in |
| 41 | + * a strict way, making the OPTIONAL mandatory. |
| 42 | + */ |
| 43 | +public class OptionalFilterJoinOptimizer implements QueryOptimizer { |
| 44 | + |
| 45 | + @Override |
| 46 | + public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings) { |
| 47 | + tupleExpr.visit(new OptionalFilterVisitor()); |
| 48 | + } |
| 49 | + |
| 50 | + private static final class OptionalFilterVisitor extends AbstractSimpleQueryModelVisitor<RuntimeException> { |
| 51 | + @Override |
| 52 | + public void meet(Filter filter) { |
| 53 | + super.meet(filter); |
| 54 | + if (!(filter.getArg() instanceof LeftJoin)) { |
| 55 | + return; |
| 56 | + } |
| 57 | + LeftJoin leftJoin = (LeftJoin) filter.getArg(); |
| 58 | + if (leftJoin.getCondition() != null) { |
| 59 | + return; |
| 60 | + } |
| 61 | + Set<String> rightOnly = new HashSet<>(leftJoin.getRightArg().getBindingNames()); |
| 62 | + rightOnly.removeAll(leftJoin.getLeftArg().getBindingNames()); |
| 63 | + if (rightOnly.isEmpty()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + if (!requiresRightVars(filter.getCondition(), rightOnly)) { |
| 67 | + return; |
| 68 | + } |
| 69 | + Join join = new Join(leftJoin.getLeftArg(), leftJoin.getRightArg()); |
| 70 | + filter.setArg(join); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static boolean requiresRightVars(ValueExpr expr, Set<String> rightOnly) { |
| 75 | + if (expr == null || rightOnly.isEmpty()) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + if (expr instanceof Var) { |
| 79 | + return rightOnly.contains(((Var) expr).getName()); |
| 80 | + } |
| 81 | + if (expr instanceof ValueConstant) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + if (expr instanceof Bound) { |
| 85 | + return rightOnly.contains(((Bound) expr).getArg().getName()); |
| 86 | + } |
| 87 | + if (expr instanceof If || expr instanceof Coalesce || expr instanceof Exists) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + if (expr instanceof Or) { |
| 91 | + Or or = (Or) expr; |
| 92 | + return requiresRightVars(or.getLeftArg(), rightOnly) && requiresRightVars(or.getRightArg(), rightOnly); |
| 93 | + } |
| 94 | + if (expr instanceof And) { |
| 95 | + And and = (And) expr; |
| 96 | + return requiresRightVars(and.getLeftArg(), rightOnly) || requiresRightVars(and.getRightArg(), rightOnly); |
| 97 | + } |
| 98 | + if (expr instanceof Not) { |
| 99 | + return requiresRightVars(((Not) expr).getArg(), rightOnly); |
| 100 | + } |
| 101 | + if (expr instanceof UnaryValueOperator) { |
| 102 | + return requiresRightVars(((UnaryValueOperator) expr).getArg(), rightOnly); |
| 103 | + } |
| 104 | + if (expr instanceof BinaryValueOperator) { |
| 105 | + BinaryValueOperator binary = (BinaryValueOperator) expr; |
| 106 | + return requiresRightVars(binary.getLeftArg(), rightOnly) |
| 107 | + || requiresRightVars(binary.getRightArg(), rightOnly); |
| 108 | + } |
| 109 | + if (expr instanceof NAryValueOperator) { |
| 110 | + for (ValueExpr arg : ((NAryValueOperator) expr).getArguments()) { |
| 111 | + if (requiresRightVars(arg, rightOnly)) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + } |
| 115 | + return false; |
| 116 | + } |
| 117 | + return false; |
| 118 | + } |
| 119 | +} |
0 commit comments