[DataQuery] Less than with nullable date/int
Description
Given the following data
[{age: 1}, {age: 2}, {age: null}]
when the oData query filter, $filter=age lt 2, is applied, you would not expect the last value to show up
Steps To Reproduce
https://stackblitz.com/edit/angular-rsabply1?file=src%2Fapp%2Fproducts.ts,src%2Fapp%2Fapp.component.ts
Set the filter to be less than today. You can see that undefined is filtered, but not null
I think it's because underneath, it's using JS default filter, i.e
[null, undefined, 1, 0].filter(x => x < 2) // [null, 1, 0]
Screenshots or video
No response
Actual Behavior
null is there
Expected Behavior
null not to be there
Browser
Chrome
Browser version
latest
OS type
Windows
OS version
No response
Last working version of the Kendo UI for Angular package (if regression).
No response
Is this a feature, or is it a bug?
Hello, @cheng93!
Indeed, you are correct that the observed behavior originates from JavaScript and is, in fact, the expected one.
Generally speaking, when performing various relational comparisons (>, <, >=, <=) between numbers, dates, and null, null is typically type coerced to 0. Consequently, null is less than positive numbers and dates(dates are converted to milliseconds for such operations), and thus, it is part of the filtered data:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than_or_equal#comparing_boolean_null_undefined_nan
On the other hand, undefined behaves slightly differently - it is coerced to NaN in such comparisons and as a result, leads to a false outcome:
- https://stackoverflow.com/questions/63203854/why-undefined-plus-undefined-equals-to-nan-in-javascript
With that being said, you could consider using the built-in isNull and isNotNull filter operators, as they would allow handling scenarios where some property of the data items is equal to null/undefined:
- https://www.telerik.com/kendo-angular-ui/components/data-query/api/filteroperator#isnull
- https://www.telerik.com/kendo-angular-ui/components/data-query/api/filteroperator#isnotnull