eugeneo17 commented on code in PR #45817:
URL: https://github.com/apache/superset/pull/45817#discussion_r4411608354
##########
superset/common/query_context_processor.py:
##########
@@ -644,6 +646,14 @@ def get_data(
if verbose_map:
df.columns = [verbose_map.get(column, column) for column in
columns]
+ if is_feature_enabled("EXPORT_FORMATTED_NUMBERS"):
Review Comment:
Good catch, fixed. `get_data` no longer formats on the `POST_PROCESSED` path
(where `apply_client_processing` re-parses the CSV and runs the pivot
arithmetic), so a `,.2f` metric can no longer be coerced to NaN. The tabular
export formats are now applied after post-processing instead, through the
existing `table()`/`pivot_table_v2()` formatters (`apply_number_format` covers
CSV/XLSX under the flag). Added a regression test asserting the pre-pivot stage
is skipped for `POST_PROCESSED`.
##########
superset/charts/client_processing.py:
##########
@@ -1222,6 +1223,87 @@ def merge_currency_formats(
return currency_formats
+def apply_export_number_formats(
+ df: pd.DataFrame,
+ coltypes: list[GenericDataType],
+ form_data: dict[str, Any],
+ datasource: Optional[Union["BaseDatasource", "Query"]] = None,
+) -> pd.DataFrame:
+ """Format numeric columns as display text for a chart-data export.
+
+ Each numeric column (by ``coltypes``, aligned with ``df.columns``) is
+ resolved to its configured d3 format, currency and small-number format the
+ same way the live Table plugin does -- ``column_config`` overrides first,
+ then the datasource's saved per-metric formats -- and handed to
+ ``format_column``. A numeric column with nothing configured takes the
+ fixed-point fallback so its cells are never serialized as raw floats.
+ Non-numeric columns, nulls, NaN and infinity are left to the existing
+ helpers and pass through unchanged. The passed frame is mutated in place
and
+ returned; the helper runs no query and performs no I/O.
+ """
+ saved_formats, verbose_map = get_datasource_column_formats(datasource)
+ saved_currency_formats, _ = get_datasource_currency_formats(datasource)
+ column_config = form_data.get("column_config") or {}
+
+ def label_of(name: str) -> str:
+ """Return the column label as it appears in ``df``
(verbose-renamed)."""
+ return name if name in df.columns else verbose_map.get(name, name)
+
+ # Index the per-column overrides by the label present in ``df`` so the
+ # lookups line up with the (possibly verbose-renamed) frame columns.
+ number_format_by_label = {
+ label_of(name): fmt for name, fmt in saved_formats.items()
+ }
+ currency_by_label = {
+ label_of(name): currency for name, currency in
saved_currency_formats.items()
+ }
+ config_by_label = {label_of(name): config for name, config in
column_config.items()}
+
+ numeric_columns = {
+ column
+ for column, coltype in zip(df.columns, coltypes, strict=True)
+ if coltype == GenericDataType.NUMERIC
+ }
+
+ for column in df.columns:
+ if column not in numeric_columns:
+ continue
+
+ config = config_by_label.get(column) or {}
+ 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")
+ number_format = config.get("d3NumberFormat") or
number_format_by_label.get(
+ column
+ )
+ currency = (
+ configured_currency
+ if configured_currency.get("symbol")
+ else currency_by_label.get(column) or {}
+ )
+ # A cleared Select leaves a blank string behind, which the browser
+ # treats as unset.
+ small_number_format = (config.get("d3SmallNumberFormat") or
"").strip() or None
Review Comment:
Yes, done. `apply_export_number_formats` now mirrors `table()`'s
`applies_small_number_format` and skips `d3SmallNumberFormat` for
`ag-grid-table`. Added a test.
##########
superset/charts/client_processing.py:
##########
@@ -1222,6 +1223,87 @@ def merge_currency_formats(
return currency_formats
+def apply_export_number_formats(
+ df: pd.DataFrame,
+ coltypes: list[GenericDataType],
+ form_data: dict[str, Any],
+ datasource: Optional[Union["BaseDatasource", "Query"]] = None,
+) -> pd.DataFrame:
+ """Format numeric columns as display text for a chart-data export.
+
+ Each numeric column (by ``coltypes``, aligned with ``df.columns``) is
+ resolved to its configured d3 format, currency and small-number format the
+ same way the live Table plugin does -- ``column_config`` overrides first,
+ then the datasource's saved per-metric formats -- and handed to
+ ``format_column``. A numeric column with nothing configured takes the
+ fixed-point fallback so its cells are never serialized as raw floats.
+ Non-numeric columns, nulls, NaN and infinity are left to the existing
+ helpers and pass through unchanged. The passed frame is mutated in place
and
+ returned; the helper runs no query and performs no I/O.
+ """
+ saved_formats, verbose_map = get_datasource_column_formats(datasource)
+ saved_currency_formats, _ = get_datasource_currency_formats(datasource)
+ column_config = form_data.get("column_config") or {}
+
+ def label_of(name: str) -> str:
+ """Return the column label as it appears in ``df``
(verbose-renamed)."""
+ return name if name in df.columns else verbose_map.get(name, name)
+
+ # Index the per-column overrides by the label present in ``df`` so the
+ # lookups line up with the (possibly verbose-renamed) frame columns.
+ number_format_by_label = {
+ label_of(name): fmt for name, fmt in saved_formats.items()
+ }
+ currency_by_label = {
+ label_of(name): currency for name, currency in
saved_currency_formats.items()
+ }
+ config_by_label = {label_of(name): config for name, config in
column_config.items()}
+
+ numeric_columns = {
+ column
+ for column, coltype in zip(df.columns, coltypes, strict=True)
+ if coltype == GenericDataType.NUMERIC
+ }
+
+ for column in df.columns:
+ if column not in numeric_columns:
+ continue
+
+ config = config_by_label.get(column) or {}
+ 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")
+ number_format = config.get("d3NumberFormat") or
number_format_by_label.get(
+ column
+ )
+ currency = (
+ configured_currency
+ if configured_currency.get("symbol")
+ else currency_by_label.get(column) or {}
+ )
+ # A cleared Select leaves a blank string behind, which the browser
+ # treats as unset.
+ small_number_format = (config.get("d3SmallNumberFormat") or
"").strip() or None
+
+ if number_format or currency.get("symbol") or small_number_format:
+ format_column(
+ df,
+ column,
+ number_format,
+ currency,
+ small_d3_format=small_number_format,
+ small_currency=small_currency,
+ )
+ else:
+ # Nothing configured: keep exact fixed-point text rather than a raw
+ # float. Non-finite and non-decimal cells pass through untouched.
+ df[column] = df[column].map(csv.format_decimal)
Review Comment:
Fixed. The fallback now renders finite floats (and large ints) as exact
fixed-point text via `Decimal(str(value))`, so `1e-07` / `19.7999992` no longer
leak and large ints are not rounded by a float64 re-inference. Added float and
large-int tests.
--
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]