SEPURI-SAI-KRISHNA commented on code in PR #45439:
URL: https://github.com/apache/superset/pull/45439#discussion_r4321838386
##########
superset/charts/client_processing.py:
##########
@@ -851,18 +869,53 @@ def format_column(
parallel ``currency_context`` series is present, AUTO uses each row/cell's
contributing currencies first. Mixed context renders a neutral number;
empty context optionally falls back to query-wide detection.
+
+ ``small_d3_format`` is the column's small-number format. The choice is per
+ cell rather than per column, matching ``formatColumnValue``: a value below
+ one in absolute value takes that format with the column config's own
+ currency, exactly what ``getSmallNumberFormatter`` builds, and every other
+ value keeps the column's normal format. An AUTO currency resolves against
+ the row's context on both paths, since the browser passes the row data to
+ either formatter.
"""
- if d3_format or currency.get("symbol"):
+ if d3_format or currency.get("symbol") or small_d3_format:
if currency_context is None:
resolved_currency = resolve_auto_currency(currency,
detected_currency)
- df[column] = df[column].apply(
- partial(format_number_with_config, d3_format,
resolved_currency)
+ format_value = partial(
+ format_number_with_config, d3_format, resolved_currency
)
+ if small_d3_format:
+ format_small = partial(
+ format_number_with_config, small_d3_format, small_currency
or {}
Review Comment:
Confirmed, and the object matters rather than the symbol, so the fix turned
out to need one more distinction than the question implies.
`CurrencyFormatter` strips the `$` in its own format accessor, and `format`
calls that before it ever looks at the symbol:
```ts
getNormalizedD3Format() {
return this.d3Format.replace(/\$/g, '');
}
format(value: number, rowData?: RowData, currencyColumn?: string): string {
const formattedValue = getNumberFormatter(this.getNormalizedD3Format())(
value,
);
const isAutoMode = this.currency?.symbol === AUTO_CURRENCY_SYMBOL;
if (!this.hasValidCurrency() && !isAutoMode) {
return formattedValue as string;
}
```
So a symbol-less config returns the stripped number, as you describe.
`getSmallNumberFormatter` builds that formatter from any `currencyFormat`
object, but with no object at all it returns
`getNumberFormatter(d3SmallNumberFormat)`, which honors the `$` and gives
`$0.50`. The report needs both: an absent `currencyFormat` is not the same as a
cleared one, and `config.get("currencyFormat") or {}` had collapsed them
together. The small path now receives the raw value:
```python
configured_currency = config.get("currencyFormat") or {}
# The small-number formatter is built from this object whether or
not a
# symbol survived in it, so an absent one has to stay distinguishable
# from a cleared one.
small_currency = config.get("currencyFormat")
```
and `format_column` strips the format when there is an object:
```python
if small_d3_format and small_currency is not None:
small_d3_format = small_d3_format.replace("$", "") or SMART_NUMBER
```
The `or SMART_NUMBER` covers a format that is only `$`. Stripping leaves an
empty string, and `NumberFormatterRegistry.get` answers an empty key with its
default key, which is SMART_NUMBER, so the report says the same thing rather
than falling through to the raw value.
That one statement covers the AUTO and context branch as well, since it runs
before either, which is why I put it at the top of `format_column` rather than
at the two call sites.
Three tests, all with `d3SmallNumberFormat: "$,.2f"` over `[0.5, 12.5]`:
`test_table_small_number_format_drops_a_dollar_for_a_cleared_currency`
expects `["0.50", "12.50"]` and fails with `$0.50` on the parent commit.
`test_table_small_number_format_keeps_a_dollar_without_a_currency_config`
expects `["$0.50", "12.50"]`, pinning the case the strip must not touch.
`test_table_small_number_format_drops_a_dollar_with_a_currency_context` runs
the same config through the per-row branch with a currency column, expecting
`["0.50", "12.50"]`, so a row's USD never reaches a symbol-less config.
--
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]