Skip to content

Commit

Permalink
GH-5153 early detection of object equality when comparing Values or g…
Browse files Browse the repository at this point in the history
…etting the effective boolean value
  • Loading branch information
hmottestad committed Oct 21, 2024
1 parent 09e98a3 commit 2377f88
Showing 1 changed file with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.base.CoreDatatype;
import org.eclipse.rdf4j.model.datatypes.XMLDatatypeUtil;
import org.eclipse.rdf4j.model.impl.BooleanLiteral;
import org.eclipse.rdf4j.model.util.Literals;
import org.eclipse.rdf4j.query.algebra.Compare.CompareOp;
import org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException;
Expand Down Expand Up @@ -62,6 +63,13 @@ public class QueryEvaluationUtil {
* @throws ValueExprEvaluationException In case the application of the EBV algorithm results in a type error.
*/
public static boolean getEffectiveBooleanValue(Value value) throws ValueExprEvaluationException {

if (value == BooleanLiteral.TRUE) {
return true;
} else if (value == BooleanLiteral.FALSE) {
return false;
}

if (value.isLiteral()) {
Literal literal = (Literal) value;
String label = literal.getLabel();
Expand Down Expand Up @@ -107,6 +115,15 @@ public static boolean compare(Value leftVal, Value rightVal, CompareOp operator)

public static boolean compare(Value leftVal, Value rightVal, CompareOp operator, boolean strict)
throws ValueExprEvaluationException {
if (leftVal == rightVal) {
switch (operator) {
case EQ:
return true;
case NE:
return false;
}
}

if (leftVal != null && leftVal.isLiteral() && rightVal != null && rightVal.isLiteral()) {
// Both left and right argument is a Literal
return compareLiterals((Literal) leftVal, (Literal) rightVal, operator, strict);
Expand Down Expand Up @@ -162,6 +179,15 @@ public static boolean compareLiterals(Literal leftLit, Literal rightLit, Compare
// - CoreDatatype.XSD:string
// - RDF term (equal and unequal only)

if (leftLit == rightLit) {
switch (operator) {
case EQ:
return true;
case NE:
return false;
}
}

CoreDatatype.XSD leftCoreDatatype = leftLit.getCoreDatatype().asXSDDatatypeOrNull();
CoreDatatype.XSD rightCoreDatatype = rightLit.getCoreDatatype().asXSDDatatypeOrNull();

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

0 comments on commit 2377f88

Please sign in to comment.