From a3ed7a11bbaebbb2c547de895e240788267b349a Mon Sep 17 00:00:00 2001 From: kgopal Date: Wed, 16 Sep 2026 15:37:12 +0000 Subject: [PATCH] fix: default chart source to last statement execution with results When editing a chart cell whose query execution has multiple statements, the composer defaulted the selected statement to statementIdList[0]. If the first statement produced no result rows (a non-result statement or a 0-row result), the chart had no data to bind to and the chart config failed to load. Default to the last DONE statement execution with result_row_count > 0 instead, and guard against missing statement executions in the filter. Co-Authored-By: Claude Opus 4.8 --- .../webapp/hooks/chart/useChartSource.ts | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/querybook/webapp/hooks/chart/useChartSource.ts b/querybook/webapp/hooks/chart/useChartSource.ts index f397bc287..33dd93da1 100644 --- a/querybook/webapp/hooks/chart/useChartSource.ts +++ b/querybook/webapp/hooks/chart/useChartSource.ts @@ -82,6 +82,20 @@ export function useChartSource( }) ); + const statementExecutions = React.useMemo( + () => + (statementIdList || []) + .map((sid) => statementExecutionById[sid]) + .filter( + (statementExecution) => + statementExecution && + statementExecution.status === + StatementExecutionStatus.DONE && + statementExecution.result_row_count > 0 + ), + [statementIdList, statementExecutionById] + ); + React.useEffect(() => { if (cellId == null && executionId) { setInitializingExecutionId(true); @@ -130,10 +144,12 @@ export function useChartSource( }, [executionId]); React.useEffect(() => { - if (statementId == null && statementIdList) { - setStatementId(statementIdList[0]); + if (statementId == null && statementExecutions.length) { + setStatementId( + statementExecutions[statementExecutions.length - 1].id + ); } - }, [statementId, statementIdList]); + }, [statementId, statementExecutions]); React.useEffect(() => { if (statementId != null) { @@ -149,19 +165,6 @@ export function useChartSource( [queryExecutionById, executionIdList] ); - const statementExecutions = React.useMemo( - () => - (statementIdList || []) - .map((sid) => statementExecutionById[sid]) - .filter( - (statementExecution) => - statementExecution.status === - StatementExecutionStatus.DONE && - statementExecution.result_row_count > 0 - ), - [statementIdList, statementExecutionById] - ); - return { statementResultData: limitedStatementResultData, queryExecutions,