-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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(sqllab): Floating numbers not sorting correctly in result column #17573
fix(sqllab): Floating numbers not sorting correctly in result column #17573
Conversation
Codecov Report
@@ Coverage Diff @@
## master #17573 +/- ##
==========================================
- Coverage 76.99% 68.58% -8.42%
==========================================
Files 1047 1589 +542
Lines 56497 64959 +8462
Branches 7799 6993 -806
==========================================
+ Hits 43499 44549 +1050
- Misses 12742 18526 +5784
- Partials 256 1884 +1628
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
|
||
// Parse any floating numbers so they'll sort correctly | ||
const parseFloatingNums = (value: any) => | ||
value.isNan ? value : parseFloat(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.
that's cool, didn't know that isNan was a function!
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.
Oops, it's Number.isNan
😅
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 think this will fail if the values are not numbers/NaN, can you check?
superset-frontend/src/components/FilterableTable/FilterableTable.tsx
Outdated
Show resolved
Hide resolved
superset-frontend/src/components/FilterableTable/FilterableTable.tsx
Outdated
Show resolved
Hide resolved
superset-frontend/src/components/FilterableTable/FilterableTable.tsx
Outdated
Show resolved
Hide resolved
const aValue = parseFloatingNums(a[sortBy]); | ||
const bValue = parseFloatingNums(b[sortBy]); | ||
|
||
String(aValue).localeCompare(String(bValue), 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.
if you're using localeCompare with the numeric option, then do you still need to convert to a float? It looks like you're converting to a float and then back again to a string? It seems redundant.
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.
Also, nice find on localeCompare, but I don't think it's going to support multilingually unless you explicitly pass the language that you want to support as the second argument.
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 need to parse the floating numbers for them to sort correctly, but localeCompare
will only take strings. The numerical option allows it to check the strings for numbers.
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.
@lyndsiWilliams but when you do String(aValue)
and String(bValue)
you're converting them back to strings. It should work if you just call localeCompare
on the values without parseFloat
, assuming you pass the numeric
option:
"20.0".localeCompare("10", undefined, {numeric: true})
1
"2.0".localeCompare("10", undefined, {numeric: true})
-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.
I did a lot of playing around with this and figured out that the sorting works without localeCompare
so I removed it and added testing in this commit
. While testing I found any floating point number that had more than 2 floating points (like 12.001
or longer) is a string
, while any other number is a number
. I think originally, the floating numbers weren't sorting correctly because the types were mixed. Just using the parseFloatingNums
function seems to solve this so maybe we can look at using localeCompare
in the future for multilingual sorting, since it just seems to be causing issues in this instance. I also didn't realize you had to explicitly pass the language you want to support as a second argument, so I don't think I'll be able to make this work with localeCompare
.
@lyndsiWilliams can you also write a test for this? |
You got it! 😁 |
// Parse any floating numbers so they'll sort correctly | ||
parseFloatingNums = (value: any) => { | ||
const floatValue = parseFloat(value); | ||
return Number.isNaN(floatValue) ? value : parseFloat(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.
Nit:
return Number.isNaN(floatValue) ? value : parseFloat(value); | |
return Number.isNaN(floatValue) ? value : floatValue |
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.
Whoops, I missed that one! Thank you, fixed in this commit
😁
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.
looks great!
…17573) * Floating nums now sorting correctly with parseFloatingNums function * Floating numbers AND strings now sorting correctly, +locale comparison * Added NULL handling back to sort function * Moved parseFloatingNums outside of sortResults * Removed localeCompare and added testing * Add equality check back to sort function * Added floatValue nit
SUMMARY
Floating numbers were not sorting correctly in the result column. This was fixed by creating a
parseFloatingNums
function to parse any numbers in the result set.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
BEFORE
AFTER
TESTING INSTRUCTIONS
ADDITIONAL INFORMATION