Skip to content

Commit

Permalink
Fixes for ProperContains, ProperIncludedIn, ProperIncludes, ProperIn …
Browse files Browse the repository at this point in the history
…evaluators handling nulls.
  • Loading branch information
antvaset committed Jun 20, 2024
1 parent a022fc5 commit 54dec11
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ public static Object[][] dataMethod() {
"cql/CqlListOperatorsTest/NotEqual/NotEqual123AndABC",
"cql/CqlListOperatorsTest/NotEqual/NotEqual123AndString123",
"cql/CqlListOperatorsTest/NotEqual/NotEqualABCAnd123",
"cql/CqlListOperatorsTest/ProperContains/ProperContainsNullRightFalse",
"cql/CqlListOperatorsTest/ProperContains/ProperContainsTimeNull",
"cql/CqlListOperatorsTest/ProperIn/ProperInTimeNull",
"cql/CqlListOperatorsTest/ProperlyIncludedIn/ProperlyIncludedInNulRight",
"cql/CqlListOperatorsTest/ProperlyIncludes/ProperlyIncludesNullLeft",
"cql/CqlListOperatorsTest/Union/UnionListNullAndListNull",
"cql/CqlStringOperatorsTest/toString tests/DateTimeToString3",
"cql/CqlTypeOperatorsTest/As/AsQuantity",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,13 +650,13 @@
</test>
</group>
<group name="ProperContains">
<test name="ProperContainsNullRightFalse">
<test name="ProperContainsNullRightWithoutNull">
<expression>{'s', 'u', 'n'} properly includes null</expression>
<output>false</output>
<output>null</output>
</test>
<test name="ProperContainsNullRightTrue">
<test name="ProperContainsNullRightWithNull">
<expression>{'s', 'u', 'n', null} properly includes null</expression>
<output>true</output>
<output>null</output>
</test>
<test name="ProperContainsTimeTrue">
<expression>{ @T15:59:59, @T20:59:59.999, @T20:59:49.999 } properly includes @T15:59:59</expression>
Expand All @@ -668,13 +668,13 @@
</test>
</group>
<group name="ProperIn">
<test name="ProperInNullRightFalse">
<test name="ProperInNullRightWithoutNull">
<expression>null properly included in {'s', 'u', 'n'}</expression>
<output>false</output>
<output>null</output>
</test>
<test name="ProperInNullRightTrue">
<test name="ProperInNullRightWithNull">
<expression>null properly included in {'s', 'u', 'n', null}</expression>
<output>true</output>
<output>null</output>
</test>
<test name="ProperInTimeTrue">
<expression>@T15:59:59 properly included in { @T15:59:59, @T20:59:59.999, @T20:59:49.999 }</expression>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public static Boolean properContains(Object left, Object right, State state) {
}

public static Boolean properContains(Object left, Object right, String precision, State state) {
if (left == null || right == null) {
return null;
}

if (left instanceof Interval && right instanceof BaseTemporal) {
Boolean startProperContains = AfterEvaluator.after(right, ((Interval) left).getStart(), precision, state);
Boolean endProperContains = BeforeEvaluator.before(right, ((Interval) left).getEnd(), precision, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public static Boolean properIn(Object left, Object right, String precision, Stat
}

public static Object internalEvaluate(Object left, Object right, String precision, State state) {
if (left == null || right == null) {
return null;
}

if (precision != null) {
return properIn(left, right, precision, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,14 @@ properly included in(left List<T>, right list<T>) Boolean
The properly included in operator for lists returns true if every element of the first list is in the second list and the
first list is strictly smaller than the second list.
This operator uses the notion of equivalence to determine whether or not two elements are the same.
If the left argument is null, the result is true if the right argument is not empty. Otherwise, if the right argument is null, the result is false.
If either argument is null, the result is null.
Note that the order of elements does not matter for the purposes of determining inclusion.
*/

public class ProperIncludedInEvaluator {

public static Object properlyIncludedIn(Object left, Object right, String precision, State state) {
if (left == null && right == null) {
return null;
}

try {
if (left == null) {
return right instanceof Interval
? ProperIncludesEvaluator.intervalProperlyIncludes((Interval) right, null, precision, state)
: ProperIncludesEvaluator.listProperlyIncludes((Iterable<?>) right, null, state);
}

if (right == null) {
return left instanceof Interval
? ProperIncludesEvaluator.intervalProperlyIncludes(null, (Interval) left, precision, state)
: ProperIncludesEvaluator.listProperlyIncludes(null, (Iterable<?>) left, state);
}

return ProperIncludesEvaluator.properlyIncludes(right, left, precision, state);
} catch (InvalidOperatorArgument e) {
throw new InvalidOperatorArgument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,21 @@ properly includes(left List<T>, right List<T>) Boolean
The properly includes operator for lists returns true if the first list contains every element of the second list, a
nd the first list is strictly larger than the second list.
This operator uses the notion of equivalence to determine whether or not two elements are the same.
If the left argument is null, the result is false, else if the right argument is null, the result is true if the left argument is not empty.
If either argument is null, the result is null.
Note that the order of elements does not matter for the purposes of determining inclusion.
*/

public class ProperIncludesEvaluator {

public static Boolean properlyIncludes(Object left, Object right, String precision, State state) {
if (left == null && right == null) {
if (left == null || right == null) {
return null;
}

if (left == null) {
return right instanceof Interval
? intervalProperlyIncludes(null, (Interval) right, precision, state)
: listProperlyIncludes(null, (Iterable<?>) right, state);
}

if (right == null) {
return left instanceof Interval
? intervalProperlyIncludes((Interval) left, null, precision, state)
: listProperlyIncludes((Iterable<?>) left, null, state);
}

if (left instanceof Interval && right instanceof Interval) {
if (left instanceof Interval) {
return intervalProperlyIncludes((Interval) left, (Interval) right, precision, state);
}
if (left instanceof Iterable && right instanceof Iterable) {
if (left instanceof Iterable) {
return listProperlyIncludes((Iterable<?>) left, (Iterable<?>) right, state);
}

Expand All @@ -62,10 +50,6 @@ public static Boolean properlyIncludes(Object left, Object right, String precisi
}

public static Boolean intervalProperlyIncludes(Interval left, Interval right, String precision, State state) {
if (left == null || right == null) {
return null;
}

Object leftStart = left.getStart();
Object leftEnd = left.getEnd();
Object rightStart = right.getStart();
Expand All @@ -88,10 +72,6 @@ public static Boolean intervalProperlyIncludes(Interval left, Interval right, St
}

public static Boolean listProperlyIncludes(Iterable<?> left, Iterable<?> right, State state) {
if (left == null) {
return false;
}

int leftCount = (int)
StreamSupport.stream(((Iterable<?>) left).spliterator(), false).count();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,28 +569,28 @@ void all_interval_operators() {
assertThat(value, is(false));

value = results.forExpression("ProperlyIncludesNullLeft").value();
assertThat(value, is(false));
assertThat(value, is(nullValue()));

value = results.forExpression("ProperlyIncludes1And111").value();
assertThat(value, is(false));

value = results.forExpression("ProperContainsNullRightFalse").value();
assertThat(value, is(false));
value = results.forExpression("ProperContainsNullRightWithoutNull").value();
assertThat(value, is(nullValue()));

value = results.forExpression("ProperContainsNullRightTrue").value();
assertThat(value, is(true));
value = results.forExpression("ProperContainsNullRightWithNull").value();
assertThat(value, is(nullValue()));

value = results.forExpression("ProperContainsTimeTrue").value();
assertThat(value, is(true));

value = results.forExpression("ProperContainsTimeNull").value();
assertThat(value, is(false));

value = results.forExpression("ProperInNullRightFalse").value();
assertThat(value, is(false));
value = results.forExpression("ProperInNullRightWithoutNull").value();
assertThat(value, is(nullValue()));

value = results.forExpression("ProperInNullRightTrue").value();
assertThat(value, is(true));
value = results.forExpression("ProperInNullRightWithNull").value();
assertThat(value, is(nullValue()));

value = results.forExpression("ProperInTimeTrue").value();
assertThat(value, is(true));
Expand Down Expand Up @@ -626,7 +626,7 @@ void all_interval_operators() {
assertThat(value, is(false));

value = results.forExpression("ProperlyIncludedInNullRight").value();
assertThat(value, is(false));
assertThat(value, is(nullValue()));

value = results.forExpression("ProperlyIncludedIn11And1").value();
assertThat(value, is(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ define ProperlyIncludesNullLeft: null properly includes {2}
define ProperlyIncludes1And111: {1} properly includes {1, 1}

//ProperContains
define ProperContainsNullRightFalse: {'s', 'u', 'n'} properly includes null
define ProperContainsNullRightTrue: {'s', 'u', 'n', null} properly includes null
define ProperContainsNullRightWithoutNull: {'s', 'u', 'n'} properly includes null
define ProperContainsNullRightWithNull: {'s', 'u', 'n', null} properly includes null
define ProperContainsTimeTrue: { @T15:59:59, @T20:59:59.999, @T20:59:49.999 } properly includes @T15:59:59
define ProperContainsTimeNull: { @T15:59:59.999, @T20:59:59.999, @T20:59:49.999 } properly includes @T15:59:59

//ProperIn
define ProperInNullRightFalse: null properly included in {'s', 'u', 'n'}
define ProperInNullRightTrue: null properly included in {'s', 'u', 'n', null}
define ProperInNullRightWithoutNull: null properly included in {'s', 'u', 'n'}
define ProperInNullRightWithNull: null properly included in {'s', 'u', 'n', null}
define ProperInTimeTrue: @T15:59:59 properly included in { @T15:59:59, @T20:59:59.999, @T20:59:49.999 }
define ProperInTimeNull: @T15:59:59 properly included in { @T15:59:59.999, @T20:59:59.999, @T20:59:49.999 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4789,8 +4789,8 @@ define test_ProperIncludes_IsSame: TestMessage(not ProperIncludes_IsSame, 'Prope
define test_ProperIncludes_IsNotIncluded: TestMessage(not ProperIncludes_IsNotIncluded, 'ProperIncludes_IsNotIncluded', toString(false), toString(ProperIncludes_IsNotIncluded))
define test_ProperIncludes_TuplesIncluded: TestMessage(ProperIncludes_TuplesIncluded, 'ProperIncludes_TuplesIncluded', toString(true), toString(ProperIncludes_TuplesIncluded))
define test_ProperIncludes_TuplesNotIncluded: TestMessage(not ProperIncludes_TuplesNotIncluded, 'ProperIncludes_TuplesNotIncluded', toString(false), toString(ProperIncludes_TuplesNotIncluded))
define test_ProperIncludes_NullIncludes: TestMessage(not ProperIncludes_NullIncludes, 'ProperIncludes_NullIncludes', toString(false), toString(ProperIncludes_NullIncludes))
define test_ProperIncludes_NullIncluded: TestMessage(ProperIncludes_NullIncluded, 'ProperIncludes_NullIncluded', toString(true), toString(ProperIncludes_NullIncluded))
define test_ProperIncludes_NullIncludes: TestMessage(ProperIncludes_NullIncludes is null, 'ProperIncludes_NullIncludes', 'null', toString(ProperIncludes_NullIncludes))
define test_ProperIncludes_NullIncluded: TestMessage(ProperIncludes_NullIncluded is null, 'ProperIncludes_NullIncluded', 'null', toString(ProperIncludes_NullIncluded))

// ProperIncludedIn
define ProperIncludedIn_IsIncluded: {2, 3, 4} properly included in {1, 2, 3, 4, 5}
Expand All @@ -4808,8 +4808,8 @@ define test_ProperIncludedIn_IsSame: TestMessage(not ProperIncludedIn_IsSame, 'P
define test_ProperIncludedIn_IsNotIncluded: TestMessage(not ProperIncludedIn_IsNotIncluded, 'ProperIncludedIn_IsNotIncluded', toString(false), toString(ProperIncludedIn_IsNotIncluded))
define test_ProperIncludedIn_TuplesIncluded: TestMessage(ProperIncludedIn_TuplesIncluded, 'ProperIncludedIn_TuplesIncluded', toString(true), toString(ProperIncludedIn_TuplesIncluded))
define test_ProperIncludedIn_TuplesNotIncluded: TestMessage(not ProperIncludedIn_TuplesNotIncluded, 'ProperIncludedIn_TuplesNotIncluded', toString(false), toString(ProperIncludedIn_TuplesNotIncluded))
define test_ProperIncludedIn_NullIncludes: TestMessage(not ProperIncludedIn_NullIncludes, 'ProperIncludedIn_NullIncludes', toString(false), toString(ProperIncludedIn_NullIncludes))
define test_ProperIncludedIn_NullIncluded: TestMessage(ProperIncludedIn_NullIncluded, 'ProperIncludedIn_NullIncluded', toString(true), toString(ProperIncludedIn_NullIncluded))
define test_ProperIncludedIn_NullIncludes: TestMessage(ProperIncludedIn_NullIncludes is null, 'ProperIncludedIn_NullIncludes', 'null', toString(ProperIncludedIn_NullIncludes))
define test_ProperIncludedIn_NullIncluded: TestMessage(ProperIncludedIn_NullIncluded is null, 'ProperIncludedIn_NullIncluded', 'null', toString(ProperIncludedIn_NullIncluded))

// Flatten
define Flatten_ListOfLists: flatten { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {9, 8, 7, 6, 5}, {4}, {3, 2, 1} }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4789,8 +4789,8 @@ define test_ProperIncludes_IsSame: TestMessage(not ProperIncludes_IsSame, 'Prope
define test_ProperIncludes_IsNotIncluded: TestMessage(not ProperIncludes_IsNotIncluded, 'ProperIncludes_IsNotIncluded', toString(false), toString(ProperIncludes_IsNotIncluded))
define test_ProperIncludes_TuplesIncluded: TestMessage(ProperIncludes_TuplesIncluded, 'ProperIncludes_TuplesIncluded', toString(true), toString(ProperIncludes_TuplesIncluded))
define test_ProperIncludes_TuplesNotIncluded: TestMessage(not ProperIncludes_TuplesNotIncluded, 'ProperIncludes_TuplesNotIncluded', toString(false), toString(ProperIncludes_TuplesNotIncluded))
define test_ProperIncludes_NullIncludes: TestMessage(not ProperIncludes_NullIncludes, 'ProperIncludes_NullIncludes', toString(false), toString(ProperIncludes_NullIncludes))
define test_ProperIncludes_NullIncluded: TestMessage(ProperIncludes_NullIncluded, 'ProperIncludes_NullIncluded', toString(true), toString(ProperIncludes_NullIncluded))
define test_ProperIncludes_NullIncludes: TestMessage(ProperIncludes_NullIncludes is null, 'ProperIncludes_NullIncludes', 'null', toString(ProperIncludes_NullIncludes))
define test_ProperIncludes_NullIncluded: TestMessage(ProperIncludes_NullIncluded is null, 'ProperIncludes_NullIncluded', 'null', toString(ProperIncludes_NullIncluded))

// ProperIncludedIn
define ProperIncludedIn_IsIncluded: {2, 3, 4} properly included in {1, 2, 3, 4, 5}
Expand All @@ -4808,8 +4808,8 @@ define test_ProperIncludedIn_IsSame: TestMessage(not ProperIncludedIn_IsSame, 'P
define test_ProperIncludedIn_IsNotIncluded: TestMessage(not ProperIncludedIn_IsNotIncluded, 'ProperIncludedIn_IsNotIncluded', toString(false), toString(ProperIncludedIn_IsNotIncluded))
define test_ProperIncludedIn_TuplesIncluded: TestMessage(ProperIncludedIn_TuplesIncluded, 'ProperIncludedIn_TuplesIncluded', toString(true), toString(ProperIncludedIn_TuplesIncluded))
define test_ProperIncludedIn_TuplesNotIncluded: TestMessage(not ProperIncludedIn_TuplesNotIncluded, 'ProperIncludedIn_TuplesNotIncluded', toString(false), toString(ProperIncludedIn_TuplesNotIncluded))
define test_ProperIncludedIn_NullIncludes: TestMessage(not ProperIncludedIn_NullIncludes, 'ProperIncludedIn_NullIncludes', toString(false), toString(ProperIncludedIn_NullIncludes))
define test_ProperIncludedIn_NullIncluded: TestMessage(ProperIncludedIn_NullIncluded, 'ProperIncludedIn_NullIncluded', toString(true), toString(ProperIncludedIn_NullIncluded))
define test_ProperIncludedIn_NullIncludes: TestMessage(ProperIncludedIn_NullIncludes is null, 'ProperIncludedIn_NullIncludes', 'null', toString(ProperIncludedIn_NullIncludes))
define test_ProperIncludedIn_NullIncluded: TestMessage(ProperIncludedIn_NullIncluded is null, 'ProperIncludedIn_NullIncluded', 'null', toString(ProperIncludedIn_NullIncluded))

// Flatten
define Flatten_ListOfLists: flatten { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {9, 8, 7, 6, 5}, {4}, {3, 2, 1} }
Expand Down

0 comments on commit 54dec11

Please sign in to comment.