You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by jl...@apache.org on 2026/04/30 17:52:04 UTC

(superset) 01/02: fix(pivot-table): guard convertToNumberIfNumeric against non-string Date values

This is an automated email from the ASF dual-hosted git repository.

sadpandajoe pushed a commit to branch fix-missing-filters-embedded
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 4b7283e3b8c33d7bbf5e502dd90c005c61273e4b
Author: Joe Li <jo...@preset.io>
AuthorDate: Thu Apr 16 09:32:15 2026 -0700

    fix(pivot-table): guard convertToNumberIfNumeric against non-string Date values
    
    When temporal columns are used in pivot table Rows/Columns, Date objects
    can reach convertToNumberIfNumeric at runtime despite the string[] type
    declaration (due to any-typed boundaries in the data pipeline). Calling
    .trim() on a Date object throws TypeError: t.trim is not a function,
    crashing the chart entirely.
    
    Widen the parameter type to unknown and add a type guard that returns
    non-string values unchanged, letting the date formatter receive them
    directly. Also update makeColPivotSettings test helper to accept unknown
    and add regression tests for the Date object passthrough paths.
    
    Co-Authored-By: Claude Sonnet 4.6 <no...@anthropic.com>
---
 .../src/react-pivottable/TableRenderers.tsx        |  5 ++-
 .../test/react-pivottable/tableRenders.test.tsx    | 44 +++++++++++++++++++++-
 2 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.tsx b/superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.tsx
index 9961958b966..78d412f76e9 100644
--- a/superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.tsx
+++ b/superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.tsx
@@ -271,7 +271,10 @@ function sortHierarchicalObject(
   return result;
 }
 
-function convertToNumberIfNumeric(value: string): string | number {
+function convertToNumberIfNumeric(value: unknown): unknown {
+  if (typeof value !== 'string') {
+    return value;
+  }
   const n = Number(value);
   return value.trim() !== '' && !Number.isNaN(n) ? n : value;
 }
diff --git a/superset-frontend/plugins/plugin-chart-pivot-table/test/react-pivottable/tableRenders.test.tsx b/superset-frontend/plugins/plugin-chart-pivot-table/test/react-pivottable/tableRenders.test.tsx
index 6250ef66a44..7c60da3baac 100644
--- a/superset-frontend/plugins/plugin-chart-pivot-table/test/react-pivottable/tableRenders.test.tsx
+++ b/superset-frontend/plugins/plugin-chart-pivot-table/test/react-pivottable/tableRenders.test.tsx
@@ -1052,7 +1052,7 @@ test('renderTableRow uses active header surface for adaptive contrast', () => {
 });
 
 function makeColPivotSettings(
-  value: string,
+  value: unknown,
 ): Parameters<TableRenderer['renderColHeaderRow']>[2] {
   return {
     rowAttrs: [],
@@ -1146,3 +1146,45 @@ test.each([
     expect(formatter).toHaveBeenCalledWith(expected);
   },
 );
+
+test('col header date formatter does not throw when value is a Date object', () => {
+  const dateValue = new Date(1700000000000);
+  const formatter = jest.fn().mockReturnValue('formatted');
+  tableRenderer = new TableRenderer({
+    ...mockProps,
+    cols: ['event_time'],
+    tableOptions: {
+      ...mockProps.tableOptions,
+      dateFormatters: { event_time: formatter },
+    },
+  });
+  expect(() =>
+    tableRenderer.renderColHeaderRow(
+      'event_time',
+      0,
+      makeColPivotSettings(dateValue),
+    ),
+  ).not.toThrow();
+  expect(formatter).toHaveBeenCalledWith(dateValue);
+});
+
+test('row header date formatter does not throw when value is a Date object', () => {
+  const dateValue = new Date(1700000000000);
+  const formatter = jest.fn().mockReturnValue('formatted');
+  tableRenderer = new TableRenderer({
+    ...mockProps,
+    rows: ['event_time'],
+    tableOptions: {
+      ...mockProps.tableOptions,
+      dateFormatters: { event_time: formatter },
+    },
+  });
+  expect(() =>
+    tableRenderer.renderTableRow(
+      [dateValue as unknown as string],
+      0,
+      makeRowPivotSettings(),
+    ),
+  ).not.toThrow();
+  expect(formatter).toHaveBeenCalledWith(dateValue);
+});