Skip to content

Commit

Permalink
GH-3947 in STANDARD mode, "foo" = "1"^^xsd:integer will return false
Browse files Browse the repository at this point in the history
Instead of a type error, we now return false when comparing two literals
with incompatible (but known) datatypes.
  • Loading branch information
abrokenjester committed May 6, 2023
1 parent 0c7fcb7 commit c4fc9fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static boolean compareLiterals(Literal leftLit, Literal rightLit, Compare
return compareWithOperator(operator, compare);
} else {
return otherCases(leftLit, rightLit, operator, leftCoreDatatype, rightCoreDatatype,
leftLangLit, rightLangLit);
leftLangLit, rightLangLit, strict);
}

} else if (commonDatatype == CoreDatatype.XSD.STRING) {
Expand All @@ -248,13 +248,14 @@ public static boolean compareLiterals(Literal leftLit, Literal rightLit, Compare
// using the operators 'EQ' and 'NE'. See SPARQL's RDFterm-equal
// operator

return otherCases(leftLit, rightLit, operator, leftCoreDatatype, rightCoreDatatype, leftLangLit, rightLangLit);
return otherCases(leftLit, rightLit, operator, leftCoreDatatype, rightCoreDatatype, leftLangLit, rightLangLit,
strict);

}

private static boolean otherCases(Literal leftLit, Literal rightLit, CompareOp operator,
CoreDatatype.XSD leftCoreDatatype, CoreDatatype.XSD rightCoreDatatype, boolean leftLangLit,
boolean rightLangLit) {
boolean rightLangLit, boolean strict) {
boolean literalsEqual = leftLit.equals(rightLit);

if (!literalsEqual) {
Expand All @@ -271,22 +272,24 @@ && isSupportedDatatype(rightCoreDatatype)) {
throw new ValueExprEvaluationException("not a valid datatype value: " + rightLit);
}

boolean leftString = leftCoreDatatype == CoreDatatype.XSD.STRING;
boolean leftNumeric = leftCoreDatatype.isNumericDatatype();
boolean leftDate = leftCoreDatatype.isCalendarDatatype();
if (strict) {
boolean leftString = leftCoreDatatype == CoreDatatype.XSD.STRING;
boolean leftNumeric = leftCoreDatatype.isNumericDatatype();
boolean leftDate = leftCoreDatatype.isCalendarDatatype();

boolean rightString = rightCoreDatatype == CoreDatatype.XSD.STRING;
boolean rightNumeric = rightCoreDatatype.isNumericDatatype();
boolean rightDate = rightCoreDatatype.isCalendarDatatype();
boolean rightString = rightCoreDatatype == CoreDatatype.XSD.STRING;
boolean rightNumeric = rightCoreDatatype.isNumericDatatype();
boolean rightDate = rightCoreDatatype.isCalendarDatatype();

if (leftString != rightString) {
throw STRING_WITH_OTHER_SUPPORTED_TYPE_EXCEPTION;
}
if (leftNumeric != rightNumeric) {
throw NUMERIC_WITH_OTHER_SUPPORTED_TYPE_EXCEPTION;
}
if (leftDate != rightDate) {
throw DATE_WITH_OTHER_SUPPORTED_TYPE_EXCEPTION;
if (leftString != rightString) {
throw STRING_WITH_OTHER_SUPPORTED_TYPE_EXCEPTION;
}
if (leftNumeric != rightNumeric) {
throw NUMERIC_WITH_OTHER_SUPPORTED_TYPE_EXCEPTION;
}
if (leftDate != rightDate) {
throw DATE_WITH_OTHER_SUPPORTED_TYPE_EXCEPTION;
}
}
} else if (!leftLangLit && !rightLangLit) {
// For literals with unsupported datatypes we don't know if their values are equal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.rdf4j.query.algebra.evaluation.util;

import static org.eclipse.rdf4j.model.util.Values.literal;
import static org.eclipse.rdf4j.query.algebra.Compare.CompareOp.EQ;
import static org.eclipse.rdf4j.query.algebra.Compare.CompareOp.LT;
import static org.eclipse.rdf4j.query.algebra.Compare.CompareOp.NE;
Expand Down Expand Up @@ -155,6 +156,9 @@ public void testCompatibleArguments() {

@Test
public void testCompareEQ() {
assertCompareFalse(literal("value NOT"), literal("1", XSD.INTEGER), EQ, false);
assertCompareException(literal("value NOT"), literal("1", XSD.INTEGER), EQ, true);

assertCompareTrue(arg1simple, arg1simple, EQ);
assertCompareTrue(arg1en, arg1en, EQ);
assertCompareTrue(arg2cy, arg2cy, EQ);
Expand Down Expand Up @@ -237,6 +241,10 @@ public void testCompareEQ() {

@Test
public void testCompareNE() {

assertCompareTrue(literal("value NOT"), literal("1", XSD.INTEGER), NE, false);
assertCompareException(literal("value NOT"), literal("1", XSD.INTEGER), NE, true);

assertCompareFalse(arg1simple, arg1simple, NE);
assertCompareFalse(arg1en, arg1en, NE);
assertCompareFalse(arg1cy, arg1cy, NE);
Expand Down Expand Up @@ -401,6 +409,7 @@ public void testCompareLT() {
assertCompareException(arg1yearMonthDuration, arg2unknown, LT);
}


/**
* Assert that there is an exception as a result of comparing the two literals with the given operator.
*
Expand Down

0 comments on commit c4fc9fc

Please sign in to comment.