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


##########
superset-frontend/plugins/preset-chart-deckgl/src/utils/addColor.test.ts:
##########
@@ -0,0 +1,83 @@
+/**
+ * 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 { QueryFormData } from '@superset-ui/core';
+import { addColorToFeatures } from './addColor';
+import { COLOR_SCHEME_TYPES } from '../utilities/utils';
+
+const baseFormData = {
+  datasource: '1__table',
+  viz_type: 'deck_scatter',
+} as unknown as QueryFormData;
+
+test('assigns distinct colors per category for a categorical palette', () => {
+  const features = [{ cat_color: 'A' }, { cat_color: 'B' }, { cat_color: 'A' 
}];
+  const result = addColorToFeatures(features, {
+    ...baseFormData,
+    color_scheme_type: COLOR_SCHEME_TYPES.categorical_palette,
+    color_scheme: 'supersetColors',
+    dimension: 'category',
+    slice_id: 1,
+  } as unknown as QueryFormData);
+
+  // Each feature gets a resolved RGBA color
+  result.forEach(d => {
+    expect(Array.isArray(d.color)).toBe(true);
+    expect(d.color).toHaveLength(4);
+  });
+  // Same category resolves to the same color, different categories differ
+  expect(result[0].color).toEqual(result[2].color);
+  expect(result[0].color).not.toEqual(result[1].color);
+});
+
+test('falls back to the fixed color picker when no dimension is set', () => {
+  const features = [{ cat_color: 'A' }, { cat_color: 'B' }];
+  const result = addColorToFeatures(features, {
+    ...baseFormData,
+    color_scheme_type: COLOR_SCHEME_TYPES.categorical_palette,
+    color_picker: { r: 10, g: 20, b: 30, a: 1 },
+  } as unknown as QueryFormData);
+
+  result.forEach(d => {
+    expect(d.color).toEqual([10, 20, 30, 255]);
+  });
+});
+
+test('applies the fixed color scheme to every feature', () => {
+  const features = [{ cat_color: 'A' }, { cat_color: 'B' }];
+  const result = addColorToFeatures(features, {
+    ...baseFormData,
+    color_scheme_type: COLOR_SCHEME_TYPES.fixed_color,
+    color_picker: { r: 1, g: 2, b: 3, a: 0.5 },
+  } as unknown as QueryFormData);
+
+  result.forEach(d => {
+    expect(d.color).toEqual([1, 2, 3, 127.5]);
+  });
+});
+
+test('returns features unchanged for an unrecognized color scheme', () => {
+  const features = [{ cat_color: 'A' }];
+  const result = addColorToFeatures(features, {
+    ...baseFormData,
+    color_scheme_type: 'something_else',
+  } as unknown as QueryFormData);
+
+  expect(result).toEqual(features);
+  expect(result[0].color).toBeUndefined();
+});

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Missing color_breakpoints test coverage</b></div>
   <div id="fix">
   
   The `addColorToFeatures` switch statement has 4 cases, but only 3 are 
tested. The `color_breakpoints` case (lines 73-105 in `addColor.ts`) which 
handles metric-based breakpoint color assignment is entirely untested. Without 
coverage, bugs in breakpoint matching (e.g., off-by-one in 
`minValue`/`maxValue` comparisons) could go undetected. Rule 6262 from BITO.md 
requires tests to verify actual business logic.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #f7d955</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset-frontend/plugins/preset-chart-deckgl/src/utils/addColor.ts:
##########
@@ -0,0 +1,113 @@
+/**
+ * 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 {
+  CategoricalColorNamespace,
+  JsonObject,
+  QueryFormData,
+} from '@superset-ui/core';
+import { hexToRGB } from './colors';
+import { ColorBreakpointType } from '../types';
+import { COLOR_SCHEME_TYPES, ColorSchemeType } from '../utilities/utils';
+import { DEFAULT_DECKGL_COLOR } from '../utilities/Shared_DeckGL';
+
+const { getScale } = CategoricalColorNamespace;
+
+/**
+ * Resolve the per-feature color for a deck.gl layer based on the form data's
+ * color scheme configuration. This mirrors the categorical/fixed/breakpoint
+ * color logic that `CategoricalDeckGLContainer` applies when a layer is
+ * rendered on its own, so that it can be reused when layers are composed
+ * inside the deck.gl Multiple Layers chart.
+ *
+ * Features whose color scheme is not recognized are returned unchanged so the
+ * layer's own fallback color logic can take over.
+ */
+export function addColorToFeatures(
+  data: JsonObject[],
+  fd: QueryFormData,
+  selectedColorScheme: ColorSchemeType = fd.color_scheme_type,
+): JsonObject[] {
+  const appliedScheme = fd.color_scheme;
+  const colorFn = getScale(appliedScheme);
+
+  switch (selectedColorScheme) {
+    case COLOR_SCHEME_TYPES.fixed_color: {
+      const color = fd.color_picker || { r: 0, g: 0, b: 0, a: 1 };
+      const colorArray = [color.r, color.g, color.b, color.a * 255];
+
+      return data.map(d => ({ ...d, color: colorArray }));
+    }
+    case COLOR_SCHEME_TYPES.categorical_palette: {
+      if (!fd.dimension) {
+        const fallbackColor = fd.color_picker || { r: 0, g: 0, b: 0, a: 1 };
+        const colorArray = [
+          fallbackColor.r,
+          fallbackColor.g,
+          fallbackColor.b,
+          fallbackColor.a * 255,
+        ];
+        return data.map(d => ({ ...d, color: colorArray }));
+      }
+
+      return data.map(d => ({
+        ...d,
+        color: hexToRGB(colorFn(d.cat_color, fd.slice_id)),
+      }));

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Wrong property name on QueryFormData</b></div>
   <div id="fix">
   
   `fd.slice_id` does not exist on `QueryFormData` — the canonical property is 
`fd.sliceId` (camelCase), as confirmed by the same call in 
`CategoricalDeckGLContainer.tsx` line 73. Passing `undefined` disables 
per-slice color domain isolation in `getScale`, breaking categorical palette 
coloring in the Multiple Layers chart.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
         return data.map(d => ({
           ...d,
           color: hexToRGB(colorFn(d.cat_color, fd.sliceId)),
           }));
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #f7d955</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



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