Skip to content

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

Show an error that variables in transforms are not supported (#63759)

Alexander Polyankind25a14aec810
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorHeader/EditorHeader.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorHeader/EditorHeader.tsx
index a817cb0690d..ebb83b57361 100644
--- a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorHeader/EditorHeader.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorHeader/EditorHeader.tsx
@@ -2,37 +2,45 @@ import { t } from "ttag";
import Button from "metabase/common/components/Button";
import EditBar from "metabase/common/components/EditBar";
+import { Tooltip } from "metabase/ui";
+
+import type { QueryValidationResult } from "../types";
type EditorHeaderProps = {
- canSave: boolean;
+ validationResult: QueryValidationResult;
isNew: boolean;
+ isQueryDirty: boolean;
isSaving: boolean;
onSave: () => void;
onCancel: () => void;
};
export function EditorHeader({
- canSave,
+ validationResult,
isNew,
+ isQueryDirty,
isSaving,
onSave,
onCancel,
}: EditorHeaderProps) {
+ const canSave =
+ (isNew || isQueryDirty) && validationResult.isValid && !isSaving;
+
return (
<EditBar
title={getTitle(isNew)}
admin
buttons={[
<Button key="cancel" small onClick={onCancel}>{t`Cancel`}</Button>,
- <Button
+ <Tooltip
key="save"
- onClick={onSave}
- primary
- small
- disabled={!canSave || isSaving}
+ label={validationResult.errorMessage}
+ disabled={validationResult.errorMessage == null}
>
- {getSaveButtonLabel(isNew, isSaving)}
- </Button>,
+ <Button onClick={onSave} primary small disabled={!canSave}>
+ {getSaveButtonLabel(isNew, isSaving)}
+ </Button>
+ </Tooltip>,
]}
/>
);
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/EditorValidationCard.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/EditorValidationCard.tsx
new file mode 100644
index 00000000000..58cb374fc3c
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/EditorValidationCard.tsx
@@ -0,0 +1,44 @@
+import { t } from "ttag";
+
+import { UserHasSeen } from "metabase/common/components/UserHasSeen/UserHasSeen";
+import { Button, Card, Stack, Text } from "metabase/ui";
+
+import type { QueryValidationResult } from "../types";
+
+import { TRANSFORM_ERROR_SEEN_KEY } from "./constants";
+
+type EditorValidationCardProps = {
+ validationResult: QueryValidationResult;
+};
+
+export function EditorValidationCard({
+ validationResult: { errorMessage },
+}: EditorValidationCardProps) {
+ if (errorMessage == null) {
+ return null;
+ }
+
+ return (
+ <UserHasSeen id={TRANSFORM_ERROR_SEEN_KEY}>
+ {({ hasSeen, ack }) =>
+ !hasSeen && <ErrorCard errorMessage={errorMessage} onClose={ack} />
+ }
+ </UserHasSeen>
+ );
+}
+
+type ErrorCardProps = {
+ errorMessage: string;
+ onClose: () => void;
+};
+
+function ErrorCard({ errorMessage, onClose }: ErrorCardProps) {
+ return (
+ <Card pos="fixed" left="2rem" bottom="2rem" maw="21rem" withBorder>
+ <Stack align="start">
+ <Text>{errorMessage}</Text>
+ <Button variant="filled" onClick={onClose}>{t`Okay`}</Button>
+ </Stack>
+ </Card>
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/EditorValidationCard.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/EditorValidationCard.unit.spec.tsx
new file mode 100644
index 00000000000..f78f3eb26ea
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/EditorValidationCard.unit.spec.tsx
@@ -0,0 +1,64 @@
+import userEvent from "@testing-library/user-event";
+
+import { setupUserKeyValueEndpoints } from "__support__/server-mocks";
+import { renderWithProviders, screen } from "__support__/ui";
+
+import type { QueryValidationResult } from "../types";
+
+import { EditorValidationCard } from "./EditorValidationCard";
+import { TRANSFORM_ERROR_SEEN_KEY } from "./constants";
+
+const VALID_RESULT = {
+ isValid: true,
+};
+
+const ERROR_RESULT = {
+ isValid: false,
+ errorMessage: "Error message",
+};
+
+type SetupOpts = {
+ validationResult: QueryValidationResult;
+ hasSeen?: boolean;
+};
+
+function setup({ validationResult, hasSeen = false }: SetupOpts) {
+ setupUserKeyValueEndpoints({
+ namespace: "user_acknowledgement",
+ key: TRANSFORM_ERROR_SEEN_KEY,
+ value: hasSeen,
+ });
+
+ renderWithProviders(
+ <EditorValidationCard validationResult={validationResult} />,
+ );
+}
+
+describe("EditorValidationCard", () => {
+ it("should not show the error when the result is valid", () => {
+ setup({ validationResult: VALID_RESULT, hasSeen: false });
+ expect(screen.queryByText("Okay")).not.toBeInTheDocument();
+ });
+
+ it("should not show the error when the user has already seen it", () => {
+ setup({ validationResult: ERROR_RESULT, hasSeen: true });
+ expect(
+ screen.queryByText(ERROR_RESULT.errorMessage),
+ ).not.toBeInTheDocument();
+ });
+
+ it("should show the error if it was not seen before", async () => {
+ setup({ validationResult: ERROR_RESULT, hasSeen: false });
+ expect(
+ await screen.findByText(ERROR_RESULT.errorMessage),
+ ).toBeInTheDocument();
+ });
+
+ it("should hide the error when the user clicks the button", async () => {
+ setup({ validationResult: ERROR_RESULT, hasSeen: false });
+ await userEvent.click(await screen.findByText("Okay"));
+ expect(
+ screen.queryByText(ERROR_RESULT.errorMessage),
+ ).not.toBeInTheDocument();
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/constants.ts b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/constants.ts
new file mode 100644
index 00000000000..07f275091a1
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/constants.ts
@@ -0,0 +1 @@
+export const TRANSFORM_ERROR_SEEN_KEY = "transform-native-variable-error";
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/index.ts b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/index.ts
new file mode 100644
index 00000000000..01a52a2cab1
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/EditorValidationCard/index.ts
@@ -0,0 +1 @@
+export * from "./EditorValidationCard";
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/QueryEditor.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/QueryEditor.tsx
index 5655a1486c0..9016f0644d7 100644
--- a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/QueryEditor.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/QueryEditor.tsx
@@ -15,13 +15,18 @@ import { useQueryState } from "../../hooks/use-query-state";
import { EditorBody } from "./EditorBody";
import { EditorHeader } from "./EditorHeader";
import { EditorSidebar } from "./EditorSidebar";
+import { EditorValidationCard } from "./EditorValidationCard";
import { EditorVisualization } from "./EditorVisualization";
import {
NativeQuerySidebar,
NativeQuerySidebarToggle,
} from "./NativeQuerySidebar";
import S from "./QueryEditor.module.css";
-import { useInsertSnippetHandler, useSelectedText } from "./util";
+import {
+ getValidationResult,
+ useInsertSnippetHandler,
+ useSelectedText,
+} from "./utils";
type QueryEditorProps = {
initialQuery: DatasetQuery;
@@ -49,9 +54,9 @@ export function QueryEditor({
runQuery,
cancelQuery,
} = useQueryResults(question);
- const canSave = Lib.canSave(question.query(), question.type());
const { isNative } = Lib.queryDisplayInfo(question.query());
const [isShowingNativeQueryPreview, toggleNativeQueryPreview] = useToggle();
+ const validationResult = getValidationResult(question.query());
const handleChange = async (newQuestion: Question) => {
setQuestion(newQuestion);
@@ -117,6 +122,7 @@ export function QueryEditor({
return (
<Stack
className={S.root}
+ pos="relative"
w="100%"
h="100%"
bg="bg-white"
@@ -124,9 +130,10 @@ export function QueryEditor({
gap={0}
>
<EditorHeader
+ validationResult={validationResult}
isNew={isNew}
isSaving={isSaving}
- canSave={canSave && (isNew || isQueryDirty)}
+ isQueryDirty={isQueryDirty}
onSave={handleSave}
onCancel={onCancel}
/>
@@ -192,6 +199,7 @@ export function QueryEditor({
onInsertSnippet={handleInsertSnippet}
/>
</Flex>
+ <EditorValidationCard validationResult={validationResult} />
</Stack>
);
}
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/types.ts b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/types.ts
new file mode 100644
index 00000000000..876d0337cf9
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/types.ts
@@ -0,0 +1,4 @@
+export type QueryValidationResult = {
+ isValid: boolean;
+ errorMessage?: string;
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/util.ts b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/utils.ts
similarity index 76%
rename from enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/util.ts
rename to enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/utils.ts
index 522f102a77b..de159064ab7 100644
--- a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/util.ts
+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/utils.ts
@@ -1,4 +1,5 @@
import { useMemo } from "react";
+import { t } from "ttag";
import type {
Location,
@@ -8,6 +9,8 @@ import * as Lib from "metabase-lib";
import type Question from "metabase-lib/v1/Question";
import type { NativeQuerySnippet } from "metabase-types/api";
+import type { QueryValidationResult } from "./types";
+
const EMPTY_SELECTION_RANGE: SelectionRange = {
start: { row: 0, column: 0 },
end: { row: 0, column: 0 },
@@ -68,3 +71,18 @@ export function useInsertSnippetHandler({
onChange(question.setQuery(newQuery));
};
}
+
+export function getValidationResult(query: Lib.Query): QueryValidationResult {
+ const { isNative } = Lib.queryDisplayInfo(query);
+ if (isNative) {
+ const tags = Object.values(Lib.templateTags(query));
+ if (tags.some((t) => t.type !== "card" && t.type !== "snippet")) {
+ return {
+ isValid: false,
+ errorMessage: t`In transforms, you can use snippets and question or model references, but not variables.`,
+ };
+ }
+ }
+
+ return { isValid: Lib.canSave(query, "question") };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/utils.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/utils.unit.spec.ts
new file mode 100644
index 00000000000..d7a06a760a9
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/QueryEditor/utils.unit.spec.ts
@@ -0,0 +1,84 @@
+import { createMockMetadata } from "__support__/metadata";
+import * as Lib from "metabase-lib";
+import {
+ createMockCard,
+ createMockNativeQuerySnippet,
+} from "metabase-types/api/mocks";
+import {
+ SAMPLE_DB_ID,
+ createSampleDatabase,
+} from "metabase-types/api/mocks/presets";
+
+import { getValidationResult } from "./utils";
+
+const METADATA = createMockMetadata({
+ databases: [createSampleDatabase()],
+ questions: [createMockCard({ id: 1 })],
+ snippets: [createMockNativeQuerySnippet({ id: 1, name: "MySnippet" })],
+});
+
+describe("getValidationResult", () => {
+ const metadataProvider = Lib.metadataProvider(SAMPLE_DB_ID, METADATA);
+
+ it("should not return errors for a query without tags", () => {
+ const query = Lib.nativeQuery(SAMPLE_DB_ID, metadataProvider, "SELECT 1");
+ expect(getValidationResult(query)).toEqual({
+ isValid: true,
+ });
+ });
+
+ it("should not return errors for a query with card or snippet tags", () => {
+ const query = Lib.nativeQuery(
+ SAMPLE_DB_ID,
+ metadataProvider,
+ "SELECT * FROM {{#1-card}} {{snippet:MySnippet}}",
+ );
+ expect(getValidationResult(query)).toEqual({
+ isValid: true,
+ });
+ });
+
+ it("should mark the query as invalid but not return the error message for an empty query", () => {
+ const query = Lib.nativeQuery(SAMPLE_DB_ID, metadataProvider, "");
+ expect(getValidationResult(query)).toEqual({
+ isValid: false,
+ errorMessage: undefined,
+ });
+ });
+
+ it("should return errors if there are variables", () => {
+ const query = Lib.nativeQuery(
+ SAMPLE_DB_ID,
+ metadataProvider,
+ "{{var1}} {{var2}}",
+ );
+ expect(getValidationResult(query)).toEqual({
+ isValid: false,
+ errorMessage: expect.stringContaining("variables"),
+ });
+ });
+
+ it("should return errors if variables are mixed with tags that are allowed", () => {
+ const query = Lib.nativeQuery(
+ SAMPLE_DB_ID,
+ metadataProvider,
+ "{{snippet:MySnippet}} {{var}}",
+ );
+ expect(getValidationResult(query)).toEqual({
+ isValid: false,
+ errorMessage: expect.stringContaining("variables"),
+ });
+ });
+
+ it("should still return the variable error if there are variables, even if there are syntax errors", () => {
+ const query = Lib.nativeQuery(
+ SAMPLE_DB_ID,
+ metadataProvider,
+ "{{snippet: }} {{var}}",
+ );
+ expect(getValidationResult(query)).toEqual({
+ isValid: false,
+ errorMessage: expect.stringContaining("variables"),
+ });
+ });
+});