SBIN2010 commented on code in PR #38080:
URL: https://github.com/apache/superset/pull/38080#discussion_r2829634619
##########
superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/utilities.ts:
##########
@@ -92,6 +92,128 @@ const numberFormat = function (optsIn?:
NumberFormatOptions): Formatter {
};
};
+/**
+ * Safely converts any value to a number for aggregation purposes
+ * Handles null/undefined, strings, and non-numeric values
+ */
+function toAggregationNumber(value: unknown): number {
+ if (value == null) return 0;
+ if (typeof value === 'number') return value;
+ if (typeof value === 'string') {
+ const num = parseFloat(value.trim());
+ return Number.isNaN(num) ? 0 : num;
+ }
+ return 0;
+}
+
+type DataFunction = (key: string[], context: never[]) => unknown;
+
+interface GroupNode {
+ auto_agg_sum: number;
+ [childKey: string]: GroupNode | number;
+}
+
+/**
+ * Precomputes aggregate sums for all group levels using safe numeric
conversion
+ */
+function buildGroupAggregates(
+ keys: string[][],
+ dataFunc: DataFunction,
+): GroupNode {
+ const root: GroupNode = { auto_agg_sum: 0 } as GroupNode;
+
+ for (const key of keys) {
+ let current: GroupNode = root;
+
+ for (let i = 0; i < key.length - 1; i++) {
+ const segment = key[i];
+
+ if (!current[segment]) {
+ current[segment] = { auto_agg_sum: 0 } as GroupNode;
+ }
+
+ const childNode = current[segment] as GroupNode;
+ childNode.auto_agg_sum += toAggregationNumber(dataFunc(key, []));
+ current = childNode;
+ }
+ }
+
+ return root;
+}
+
+/**
+ * Creates a comparator function for hierarchical keys with subtotal awareness
+ */
+function createHierarchicalComparator(
+ groups: GroupNode,
+ top: boolean,
+ asc: boolean,
+ dataFunc: DataFunction,
+): (a: string[], b: string[]) => number {
+ const topBasis = top ? 1 : -1;
+ const orderBasis = asc ? 1 : -1;
+
+ return (a: string[], b: string[]): number => {
+ let current: GroupNode = groups;
+ const maxDepth = Math.max(a.length, b.length) - 1;
+
+ for (let depth = 0; depth < maxDepth; depth++) {
+ const aSeg = a[depth];
+ const bSeg = b[depth];
+
+ if (aSeg !== bSeg) {
+ const nodeA = current[aSeg] as GroupNode | undefined;
+ const nodeB = current[bSeg] as GroupNode | undefined;
+ const sumA = nodeA?.auto_agg_sum ?? 0;
+ const sumB = nodeB?.auto_agg_sum ?? 0;
+
+ if (sumA === sumB) {
+ return aSeg.localeCompare(bSeg);
+ }
+ return (sumA > sumB ? 1 : -1) * orderBasis;
+ }
+ if (depth + 1 < maxDepth && depth + 1 >= b.length) {
+ return topBasis;
+ }
+ if (depth + 1 < maxDepth && depth + 1 >= a.length) {
+ return -topBasis;
+ }
Review Comment:
Should the position of subtotals be taken into account when sorting values?
It seems more logical to ignore the specified position and perform the
specified sort. This is how it's currently implemented. @rusackas I'd like to
hear your opinion on this matter.
--
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]