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/08/13 18:07:13 UTC

(superset) branch master updated: fix(sunburst): keep SQL null and literal string "null" as distinct groups (#43013)

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

sadpandajoe 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 e1468a709c0 fix(sunburst): keep SQL null and literal string "null" as distinct groups (#43013)
e1468a709c0 is described below

commit e1468a709c0483323005d52ebd37a433474603e2
Author: Evan Rusackas <ev...@preset.io>
AuthorDate: Thu Aug 13 11:07:06 2026 -0700

    fix(sunburst): keep SQL null and literal string "null" as distinct groups (#43013)
    
    Co-authored-by: Claude <no...@anthropic.com>
---
 .../plugin-chart-echarts/src/utils/treeBuilder.ts  | 125 +++++++++++++--------
 .../test/utils/treeBuilder.test.ts                 |  72 ++++++++++++
 2 files changed, 152 insertions(+), 45 deletions(-)

diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/utils/treeBuilder.ts b/superset-frontend/plugins/plugin-chart-echarts/src/utils/treeBuilder.ts
index a0fbb34640f..864659917b3 100644
--- a/superset-frontend/plugins/plugin-chart-echarts/src/utils/treeBuilder.ts
+++ b/superset-frontend/plugins/plugin-chart-echarts/src/utils/treeBuilder.ts
@@ -17,7 +17,6 @@
  * under the License.
  */
 import { DataRecord, DataRecordValue } from '@superset-ui/core';
-import { groupBy as _groupBy, transform } from 'lodash-es';
 
 export type TreeNode = {
   name: DataRecordValue;
@@ -31,6 +30,47 @@ function getMetricValue(datum: DataRecord, metric: string) {
   return typeof datum[metric] === 'number' ? (datum[metric] as number) : 0;
 }
 
+// Groups records by the raw value of `groupByKey`, keyed with a Map instead
+// of a plain object. A plain-object accumulator (e.g. lodash's `groupBy`)
+// has to coerce every key to a string to use it as a property name, so a SQL
+// NULL and the literal string "null" both collapse to the same "null" key
+// and get merged into a single group. A Map keeps them as distinct keys, so
+// null filtering stays deterministic regardless of what else is in the
+// column.
+//
+// `Date` values need special handling: a `Map` compares object keys by
+// identity, not value, so two separately-parsed `Date` instances for the
+// same timestamp would otherwise land in different groups. Canonicalizing
+// by epoch time keeps identical timestamps together while still keeping
+// `null` and `'null'` distinct.
+function groupByValue(
+  data: DataRecord[],
+  groupByKey: string,
+): Map<DataRecordValue, DataRecord[]> {
+  const groups = new Map<DataRecordValue, DataRecord[]>();
+  const keysByCanonicalTime = new Map<number, DataRecordValue>();
+  data.forEach(datum => {
+    const rawKey = datum[groupByKey];
+    let key = rawKey;
+    if (rawKey instanceof Date) {
+      const time = rawKey.getTime();
+      const existingKey = keysByCanonicalTime.get(time);
+      if (existingKey !== undefined) {
+        key = existingKey;
+      } else {
+        keysByCanonicalTime.set(time, rawKey);
+      }
+    }
+    const group = groups.get(key);
+    if (group) {
+      group.push(datum);
+    } else {
+      groups.set(key, [datum]);
+    }
+  });
+  return groups;
+}
+
 export function treeBuilder(
   data: DataRecord[],
   groupBy: string[],
@@ -39,57 +79,52 @@ export function treeBuilder(
   filterNullNames?: boolean,
 ): TreeNode[] {
   const [curGroupBy, ...restGroupby] = groupBy;
-  const curData = _groupBy(data, curGroupBy);
-  const nodes = transform(
-    curData,
-    (result, value, key) => {
-      const name = curData[key][0][curGroupBy]!;
-      if (!restGroupby.length) {
-        (value ?? []).forEach(datum => {
-          const metricValue = getMetricValue(datum, metric);
-          const secondaryValue = secondaryMetric
-            ? getMetricValue(datum, secondaryMetric)
-            : metricValue;
-          const item = {
-            name,
-            value: metricValue,
-            secondaryValue,
-            groupBy: curGroupBy,
-          };
-          result.push(item);
-        });
-      } else {
-        // Children are already null-filtered by the recursive call, so the
-        // parent's value/secondaryValue exclude hidden nulls. This keeps the
-        // parent arc sized to its visible children (no empty gap).
-        const children = treeBuilder(
-          value,
-          restGroupby,
-          metric,
-          secondaryMetric,
-          filterNullNames,
-        );
-        const metricValue = children.reduce(
-          (prev, cur) => prev + (cur.value as number),
-          0,
-        );
+  const curData = groupByValue(data, curGroupBy);
+  const nodes: TreeNode[] = [];
+  curData.forEach((value, name) => {
+    if (!restGroupby.length) {
+      value.forEach(datum => {
+        const metricValue = getMetricValue(datum, metric);
         const secondaryValue = secondaryMetric
-          ? children.reduce(
-              (prev, cur) => prev + (cur.secondaryValue as number),
-              0,
-            )
+          ? getMetricValue(datum, secondaryMetric)
           : metricValue;
-        result.push({
+        nodes.push({
           name,
-          children,
           value: metricValue,
           secondaryValue,
           groupBy: curGroupBy,
         });
-      }
-    },
-    [] as TreeNode[],
-  );
+      });
+    } else {
+      // Children are already null-filtered by the recursive call, so the
+      // parent's value/secondaryValue exclude hidden nulls. This keeps the
+      // parent arc sized to its visible children (no empty gap).
+      const children = treeBuilder(
+        value,
+        restGroupby,
+        metric,
+        secondaryMetric,
+        filterNullNames,
+      );
+      const metricValue = children.reduce(
+        (prev, cur) => prev + (cur.value as number),
+        0,
+      );
+      const secondaryValue = secondaryMetric
+        ? children.reduce(
+            (prev, cur) => prev + (cur.secondaryValue as number),
+            0,
+          )
+        : metricValue;
+      nodes.push({
+        name,
+        children,
+        value: metricValue,
+        secondaryValue,
+        groupBy: curGroupBy,
+      });
+    }
+  });
   // Filter at every level so single-level charts and root nodes are covered,
   // not just nested children. A parent whose children were all null-filtered
   // is dropped too: keeping it would leave a zero-value arc that yields a NaN
diff --git a/superset-frontend/plugins/plugin-chart-echarts/test/utils/treeBuilder.test.ts b/superset-frontend/plugins/plugin-chart-echarts/test/utils/treeBuilder.test.ts
index ca5dd2a9622..971d914652d 100644
--- a/superset-frontend/plugins/plugin-chart-echarts/test/utils/treeBuilder.test.ts
+++ b/superset-frontend/plugins/plugin-chart-echarts/test/utils/treeBuilder.test.ts
@@ -646,4 +646,76 @@ describe('test treeBuilder', () => {
       },
     ]);
   });
+
+  // Regression: a plain-object grouping accumulator coerces every key to a
+  // string, so a SQL NULL and the literal string "null" both land under the
+  // "null" property and merge into one group. Keep them as separate nodes.
+  test('keeps a SQL null and the literal string "null" as distinct groups', () => {
+    const tree = treeBuilder(
+      [
+        { foo: null, count: 2, count2: 3 },
+        { foo: 'null', count: 5, count2: 7 },
+        { foo: 'a', count: 1, count2: 1 },
+      ],
+      ['foo'],
+      'count',
+    );
+    expect(tree).toEqual([
+      { groupBy: 'foo', name: null, secondaryValue: 2, value: 2 },
+      { groupBy: 'foo', name: 'null', secondaryValue: 5, value: 5 },
+      { groupBy: 'foo', name: 'a', secondaryValue: 1, value: 1 },
+    ]);
+  });
+
+  // Regression: distinct `Date` instances representing the same timestamp
+  // must not fragment into separate groups. A `Map` keyed on the raw object
+  // would otherwise compare `Date`s by identity rather than value.
+  test('groups distinct Date instances with the same timestamp together', () => {
+    const tree = treeBuilder(
+      [
+        { foo: new Date('2020-01-01T00:00:00Z'), bar: 'a', count: 2 },
+        { foo: new Date('2020-01-01T00:00:00Z'), bar: 'b', count: 3 },
+        { foo: new Date('2020-01-02T00:00:00Z'), bar: 'c', count: 5 },
+      ],
+      ['foo', 'bar'],
+      'count',
+    );
+    expect(tree).toEqual([
+      {
+        children: [
+          { groupBy: 'bar', name: 'a', secondaryValue: 2, value: 2 },
+          { groupBy: 'bar', name: 'b', secondaryValue: 3, value: 3 },
+        ],
+        groupBy: 'foo',
+        name: new Date('2020-01-01T00:00:00Z'),
+        secondaryValue: 5,
+        value: 5,
+      },
+      {
+        children: [{ groupBy: 'bar', name: 'c', secondaryValue: 5, value: 5 }],
+        groupBy: 'foo',
+        name: new Date('2020-01-02T00:00:00Z'),
+        secondaryValue: 5,
+        value: 5,
+      },
+    ]);
+  });
+
+  test('filters only the SQL null, not the literal string "null"', () => {
+    const tree = treeBuilder(
+      [
+        { foo: null, count: 2, count2: 3 },
+        { foo: 'null', count: 5, count2: 7 },
+        { foo: 'a', count: 1, count2: 1 },
+      ],
+      ['foo'],
+      'count',
+      undefined,
+      true,
+    );
+    expect(tree).toEqual([
+      { groupBy: 'foo', name: 'null', secondaryValue: 5, value: 5 },
+      { groupBy: 'foo', name: 'a', secondaryValue: 1, value: 1 },
+    ]);
+  });
 });