-
Notifications
You must be signed in to change notification settings - Fork 14k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(time-series table): display null values in time-series table and sortable #19024
fix(time-series table): display null values in time-series table and sortable #19024
Conversation
Codecov Report
@@ Coverage Diff @@
## master #19024 +/- ##
==========================================
- Coverage 66.56% 66.50% -0.07%
==========================================
Files 1641 1643 +2
Lines 63495 63455 -40
Branches 6425 6448 +23
==========================================
- Hits 42265 42200 -65
- Misses 19550 19585 +35
+ Partials 1680 1670 -10
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
before we make this fix could we either:
- add unit tests for this function
- migrate the component to TS
or even both? since we know related changes have broken this chart before. thanks!
@@ -129,7 +129,16 @@ const TimeTable = ({ | |||
sortType: (rowA, rowB, columnId) => { | |||
const rowAVal = rowA.values[columnId].props['data-value']; | |||
const rowBVal = rowB.values[columnId].props['data-value']; | |||
return rowAVal - rowBVal; | |||
if (typeof rowAVal === 'number' && typeof rowBVal === 'number') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be simplified as:
return (
(rowAVal == null) - (rowBVal == null) ||
Number.isNaN(rowAVal) - Number.isNaN(rowBVal) ||
Number(rowAVal) - Number(rowBVal)
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ktmud thank you for the simplified expression, but i feel it is a little hard to digest. I would prefer straightforward expression, given there is no significant space or complexity optimization. And after user research, we might want to add extra logic to always push null values at the bottom (instead of sorting as smallest), i think my original expression is easier to update and understood.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My proposed expression already included logics to sort null
and NaN
to the bottom.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Push null and NaN to the bottom is not decided yet. I think we will run user research to decide sort or push.
i added unit test. |
// @ts-ignore | ||
expect(sortFn(rowA, rowB, columnId)).toBe(-1); | ||
// @ts-ignore | ||
expect(sortFn(rowA, rowC, columnId)).toBe(-1); | ||
// @ts-ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to ts ignore here? if the rows are the improper types, then you can define them as the correct ones above instead of ignoring types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Row type is very complicated object, i am not sure how to mock my input data:
export interface UseTableRowProps<D extends object> {
cells: Array<Cell<D>>;
allCells: Array<Cell<D>>;
values: Record<IdType<D>, CellValue>;
getRowProps: (propGetter?: RowPropGetter<D>) => TableRowProps;
index: number;
original: D;
id: string;
subRows: Array<Row<D>>;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const rowA = {} as unknown as UseTableRowProps;
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow this trick works!!
if (typeof rowBVal === 'number') { | ||
return -1; | ||
} | ||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wanna add the test case where both are not a number?
also, can these ever be strings or objects or other types? or only ever number or null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return (
(rowAVal == null) - (rowBVal == null) ||
Number.isNaN(Number(rowAVal)) - Number.isNaN(Number(rowBVal)) ||
Number(rowAVal) - Number(rowBVal)
);
This should handle all cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the time-series table viz type, i think the cell values could be string, number, null, number mixed with null.
I agree, this should handle all cases. But Typescript doesn't like
(rowAVal == null) - (rowBVal == null)
any suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I double checked, the first column metric
doesn't use this sorting function. So the cell values could be null, number, no string type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can wrap them with Number
as well. It should convert false
to 0
and true
to 1
.
return (
Number(rowAVal == null) - Number(rowBVal == null) ||
Number.isNaN(Number(rowAVal)) - Number.isNaN(Number(rowBVal)) ||
Number(rowAVal) - Number(rowBVal)
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry...typescript doesn't like
Number.isNaN(Number(rowAVal)) - Number.isNaN(Number(rowBVal))
|
||
const sortNumberWithMixedTypes = (rowA: Row, rowB: Row, columnId: string) => { | ||
const rowAVal = rowA.values[columnId].props['data-value']; | ||
const rowBVal = rowB.values[columnId].props['data-value']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move Row
and columnId
out of this utility function to make it more generic? I'd imagine it can be useful in other places as well.
import { JSONPrimitive } from '@superset-ui/core';
export default function sortNumberWithMixedTypes(
valueA: JSONPrimitive | undefined,
valueB: JSONPrimitive | undefined
) {
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sortType
is prop from react-table useSortBy
: https://github.com/TanStack/react-table/blob/alpha/docs/api/useSortBy.md
I am not sure I can change my function signature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can always have wrapper function for sortType
:
sortType: useCallback((rowA, rowB, columnId) =>
sortNumberWithMixedTypes(
rowA.values[columnId].props['data-value'],
rowB.values[columnId].props['data-value'],
), []);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ESLint: React Hook "useCallback" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function.(react-hooks/rules-of-hooks)
this sortType must be memoized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it needs to be memorized, which is why I added useCallback.
You can either move the useCallback up a level or move the wrapper outside of the component as a simple one liner in the component file without using useCallback.
Point is, such utility functions should not depend on too specific input data types if we want to maximize reusability.
9475984
to
4bd809b
Compare
{ descending, nanTreatment: 'asSmallest' }, | ||
) * | ||
// react-table sort function always expects -1 for smaller number | ||
(descending ? -1 : 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@graceguo-supercat @etr2460 I took a different approach and added the option to change how null/nans are treated. Please take a look
i am ok with new solution, it is a more general comparator sort string/number/NaN/null like a number type way. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, thanks for the tests, the iteration, and all the work here to make sure we don't introduce any more bugs!
SUMMARY
Currently Time-series table display null value as number 0. I had a fix #18039 but it wasn't handle sorting correctly. in airbnb we found when null value mixed with number, the sort function didn't work, so i reverted the previous PR.
This PR is still try to fix the null value issue with similar approach, and add extra logic fix sorting: if null value is mixed with number values, all null value will be pushed to the smaller end.
TESTING INSTRUCTIONS
CI and manual test
ADDITIONAL INFORMATION