codeant-ai-for-open-source[bot] commented on code in PR #42539: URL: https://github.com/apache/superset/pull/42539#discussion_r3730091144
########## superset-frontend/src/dashboard/components/nativeFilters/useDisplayControlDatasource.ts: ########## @@ -0,0 +1,133 @@ +/** + * 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 { useEffect, useRef, useState } from 'react'; +import { DatasourceType } from '@superset-ui/core'; +import { cachedSupersetGet } from 'src/utils/cachedSupersetGet'; +import { + fetchSemanticViewStructure, + semanticViewDimensionsToColumns, +} from './FiltersConfigModal/FiltersConfigForm/utils'; + +/** The column fields display controls read to build their options. */ +export interface DisplayControlColumn { + column_name?: string; + name?: string; + verbose_name?: string | null; + filterable?: boolean; +} + +export interface DisplayControlDatasource { + /** The datasource's display name (dataset table_name / semantic view name). */ + name: string | undefined; + columns: DisplayControlColumn[]; + loading: boolean; + error: Error | undefined; +} + +/** + * The single, type-aware datasource resolution point for dashboard display + * controls (Dynamic group by today; siblings adopt this hook as they gain + * loaders). + * + * Semantic views and regular datasets have independent numeric-id + * sequences, so a binding must be resolved by BOTH id and datasourceType — + * an id-only lookup silently reads whatever regular dataset shares the + * view's number (sc-111089). The default branch preserves the legacy + * display-control request (`GET /api/v1/dataset/<id>`, full resource, no + * projection) byte-for-byte; changing that shape is a deliberate, + * separately-tested decision, not a drive-by. + * + * Responses are guarded by a monotonic request id: on a same-id type flip + * a slow response for the previous binding is discarded rather than + * overwriting the newer one — reintroducing the wrong-object bug through + * a race would otherwise be possible. + */ +export function useDisplayControlDatasource( + datasetId: number | string | undefined | null, + datasourceType?: DatasourceType, +): DisplayControlDatasource { + const [name, setName] = useState<string | undefined>(); + const [columns, setColumns] = useState<DisplayControlColumn[]>([]); + const [loading, setLoading] = useState(false); + const [error, setError] = useState<Error | undefined>(); + const requestIdRef = useRef(0); + + useEffect(() => { + // Invalidate any in-flight request on every binding change (and on + // unmount, via the cleanup below). + requestIdRef.current += 1; + const requestId = requestIdRef.current; + + if (datasetId === undefined || datasetId === null || datasetId === '') { + // Unbound control: inert, nothing to fetch. + setName(undefined); + setColumns([]); + setLoading(false); + setError(undefined); + return undefined; + } + + setLoading(true); + setError(undefined); Review Comment: **Suggestion:** When `datasetId` or `datasourceType` changes, the hook marks the new request as loading but retains the previous `name` and `columns` until the request completes. `GroupByFilterCard` continues to expose those retained columns while `Select` is only visually marked as loading, so users can select a column belonging to the previous datasource and persist it against the new binding. Clear the datasource state when starting a new request, or disable selection until the new response arrives. [stale reference] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ⚠️ Datasource switches expose stale group-by columns. - ❌ Invalid columns can be persisted against new bindings. - ⚠️ Dynamic group-by configuration may fail after switching types. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=0adeb6df7387401291e7b98eed0be572&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=0adeb6df7387401291e7b98eed0be572&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/src/dashboard/components/nativeFilters/useDisplayControlDatasource.ts **Line:** 86:87 **Comment:** *Stale Reference: When `datasetId` or `datasourceType` changes, the hook marks the new request as loading but retains the previous `name` and `columns` until the request completes. `GroupByFilterCard` continues to expose those retained columns while `Select` is only visually marked as loading, so users can select a column belonging to the previous datasource and persist it against the new binding. Clear the datasource state when starting a new request, or disable selection until the new response arrives. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42539&comment_hash=3a33969dffe1d2bc5e799f0a8839407a0d4f7a536dd1b33ed2945abf51bd5695&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42539&comment_hash=3a33969dffe1d2bc5e799f0a8839407a0d4f7a536dd1b33ed2945abf51bd5695&reaction=dislike'>👎</a> ########## superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FiltersConfigForm.tsx: ########## @@ -763,34 +764,19 @@ const FiltersConfigForm = ( useEffect(() => { if (datasetId) { if (datasourceType === DatasourceType.SemanticView) { - cachedSupersetGet({ - endpoint: `/api/v1/semantic_view/${datasetId}/structure`, - }) - .then((response: JsonResponse) => { - const { - name: svName, - dimensions = [], - metrics: svMetrics = [], - } = response.json?.result ?? {}; - const columns = dimensions.map( - (dim: { name: string; type: string }) => { - const mappedType = mapSemanticTypeToGenericDataType(dim.type); - return { - column_name: dim.name, - type: dim.type, - is_dttm: mappedType === GenericDataType.Temporal, - filterable: true, - type_generic: mappedType, - }; - }, - ); + fetchSemanticViewStructure(datasetId) + .then(({ name: svName, dimensions, metrics: svMetrics }) => { + const columns = semanticViewDimensionsToColumns(dimensions); Review Comment: **Suggestion:** The asynchronous structure request is not associated with the current `datasetId` and `datasourceType`. If the user changes the datasource while this request is pending, its completion can overwrite `datasetDetails` and `metrics` for the newly selected datasource with the previous semantic view's data. Add an effect cleanup or request-generation guard before applying either success or failure results. [race condition] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ❌ Filter configuration shows columns from the wrong datasource. - ⚠️ Ad hoc filters use stale metrics and metadata. - ⚠️ Rapid datasource changes can silently misconfigure filters. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=e307bcbf71b8498182f355738e850e0e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=e307bcbf71b8498182f355738e850e0e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FiltersConfigForm.tsx **Line:** 767:769 **Comment:** *Race Condition: The asynchronous structure request is not associated with the current `datasetId` and `datasourceType`. If the user changes the datasource while this request is pending, its completion can overwrite `datasetDetails` and `metrics` for the newly selected datasource with the previous semantic view's data. Add an effect cleanup or request-generation guard before applying either success or failure results. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42539&comment_hash=ee7d283fdd3d7abda361b066972d0ffe32e4cfab73b67f59c1be99d3f2019285&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42539&comment_hash=ee7d283fdd3d7abda361b066972d0ffe32e4cfab73b67f59c1be99d3f2019285&reaction=dislike'>👎</a> -- 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]
