EnxDev commented on code in PR #44739: URL: https://github.com/apache/superset/pull/44739#discussion_r4137521487
########## superset-frontend/src/components/FilterableTable/sortResults.test.ts: ########## @@ -0,0 +1,118 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { decimalParts, isNumericText, sortResults } from './sortResults'; + +test.each([ + ['2', '10', -1], + [ + '12345678901234567890.123456789012345678', + '12345678901234567890.123456789012345679', + -1, + ], + [ + '-12345678901234567890.123456789012345678', + '-12345678901234567890.123456789012345679', + 1, + ], + ['1E-18', '0.000000000000000002', -1], + ['1E+100000', '9E+99999', 1], + ['1E-100000', '9E-100001', 1], + ['0.00000000000000000001', '0', 1], + ['0010.500', 10.5, 0], + ['-0.00', 0, 0], + ['.5', '0.50', 0], + ['9.99', 10, -1], + [null, '1.23', 1], + ['1.23', null, -1], + [null, null, 0], + ['apple', 'pear', -1], + ['2024-01-01', '2025-01-01', -1], + ['-Infinity', '-1.2', -1], + ['Infinity', '1.2', 1], +] as const)('sorts %s vs %s exactly', (a, b, expected) => { + expect(sortResults(a, b)).toBe(expected); +}); + +test.each([ + ['apple', 5], + ['apple', 'NaN'], + [NaN, 1.5], + ['2024-01-01', 7], +] as const)('orders %s against %s in one direction only', (a, b) => { + expect(sortResults(a, b)).toBe(-sortResults(b, a)); +}); + +test('treats two NaNs as equal rather than unordered', () => { + expect(sortResults(NaN, NaN)).toBe(0); + expect(sortResults('NaN', 'NaN')).toBe(0); +}); + +test('sorts a mixed text and number column transitively', () => { + const column = [5, 'apple', 'NaN', 10, 'banana']; + const ascending = [...column].sort(sortResults); + const descending = [...column].reverse().sort(sortResults); + expect(descending).toEqual(ascending); +}); + +// The regular expressions these parsers replaced, kept as a reference oracle. Review Comment: Nit, take it or leave it. `LEGACY_DECIMAL` was never on master, it came from an earlier commit in this PR, so after the squash "the regexes these parsers replaced" sends readers looking for something that isn't in history. Maybe just call them the reference regexes the linear parsers must agree with. ########## superset/dataframe.py: ########## @@ -85,8 +85,27 @@ def _is_trusted_missing_or_nonfinite(value: Any) -> bool: return False +def _convert_decimals(value: Any) -> Any: + """Keep SQL Lab decimals exact across JSON, MessagePack and browser parsing.""" + # Match exact types only, as below, so object-column values never get to run + # overridden ``items``/``__iter__`` hooks during conversion. + value_type = type(value) + if value_type is Decimal: + return str(value) if Decimal.is_finite(value) else None Review Comment: `str()` on a Decimal goes scientific once the adjusted exponent drops below -6, and that happens a lot with high-scale columns. A zero from `DECIMAL(38,18)` comes back as `Decimal('0.000000000000000000')`, and `str()` makes that `0E-18`, which is what SQL Lab will show in every zero cell. `-0.0000001` turns into `-1.00000000000E-7`, and then `_is_negative_number` in `superset/utils/csv.py` doesn't match it (it only allows digits and dots), so the CSV export writes `'-1.00000000000E-7` with the formula-escape apostrophe. `format(value, "f")` keeps every digit and the scale but stays in fixed notation, and it fixes both problems. The `1E+30` test case would then expect `1000000000000000000000000000000`. Could you also add `0.000000000000000000` and `-0.000000100000000000` to that parametrize, plus a `df_to_escaped_csv` assertion on the negative one? ```suggestion return format(value, "f") if Decimal.is_finite(value) else None ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
