codeant-ai-for-open-source[bot] commented on code in PR #41098:
URL: https://github.com/apache/superset/pull/41098#discussion_r3418254883


##########
superset-frontend/plugins/plugin-chart-table/src/utils/formatValue.ts:
##########
@@ -27,6 +27,18 @@ import { GenericDataType } from 
'@apache-superset/core/common';
 import { DataColumnMeta } from '../types';
 import DateWithFormatter from './DateWithFormatter';
 
+/**
+ * Returns true if the given D3 format string represents a percentage format.
+ * Percentage values are stored as decimals (e.g. 0.05 = 5%), so
+ * Math.abs(value) < 1 is always true for them. Without this guard the
+ * small-number formatter would intercept every value in a percentage column
+ * and render it without the intended percentage formatting.
+ * See https://github.com/apache/superset/issues/36189
+ */
+function isPercentageFormat(formatString?: string): boolean {
+  return typeof formatString === 'string' && formatString.trim().endsWith('%');

Review Comment:
   **Suggestion:** `isPercentageFormat` only treats format strings ending with 
`%` as percentages, but D3 also has the `p` type for percentage formatting. For 
formats like `.2p`, this helper returns false and small values are still 
incorrectly routed to the small-number formatter. Extend detection to include 
all percentage-capable D3 types (at least `%` and `p`). [logic error]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Table chart misformats columns using D3 `.p` percent formats.
   - ⚠️ Drill-to-detail menu shows incorrect formatted percentage values.
   - ⚠️ Users configuring `.p` formats see inconsistent behavior.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. In a Table chart's column configuration UI (backed by 
`SHARED_COLUMN_CONFIG_PROPS` in
   
`src/explore/components/controls/ColumnConfigControl/constants.tsx:186-199`), 
set a
   numeric or percent metric column's `d3NumberFormat` to a D3 percentage type 
using `p`, for
   example `.2p`.
   
   2. `processColumns` in 
`plugins/plugin-chart-table/src/transformProps.ts:17-113` reads
   `column_config[key]`, computes `numberFormat = config.d3NumberFormat || 
savedFormat` at
   line 38, and for a percent metric builds `formatter = 
getNumberFormatter(numberFormat ||
   PERCENT_3_POINT)` at lines 75-78, so the effective formatter is a D3 `.2p` 
percentage.
   
   3. When the Table chart renders each cell, `TableChart.tsx:1038-1048` calls
   `formatColumnValue(column, value, row.original)` for the column configured 
with `.2p`.
   
   4. Inside `formatColumnValue` in `utils/formatValue.ts:74-106`, 
`useSmallNumberFormatter`
   is computed at lines 95-99 using 
`!isPercentageFormat(config.d3NumberFormat)`. For
   `config.d3NumberFormat === '.2p'`, `isPercentageFormat` is implemented at 
lines 38-40 as
   `return typeof formatString === 'string' && 
formatString.trim().endsWith('%');`, so it
   returns false because the format string ends with `'p'` not `'%'`. Thus for 
any small
   decimal value (e.g. `0.0001`), `useSmallNumberFormatter` is true and 
`formatValue` is
   invoked with the small-number formatter instead of the `.2p` percentage 
formatter, causing
   the cell to display a non-percent representation (e.g. `'0.0001'` formatted 
via
   small-number D3 format) rather than the intended percentage (e.g. 
`'0.01%'`). This
   demonstrates that limiting `isPercentageFormat` to specifiers ending with 
`%` misses valid
   D3 percentage formats like `.2p`, leaving those configurations affected by 
the original
   small-number routing bug.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=012d432854ad44aebd5b3993a1cfd282&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=012d432854ad44aebd5b3993a1cfd282&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** 
superset-frontend/plugins/plugin-chart-table/src/utils/formatValue.ts
   **Line:** 39:39
   **Comment:**
        *Logic Error: `isPercentageFormat` only treats format strings ending 
with `%` as percentages, but D3 also has the `p` type for percentage 
formatting. For formats like `.2p`, this helper returns false and small values 
are still incorrectly routed to the small-number formatter. Extend detection to 
include all percentage-capable D3 types (at least `%` and `p`).
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41098&comment_hash=fc7426578fe7a2bafeeb86ec34b6fe3535b074fff0d95d9041daff572e5f5dad&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41098&comment_hash=fc7426578fe7a2bafeeb86ec34b6fe3535b074fff0d95d9041daff572e5f5dad&reaction=dislike'>👎</a>



##########
superset-frontend/plugins/plugin-chart-table/src/utils/formatValue.ts:
##########
@@ -78,10 +87,19 @@ export function formatColumnValue(
             currency: config.currencyFormat,
           })
         : getNumberFormatter(config.d3SmallNumberFormat);
