From bc816e112f79b402f0a2e4c5e3e171cbfad81487 Mon Sep 17 00:00:00 2001 From: Claude Code Date: Mon, 11 May 2026 16:39:19 -0700 Subject: [PATCH] fix(sql-lab): correct createCtasDatasource return type and fix latent bug Address codeant/bito review on #40037. The /api/v1/dataset/get_or_create/ endpoint returns { result: { table_id: number } } per its OpenAPI schema, but createCtasDatasource was annotated as Promise<{ id: number }> and passed json.result straight into createDatasourceSuccess (which builds `${data.id}__table`). The result.id read was always undefined, so the CTAS Explore flow has been silently constructing "undefined__table" as the datasource identifier at runtime. The previous as-unknown-as cast in ExploreCtasResultsButton hid the contract drift from TypeScript. - createCtasDatasource now returns Promise<{ table_id: number }>, matching the actual API payload. - Normalize result.table_id -> { id } when dispatching createDatasourceSuccess, so the existing reducer/action contract is preserved (createDatasource still calls it with a real { id }). - Drop the cast in ExploreCtasResultsButton; data.table_id is now properly type-checked. Co-Authored-By: Claude Sonnet 4.6 --- superset-frontend/src/SqlLab/actions/sqlLab.ts | 11 ++++++++--- .../components/ExploreCtasResultsButton/index.tsx | 6 +----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/superset-frontend/src/SqlLab/actions/sqlLab.ts b/superset-frontend/src/SqlLab/actions/sqlLab.ts index 883f50d40f1..302f0b82cb0 100644 --- a/superset-frontend/src/SqlLab/actions/sqlLab.ts +++ b/superset-frontend/src/SqlLab/actions/sqlLab.ts @@ -1712,7 +1712,7 @@ export function createDatasource( export function createCtasDatasource( vizOptions: Record, -): SqlLabThunkAction> { +): SqlLabThunkAction> { return (dispatch: AppDispatch) => { dispatch(createDatasourceStarted()); return SupersetClient.post({ @@ -1720,9 +1720,14 @@ export function createCtasDatasource( jsonPayload: vizOptions, }) .then(({ json }) => { - dispatch(createDatasourceSuccess(json.result)); + const result = json.result as { table_id: number }; + // The endpoint's `result.table_id` IS the dataset id; normalize so + // createDatasourceSuccess's `${data.id}__table` resolves correctly. + // Without this, the CTAS Explore button silently produced + // `"undefined__table"` because `result.id` doesn't exist. + dispatch(createDatasourceSuccess({ id: result.table_id })); - return json.result; + return result; }) .catch(() => { const errorMsg = t('An error occurred while creating the data source'); diff --git a/superset-frontend/src/SqlLab/components/ExploreCtasResultsButton/index.tsx b/superset-frontend/src/SqlLab/components/ExploreCtasResultsButton/index.tsx index 5b9b7618010..4cb4bcc0718 100644 --- a/superset-frontend/src/SqlLab/components/ExploreCtasResultsButton/index.tsx +++ b/superset-frontend/src/SqlLab/components/ExploreCtasResultsButton/index.tsx @@ -56,11 +56,7 @@ const ExploreCtasResultsButton = ({ }; const visualize = () => { - ( - dispatch(createCtasDatasource(buildVizOptions)) as unknown as Promise<{ - table_id: number; - }> - ) + dispatch(createCtasDatasource(buildVizOptions)) .then(data => { const formData = { datasource: `${data.table_id}__table`,