bito-code-review[bot] commented on code in PR #43027:
URL: https://github.com/apache/superset/pull/43027#discussion_r3754656229


##########
superset-frontend/packages/superset-ui-chart-controls/src/utils/getTotalsMetrics.ts:
##########
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { isAdhocMetricSimple, QueryFormMetric } from '@superset-ui/core';
+
+export type TotalsAggregate = 'SUM' | 'AVG';
+
+/**
+ * Build the metrics for a chart's "Show summary" totals query, overriding
+ * each Simple (adhoc) metric's aggregate function with the user-chosen
+ * totals aggregate. The totals query has no GROUP BY, so the database
+ * evaluates each metric fresh over all rows -- swapping the aggregate here
+ * is a correct, independent computation, not a re-aggregation of
+ * already-aggregated per-row values.
+ *
+ * Custom-SQL metrics and saved (string) metrics pass through unchanged:
+ * there is no safe way to rewrite an arbitrary SQL expression's aggregate
+ * function without parsing it, so the totals row keeps their own native
+ * aggregate for those.
+ */
+export function getTotalsMetrics(
+  metrics: QueryFormMetric[],
+  aggregate: TotalsAggregate,
+): QueryFormMetric[] {
+  return metrics.map(metric =>
+    isAdhocMetricSimple(metric) ? { ...metric, aggregate } : metric,
+  );
+}

Review Comment:
   <!-- Bito Reply -->
   The addition of unit tests for `getTotalsMetrics` addresses the reviewer's 
suggestion regarding the lack of test coverage for this new utility function. 
By covering the simple-metric override, SQL/saved-metric passthrough, 
mixed-type lists, and empty-array cases, the implementation now aligns with the 
requirement to verify success paths and edge cases.



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