+
+  // Do not apply the small-number formatter when the column uses a percentage
+  // format. Percentage values are stored as decimals (0-1), so the threshold
+  // Math.abs(value) < 1 would fire for every value and bypass the intended
+  // percentage formatter. See https://github.com/apache/superset/issues/36189
+  const useSmallNumberFormatter =
+    isNumber &&
+    typeof value === 'number' &&
+    Math.abs(value) < 1 &&
+    !isPercentageFormat(config.d3NumberFormat);

Review Comment:
   **Suggestion:** The percentage bypass check only inspects 
`config.d3NumberFormat`, but the active formatter can also come from 
datasource-level `savedFormat` (via `formatter`) when column config is empty. 
In that common path, percentage columns still satisfy `Math.abs(value) < 1` and 
get routed to the small-number formatter, so the original bug remains for 
datasource-defined percent formats. Base the percentage detection on the 
effective formatter/format id (or propagate resolved number format into config) 
rather than only `config.d3NumberFormat`. [incomplete implementation]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Table chart misformats small percentage metrics with savedFormat.
   - ⚠️ Drill-to-detail context values show incorrect non-percent numbers.
   - ⚠️ Users see inconsistent formatting between small and larger values.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Configure a dataset column as a percent metric so it appears in 
`percent_metrics` and
   query results (used by `processColumns` in
   `plugins/plugin-chart-table/src/transformProps.ts:200-219`).
   
   2. In the datasource metadata, set a D3 percentage format (e.g. `.8%`) for 
that column so
   `columnFormats[key]` holds `.8%` and `savedFormat` is populated in
   `transformProps.ts:36-38`.
   
   3. On a Table chart, open the column config UI (driven by 
`SHARED_COLUMN_CONFIG_PROPS` in
   `src/explore/components/controls/ColumnConfigControl/constants.tsx:186-199`) 
and set only
   `d3SmallNumberFormat` (e.g. `.4f`) for this column, leaving `d3NumberFormat` 
empty so
   `column_config[key]` has `{ d3SmallNumberFormat: '.4f' }` but no 
`d3NumberFormat`.
   
   4. At runtime, `processColumns` in `transformProps.ts:17-113` computes 
`numberFormat =
   config.d3NumberFormat || savedFormat` so `numberFormat === '.8%'`, and for 
the percent
   metric sets `formatter = getNumberFormatter(numberFormat || 
PERCENT_3_POINT)` at
   `transformProps.ts:75-78`. The resulting `DataColumnMeta` has `formatter` 
for `.8%` and
   `config` containing only `d3SmallNumberFormat`. When 
`TableChart.tsx:1038-1048` renders
   cells, it calls `formatColumnValue(column, value, row.original)`. Inside
   `formatColumnValue` (`utils/formatValue.ts:74-106`), `smallNumberFormatter` 
is built from
   `config.d3SmallNumberFormat` (line 82-89) and `useSmallNumberFormatter` is 
computed at
   lines 95-99 as `isNumber && typeof value === 'number' && Math.abs(value) < 1 
&&
   !isPercentageFormat(config.d3NumberFormat)`. Because `config.d3NumberFormat` 
is undefined,
   `isPercentageFormat(config.d3NumberFormat)` at line 99 returns false, so
   `useSmallNumberFormatter` is true for any small decimal like `0.0001`. The 
call
   `formatValue(useSmallNumberFormatter ? smallNumberFormatter : formatter, 
value, ...)` at
   lines 101-105 then uses the small-number formatter `.4f` instead of the 
intended
   datasource percentage formatter `.8%`, causing small percent values to 
render as raw
   decimals (e.g. `0.0001`) instead of percentages (e.g. `0.01000000%`). This 
shows that
   relying only on `config.d3NumberFormat` in the percentage check fails for 
percentage
   formats inherited purely from datasource-level `savedFormat`, leaving those 
columns
   misformatted when a small-number format is configured.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=7c2f6fd729ad4010b4c515ed41afb660&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=7c2f6fd729ad4010b4c515ed41afb660&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** 
superset-frontend/plugins/plugin-chart-table/src/utils/formatValue.ts
   **Line:** 99:99
   **Comment:**
        *Incomplete Implementation: The percentage bypass check only inspects 
`config.d3NumberFormat`, but the active formatter can also come from 
datasource-level `savedFormat` (via `formatter`) when column config is empty. 
In that common path, percentage columns still satisfy `Math.abs(value) < 1` and 
get routed to the small-number formatter, so the original bug remains for 
datasource-defined percent formats. Base the percentage detection on the 
effective formatter/format id (or propagate resolved number format into config) 
rather than only `config.d3NumberFormat`.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41098&comment_hash=af5ef14392356cfae65b7f95e3dce9b327c54de38f57eae3d42f3913811c18ec&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41098&comment_hash=af5ef14392356cfae65b7f95e3dce9b327c54de38f57eae3d42f3913811c18ec&reaction=dislike'>👎</a>



-- 
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]

Reply via email to