Skip to content

Commit 2377f88

Browse files
committed
GH-5153 early detection of object equality when comparing Values or getting the effective boolean value
1 parent 09e98a3 commit 2377f88

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/util/QueryEvaluationUtil.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.eclipse.rdf4j.model.Value;
2121
import org.eclipse.rdf4j.model.base.CoreDatatype;
2222
import org.eclipse.rdf4j.model.datatypes.XMLDatatypeUtil;
23+
import org.eclipse.rdf4j.model.impl.BooleanLiteral;
2324
import org.eclipse.rdf4j.model.util.Literals;
2425
import org.eclipse.rdf4j.query.algebra.Compare.CompareOp;
2526
import org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException;
@@ -62,6 +63,13 @@ public class QueryEvaluationUtil {
6263
* @throws ValueExprEvaluationException In case the application of the EBV algorithm results in a type error.
6364
*/
6465
public static boolean getEffectiveBooleanValue(Value value) throws ValueExprEvaluationException {
66+
67+
if (value == BooleanLiteral.TRUE) {
68+
return true;
69+
} else if (value == BooleanLiteral.FALSE) {
70+
return false;
71+
}
72+
6573
if (value.isLiteral()) {
6674
Literal literal = (Literal) value;
6775
String label = literal.getLabel();
@@ -107,6 +115,15 @@ public static boolean compare(Value leftVal, Value rightVal, CompareOp operator)
107115

108116
public static boolean compare(Value leftVal, Value rightVal, CompareOp operator, boolean strict)
109117
throws ValueExprEvaluationException {
118+
if (leftVal == rightVal) {
119+
switch (operator) {
120+
case EQ:
121+
return true;
122+
case NE:
123+
return false;
124+
}
125+
}
126+
110127
if (leftVal != null && leftVal.isLiteral() && rightVal != null && rightVal.isLiteral()) {
111128
// Both left and right argument is a Literal
112129
return compareLiterals((Literal) leftVal, (Literal) rightVal, operator, strict);
@@ -162,6 +179,15 @@ public static boolean compareLiterals(Literal leftLit, Literal rightLit, Compare
162179
// - CoreDatatype.XSD:string
163180
// - RDF term (equal and unequal only)
164181

182+
if (leftLit == rightLit) {
183+
switch (operator) {
184+
case EQ:
185+
return true;
186+
case NE:
187+
return false;
188+
}
189+
}
190+
165191
CoreDatatype.XSD leftCoreDatatype = leftLit.getCoreDatatype().asXSDDatatypeOrNull();
166192
CoreDatatype.XSD rightCoreDatatype = rightLit.getCoreDatatype().asXSDDatatypeOrNull();
167193

@@ -329,7 +355,7 @@ && isSupportedDatatype(rightCoreDatatype)) {
329355
* <a href="http://www.w3.org/TR/rdf-concepts/#section-blank-nodes">6.6 Blank Nodes of [CONCEPTS]</a>.
330356
* </ul>
331357
* </blockquote>
332-
*
358+
* <p>
333359
* (emphasis ours)
334360
* <p>
335361
* When applying the SPARQL specification in a minimally-conforming manner, RDFterm-equal is supposed to return a
@@ -349,7 +375,6 @@ && isSupportedDatatype(rightCoreDatatype)) {
349375
* @param rightCoreDatatype the right datatype to compare
350376
* @throws ValueExprEvaluationException if query evaluation is operating in strict mode, and the two supplied
351377
* datatypes are both supported datatypes but not comparable.
352-
*
353378
* @see <a href="https://github.com/eclipse/rdf4j/issues/3947">Github issue #3947</a>
354379
*/
355380
private static void validateDatatypeCompatibility(boolean strict, CoreDatatype.XSD leftCoreDatatype,

0 commit comments

Comments
 (0)