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

(superset) branch master updated: fix(time-format): handle string input in TimeFormatter to fix pivot table NaN dates (#38949)

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

rusackas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new a135e290355 fix(time-format): handle string input in TimeFormatter to fix pivot table NaN dates (#38949)
a135e290355 is described below

commit a135e290355c10a694bba28edacc8bdf8e50c78a
Author: mapledan <ma...@gmail.com>
AuthorDate: Thu Apr 30 21:51:37 2026 +0800

    fix(time-format): handle string input in TimeFormatter to fix pivot table NaN dates (#38949)
    
    Co-authored-by: RD-Dan <ma...@staff.ruten.com.tw>
    Co-authored-by: Claude Sonnet 4.6 <no...@anthropic.com>
    Co-authored-by: codeant-ai-for-open-source[bot] <24...@users.noreply.github.com>
---
 .../packages/superset-ui-core/src/time-format/TimeFormatter.ts   | 8 +++++---
 .../superset-ui-core/src/time-format/utils/stringifyTimeInput.ts | 8 +++++++-
 .../superset-ui-core/test/time-format/TimeFormatter.test.ts      | 9 +++++++++
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/superset-frontend/packages/superset-ui-core/src/time-format/TimeFormatter.ts b/superset-frontend/packages/superset-ui-core/src/time-format/TimeFormatter.ts
index 9800b99dcc0..fbe84f35afb 100644
--- a/superset-frontend/packages/superset-ui-core/src/time-format/TimeFormatter.ts
+++ b/superset-frontend/packages/superset-ui-core/src/time-format/TimeFormatter.ts
@@ -28,7 +28,7 @@ export const PREVIEW_TIME = new Date(Date.UTC(2017, 1, 14, 11, 22, 33));
 // Use type augmentation to indicate that
 // an instance of TimeFormatter is also a function
 interface TimeFormatter {
-  (value: Date | number | null | undefined): string;
+  (value: Date | number | string | null | undefined): string;
 }
 
 class TimeFormatter extends ExtensibleFunction {
@@ -49,7 +49,9 @@ class TimeFormatter extends ExtensibleFunction {
     formatFunc: TimeFormatFunction;
     useLocalTime?: boolean;
   }) {
-    super((value: Date | number | null | undefined) => this.format(value));
+    super((value: Date | number | string | null | undefined) =>
+      this.format(value),
+    );
 
     const {
       id = isRequired('config.id'),
@@ -66,7 +68,7 @@ class TimeFormatter extends ExtensibleFunction {
     this.useLocalTime = useLocalTime;
   }
 
-  format(value: Date | number | null | undefined) {
+  format(value: Date | number | string | null | undefined) {
     return stringifyTimeInput(value, time => this.formatFunc(time));
   }
 
diff --git a/superset-frontend/packages/superset-ui-core/src/time-format/utils/stringifyTimeInput.ts b/superset-frontend/packages/superset-ui-core/src/time-format/utils/stringifyTimeInput.ts
index 098002f0a11..55d722bd875 100644
--- a/superset-frontend/packages/superset-ui-core/src/time-format/utils/stringifyTimeInput.ts
+++ b/superset-frontend/packages/superset-ui-core/src/time-format/utils/stringifyTimeInput.ts
@@ -18,12 +18,18 @@
  */
 
 export default function stringifyTimeInput(
-  value: Date | number | undefined | null,
+  value: Date | number | string | undefined | null,
   fn: (time: Date) => string,
 ) {
   if (value === null || value === undefined) {
     return `${value}`;
   }
 
+  if (typeof value === 'string') {
+    const trimmed = value.trim();
+    const isIntegerString = /^-?\d+$/.test(trimmed);
+    return fn(new Date(isIntegerString ? Number(trimmed) : value));
+  }
+
   return fn(value instanceof Date ? value : new Date(value));
 }
diff --git a/superset-frontend/packages/superset-ui-core/test/time-format/TimeFormatter.test.ts b/superset-frontend/packages/superset-ui-core/test/time-format/TimeFormatter.test.ts
index 2edad835bb6..4bfe534ed88 100644
--- a/superset-frontend/packages/superset-ui-core/test/time-format/TimeFormatter.test.ts
+++ b/superset-frontend/packages/superset-ui-core/test/time-format/TimeFormatter.test.ts
@@ -67,6 +67,15 @@ describe('TimeFormatter', () => {
     test('handles number, treating it as a timestamp', () => {
       expect(formatter.format(PREVIEW_TIME.getTime())).toEqual('2017');
     });
+    test('handles numeric string, treating it as a timestamp', () => {
+      // PivotData.processRecord coerces values with String(), turning numeric
+      // timestamps into strings.
+      const timestamp = PREVIEW_TIME.getTime().toString();
+      expect(formatter.format(timestamp)).toEqual('2017');
+    });
+    test('handles ISO-8601 string without misinterpreting it as a number', () => {
+      expect(formatter.format('2017-02-14T11:22:33.000Z')).toEqual('2017');
+    });
     test('otherwise returns formatted value', () => {
       expect(formatter.format(PREVIEW_TIME)).toEqual('2017');
     });