|
| 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 | +package org.eclipse.rdf4j.query.algebra.evaluation.iterator; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | +import java.util.Set; |
| 17 | +import java.util.concurrent.atomic.AtomicInteger; |
| 18 | + |
| 19 | +import org.eclipse.rdf4j.common.iteration.CloseableIteration; |
| 20 | +import org.eclipse.rdf4j.model.*; |
| 21 | +import org.eclipse.rdf4j.model.impl.BooleanLiteral; |
| 22 | +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; |
| 23 | +import org.eclipse.rdf4j.query.BindingSet; |
| 24 | +import org.eclipse.rdf4j.query.QueryEvaluationException; |
| 25 | +import org.eclipse.rdf4j.query.algebra.*; |
| 26 | +import org.eclipse.rdf4j.query.algebra.evaluation.*; |
| 27 | +import org.eclipse.rdf4j.query.algebra.evaluation.impl.DefaultEvaluationStrategy; |
| 28 | +import org.junit.jupiter.api.BeforeAll; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.DisplayName; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +class LeftJoinIteratorTest { |
| 34 | + |
| 35 | + private static final QueryBindingSet RIGHT_BINDINGS = new QueryBindingSet(1); |
| 36 | + private static final ValueFactory VALUE_FACTORY = SimpleValueFactory.getInstance(); |
| 37 | + private static final EvaluationStrategy EVALUATOR = new DefaultEvaluationStrategy(new TripleSource() { |
| 38 | + |
| 39 | + @Override |
| 40 | + public ValueFactory getValueFactory() { |
| 41 | + return SimpleValueFactory.getInstance(); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public CloseableIteration<? extends Statement> getStatements(Resource subj, IRI pred, |
| 46 | + Value obj, Resource... contexts) throws QueryEvaluationException { |
| 47 | + return null; |
| 48 | + } |
| 49 | + }, null); |
| 50 | + |
| 51 | + private QueryBindingSet bindingSet; |
| 52 | + private BindingSetAssignment left; |
| 53 | + private QueryEvaluationStep leftHandSide; |
| 54 | + private RightHandSideQueryEvaluationStep rightHandSide; |
| 55 | + |
| 56 | + @BeforeAll |
| 57 | + static void beforeAll() { |
| 58 | + RIGHT_BINDINGS.addBinding("right", VALUE_FACTORY.createLiteral(42)); |
| 59 | + } |
| 60 | + |
| 61 | + @BeforeEach |
| 62 | + void setUp() { |
| 63 | + bindingSet = new QueryBindingSet(1); |
| 64 | + bindingSet.addBinding("left", VALUE_FACTORY.createLiteral(42)); |
| 65 | + |
| 66 | + left = new BindingSetAssignment(); |
| 67 | + left.setBindingSets(List.of(bindingSet)); |
| 68 | + |
| 69 | + leftHandSide = EVALUATOR.precompile(left); |
| 70 | + rightHandSide = new RightHandSideQueryEvaluationStep(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @DisplayName("when condition can be evaluated only with left hand side and condition evaluates to false, then don't evaluate right hand side") |
| 75 | + void skipsRightHandSideEvaluation() { |
| 76 | + QueryValueEvaluationStep condition = bindings -> BooleanLiteral.valueOf(false); |
| 77 | + Compare compare = new Compare(new Var("left"), new ValueConstant(VALUE_FACTORY.createLiteral(1337))); |
| 78 | + |
| 79 | + runLeftOnce(condition, leftJoin(compare)); |
| 80 | + |
| 81 | + assertFalse(rightHandSide.isEvaluated); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @DisplayName("when condition can be evaluated only with left hand side and condition evaluates to false, then return left bindings") |
| 86 | + void onlyReturnLeftHandSideBindings() { |
| 87 | + QueryValueEvaluationStep condition = bindings -> BooleanLiteral.valueOf(false); |
| 88 | + Compare compare = new Compare(new Var("left"), new ValueConstant(VALUE_FACTORY.createLiteral(1337))); |
| 89 | + |
| 90 | + var result = runLeftOnce(condition, leftJoin(compare)); |
| 91 | + |
| 92 | + assertIterableEquals(left.getBindingSets(), Set.of(result)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @DisplayName("when condition can be evaluated only with left hand side and condition evaluates to true, then evaluate right hand side") |
| 97 | + void evaluatesRightHandSideWithTrueCondition() { |
| 98 | + QueryValueEvaluationStep condition = bindings -> BooleanLiteral.valueOf(true); |
| 99 | + Compare compare = new Compare(new Var("left"), new ValueConstant(VALUE_FACTORY.createLiteral(42))); |
| 100 | + |
| 101 | + runLeftOnce(condition, leftJoin(compare)); |
| 102 | + |
| 103 | + assertTrue(rightHandSide.isEvaluated); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + @DisplayName("when condition can be evaluated only with left hand side and condition evaluates to true, then return joined bindings") |
| 108 | + void returnsRightBindings() { |
| 109 | + QueryValueEvaluationStep condition = bindings -> BooleanLiteral.valueOf(true); |
| 110 | + Compare compare = new Compare(new Var("left"), new ValueConstant(VALUE_FACTORY.createLiteral(42))); |
| 111 | + |
| 112 | + var result = runLeftOnce(condition, leftJoin(compare)); |
| 113 | + |
| 114 | + var expected = new QueryBindingSet(2); |
| 115 | + bindingSet.forEach(expected::addBinding); |
| 116 | + RIGHT_BINDINGS.forEach(expected::addBinding); |
| 117 | + assertIterableEquals(expected, result); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + @DisplayName("when condition can be evaluated only with left hand side and condition evaluates to true, then only evaluate condition once") |
| 122 | + void onlyEvaluatesConditionOnce() { |
| 123 | + var evaluations = new AtomicInteger(0); |
| 124 | + QueryValueEvaluationStep condition = bindings -> { |
| 125 | + evaluations.incrementAndGet(); |
| 126 | + return BooleanLiteral.valueOf(true); |
| 127 | + }; |
| 128 | + Compare compare = new Compare(new Var("left"), new ValueConstant(VALUE_FACTORY.createLiteral(42))); |
| 129 | + |
| 130 | + runLeftOnce(condition, leftJoin(compare)); |
| 131 | + |
| 132 | + assertEquals(1, evaluations.get()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + @DisplayName("when condition cannot be evaluated only with left hand side, then evaluate right hand side") |
| 137 | + void evaluatesRightHandSideEvaluation() { |
| 138 | + QueryValueEvaluationStep condition = bindings -> BooleanLiteral.valueOf(true); |
| 139 | + Compare compare = new Compare(new Var("right"), new ValueConstant(VALUE_FACTORY.createLiteral(42))); |
| 140 | + |
| 141 | + runLeftOnce(condition, leftJoin(compare)); |
| 142 | + |
| 143 | + assertTrue(rightHandSide.isEvaluated); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + @DisplayName("when no condition, then evaluate right hand side") |
| 148 | + void noCondition() { |
| 149 | + Compare compare = new Compare(new Var("right"), new ValueConstant(VALUE_FACTORY.createLiteral(42))); |
| 150 | + |
| 151 | + runLeftOnce(null, leftJoin(compare)); |
| 152 | + |
| 153 | + assertTrue(rightHandSide.isEvaluated); |
| 154 | + } |
| 155 | + |
| 156 | + private LeftJoin leftJoin(Compare compare) { |
| 157 | + return new LeftJoin(left, new EmptySet(), compare); |
| 158 | + } |
| 159 | + |
| 160 | + private BindingSet runLeftOnce(QueryValueEvaluationStep condition, LeftJoin leftJoin) { |
| 161 | + try (LeftJoinIterator iterator = new LeftJoinIterator( |
| 162 | + leftHandSide, |
| 163 | + rightHandSide, |
| 164 | + condition, |
| 165 | + bindingSet, |
| 166 | + Set.of("left", "right"), |
| 167 | + leftJoin)) { |
| 168 | + return iterator.getNextElement(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private static class RightHandSideQueryEvaluationStep implements QueryEvaluationStep { |
| 173 | + |
| 174 | + private boolean isEvaluated = false; |
| 175 | + |
| 176 | + @Override |
| 177 | + public CloseableIteration<BindingSet> evaluate(BindingSet bindings) { |
| 178 | + isEvaluated = true; |
| 179 | + var bothBindings = new QueryBindingSet(2); |
| 180 | + bindings.forEach(bothBindings::addBinding); |
| 181 | + RIGHT_BINDINGS.forEach(bothBindings::addBinding); |
| 182 | + var rightIterator = List.of(bothBindings).iterator(); |
| 183 | + |
| 184 | + return new CloseableIteration<>() { |
| 185 | + @Override |
| 186 | + public void close() { |
| 187 | + // Nothing to close |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public boolean hasNext() { |
| 192 | + return rightIterator.hasNext(); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public BindingSet next() { |
| 197 | + return rightIterator.next(); |
| 198 | + } |
| 199 | + }; |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments