Skip to content

Commit 513c539

Browse files
committed
Fix tuple equality with null values
Previously, if tuples had the same fields, and at least one field had a null value on one tuple and a non-null value on the other, then: * if the null/non-null field came after a field with different values, it returned false (which is correct) * if the null/non-null field came before a field with different values, it returned null (which is incorrect) Now it will return the correct answer regardless of where the null/non-null field is.
1 parent 94cea07 commit 513c539

6 files changed

Lines changed: 841 additions & 479 deletions

File tree

examples/browser/cql4browsers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8697,14 +8697,15 @@ function deepCompareKeysAndValues(a, b, comparisonFunction) {
86978697
const comparisonResult = comparisonFunction(a[key], b[key]);
86988698
if (comparisonResult === null) {
86998699
shouldReturnNull = true;
8700+
return true;
87008701
}
87018702
return comparisonResult;
87028703
});
87038704
}
87048705
else {
87058706
finalComparisonResult = false;
87068707
}
8707-
if (shouldReturnNull) {
8708+
if (finalComparisonResult && shouldReturnNull) {
87088709
return null;
87098710
}
87108711
return finalComparisonResult;

src/util/comparison.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,15 @@ function deepCompareKeysAndValues(a: any, b: any, comparisonFunction: any) {
165165
const comparisonResult = comparisonFunction(a[key], b[key]);
166166
if (comparisonResult === null) {
167167
shouldReturnNull = true;
168+
return true;
168169
}
169170
return comparisonResult;
170171
});
171172
} else {
172173
finalComparisonResult = false;
173174
}
174175

175-
if (shouldReturnNull) {
176+
if (finalComparisonResult && shouldReturnNull) {
176177
return null;
177178
}
178179
return finalComparisonResult;

test/elm/comparison/comparison-test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ describe('Equal', () => {
3636

3737
it('should identify uncertian tuples with same fields but one has a null field', async function () {
3838
should(await this.uncertTuplesWithNullFieldOnOne.exec(this.ctx)).be.null();
39+
should(await this.uncertTuplesWithNullFieldOnFirstOne.exec(this.ctx)).be.null();
40+
});
41+
42+
it('should identify unequal tuples with different values but one has a null field', async function () {
43+
should(await this.uneqTuplesWithNullFieldOnOne.exec(this.ctx)).be.false();
44+
should(await this.uneqTuplesWithNullFieldOnFirstOne.exec(this.ctx)).be.false();
3945
});
4046

4147
it('should identify equal/unequal DateTimes in same timezone', async function () {

test/elm/comparison/data.cql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ define UneqTuples: Tuple{a: 1, b: Tuple{c: 1}} = Tuple{a: 1, b: Tuple{c: -1}}
77
define EqTuplesWithNullFields: Tuple{a: 'Hello', b: null} = Tuple{a: 'Hello', b: null}
88
define UneqTuplesWithNullFields: Tuple{a: 'Hello', b: null} = Tuple{a: 'Goodbye', b: null}
99
define UncertTuplesWithNullFieldOnOne: Tuple{a: 'Hello', b: null} = Tuple{a: 'Hello', b: 'null'}
10+
define UncertTuplesWithNullFieldOnFirstOne: Tuple{a: null, b: 'Goodbye'} = Tuple{a: 'Hello', b: 'Goodbye'}
11+
define UneqTuplesWithNullFieldOnOne: Tuple{a: 'Hello', b: null} = Tuple{a: 'Goodbye', b: 'null'}
12+
define UneqTuplesWithNullFieldOnFirstOne: Tuple{a: null, b: 'Hello'} = Tuple{a: 'null', b: 'Goodbye'}
1013
define EqDateTimes: DateTime(2000, 3, 15, 13, 30, 25, 200, +1.0) = DateTime(2000, 3, 15, 13, 30, 25, 200, +1.0)
1114
define UneqDateTimes: DateTime(2000, 3, 15, 13, 30, 25, 200, +1.0) = DateTime(2000, 3, 15, 13, 30, 25, 201, +1.0)
1215
define EqDateTimesTZ: DateTime(2000, 3, 15, 23, 30, 25, 200, +1.0) = DateTime(2000, 3, 16, 2, 30, 25, 200, +4.0)

0 commit comments

Comments
 (0)