Skip to content
Open
17 changes: 16 additions & 1 deletion superset-frontend/src/dashboard/actions/datasources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export function setDatasource(datasource: Datasource, key: string) {
};
}

const inFlightDatasourceKeys = new Set<string>();

export function fetchDatasourceMetadata(key: string) {
return (dispatch: Dispatch, getState: () => RootState) => {
const { datasources } = getState();
Expand All @@ -61,8 +63,21 @@ export function fetchDatasourceMetadata(key: string) {
return dispatch(setDatasource(datasource, key));
}

if (inFlightDatasourceKeys.has(key)) {
return undefined;
Comment thread
rusackas marked this conversation as resolved.
Resolved
}

inFlightDatasourceKeys.add(key);
return SupersetClient.get({
endpoint: `/superset/fetch_datasource_metadata?datasourceKey=${key}`,
}).then(({ json }) => dispatch(setDatasource(json as Datasource, key)));
})
.then(({ json }) => {
inFlightDatasourceKeys.delete(key);
return dispatch(setDatasource(json as Datasource, key));
})
.catch((err: unknown) => {
inFlightDatasourceKeys.delete(key);
throw err;
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
onFiltersRefreshSuccess,
setDirectPathToChild,
} from 'src/dashboard/actions/dashboardState';
import { fetchDatasourceMetadata } from 'src/dashboard/actions/datasources';
import {
setHoveredChartCustomization,
unsetHoveredChartCustomization,
Expand Down Expand Up @@ -185,6 +186,28 @@ const FilterValue: FC<FilterValueProps> = ({
const [isRefreshing, setIsRefreshing] = useState(false);
const dispatch = useDispatch();

// When a Date Range parent filter is active, the child filter needs
// granularity_sqla to apply the temporal WHERE clause. Read main_dttm_col
// from the Redux datasource cache as a fallback.
//
// The dashboard datasets API (/api/v1/dashboard/:id/datasets) only returns
// datasets used by chart slices, so native-filter-only datasets are never
// in state.datasources. Dispatch fetchDatasourceMetadata at mount time to
// populate the cache explicitly for those datasets.
const datasourceMainDttmCol = useSelector<RootState, string | null | undefined>(
state =>
datasetId != null
? (state.datasources as Record<string, any>)?.[`${datasetId}__table`]
?.main_dttm_col
: undefined,
);

useEffect(() => {
if (datasetId != null) {
dispatch(fetchDatasourceMetadata(`${datasetId}__table`));
Comment thread
rusackas marked this conversation as resolved.
Resolved
}
}, [datasetId, dispatch]);
Comment thread
rusackas marked this conversation as resolved.
Resolved
Comment thread
rusackas marked this conversation as resolved.
Resolved

const { outlinedFilterId, lastUpdated } = useFilterOutlined();

const handleFilterLoadFinish = useCallback(() => {
Expand Down Expand Up @@ -216,7 +239,11 @@ const FilterValue: FC<FilterValueProps> = ({
groupby,
adhoc_filters: adhocFilters,
time_range: timeRange,
granularity_sqla: granularitySqla,
granularity_sqla:
granularitySqla ||
(dependencies.time_range
? datasourceMainDttmCol ?? undefined
: undefined),
dashboardId,
});
const filterOwnState = filter.dataMask?.ownState || {};
Expand Down Expand Up @@ -347,6 +374,7 @@ const FilterValue: FC<FilterValueProps> = ({
dataMaskSelected,
setHasDepsFilterValue,
transitiveParentIds,
datasourceMainDttmCol,
]);

useEffect(() => {
Expand Down
Loading