Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

display sql transforms errors in editor (#71092)

Jakub Chodorowiczebbc0adc7054
diff --git a/enterprise/frontend/src/metabase-enterprise/tenants/components/CreateTenantsOnboardingStep/hooks/use-field-distinct-values.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/tenants/components/CreateTenantsOnboardingStep/hooks/use-field-distinct-values.unit.spec.tsx
index 3bb0c7069a9..eea72cee290 100644
--- a/enterprise/frontend/src/metabase-enterprise/tenants/components/CreateTenantsOnboardingStep/hooks/use-field-distinct-values.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/tenants/components/CreateTenantsOnboardingStep/hooks/use-field-distinct-values.unit.spec.tsx
@@ -30,7 +30,7 @@ function setup({ fieldId, rows }: SetupOpts) {
setupFieldEndpoints(field);
if (rows) {
- setupCardDataset({ data: { rows } });
+ setupCardDataset({ dataset: { data: { rows } } });
}
}
diff --git a/frontend/src/metabase/admin/datamodel/containers/SegmentApp.unit.spec.tsx b/frontend/src/metabase/admin/datamodel/containers/SegmentApp.unit.spec.tsx
index 03993aff2c0..0b12741035d 100644
--- a/frontend/src/metabase/admin/datamodel/containers/SegmentApp.unit.spec.tsx
+++ b/frontend/src/metabase/admin/datamodel/containers/SegmentApp.unit.spec.tsx
@@ -40,8 +40,10 @@ const setup = ({ initialRoute = FORM_URL }: SetupOpts = {}) => {
setupDatabasesEndpoints([createSampleDatabase()]);
setupSearchEndpoints([]);
setupCardDataset({
- data: {
- rows: [[1, 2, 3]],
+ dataset: {
+ data: {
+ rows: [[1, 2, 3]],
+ },
},
});
setupRecentViewsAndSelectionsEndpoints([], ["selections"]);
diff --git a/frontend/src/metabase/detail-view/components/Relationships/Relationship/Relationship.unit.spec.tsx b/frontend/src/metabase/detail-view/components/Relationships/Relationship/Relationship.unit.spec.tsx
index 1ecc200ad11..877f150c314 100644
--- a/frontend/src/metabase/detail-view/components/Relationships/Relationship/Relationship.unit.spec.tsx
+++ b/frontend/src/metabase/detail-view/components/Relationships/Relationship/Relationship.unit.spec.tsx
@@ -61,7 +61,7 @@ function setup({ dataset = DATASET }: SetupOpts = {}) {
<Relationship fk={FK} rowId={ROW_ID} onClick={onClick} />
);
- setupCardDataset(dataset);
+ setupCardDataset({ dataset });
renderWithProviders(<Route path="/" component={TestComponent} />, {
withRouter: true,
diff --git a/frontend/src/metabase/query_builder/containers/test-utils.tsx b/frontend/src/metabase/query_builder/containers/test-utils.tsx
index 1accdc074dc..16f78604fe7 100644
--- a/frontend/src/metabase/query_builder/containers/test-utils.tsx
+++ b/frontend/src/metabase/query_builder/containers/test-utils.tsx
@@ -246,7 +246,7 @@ export const setup = async ({
}`,
}: SetupOpts) => {
setupDatabasesEndpoints([TEST_DB]);
- setupCardDataset(dataset);
+ setupCardDataset({ dataset });
setupSearchEndpoints([]);
setupPropertiesEndpoints(createMockSettings());
setupCollectionsEndpoints({ collections: [] });
diff --git a/frontend/src/metabase/querying/editor/hooks/use-query-results/use-query-results.ts b/frontend/src/metabase/querying/editor/hooks/use-query-results/use-query-results.ts
index ccc7c0c7ecc..f844686d8a4 100644
--- a/frontend/src/metabase/querying/editor/hooks/use-query-results/use-query-results.ts
+++ b/frontend/src/metabase/querying/editor/hooks/use-query-results/use-query-results.ts
@@ -4,7 +4,8 @@ import { useLazyGetAdhocQueryQuery } from "metabase/api";
import * as Lib from "metabase-lib";
import Question from "metabase-lib/v1/Question";
import { normalizeParameters } from "metabase-lib/v1/parameters/utils/parameter-values";
-import type { DatasetQuery } from "metabase-types/api";
+import type { Dataset, DatasetQuery } from "metabase-types/api";
+import { isObject } from "metabase-types/guards";
import type { QueryEditorUiState } from "../../types";
@@ -60,9 +61,10 @@ export function useQueryResults(
parameters: normalizeParameters(question.parameters()),
});
abortRef.current = action.abort;
- const { data: lastRunResult = null } = await action;
+ const { data, error } = await action;
abortRef.current = undefined;
+ const lastRunResult = data ?? (hasDataset(error) ? error.data : null);
onChangeUiState({ ...uiState, lastRunResult, lastRunQuery });
};
@@ -85,3 +87,7 @@ export function useQueryResults(
cancelQuery,
};
}
+
+function hasDataset(value: unknown): value is { data: Dataset } {
+ return isObject(value) && "data" in value;
+}
diff --git a/frontend/src/metabase/querying/editor/hooks/use-query-results/use-query-results.unit.spec.ts b/frontend/src/metabase/querying/editor/hooks/use-query-results/use-query-results.unit.spec.ts
new file mode 100644
index 00000000000..3c03feed146
--- /dev/null
+++ b/frontend/src/metabase/querying/editor/hooks/use-query-results/use-query-results.unit.spec.ts
@@ -0,0 +1,96 @@
+import { setupCardDataset } from "__support__/server-mocks";
+import { renderHookWithProviders, waitFor } from "__support__/ui";
+import Question from "metabase-lib/v1/Question";
+import type { Dataset } from "metabase-types/api";
+import { createMockDataset } from "metabase-types/api/mocks";
+import { createMockNativeDatasetQuery } from "metabase-types/api/mocks/query";
+
+import type { QueryEditorUiState } from "../../types";
+
+import { useQueryResults } from "./use-query-results";
+
+const NATIVE_QUERY = createMockNativeDatasetQuery({
+ native: { query: "SELECXT 1" },
+});
+
+const ERROR_DATASET = createMockDataset({
+ error: 'ERROR: syntax error at or near "SELECXT"',
+ error_type: "invalid-query",
+ status: "failed",
+});
+
+function setup({
+ mockResponse,
+ onChangeUiState,
+}: {
+ mockResponse: { status?: number; dataset?: Dataset };
+ onChangeUiState: (uiState: QueryEditorUiState) => void;
+}) {
+ setupCardDataset({
+ status: mockResponse.status,
+ dataset: mockResponse.dataset,
+ });
+
+ const question = Question.create({
+ dataset_query: NATIVE_QUERY,
+ metadata: undefined,
+ });
+
+ const initialUiState: QueryEditorUiState = {
+ lastRunResult: null,
+ lastRunQuery: null,
+ selectionRange: [],
+ modalSnippet: null,
+ modalType: null,
+ sidebarType: null,
+ };
+
+ const { result } = renderHookWithProviders(
+ () => useQueryResults(question, initialUiState, onChangeUiState),
+ {},
+ );
+
+ return { result, onChangeUiState };
+}
+
+describe("useQueryResults", () => {
+ it("should propagate a successful dataset result", async () => {
+ const successDataset = createMockDataset();
+ const onChangeUiState = jest.fn();
+ const { result } = setup({
+ mockResponse: { dataset: successDataset },
+ onChangeUiState,
+ });
+
+ await result.current.runQuery();
+
+ await waitFor(() => {
+ expect(onChangeUiState).toHaveBeenCalled();
+ });
+
+ const call = onChangeUiState.mock.calls[0][0];
+ expect(call.lastRunResult).toEqual(successDataset);
+ expect(call.lastRunQuery).toEqual(NATIVE_QUERY);
+ });
+
+ it("should propagate error.data as lastRunResult when the API returns a 400", async () => {
+ const onChangeUiState = jest.fn();
+ const { result } = setup({
+ onChangeUiState,
+ mockResponse: { status: 400, dataset: ERROR_DATASET },
+ });
+
+ await result.current.runQuery();
+
+ await waitFor(() => {
+ expect(onChangeUiState).toHaveBeenCalled();
+ });
+
+ const call = onChangeUiState.mock.calls[0][0];
+ expect(call.lastRunResult).toEqual(ERROR_DATASET);
+ expect(call.lastRunResult.error).toBe(
+ 'ERROR: syntax error at or near "SELECXT"',
+ );
+ expect(call.lastRunQuery).toEqual(NATIVE_QUERY);
+ });
+});
diff --git a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailView.unit.spec.tsx b/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailView.unit.spec.tsx
index 6b94dce6b29..4e6df02d5b2 100644
--- a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailView.unit.spec.tsx
+++ b/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailView.unit.spec.tsx
@@ -294,19 +294,21 @@ describe("ObjectDetailView", () => {
it("fetches a missing row", async () => {
setupCardDataset({
- data: {
- rows: [
- [
- "101",
- "1807963902339",
- "Extremely Hungry Toucan",
- "Gizmo",
- "Larson, Pfeffer and Klocko",
- 31.78621880685793,
- 4.3,
- "2017-01-09T09:51:20.352-07:00",
+ dataset: {
+ data: {
+ rows: [
+ [
+ "101",
+ "1807963902339",
+ "Extremely Hungry Toucan",
+ "Gizmo",
+ "Larson, Pfeffer and Klocko",
+ 31.78621880685793,
+ 4.3,
+ "2017-01-09T09:51:20.352-07:00",
+ ],
],
- ],
+ },
},
});
@@ -336,7 +338,7 @@ describe("ObjectDetailView", () => {
});
it("shows not found if it can't find a missing row", async () => {
- setupCardDataset({ data: { rows: [] } });
+ setupCardDataset({ dataset: { data: { rows: [] } } });
// because this row is not in the test dataset, it should trigger a fetch
setup({ question: mockQuestion, zoomedRowID: "102", zoomedRow: undefined });
diff --git a/frontend/test/__support__/server-mocks/dataset.ts b/frontend/test/__support__/server-mocks/dataset.ts
index d1fc17811b1..6aba4edb25b 100644
--- a/frontend/test/__support__/server-mocks/dataset.ts
+++ b/frontend/test/__support__/server-mocks/dataset.ts
@@ -29,8 +29,19 @@ export function setupParameterSearchValuesEndpoint(
});
}
-export function setupCardDataset(options: MockDatasetOpts = {}) {
- fetchMock.post("path:/api/dataset", createMockDataset(options), {
- name: "dataset-post",
- });
+export function setupCardDataset(
+ args: {
+ dataset?: MockDatasetOpts;
+ status?: number;
+ } = {},
+) {
+ const { dataset, status = 200 } = args;
+
+ fetchMock.post(
+ "path:/api/dataset",
+ new Response(JSON.stringify(createMockDataset(dataset)), { status }),
+ {
+ name: "dataset-post",
+ },
+ );
}