korbit-ai[bot] commented on code in PR #36050:
URL: https://github.com/apache/superset/pull/36050#discussion_r2507777233


##########
superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.jsx:
##########
@@ -348,6 +418,72 @@ export class TableRenderer extends Component {
     return spans;
   }
 
+  calculateGroups(pivotData, visibleColKey, columnIndex) {
+    const groups = {};
+    const rows = pivotData.rowKeys;
+    rows.forEach(rowKey => {
+      let current = groups;
+      let sumGroup = 0;
+      rowKey.forEach(key => {
+        if (!current[key]) {
+          current[key] = { currentVal: 0 };
+        }
+        sumGroup += pivotData
+          .getAggregator(rowKey, visibleColKey[columnIndex])
+          .value();
+        current[key].currentVal = sumGroup;
+        current = current[key];
+      });
+    });
+    return groups;
+  }
+
+  sortAndCacheData(groups, sortOrder, subtotals, maxRowIndex) {
+    const { rowEnabled, rowPartialOnTop } = subtotals;
+    const sortedGroups = sortHierarchicalObject(
+      groups,
+      sortOrder,
+      rowPartialOnTop,
+    );
+    return convertToArray(
+      sortedGroups,
+      rowEnabled,
+      rowPartialOnTop,
+      maxRowIndex,
+    );
+  }
+
+  sortData(columnIndex, visibleColKeys, pivotData, maxRowIndex) {
+    this.setState(state => {
+      const { sortingOrder, activeSortColumn } = state;
+
+      let newSortingOrder = [];
+      let newDirection = 'asc';
+
+      if (activeSortColumn === columnIndex) {
+        newDirection = sortingOrder[columnIndex] === 'asc' ? 'desc' : 'asc';
+      }
+
+      newSortingOrder[columnIndex] = newDirection;
+      const groups = this.calculateGroups(
+        pivotData,
+        visibleColKeys,
+        columnIndex,
+      );
+      const sortedRowKeys = this.sortAndCacheData(
+        groups,
+        newSortingOrder[columnIndex],
+        pivotData.subtotals,
+        maxRowIndex,
+      );
+      this.cachedBasePivotSettings.rowKeys = sortedRowKeys;

Review Comment:
   ### Direct mutation of cached pivot settings <sub>![category 
Performance](https://img.shields.io/badge/Performance-4f46e5)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   Direct mutation of cached object property during state update can cause 
performance issues and potential race conditions.
   
   
   ###### Why this matters
   Mutating cached objects directly can lead to stale data being used in 
subsequent renders, cause unnecessary re-computations, and create hard-to-debug 
issues when multiple sort operations occur rapidly.
   
   ###### Suggested change ∙ *Feature Preview*
   Instead of mutating the cached object, create a new cached settings object 
or update it through proper state management:
   
   ```jsx
   this.setState(state => {
     // ... existing code ...
     
     // Create new cached settings instead of mutating
     this.cachedBasePivotSettings = {
       ...this.cachedBasePivotSettings,
       rowKeys: sortedRowKeys
     };
     
     return {
       sortingOrder: newSortingOrder,
       activeSortColumn: columnIndex,
     };
   });
   ```
   
   
   ###### Provide feedback to improve future suggestions
   [![Nice 
Catch](https://img.shields.io/badge/👍%20Nice%20Catch-71BC78)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/00db923c-4cb8-49f8-bd1e-c16744527cf4/upvote)
 
[![Incorrect](https://img.shields.io/badge/👎%20Incorrect-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/00db923c-4cb8-49f8-bd1e-c16744527cf4?what_not_true=true)
  [![Not in 
Scope](https://img.shields.io/badge/👎%20Out%20of%20PR%20scope-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/00db923c-4cb8-49f8-bd1e-c16744527cf4?what_out_of_scope=true)
 [![Not in coding 
standard](https://img.shields.io/badge/👎%20Not%20in%20our%20standards-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/00db923c-4cb8-49f8-bd1e-c16744527cf4?what_not_in_standard=true)
 
[![Other](https://img.shields.io/badge/👎%20Other-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/00db923c-4cb8-49f8-bd1e-c16744527cf4)
   </details>
   
   <sub>
   
   💬 Looking for more details? Reply to this comment to chat with Korbit.
   </sub>
   
   <!--- korbi internal id:e0dd44c3-cfe3-4901-a84a-68e1eea418b9 -->
   
   
   [](e0dd44c3-cfe3-4901-a84a-68e1eea418b9)



##########
superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.jsx:
##########
@@ -348,6 +418,72 @@ export class TableRenderer extends Component {
     return spans;
   }
 
+  calculateGroups(pivotData, visibleColKey, columnIndex) {
+    const groups = {};
+    const rows = pivotData.rowKeys;
+    rows.forEach(rowKey => {
+      let current = groups;
+      let sumGroup = 0;
+      rowKey.forEach(key => {
+        if (!current[key]) {
+          current[key] = { currentVal: 0 };
+        }
+        sumGroup += pivotData
+          .getAggregator(rowKey, visibleColKey[columnIndex])
+          .value();
+        current[key].currentVal = sumGroup;
+        current = current[key];
+      });
+    });
+    return groups;
+  }
+
+  sortAndCacheData(groups, sortOrder, subtotals, maxRowIndex) {
+    const { rowEnabled, rowPartialOnTop } = subtotals;
+    const sortedGroups = sortHierarchicalObject(
+      groups,
+      sortOrder,
+      rowPartialOnTop,
+    );
+    return convertToArray(
+      sortedGroups,
+      rowEnabled,
+      rowPartialOnTop,
+      maxRowIndex,
+    );
+  }
+
+  sortData(columnIndex, visibleColKeys, pivotData, maxRowIndex) {
+    this.setState(state => {
+      const { sortingOrder, activeSortColumn } = state;
+
+      let newSortingOrder = [];
+      let newDirection = 'asc';
+
+      if (activeSortColumn === columnIndex) {
+        newDirection = sortingOrder[columnIndex] === 'asc' ? 'desc' : 'asc';
+      }
+
+      newSortingOrder[columnIndex] = newDirection;

Review Comment:
   ### Direct array mutation before setState <sub>![category 
Functionality](https://img.shields.io/badge/Functionality-0284c7)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   The sortingOrder array is being mutated directly before being set in state, 
which can cause issues with React's state comparison and re-rendering logic.
   
   
   ###### Why this matters
   React may not detect the state change properly due to the mutation, leading 
to components not re-rendering when they should, resulting in stale UI state.
   
   ###### Suggested change ∙ *Feature Preview*
   Create a new array instead of mutating the existing one:
   
   ```jsx
   const newSortingOrder = [...sortingOrder];
   newSortingOrder[columnIndex] = newDirection;
   ```
   
   
   ###### Provide feedback to improve future suggestions
   [![Nice 
Catch](https://img.shields.io/badge/👍%20Nice%20Catch-71BC78)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/77851499-2839-43c1-9c5e-1ade1696b02e/upvote)
 
[![Incorrect](https://img.shields.io/badge/👎%20Incorrect-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/77851499-2839-43c1-9c5e-1ade1696b02e?what_not_true=true)
  [![Not in 
Scope](https://img.shields.io/badge/👎%20Out%20of%20PR%20scope-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/77851499-2839-43c1-9c5e-1ade1696b02e?what_out_of_scope=true)
 [![Not in coding 
standard](https://img.shields.io/badge/👎%20Not%20in%20our%20standards-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/77851499-2839-43c1-9c5e-1ade1696b02e?what_not_in_standard=true)
 
[![Other](https://img.shields.io/badge/👎%20Other-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/77851499-2839-43c1-9c5e-1ade1696b02e)
   </details>
   
   <sub>
   
   💬 Looking for more details? Reply to this comment to chat with Korbit.
   </sub>
   
   <!--- korbi internal id:21cc613b-d7ba-4f0a-af73-01dd0c905c80 -->
   
   
   [](21cc613b-d7ba-4f0a-af73-01dd0c905c80)



##########
superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.jsx:
##########
@@ -348,6 +418,72 @@ export class TableRenderer extends Component {
     return spans;
   }
 
+  calculateGroups(pivotData, visibleColKey, columnIndex) {
+    const groups = {};
+    const rows = pivotData.rowKeys;
+    rows.forEach(rowKey => {
+      let current = groups;
+      let sumGroup = 0;
+      rowKey.forEach(key => {
+        if (!current[key]) {
+          current[key] = { currentVal: 0 };
+        }
+        sumGroup += pivotData
+          .getAggregator(rowKey, visibleColKey[columnIndex])
+          .value();
+        current[key].currentVal = sumGroup;
+        current = current[key];
+      });
+    });
+    return groups;
+  }
+
+  sortAndCacheData(groups, sortOrder, subtotals, maxRowIndex) {
+    const { rowEnabled, rowPartialOnTop } = subtotals;
+    const sortedGroups = sortHierarchicalObject(
+      groups,
+      sortOrder,
+      rowPartialOnTop,
+    );
+    return convertToArray(
+      sortedGroups,
+      rowEnabled,
+      rowPartialOnTop,
+      maxRowIndex,
+    );
+  }
+
+  sortData(columnIndex, visibleColKeys, pivotData, maxRowIndex) {
+    this.setState(state => {
+      const { sortingOrder, activeSortColumn } = state;
+
+      let newSortingOrder = [];
+      let newDirection = 'asc';
+
+      if (activeSortColumn === columnIndex) {
+        newDirection = sortingOrder[columnIndex] === 'asc' ? 'desc' : 'asc';
+      }
+
+      newSortingOrder[columnIndex] = newDirection;
+      const groups = this.calculateGroups(
+        pivotData,
+        visibleColKeys,
+        columnIndex,
+      );
+      const sortedRowKeys = this.sortAndCacheData(
+        groups,
+        newSortingOrder[columnIndex],
+        pivotData.subtotals,
+        maxRowIndex,
+      );

Review Comment:
   ### Missing memoization for expensive sort operations <sub>![category 
Performance](https://img.shields.io/badge/Performance-4f46e5)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   Expensive sorting calculations are performed on every sort click without 
memoization or caching.
   
   
   ###### Why this matters
   Recalculating groups and sorting data on every click can cause significant 
performance degradation, especially with large datasets, as these operations 
have O(n log n) complexity and involve deep object traversal.
   
   ###### Suggested change ∙ *Feature Preview*
   Implement memoization to cache sort results based on column index and sort 
direction:
   
   ```jsx
   // Add to constructor
   this.sortCache = new Map();
   
   // In sortData method
   const cacheKey = `${columnIndex}-${newDirection}`;
   if (this.sortCache.has(cacheKey)) {
     const cachedRowKeys = this.sortCache.get(cacheKey);
     this.cachedBasePivotSettings = {
       ...this.cachedBasePivotSettings,
       rowKeys: cachedRowKeys
     };
   } else {
     const groups = this.calculateGroups(pivotData, visibleColKeys, 
columnIndex);
     const sortedRowKeys = this.sortAndCacheData(groups, newDirection, 
pivotData.subtotals, maxRowIndex);
     this.sortCache.set(cacheKey, sortedRowKeys);
   }
   ```
   
   
   ###### Provide feedback to improve future suggestions
   [![Nice 
Catch](https://img.shields.io/badge/👍%20Nice%20Catch-71BC78)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/7c816aeb-4bce-4409-968b-ca6ff229372c/upvote)
 
[![Incorrect](https://img.shields.io/badge/👎%20Incorrect-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/7c816aeb-4bce-4409-968b-ca6ff229372c?what_not_true=true)
  [![Not in 
Scope](https://img.shields.io/badge/👎%20Out%20of%20PR%20scope-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/7c816aeb-4bce-4409-968b-ca6ff229372c?what_out_of_scope=true)
 [![Not in coding 
standard](https://img.shields.io/badge/👎%20Not%20in%20our%20standards-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/7c816aeb-4bce-4409-968b-ca6ff229372c?what_not_in_standard=true)
 
[![Other](https://img.shields.io/badge/👎%20Other-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/7c816aeb-4bce-4409-968b-ca6ff229372c)
   </details>
   
   <sub>
   
   💬 Looking for more details? Reply to this comment to chat with Korbit.
   </sub>
   
   <!--- korbi internal id:e0d1a131-3991-4df1-87ae-8e7b07b41b73 -->
   
   
   [](e0d1a131-3991-4df1-87ae-8e7b07b41b73)



##########
superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.jsx:
##########
@@ -348,6 +418,72 @@ export class TableRenderer extends Component {
     return spans;
   }
 
+  calculateGroups(pivotData, visibleColKey, columnIndex) {
+    const groups = {};
+    const rows = pivotData.rowKeys;
+    rows.forEach(rowKey => {
+      let current = groups;
+      let sumGroup = 0;
+      rowKey.forEach(key => {
+        if (!current[key]) {
+          current[key] = { currentVal: 0 };
+        }
+        sumGroup += pivotData
+          .getAggregator(rowKey, visibleColKey[columnIndex])
+          .value();
+        current[key].currentVal = sumGroup;
+        current = current[key];
+      });
+    });
+    return groups;
+  }
+
+  sortAndCacheData(groups, sortOrder, subtotals, maxRowIndex) {
+    const { rowEnabled, rowPartialOnTop } = subtotals;
+    const sortedGroups = sortHierarchicalObject(
+      groups,
+      sortOrder,
+      rowPartialOnTop,
+    );
+    return convertToArray(
+      sortedGroups,
+      rowEnabled,
+      rowPartialOnTop,
+      maxRowIndex,
+    );
+  }
+
+  sortData(columnIndex, visibleColKeys, pivotData, maxRowIndex) {
+    this.setState(state => {
+      const { sortingOrder, activeSortColumn } = state;
+
+      let newSortingOrder = [];
+      let newDirection = 'asc';
+
+      if (activeSortColumn === columnIndex) {
+        newDirection = sortingOrder[columnIndex] === 'asc' ? 'desc' : 'asc';
+      }
+
+      newSortingOrder[columnIndex] = newDirection;
+      const groups = this.calculateGroups(
+        pivotData,
+        visibleColKeys,
+        columnIndex,
+      );
+      const sortedRowKeys = this.sortAndCacheData(
+        groups,
+        newSortingOrder[columnIndex],
+        pivotData.subtotals,
+        maxRowIndex,
+      );
+      this.cachedBasePivotSettings.rowKeys = sortedRowKeys;

Review Comment:
   ### Direct mutation of cached pivot settings <sub>![category 
Functionality](https://img.shields.io/badge/Functionality-0284c7)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   Direct mutation of cached pivot settings can cause inconsistent state and 
unexpected behavior when the component re-renders or when other methods access 
the cached data.
   
   
   ###### Why this matters
   This mutation bypasses React's state management system and can lead to stale 
data being used in subsequent renders, causing the UI to display incorrect 
sorting results or fail to update properly.
   
   ###### Suggested change ∙ *Feature Preview*
   Store the sorted row keys in component state instead of mutating the cached 
settings:
   
   ```jsx
   this.setState(state => {
     // ... existing state updates
     return {
       sortingOrder: newSortingOrder,
       activeSortColumn: columnIndex,
       sortedRowKeys: sortedRowKeys,
     };
   });
   ```
   
   Then use the sorted keys from state in the render method when available.
   
   
   ###### Provide feedback to improve future suggestions
   [![Nice 
Catch](https://img.shields.io/badge/👍%20Nice%20Catch-71BC78)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/6935484d-6881-4e98-95c3-ceefaf0840a7/upvote)
 
[![Incorrect](https://img.shields.io/badge/👎%20Incorrect-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/6935484d-6881-4e98-95c3-ceefaf0840a7?what_not_true=true)
  [![Not in 
Scope](https://img.shields.io/badge/👎%20Out%20of%20PR%20scope-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/6935484d-6881-4e98-95c3-ceefaf0840a7?what_out_of_scope=true)
 [![Not in coding 
standard](https://img.shields.io/badge/👎%20Not%20in%20our%20standards-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/6935484d-6881-4e98-95c3-ceefaf0840a7?what_not_in_standard=true)
 
[![Other](https://img.shields.io/badge/👎%20Other-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/6935484d-6881-4e98-95c3-ceefaf0840a7)
   </details>
   
   <sub>
   
   💬 Looking for more details? Reply to this comment to chat with Korbit.
   </sub>
   
   <!--- korbi internal id:6e42ed20-cc00-4d92-b7bc-83849bc20554 -->
   
   
   [](6e42ed20-cc00-4d92-b7bc-83849bc20554)



##########
superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.jsx:
##########
@@ -470,6 +631,15 @@ export class TableRenderer extends Component {
               namesMapping,
               allowRenderHtml,
             )}
+            <span
+              role="columnheader button"
+              tabIndex={0}
+              onClick={e => {
+                e.stopPropagation();
+              }}
+            >
+              {visibleSortIcon && getSortIcon(Object.keys(visibleColKeys)[i])}

Review Comment:
   ### Incorrect data structure access for sort icons <sub>![category 
Functionality](https://img.shields.io/badge/Functionality-0284c7)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   The function is trying to get keys from visibleColKeys as if it's an object, 
but visibleColKeys is an array of column key arrays, not an object with 
enumerable keys.
   
   
   ###### Why this matters
   This will cause the sort icon functionality to fail because Object.keys() on 
an array returns string indices, not the actual column data needed for sorting 
logic.
   
   ###### Suggested change ∙ *Feature Preview*
   Use the correct index or column identifier for the sort icon:
   
   ```jsx
   {visibleSortIcon && getSortIcon(i)}
   ```
   
   Or if you need a specific column identifier, extract it from the colKey 
array structure.
   
   
   ###### Provide feedback to improve future suggestions
   [![Nice 
Catch](https://img.shields.io/badge/👍%20Nice%20Catch-71BC78)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/91e2248d-88fc-45e7-9d3d-e0188e27358f/upvote)
 
[![Incorrect](https://img.shields.io/badge/👎%20Incorrect-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/91e2248d-88fc-45e7-9d3d-e0188e27358f?what_not_true=true)
  [![Not in 
Scope](https://img.shields.io/badge/👎%20Out%20of%20PR%20scope-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/91e2248d-88fc-45e7-9d3d-e0188e27358f?what_out_of_scope=true)
 [![Not in coding 
standard](https://img.shields.io/badge/👎%20Not%20in%20our%20standards-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/91e2248d-88fc-45e7-9d3d-e0188e27358f?what_not_in_standard=true)
 
[![Other](https://img.shields.io/badge/👎%20Other-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/91e2248d-88fc-45e7-9d3d-e0188e27358f)
   </details>
   
   <sub>
   
   💬 Looking for more details? Reply to this comment to chat with Korbit.
   </sub>
   
   <!--- korbi internal id:5bd9e2c3-baaa-40e5-823c-35e1b70d0ec6 -->
   
   
   [](5bd9e2c3-baaa-40e5-823c-35e1b70d0ec6)



##########
superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.jsx:
##########
@@ -348,6 +418,72 @@ export class TableRenderer extends Component {
     return spans;
   }
 
+  calculateGroups(pivotData, visibleColKey, columnIndex) {
+    const groups = {};
+    const rows = pivotData.rowKeys;
+    rows.forEach(rowKey => {
+      let current = groups;
+      let sumGroup = 0;
+      rowKey.forEach(key => {
+        if (!current[key]) {
+          current[key] = { currentVal: 0 };
+        }
+        sumGroup += pivotData
+          .getAggregator(rowKey, visibleColKey[columnIndex])
+          .value();
+        current[key].currentVal = sumGroup;
+        current = current[key];
+      });
+    });

Review Comment:
   ### Method with Multiple Responsibilities <sub>![category 
Design](https://img.shields.io/badge/Design-0d9488)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   The calculateGroups method has multiple responsibilities: it creates a 
hierarchical structure, calculates sums, and mutates state, violating the 
Single Responsibility Principle.
   
   
   ###### Why this matters
   This makes the code harder to test, maintain and modify. Each responsibility 
should be in its own function to improve code clarity and reusability.
   
   ###### Suggested change ∙ *Feature Preview*
   Split into separate methods:
   ```javascript
   constructHierarchy(rows) { ... }
   calculateGroupSums(hierarchy, pivotData, visibleColKey) { ... }
   
   calculateGroups(pivotData, visibleColKey, columnIndex) {
     const hierarchy = this.constructHierarchy(pivotData.rowKeys);
     return this.calculateGroupSums(hierarchy, pivotData, visibleColKey);
   }
   ```
   
   
   ###### Provide feedback to improve future suggestions
   [![Nice 
Catch](https://img.shields.io/badge/👍%20Nice%20Catch-71BC78)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/74108173-0900-4d3e-a487-647cd499910f/upvote)
 
[![Incorrect](https://img.shields.io/badge/👎%20Incorrect-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/74108173-0900-4d3e-a487-647cd499910f?what_not_true=true)
  [![Not in 
Scope](https://img.shields.io/badge/👎%20Out%20of%20PR%20scope-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/74108173-0900-4d3e-a487-647cd499910f?what_out_of_scope=true)
 [![Not in coding 
standard](https://img.shields.io/badge/👎%20Not%20in%20our%20standards-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/74108173-0900-4d3e-a487-647cd499910f?what_not_in_standard=true)
 
[![Other](https://img.shields.io/badge/👎%20Other-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/74108173-0900-4d3e-a487-647cd499910f)
   </details>
   
   <sub>
   
   💬 Looking for more details? Reply to this comment to chat with Korbit.
   </sub>
   
   <!--- korbi internal id:0b3a6654-bdef-4de7-a504-768c6a3a0430 -->
   
   
   [](0b3a6654-bdef-4de7-a504-768c6a3a0430)



##########
superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.jsx:
##########
@@ -71,14 +74,81 @@ function displayHeaderCell(
   );
 }
 
+function sortHierarchicalObject(obj, objSort, rowPartialOnTop) {
+  const sortedKeys = Object.keys(obj).sort((a, b) => {
+    const valA = obj[a].currentVal || 0;
+    const valB = obj[b].currentVal || 0;
+    if (rowPartialOnTop) {
+      if (obj[a].currentVal !== undefined && obj[b].currentVal === undefined) {
+        return -1;
+      }
+      if (obj[b].currentVal !== undefined && obj[a].currentVal === undefined) {
+        return 1;
+      }
+    }
+    return objSort === 'asc' ? valA - valB : valB - valA;
+  });
+  return sortedKeys.reduce((acc, key) => {
+    acc[key] =
+      typeof obj[key] === 'object' && !Array.isArray(obj[key])
+        ? sortHierarchicalObject(obj[key], objSort, rowPartialOnTop)
+        : obj[key];
+    return acc;
+  }, {});
+}
+
+function convertToArray(
+  obj,
+  rowEnabled,
+  rowPartialOnTop,
+  maxRowIndex,
+  parentKeys = [],
+  result = [],
+  flag = false,
+) {
+  let updatedFlag = flag;
+  Object.keys(obj).forEach(key => {
+    if (key === 'currentVal') {
+      return;
+    }
+    if (rowEnabled && rowPartialOnTop && parentKeys.length < maxRowIndex - 1) {
+      result.push(parentKeys.length > 0 ? [...parentKeys, key] : [key]);
+      updatedFlag = true;
+    }

Review Comment:
   ### Incorrect flag logic in array conversion <sub>![category 
Functionality](https://img.shields.io/badge/Functionality-0284c7)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   The updatedFlag variable is set inside a forEach loop but the logic that 
depends on it executes after the loop, potentially causing incorrect behavior 
in the array conversion logic.
   
   
   ###### Why this matters
   The flag is meant to track whether items were added to the result in the 
current iteration, but setting it inside forEach and using it later can lead to 
incorrect conditional logic execution and malformed result arrays.
   
   ###### Suggested change ∙ *Feature Preview*
   Restructure the logic to properly track the flag per iteration:
   
   ```jsx
   Object.keys(obj).forEach(key => {
     if (key === 'currentVal') {
       return;
     }
     let itemAdded = false;
     if (rowEnabled && rowPartialOnTop && parentKeys.length < maxRowIndex - 1) {
       result.push(parentKeys.length > 0 ? [...parentKeys, key] : [key]);
       itemAdded = true;
     }
     // ... rest of logic using itemAdded instead of updatedFlag
   });
   ```
   
   
   ###### Provide feedback to improve future suggestions
   [![Nice 
Catch](https://img.shields.io/badge/👍%20Nice%20Catch-71BC78)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/2960f114-13af-4b2c-adfd-7818008167dd/upvote)
 
[![Incorrect](https://img.shields.io/badge/👎%20Incorrect-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/2960f114-13af-4b2c-adfd-7818008167dd?what_not_true=true)
  [![Not in 
Scope](https://img.shields.io/badge/👎%20Out%20of%20PR%20scope-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/2960f114-13af-4b2c-adfd-7818008167dd?what_out_of_scope=true)
 [![Not in coding 
standard](https://img.shields.io/badge/👎%20Not%20in%20our%20standards-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/2960f114-13af-4b2c-adfd-7818008167dd?what_not_in_standard=true)
 
[![Other](https://img.shields.io/badge/👎%20Other-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/2960f114-13af-4b2c-adfd-7818008167dd)
   </details>
   
   <sub>
   
   💬 Looking for more details? Reply to this comment to chat with Korbit.
   </sub>
   
   <!--- korbi internal id:22f47381-0b32-4a7b-acf0-a41bdcf2554b -->
   
   
   [](22f47381-0b32-4a7b-acf0-a41bdcf2554b)



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