A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
sql mode - validate for duckdb (#6460)
Shahmir Varqha075a34c04718
diff --git a/frontend/src/__mocks__/requests.ts b/frontend/src/__mocks__/requests.tsindex aa9a50a289..aea49e3532 100644--- a/frontend/src/__mocks__/requests.ts+++ b/frontend/src/__mocks__/requests.ts@@ -40,6 +40,7 @@ export const MockRequestClient = {previewSQLTable: vi.fn().mockResolvedValue({}),previewSQLTableList: vi.fn().mockResolvedValue({ tables: [] }),previewDataSourceConnection: vi.fn().mockResolvedValue({}),+ validateSQL: vi.fn().mockResolvedValue({}),openFile: vi.fn().mockResolvedValue({}),getUsageStats: vi.fn().mockResolvedValue({}),sendPdb: vi.fn().mockResolvedValue({}),diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsxindex d0b3bbfcd5..27b1ee579f 100644--- a/frontend/src/components/editor/Cell.tsx+++ b/frontend/src/components/editor/Cell.tsx@@ -80,6 +80,7 @@ import { useDeleteCellCallback } from "./cell/useDeleteCell";import { useRunCell } from "./cell/useRunCells";import { HideCodeButton } from "./code/readonly-python-code";import { cellDomProps } from "./common";+import { SqlValidationErrorBanner } from "./errors/sql-validation-errors";import { useCellNavigationProps } from "./navigation/navigation";import {useTemporarilyShownCode,@@ -653,6 +654,7 @@ const EditableCellComponent = ({)}</div></div>+ <SqlValidationErrorBanner cellId={cellId} />{cellOutput === "below" && outputArea}{cellRuntime.serialization && (<div className="py-1 px-2 flex items-center justify-end gap-2 last:rounded-b">diff --git a/frontend/src/components/editor/errors/sql-validation-errors.tsx b/frontend/src/components/editor/errors/sql-validation-errors.tsxnew file mode 100644index 0000000000..835be89321--- /dev/null+++ b/frontend/src/components/editor/errors/sql-validation-errors.tsx@@ -0,0 +1,34 @@+/* Copyright 2024 Marimo. All rights reserved. */++import { AlertCircleIcon } from "lucide-react";+import type { CellId } from "@/core/cells/ids";+import { useSqlValidationErrorsForCell } from "@/core/codemirror/language/languages/sql/validation-errors";++export const SqlValidationErrorBanner = ({ cellId }: { cellId: CellId }) => {+ const error = useSqlValidationErrorsForCell(cellId);++ if (!error) {+ return;+ }++ return (+ <div className="p-3 text-sm flex flex-col text-muted-foreground gap-1.5 bg-destructive/5">+ <div className="flex items-start gap-1.5">+ <AlertCircleIcon size={13} className="mt-[3px] text-destructive" />+ <p>+ <span className="font-bold text-destructive">{error.errorType}:</span>{" "}+ {error.errorMessage}+ </p>+ </div>++ {error.codeblock && (+ <pre+ lang="sql"+ className="text-xs bg-muted rounded p-2 pb-0 mx-3 overflow-x-auto font-mono whitespace-pre-wrap"+ >+ {error.codeblock}+ </pre>+ )}+ </div>+ );+};diff --git a/frontend/src/core/codemirror/language/__tests__/extension.test.ts b/frontend/src/core/codemirror/language/__tests__/extension.test.tsindex 4f0188916f..e6d04c1672 100644--- a/frontend/src/core/codemirror/language/__tests__/extension.test.ts+++ b/frontend/src/core/codemirror/language/__tests__/extension.test.ts@@ -14,6 +14,7 @@ import {languageAdapterState,switchLanguage,} from "../extension";+import { exportedForTesting as sqlValidationErrorsForTesting } from "../languages/sql/validation-errors";import { languageMetadataField } from "../metadata";let view: EditorView | null = null;@@ -258,3 +259,26 @@ describe("switchLanguage", () => {});});});++describe("sqlValidationErrors", () => {+ const { splitErrorMessage } = sqlValidationErrorsForTesting;++ describe("split error message", () => {+ it("should split the error message into error type and error message", () => {+ const error = "SyntaxError: SELECT * FROM df";+ const { errorType, errorMessage } = splitErrorMessage(error);+ expect(errorType).toBe("SyntaxError");+ expect(errorMessage).toBe("SELECT * FROM df");+ });++ it("should handle multiple colons", () => {+ const error =+ "SyntaxError: SELECT * FROM df:SyntaxError: SELECT * FROM df";+ const { errorType, errorMessage } = splitErrorMessage(error);+ expect(errorType).toBe("SyntaxError");+ expect(errorMessage).toBe(+ "SELECT * FROM df:SyntaxError: SELECT * FROM df",+ );+ });+ });+});diff --git a/frontend/src/core/codemirror/language/__tests__/sql-validation.test.ts b/frontend/src/core/codemirror/language/__tests__/sql-validation.test.tsnew file mode 100644index 0000000000..9edd9c1e73--- /dev/null+++ b/frontend/src/core/codemirror/language/__tests__/sql-validation.test.ts@@ -0,0 +1,133 @@+/* Copyright 2024 Marimo. All rights reserved. */++import { describe, expect, it } from "vitest";+import { exportedForTesting } from "../languages/sql/validation-errors";++describe("Error Message Splitting", () => {+ it("should handle error message splitting correctly", () => {+ const { splitErrorMessage } = exportedForTesting;++ const result1 = splitErrorMessage("Syntax error: unexpected token");+ expect(result1.errorType).toBe("Syntax error");+ expect(result1.errorMessage).toBe("unexpected token");++ const result2 = splitErrorMessage("Multiple: colons: in error");+ expect(result2.errorType).toBe("Multiple");+ expect(result2.errorMessage).toBe("colons: in error");++ const result3 = splitErrorMessage("No colon error");+ expect(result3.errorType).toBe("No colon error");+ expect(result3.errorMessage).toBe("");+ });+});++describe("DuckDB Error Handling", () => {+ it("should extract codeblock from error with LINE information", () => {+ const { handleDuckdbError } = exportedForTesting;++ const error =+ 'Binder Error: Referenced column "attacks" not found in FROM clause! Candidate bindings: "Attack", "Total" LINE 1:... from pokemon WHERE \'type_2\' = 32 and attack = 32 and not attacks = \'hi\' ^';++ const result = handleDuckdbError(error);++ expect(result.errorType).toBe("Binder Error");+ expect(result.errorMessage).toBe(+ 'Referenced column "attacks" not found in FROM clause! Candidate bindings: "Attack", "Total"',+ );+ expect(result.codeblock).toBe(+ "LINE 1:... from pokemon WHERE 'type_2' = 32 and attack = 32 and not attacks = 'hi' ^",+ );+ });++ it("should handle error without LINE information", () => {+ const { handleDuckdbError } = exportedForTesting;++ const error = "Syntax Error: Invalid syntax near WHERE";++ const result = handleDuckdbError(error);++ expect(result.errorType).toBe("Syntax Error");+ expect(result.errorMessage).toBe("Invalid syntax near WHERE");+ expect(result.codeblock).toBeUndefined();+ });++ it("should handle error with LINE at the beginning", () => {+ const { handleDuckdbError } = exportedForTesting;++ const error = "LINE 1: SELECT * FROM table WHERE invalid_column = 1 ^";++ const result = handleDuckdbError(error);++ expect(result.errorType).toBe("LINE 1");+ expect(result.errorMessage).toBe(+ "SELECT * FROM table WHERE invalid_column = 1 ^",+ );+ expect(result.codeblock).toBeUndefined();+ });++ it("should handle error with multiple LINE occurrences", () => {+ const { handleDuckdbError } = exportedForTesting;++ const error =+ "Error: Something went wrong LINE 1: SELECT * FROM table WHERE invalid_column = 1 ^";++ const result = handleDuckdbError(error);++ expect(result.errorType).toBe("Error");+ expect(result.errorMessage).toBe("Something went wrong");+ expect(result.codeblock).toBe(+ "LINE 1: SELECT * FROM table WHERE invalid_column = 1 ^",+ );+ });++ it("should handle complex error with nested quotes", () => {+ const { handleDuckdbError } = exportedForTesting;++ const error =+ "Binder Error: Column \"name\" not found! LINE 1: SELECT * FROM users WHERE name = 'John' AND age > 25 ^";++ const result = handleDuckdbError(error);++ expect(result.errorType).toBe("Binder Error");+ expect(result.errorMessage).toBe('Column "name" not found!');+ expect(result.codeblock).toBe(+ "LINE 1: SELECT * FROM users WHERE name = 'John' AND age > 25 ^",+ );+ });++ it("should handle error with LINE but no caret", () => {+ const { handleDuckdbError } = exportedForTesting;++ const error = "Error: Invalid query LINE 1: SELECT * FROM table";++ const result = handleDuckdbError(error);++ expect(result.errorType).toBe("Error");+ expect(result.errorMessage).toBe("Invalid query");+ expect(result.codeblock).toBe("LINE 1: SELECT * FROM table");+ });++ it("should trim whitespace from codeblock", () => {+ const { handleDuckdbError } = exportedForTesting;++ const error = "Error: Something wrong LINE 1: SELECT * FROM table ^ ";++ const result = handleDuckdbError(error);++ expect(result.errorType).toBe("Error");+ expect(result.errorMessage).toBe("Something wrong");+ expect(result.codeblock).toBe("LINE 1: SELECT * FROM table ^");+ });++ it("should handle empty error message", () => {+ const { handleDuckdbError } = exportedForTesting;++ const error = "";++ const result = handleDuckdbError(error);++ expect(result.errorType).toBe("");+ expect(result.errorMessage).toBe("");+ expect(result.codeblock).toBeUndefined();+ });+});diff --git a/frontend/src/core/codemirror/language/languages/sql/sql-mode.ts b/frontend/src/core/codemirror/language/languages/sql/sql-mode.tsnew file mode 100644index 0000000000..3158762939--- /dev/null+++ b/frontend/src/core/codemirror/language/languages/sql/sql-mode.ts@@ -0,0 +1,20 @@+/* Copyright 2024 Marimo. All rights reserved. */++import { useAtom } from "jotai";+import { atomWithStorage } from "jotai/utils";+import { store } from "@/core/state/jotai";++const BASE_KEY = "marimo:notebook-sql-mode";++export type SQLMode = "validate" | "default";++const sqlModeAtom = atomWithStorage<SQLMode>(BASE_KEY, "default");++export function useSQLMode() {+ const [sqlMode, setSQLMode] = useAtom(sqlModeAtom);+ return { sqlMode, setSQLMode };+}++export function getSQLMode() {+ return store.get(sqlModeAtom);+}diff --git a/frontend/src/core/codemirror/language/languages/sql/sql.ts b/frontend/src/core/codemirror/language/languages/sql/sql.tsindex bc6c6e0fb8..3ef15ed407 100644--- a/frontend/src/core/codemirror/language/languages/sql/sql.ts+++ b/frontend/src/core/codemirror/language/languages/sql/sql.ts@@ -5,7 +5,7 @@ import { insertTab } from "@codemirror/commands";import { type SQLDialect, type SQLNamespace, sql } from "@codemirror/lang-sql";import type { EditorState, Extension } from "@codemirror/state";import { Compartment } from "@codemirror/state";-import { type EditorView, keymap } from "@codemirror/view";+import { EditorView, keymap } from "@codemirror/view";import type { SyntaxNode, TreeCursor } from "@lezer/common";import { parser } from "@lezer/python";import {@@ -16,12 +16,19 @@ import {} from "@marimo-team/codemirror-sql";import { DuckDBDialect } from "@marimo-team/codemirror-sql/dialects";import dedent from "string-dedent";+import { cellIdState } from "@/core/codemirror/cells/state";import { getFeatureFlag } from "@/core/config/feature-flag";import {dataSourceConnectionsAtom,setLatestEngineSelected,} from "@/core/datasets/data-source-connections";-import { type ConnectionName, DUCKDB_ENGINE } from "@/core/datasets/engines";+import {+ type ConnectionName,+ DUCKDB_ENGINE,+ INTERNAL_SQL_ENGINES,+} from "@/core/datasets/engines";+import { ValidateSQL } from "@/core/datasets/request-registry";+import type { ValidateSQLResult } from "@/core/kernel/messages";import { store } from "@/core/state/jotai";import { resolvedThemeAtom } from "@/theme/useTheme";import { Logger } from "@/utils/Logger";@@ -37,6 +44,11 @@ import {tablesCompletionSource,} from "./completion-sources";import { SCHEMA_CACHE } from "./completion-store";+import { getSQLMode } from "./sql-mode";+import {+ clearSqlValidationError,+ setSqlValidationError,+} from "./validation-errors";const DEFAULT_DIALECT = DuckDBDialect;const DEFAULT_PARSER_DIALECT = "DuckDB";@@ -64,12 +76,15 @@ export class SQLLanguageAdapter{readonly type = "sql";sqlLinterEnabled: boolean;+ sqlModeEnabled: boolean;constructor() {try {this.sqlLinterEnabled = getFeatureFlag("sql_linter");+ this.sqlModeEnabled = getFeatureFlag("sql_mode");} catch {this.sqlLinterEnabled = false;+ this.sqlModeEnabled = false;}}@@ -265,6 +280,10 @@ export class SQLLanguageAdapter);}+ if (this.sqlModeEnabled) {+ extensions.push(sqlValidationExtension());+ }+return extensions;}}@@ -315,9 +334,14 @@ function getSchema(view: EditorView): SQLNamespace {function guessParserDialect(state: EditorState): ParserDialects | null {const metadata = getSQLMetadata(state);const connectionName = metadata.engine;+ return connectionNameToParserDialect(connectionName);+}++function connectionNameToParserDialect(+ connectionName: ConnectionName,+): ParserDialects | null {const dialect =SCHEMA_CACHE.getInternalDialect(connectionName)?.toLowerCase();-switch (dialect) {case "postgresql":case "postgres":@@ -543,3 +567,66 @@ function safeDedent(code: string): string {return code;}}++function sqlValidationExtension(): Extension {+ let debounceTimeout: NodeJS.Timeout | null = null;+ let lastValidationRequest: string | null = null;++ return EditorView.updateListener.of((update) => {+ const sqlMode = getSQLMode();+ if (sqlMode !== "validate") {+ return;+ }++ const metadata = getSQLMetadata(update.state);+ const connectionName = metadata.engine;+ if (!INTERNAL_SQL_ENGINES.has(connectionName)) {+ // Currently only internal engines are supported+ return;+ }++ if (!update.docChanged) {+ return;+ }++ const doc = update.state.doc;+ const sqlContent = doc.toString();++ // Clear existing timeout+ if (debounceTimeout) {+ clearTimeout(debounceTimeout);+ }++ // Debounce the validation call+ debounceTimeout = setTimeout(async () => {+ // Skip if content hasn't changed since last validation+ if (lastValidationRequest === sqlContent) {+ return;+ }++ lastValidationRequest = sqlContent;+ const cellId = update.view.state.facet(cellIdState);++ if (sqlContent === "") {+ clearSqlValidationError(cellId);+ return;+ }++ try {+ const result: ValidateSQLResult = await ValidateSQL.request({+ engine: connectionName,+ query: sqlContent,+ });++ if (result.error) {+ const dialect = connectionNameToParserDialect(connectionName);+ setSqlValidationError({ cellId, error: result.error, dialect });+ } else {+ clearSqlValidationError(cellId);+ }+ } catch (error) {+ Logger.warn("Failed to validate SQL", { error });+ }+ }, 300);+ });+}diff --git a/frontend/src/core/codemirror/language/languages/sql/validation-errors.ts b/frontend/src/core/codemirror/language/languages/sql/validation-errors.tsnew file mode 100644index 0000000000..052eb23f6f--- /dev/null+++ b/frontend/src/core/codemirror/language/languages/sql/validation-errors.ts@@ -0,0 +1,79 @@+/* Copyright 2024 Marimo. All rights reserved. */++import type { SupportedDialects } from "@marimo-team/codemirror-sql";+import { atom, useAtomValue } from "jotai";+import type { CellId } from "@/core/cells/ids";+import { store } from "@/core/state/jotai";++export interface SQLValidationError {+ errorType: string;+ errorMessage: string;+ codeblock?: string; // Code block that caused the error+}++type CellToSQLErrors = Map<CellId, SQLValidationError>;++export const sqlValidationErrorsAtom = atom<CellToSQLErrors>(+ new Map<CellId, SQLValidationError>(),+);++export const useSqlValidationErrorsForCell = (cellId: CellId) => {+ const sqlValidationErrors = useAtomValue(sqlValidationErrorsAtom);+ return sqlValidationErrors.get(cellId);+};++export function clearSqlValidationError(cellId: CellId) {+ const sqlValidationErrors = store.get(sqlValidationErrorsAtom);+ const newErrors = new Map(sqlValidationErrors);+ newErrors.delete(cellId);+ store.set(sqlValidationErrorsAtom, newErrors);+}++export function setSqlValidationError({+ cellId,+ error,+ dialect,+}: {+ cellId: CellId;+ error: string;+ dialect: SupportedDialects | null;+}) {+ const sqlValidationErrors = store.get(sqlValidationErrorsAtom);+ const newErrors = new Map(sqlValidationErrors);++ const errorResult: SQLValidationError =+ dialect === "DuckDB" ? handleDuckdbError(error) : splitErrorMessage(error);++ newErrors.set(cellId, errorResult);+ store.set(sqlValidationErrorsAtom, newErrors);+}++function handleDuckdbError(error: string): SQLValidationError {+ const { errorType, errorMessage } = splitErrorMessage(error);+ let newErrorMessage = errorMessage;++ // Extract the LINE and the rest of the message as codeblock, keep errorMessage as whatever is before+ let codeblock: string | undefined;+ const lineIndex = errorMessage.indexOf("LINE ");+ if (lineIndex !== -1) {+ codeblock = errorMessage.slice(Math.max(0, lineIndex)).trim();+ newErrorMessage = errorMessage.slice(0, Math.max(0, lineIndex)).trim();+ }++ return {+ errorType,+ errorMessage: newErrorMessage,+ codeblock,+ };+}++function splitErrorMessage(error: string) {+ const errorType = error.split(":")[0].trim();+ const errorMessage = error.split(":").slice(1).join(":").trim();+ return { errorType, errorMessage };+}++export const exportedForTesting = {+ splitErrorMessage,+ handleDuckdbError,+};diff --git a/frontend/src/core/codemirror/language/panel/panel.tsx b/frontend/src/core/codemirror/language/panel/panel.tsxindex a5404143a7..c914715321 100644--- a/frontend/src/core/codemirror/language/panel/panel.tsx+++ b/frontend/src/core/codemirror/language/panel/panel.tsx@@ -5,7 +5,8 @@ import { Button } from "@/components/ui/button";import { Checkbox } from "@/components/ui/checkbox";import { Tooltip, TooltipProvider } from "@/components/ui/tooltip";import { normalizeName } from "@/core/cells/names";-import type { ConnectionName } from "@/core/datasets/engines";+import { getFeatureFlag } from "@/core/config/feature-flag";+import { type ConnectionName, DUCKDB_ENGINE } from "@/core/datasets/engines";import { useAutoGrowInputProps } from "@/hooks/useAutoGrowInputProps";import { formatSQL } from "../../format";import { languageAdapterState } from "../extension";@@ -22,7 +23,7 @@ import {import type { LanguageMetadataOf } from "../types";import type { QuotePrefixKind } from "../utils/quotes";import { getQuotePrefix, MarkdownQuotePrefixTooltip } from "./markdown";-import { SQLEngineSelect } from "./sql";+import { SQLEngineSelect, SQLModeSelect } from "./sql";const Divider = () => <div className="h-4 border-r border-border" />;@@ -70,6 +71,8 @@ export const LanguagePanelComponent: React.FC<{updateSQLDialectFromConnection(view, engine);};+ const sqlModeEnabled = getFeatureFlag("sql_mode");+actions = (<div className="flex flex-1 gap-2 items-center"><label className="flex gap-2 items-center">@@ -95,6 +98,9 @@ export const LanguagePanelComponent: React.FC<{onChange={switchEngine}/><div className="flex items-center gap-2 ml-auto">+ {sqlModeEnabled && metadata.engine === DUCKDB_ENGINE && (+ <SQLModeSelect />+ )}<Tooltip content="Format SQL"><Buttonvariant="text"diff --git a/frontend/src/core/codemirror/language/panel/sql.tsx b/frontend/src/core/codemirror/language/panel/sql.tsxindex 2d45840d97..30a0b6f749 100644--- a/frontend/src/core/codemirror/language/panel/sql.tsx+++ b/frontend/src/core/codemirror/language/panel/sql.tsx@@ -1,9 +1,16 @@/* Copyright 2024 Marimo. All rights reserved. */+import type { SelectTriggerProps } from "@radix-ui/react-select";import { useAtomValue } from "jotai";-import { AlertCircle, CircleHelpIcon } from "lucide-react";+import {+ AlertCircle,+ CircleHelpIcon,+ DatabaseBackup,+ SearchCheck,+} from "lucide-react";import { transformDisplayName } from "@/components/databases/display";import { DatabaseLogo } from "@/components/databases/icon";+import { Button } from "@/components/ui/button";import {Select,SelectContent,@@ -14,6 +21,7 @@ import {SelectTrigger,SelectValue,} from "@/components/ui/select";+import { Tooltip } from "@/components/ui/tooltip";import {dataConnectionsMapAtom,setLatestEngineSelected,@@ -24,6 +32,7 @@ import {} from "@/core/datasets/engines";import type { DataSourceConnection } from "@/core/kernel/messages";import { useNonce } from "@/hooks/useNonce";+import { type SQLMode, useSQLMode } from "../languages/sql/sql-mode";interface SelectProps {selectedEngine: ConnectionName;@@ -77,7 +86,7 @@ export const SQLEngineSelect: React.FC<SelectProps> = ({<SelectItem key={connection.name} value={connection.name}><div className="flex items-center gap-1"><DatabaseLogo className="h-3 w-3" name={connection.dialect} />- <span className="truncate">+ <span className="truncate ml-0.5">{transformDisplayName(connection.display_name)}</span></div>@@ -88,9 +97,9 @@ export const SQLEngineSelect: React.FC<SelectProps> = ({return (<div className="flex flex-row gap-1 items-center"><Select value={selectedEngine} onValueChange={handleSelectEngine}>- <SelectTrigger className="text-xs border-border shadow-none! ring-0! h-4.5 px-1.5">+ <SQLSelectTrigger><SelectValue placeholder="Select an engine" />- </SelectTrigger>+ </SQLSelectTrigger><SelectContent><SelectGroup><SelectLabel>Database connections</SelectLabel>@@ -130,3 +139,71 @@ export const SQLEngineSelect: React.FC<SelectProps> = ({const HELP_KEY = "__help__";const HELP_URL ="http://docs.marimo.io/guides/working_with_data/sql/#connecting-to-a-custom-database";++export const SQLModeSelect: React.FC = () => {+ const { sqlMode, setSQLMode } = useSQLMode();++ const handleToggleMode = () => {+ setSQLMode(sqlMode === "validate" ? "default" : "validate");+ };++ const getModeIcon = (mode: SQLMode) => {+ return mode === "validate" ? (+ <SearchCheck className="h-3 w-3" />+ ) : (+ <DatabaseBackup className="h-3 w-3" />+ );+ };++ const getTooltipContent = (mode: SQLMode) => {+ return mode === "validate" ? (+ <div className="text-xs">+ <div className="font-semibold mb-1 flex flex-row items-center gap-1">+ <SearchCheck className="h-3 w-3" />+ Validate Mode+ </div>+ <p>Queries are validated as you write them</p>+ </div>+ ) : (+ <div className="text-xs">+ <div className="font-semibold mb-1 flex flex-row items-center gap-1">+ <DatabaseBackup className="h-3 w-3" />+ Default Mode+ </div>+ <p>Standard editing</p>+ </div>+ );+ };++ return (+ <div className="flex flex-row gap-1 items-center">+ <Tooltip delayDuration={300} content={getTooltipContent(sqlMode)}>+ <Button+ variant="ghost"+ size="sm"+ onClick={handleToggleMode}+ className="h-5 px-1.5 text-xs border-border shadow-none hover:bg-accent"+ >+ {getModeIcon(sqlMode)}+ <span className="ml-1">+ {sqlMode === "validate" ? "Validate" : "Default"}+ </span>+ </Button>+ </Tooltip>+ </div>+ );+};++const SQLSelectTrigger: React.FC<SelectTriggerProps> = ({+ children,+ ...props+}) => {+ return (+ <SelectTrigger+ className="text-xs border-border shadow-none! ring-0! h-5 px-1.5 hover:bg-accent transition-colors"+ {...props}+ >+ {children}+ </SelectTrigger>+ );+};diff --git a/frontend/src/core/config/feature-flag.tsx b/frontend/src/core/config/feature-flag.tsxindex c32b14d346..580c238b92 100644--- a/frontend/src/core/config/feature-flag.tsx+++ b/frontend/src/core/config/feature-flag.tsx@@ -14,6 +14,7 @@ export interface ExperimentalFeatures {mcp_docs: boolean;sql_linter: boolean;external_agents: boolean;+ sql_mode: boolean;// Add new feature flags here}@@ -26,13 +27,14 @@ const defaultValues: ExperimentalFeatures = {mcp_docs: false,sql_linter: false,external_agents: import.meta.env.DEV,+ sql_mode: false,};export function getFeatureFlag<T extends keyof ExperimentalFeatures>(feature: T,): ExperimentalFeatures[T] {return (- (getResolvedMarimoConfig().experimental?.[+ (getResolvedMarimoConfig()?.experimental?.[feature] as ExperimentalFeatures[T]) ?? defaultValues[feature]);diff --git a/frontend/src/core/datasets/request-registry.ts b/frontend/src/core/datasets/request-registry.tsindex 5f6e34d1bb..4b1e324703 100644--- a/frontend/src/core/datasets/request-registry.ts+++ b/frontend/src/core/datasets/request-registry.ts@@ -1,10 +1,15 @@/* Copyright 2024 Marimo. All rights reserved. */-import type { SQLTableListPreview, SQLTablePreview } from "../kernel/messages";+import type {+ SQLTableListPreview,+ SQLTablePreview,+ ValidateSQLResult,+} from "../kernel/messages";import { DeferredRequestRegistry } from "../network/DeferredRequestRegistry";import { getRequestClient } from "../network/requests";import type {PreviewSQLTableListRequest,PreviewSQLTableRequest,+ ValidateSQLRequest,} from "../network/types";// We make a request to the backend to preview the table, passing in Engine, DB, Schema, and Table@@ -32,3 +37,14 @@ export const PreviewSQLTableList = new DeferredRequestRegistry<...req,});});++export const ValidateSQL = new DeferredRequestRegistry<+ Omit<ValidateSQLRequest, "requestId">,+ ValidateSQLResult+>("validate-sql", async (requestId, req) => {+ const client = getRequestClient();+ await client.validateSQL({+ requestId: requestId,+ ...req,+ });+});diff --git a/frontend/src/core/islands/bridge.ts b/frontend/src/core/islands/bridge.tsindex 91833d9d2e..8458284923 100644--- a/frontend/src/core/islands/bridge.ts+++ b/frontend/src/core/islands/bridge.ts@@ -153,6 +153,7 @@ export class IslandsPyodideBridge implements RunRequests, EditRequests {previewSQLTable = throwNotImplemented;previewSQLTableList = throwNotImplemented;previewDataSourceConnection = throwNotImplemented;+ validateSQL = throwNotImplemented;openFile = throwNotImplemented;sendListFiles = throwNotImplemented;sendSearchFiles = throwNotImplemented;diff --git a/frontend/src/core/islands/main.ts b/frontend/src/core/islands/main.tsindex 6eee13aa0b..721913233e 100644--- a/frontend/src/core/islands/main.ts+++ b/frontend/src/core/islands/main.ts@@ -119,6 +119,7 @@ export async function initialize() {case "sql-table-list-preview":case "datasets":case "data-source-connections":+ case "validate-sql-result":case "secret-keys-result":case "startup-logs":// Unsupporteddiff --git a/frontend/src/core/kernel/messages.ts b/frontend/src/core/kernel/messages.tsindex 0f880a27e2..ecec604a77 100644--- a/frontend/src/core/kernel/messages.ts+++ b/frontend/src/core/kernel/messages.ts@@ -38,6 +38,7 @@ export type DataColumnPreview = OperationMessageData<"data-column-preview">;export type SQLTablePreview = OperationMessageData<"sql-table-preview">;export type SQLTableListPreview =OperationMessageData<"sql-table-list-preview">;+export type ValidateSQLResult = OperationMessageData<"validate-sql-result">;export type SecretKeysResult = OperationMessageData<"secret-keys-result">;export type StartupLogs = OperationMessageData<"startup-logs">;export type CellMessage = OperationMessageData<"cell-op">;diff --git a/frontend/src/core/network/requests-network.ts b/frontend/src/core/network/requests-network.tsindex b7c1e44a5d..5a7fae5a48 100644--- a/frontend/src/core/network/requests-network.ts+++ b/frontend/src/core/network/requests-network.ts@@ -207,6 +207,13 @@ export function createNetworkRequests(): EditRequests & RunRequests {}).then(handleResponseReturnNull);},+ validateSQL: (request) => {+ return getClient()+ .POST("/api/sql/validate", {+ body: request,+ })+ .then(handleResponseReturnNull);+ },openFile: async (request) => {await waitForConnectionOpen();await getClient()diff --git a/frontend/src/core/network/requests-static.ts b/frontend/src/core/network/requests-static.tsindex d32462ef61..f789f88d91 100644--- a/frontend/src/core/network/requests-static.ts+++ b/frontend/src/core/network/requests-static.ts@@ -57,6 +57,7 @@ export function createStaticRequests(): EditRequests & RunRequests {previewSQLTable: throwNotInEditMode,previewSQLTableList: throwNotInEditMode,previewDataSourceConnection: throwNotInEditMode,+ validateSQL: throwNotInEditMode,openFile: throwNotInEditMode,getUsageStats: throwNotInEditMode,sendListFiles: throwNotInEditMode,diff --git a/frontend/src/core/network/requests-toasting.ts b/frontend/src/core/network/requests-toasting.tsindex 701364e175..f90c3b0ee5 100644--- a/frontend/src/core/network/requests-toasting.ts+++ b/frontend/src/core/network/requests-toasting.ts@@ -35,6 +35,7 @@ export function createErrorToastingRequests(previewSQLTable: "Failed to fetch SQL table",previewSQLTableList: "Failed to fetch SQL table list",previewDataSourceConnection: "Failed to preview data source connection",+ validateSQL: "Failed to validate SQL",openFile: "Failed to open file",getUsageStats: "", // No toastsendListFiles: "Failed to list files",diff --git a/frontend/src/core/network/types.ts b/frontend/src/core/network/types.tsindex e7846cc907..42a563e986 100644--- a/frontend/src/core/network/types.ts+++ b/frontend/src/core/network/types.ts@@ -62,6 +62,7 @@ export type PreviewSQLTableRequest = schemas["PreviewSQLTableRequest"];export type PreviewSQLTableListRequest = schemas["PreviewSQLTableListRequest"];export type PreviewDataSourceConnectionRequest =schemas["PreviewDataSourceConnectionRequest"];+export type ValidateSQLRequest = schemas["ValidateSQLRequest"];export type PdbRequest = schemas["PdbRequest"];export type ReadCodeResponse = schemas["ReadCodeResponse"];export type RecentFilesResponse = schemas["RecentFilesResponse"];@@ -140,6 +141,7 @@ export interface EditRequests {previewDataSourceConnection: (request: PreviewDataSourceConnectionRequest,) => Promise<null>;+ validateSQL: (request: ValidateSQLRequest) => Promise<null>;openFile: (request: { path: string }) => Promise<null>;getUsageStats: () => Promise<UsageResponse>;// Debuggerdiff --git a/frontend/src/core/wasm/bridge.ts b/frontend/src/core/wasm/bridge.tsindex 3b2eb8f6fb..79603549e9 100644--- a/frontend/src/core/wasm/bridge.ts+++ b/frontend/src/core/wasm/bridge.ts@@ -496,6 +496,11 @@ export class PyodideBridge implements RunRequests, EditRequests {return null;};+ validateSQL: EditRequests["validateSQL"] = async (request) => {+ await this.putControlRequest(request);+ return null;+ };+sendModelValue: RunRequests["sendModelValue"] = async (request) => {await this.putControlRequest(request);return null;diff --git a/frontend/src/core/websocket/useMarimoWebSocket.tsx b/frontend/src/core/websocket/useMarimoWebSocket.tsxindex dacb0a3ce0..e0cb61180b 100644--- a/frontend/src/core/websocket/useMarimoWebSocket.tsx+++ b/frontend/src/core/websocket/useMarimoWebSocket.tsx@@ -32,6 +32,7 @@ import type { ConnectionName } from "../datasets/engines";import {PreviewSQLTable,PreviewSQLTableList,+ ValidateSQL,} from "../datasets/request-registry";import { useDatasetsActions } from "../datasets/state";import { UI_ELEMENT_REGISTRY } from "../dom/uiregistry";@@ -238,6 +239,9 @@ export function useMarimoWebSocket(opts: {case "sql-table-list-preview":PreviewSQLTableList.resolve(msg.data.request_id as RequestId, msg.data);return;+ case "validate-sql-result":+ ValidateSQL.resolve(msg.data.request_id as RequestId, msg.data);+ return;case "secret-keys-result":SECRETS_REGISTRY.resolve(msg.data.request_id as RequestId, msg.data);return;diff --git a/marimo/_cli/development/commands.py b/marimo/_cli/development/commands.pyindex 9cc60a3b88..f1ba0891e2 100644--- a/marimo/_cli/development/commands.py+++ b/marimo/_cli/development/commands.py@@ -222,6 +222,7 @@ def _generate_server_api_schema() -> dict[str, Any]:requests.PreviewSQLTableListRequest,requests.PreviewDataSourceConnectionRequest,requests.PreviewSQLTableRequest,+ requests.ValidateSQLRequest,requests.RenameRequest,requests.SetCellConfigRequest,requests.ModelMessage,diff --git a/marimo/_config/config.py b/marimo/_config/config.pyindex 9183f4e49a..79ba360ccc 100644--- a/marimo/_config/config.py+++ b/marimo/_config/config.py@@ -508,6 +508,7 @@ class ExperimentalConfig(TypedDict, total=False):performant_table_charts: boolmcp_docs: boolsql_linter: bool+ sql_mode: bool# Internal featurescache: CacheConfigdiff --git a/marimo/_messaging/ops.py b/marimo/_messaging/ops.pyindex a9e4bb1a88..12f3c42af5 100644--- a/marimo/_messaging/ops.py+++ b/marimo/_messaging/ops.py@@ -626,6 +626,13 @@ class DataSourceConnections(Op, tag="data-source-connections"):connections: list[DataSourceConnection]+class ValidateSQLResult(Op, tag="validate-sql-result"):+ name: ClassVar[str] = "validate-sql-result"+ request_id: RequestId+ result: Optional[Any] = None+ error: Optional[str] = None++class QueryParamsSet(Op, tag="query-params-set"):"""Set query parameters."""@@ -721,6 +728,7 @@ MessageOperation = Union[SQLTablePreview,SQLTableListPreview,DataSourceConnections,+ ValidateSQLResult,# SecretsSecretKeysResult,# Kiosk specificdiff --git a/marimo/_runtime/requests.py b/marimo/_runtime/requests.pyindex 31a853c8f2..6b6ca0f0af 100644--- a/marimo/_runtime/requests.py+++ b/marimo/_runtime/requests.py@@ -338,6 +338,14 @@ class PreviewDataSourceConnectionRequest(msgspec.Struct, rename="camel"):engine: str+class ValidateSQLRequest(msgspec.Struct, rename="camel"):+ """Validate an SQL query"""++ request_id: RequestId+ engine: str+ query: str++class ListSecretKeysRequest(msgspec.Struct, rename="camel"):request_id: RequestId@@ -397,6 +405,7 @@ ControlRequest = Union[SetUserConfigRequest,ListSecretKeysRequest,PreviewDataSourceConnectionRequest,+ ValidateSQLRequest,# Requests with no fields (will match any empty object)StopRequest,RefreshSecretsRequest,diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex e08beda52d..843fb6f193 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -67,6 +67,7 @@ from marimo._messaging.ops import (SecretKeysResult,SQLTableListPreview,SQLTablePreview,+ ValidateSQLResult,VariableDeclaration,Variables,VariableValue,@@ -149,6 +150,7 @@ from marimo._runtime.requests import (SetUIElementValueRequest,SetUserConfigRequest,StopRequest,+ ValidateSQLRequest,)from marimo._runtime.runner import cell_runnerfrom marimo._runtime.runner.hooks import (@@ -176,7 +178,8 @@ from marimo._secrets.load_dotenv import (from marimo._secrets.secrets import get_secret_keysfrom marimo._server.model import SessionModefrom marimo._server.types import QueueType-from marimo._sql.engines.types import EngineCatalog+from marimo._sql.engines.duckdb import INTERNAL_DUCKDB_ENGINE, DuckDBEngine+from marimo._sql.engines.types import EngineCatalog, QueryEnginefrom marimo._sql.get_engines import (engine_to_data_source_connection,get_engines_from_variables,@@ -523,6 +526,7 @@ class Kernel:self.secrets_callbacks = SecretsCallbacks(self)self.datasets_callbacks = DatasetCallbacks(self)self.packages_callbacks = PackagesCallbacks(self)+ self.sql_callbacks = SqlCallbacks(self)# Apply pythonpath from config at initializationpythonpath = user_config["runtime"].get("pythonpath")@@ -2220,6 +2224,8 @@ class Kernel:PreviewDataSourceConnectionRequest,self.datasets_callbacks.preview_datasource_connection,)+ # SQL+ handler.register(ValidateSQLRequest, self.sql_callbacks.validate_sql)# Secretshandler.register(ListSecretKeysRequest, self.secrets_callbacks.list_secrets@@ -2246,6 +2252,31 @@ class Kernel:await self.request_handler.handle(request)LOGGER.debug("Handled control request: %s", request)+ def get_engine_catalog(+ self, variable_name: str+ ) -> tuple[Optional[EngineCatalog[Any]], Optional[str]]:+ """Fetch the catalog-capable engine associated with the given variable name.++ Returns the engine if it supports catalog operations, or an error message if not."""+ variable_name = cast(VariableName, variable_name)++ try:+ # Should we find the existing engine instead?+ engine_val = self.globals.get(variable_name)+ engines = get_engines_from_variables([(variable_name, engine_val)])+ if engines is None or len(engines) == 0:+ return None, "Engine not found"+ engine = engines[0][1]+ if isinstance(engine, EngineCatalog):+ return engine, None+ else:+ return None, "Connection does not support catalog operations"+ except Exception as e:+ LOGGER.warning(+ "Failed to get engine %s", variable_name, exc_info=e+ )+ return None, str(e)+class DatasetCallbacks:def __init__(self, kernel: Kernel):@@ -2320,31 +2351,6 @@ class DatasetCallbacks:).broadcast()return- def _get_engine_catalog(- self, variable_name: str- ) -> tuple[Optional[EngineCatalog[Any]], Optional[str]]:- """Fetch the catalog-capable engine associated with the given variable name.-- Returns the engine if it supports catalog operations, or an error message if not."""- variable_name = cast(VariableName, variable_name)-- try:- # Should we find the existing engine instead?- engine_val = self._kernel.globals.get(variable_name)- engines = get_engines_from_variables([(variable_name, engine_val)])- if engines is None or len(engines) == 0:- return None, "Engine not found"- engine = engines[0][1]- if isinstance(engine, EngineCatalog):- return engine, None- else:- return None, "Connection does not support catalog operations"- except Exception as e:- LOGGER.warning(- "Failed to get engine %s", variable_name, exc_info=e- )- return None, str(e)-@kernel_tracer.start_as_current_span("preview_sql_table")async def preview_sql_table(self, request: PreviewSQLTableRequest) -> None:"""Get table details for an SQL table.@@ -2361,7 +2367,7 @@ class DatasetCallbacks:schema_name = request.schematable_name = request.table_name- engine, error = self._get_engine_catalog(variable_name)+ engine, error = self._kernel.get_engine_catalog(variable_name)if error is not None or engine is None:SQLTablePreview(request_id=request.request_id, table=None, error=error@@ -2406,7 +2412,7 @@ class DatasetCallbacks:database_name = request.databaseschema_name = request.schema- engine, error = self._get_engine_catalog(variable_name)+ engine, error = self._kernel.get_engine_catalog(variable_name)if error is not None or engine is None:SQLTableListPreview(request_id=request.request_id, tables=[], error=error@@ -2438,7 +2444,7 @@ class DatasetCallbacks:) -> None:"""Broadcasts a datasource connection for a given engine"""variable_name = cast(VariableName, request.engine)- engine, error = self._get_engine_catalog(variable_name)+ engine, error = self._kernel.get_engine_catalog(variable_name)if error is not None or engine is None:LOGGER.error("Failed to get engine %s", variable_name)return@@ -2455,6 +2461,43 @@ class DatasetCallbacks:).broadcast()+class SqlCallbacks:+ def __init__(self, kernel: Kernel):+ self._kernel = kernel++ @kernel_tracer.start_as_current_span("validate_sql_query")+ async def validate_sql(self, request: ValidateSQLRequest) -> None:+ """Validate an SQL query"""+ # TODO: Place request in queue+ variable_name = cast(VariableName, request.engine)+ engine: Optional[EngineCatalog[Any]] = None+ if variable_name == INTERNAL_DUCKDB_ENGINE:+ engine = DuckDBEngine(connection=None)+ error = None+ else:+ engine, error = self._kernel.get_engine_catalog(variable_name)++ if error is not None or engine is None:+ LOGGER.error("Failed to get engine %s", variable_name)+ ValidateSQLResult(+ request_id=request.request_id,+ result=None,+ error="Engine not found",+ ).broadcast()+ return++ if isinstance(engine, QueryEngine):+ result, error = engine.execute_in_explain_mode(request.query) # type: ignore+ else:+ error = "Engine does not support explain mode"++ ValidateSQLResult(+ request_id=request.request_id,+ result=None, # We aren't using the result yet+ error=error,+ ).broadcast()++class SecretsCallbacks:def __init__(self, kernel: Kernel):self._kernel = kerneldiff --git a/marimo/_server/api/endpoints/sql.py b/marimo/_server/api/endpoints/sql.pynew file mode 100644index 0000000000..e153b33670--- /dev/null+++ b/marimo/_server/api/endpoints/sql.py@@ -0,0 +1,47 @@+# Copyright 2024 Marimo. All rights reserved.+from __future__ import annotations++from typing import TYPE_CHECKING++from starlette.authentication import requires++from marimo._runtime.requests import (+ ValidateSQLRequest,+)+from marimo._server.api.deps import AppState+from marimo._server.api.utils import parse_request+from marimo._server.models.models import BaseResponse, SuccessResponse+from marimo._server.router import APIRouter+from marimo._types.ids import ConsumerId++if TYPE_CHECKING:+ from starlette.requests import Request++# Router for SQL endpoints+router = APIRouter()++[email protected]("/validate")+@requires("edit")+async def validate_sql(request: Request) -> BaseResponse:+ """+ requestBody:+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/ValidateSQLRequest"+ responses:+ 200:+ description: Validate an SQL query+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/SuccessResponse"+ """+ app_state = AppState(request)+ body = await parse_request(request, ValidateSQLRequest)+ app_state.require_current_session().put_control_request(+ body,+ from_consumer_id=ConsumerId(app_state.require_current_session_id()),+ )+ return SuccessResponse()diff --git a/marimo/_server/api/router.py b/marimo/_server/api/router.pyindex 288f2a7d50..59945332af 100644--- a/marimo/_server/api/router.py+++ b/marimo/_server/api/router.py@@ -24,6 +24,7 @@ from marimo._server.api.endpoints.home import router as home_routerfrom marimo._server.api.endpoints.login import router as login_routerfrom marimo._server.api.endpoints.packages import router as packages_routerfrom marimo._server.api.endpoints.secrets import router as secrets_router+from marimo._server.api.endpoints.sql import router as sql_routerfrom marimo._server.api.endpoints.terminal import router as terminal_routerfrom marimo._server.api.endpoints.ws import router as ws_routerfrom marimo._server.router import APIRouter@@ -57,6 +58,7 @@ def build_routes(base_url: str = "") -> list[BaseRoute]:app_router.include_router(datasources_router, prefix="/api/datasources", name="datasources")+ app_router.include_router(sql_router, prefix="/api/sql", name="sql")app_router.include_router(ai_router, prefix="/api/ai", name="ai")app_router.include_router(home_router, prefix="/api/home", name="home")app_router.include_router(login_router, prefix="/auth", name="auth")diff --git a/marimo/_sql/engines/types.py b/marimo/_sql/engines/types.pyindex 84e505ef50..7097a3f416 100644--- a/marimo/_sql/engines/types.py+++ b/marimo/_sql/engines/types.py@@ -13,6 +13,7 @@ from marimo._runtime.context.types import (get_context,runtime_context_installed,)+from marimo._sql.utils import is_query_empty, wrap_query_with_explainfrom marimo._types.ids import VariableNameNO_SCHEMA_NAME = ""@@ -133,6 +134,17 @@ class QueryEngine(BaseEngine[CONN], ABC):return "auto"return "auto"+ def execute_in_explain_mode(self, query: str) -> tuple[Any, Optional[str]]:+ """Execute a query in explain mode. Returns a tuple of the result and an error if there is one."""++ explain_query = wrap_query_with_explain(query)+ try:+ return self.execute(explain_query), None+ except Exception as e:+ if is_query_empty(query):+ return None, None+ return None, str(e)+class SQLConnection(EngineCatalog[CONN], QueryEngine[CONN]):"""Combines the catalog and query interfaces for an SQL engine."""diff --git a/marimo/_sql/get_engines.py b/marimo/_sql/get_engines.pyindex db9d3fba68..fd3e3aebe2 100644--- a/marimo/_sql/get_engines.py+++ b/marimo/_sql/get_engines.py@@ -16,10 +16,7 @@ from marimo._sql.engines.clickhouse import (ClickhouseServer,)from marimo._sql.engines.dbapi import DBAPIEngine-from marimo._sql.engines.duckdb import (- INTERNAL_DUCKDB_ENGINE,- DuckDBEngine,-)+from marimo._sql.engines.duckdb import INTERNAL_DUCKDB_ENGINE, DuckDBEnginefrom marimo._sql.engines.ibis import IbisEnginefrom marimo._sql.engines.pyiceberg import PyIcebergEnginefrom marimo._sql.engines.redshift import RedshiftEnginediff --git a/marimo/_sql/utils.py b/marimo/_sql/utils.pyindex 213b97184e..c19c1e8dfc 100644--- a/marimo/_sql/utils.py+++ b/marimo/_sql/utils.py@@ -183,6 +183,51 @@ def is_explain_query(query: str) -> bool:return query.lstrip().lower().startswith("explain ")+def wrap_query_with_explain(query: str) -> str:+ """+ Wrap a SQL query with an EXPLAIN query if it is not already.++ If the query is just comments, return it. Executing this would return nothing.+ """+ if is_explain_query(query):+ return query++ return f"EXPLAIN {query}"+++def is_query_empty(query: str) -> bool:+ """Check if a SQL query is empty or just comments"""+ stripped = query.strip()+ if not stripped:+ return True++ # If the query starts with -- or /*, it's likely just comments+ if stripped.startswith("--") or stripped.startswith("/*"):+ import re++ # Remove /* */ comments+ no_block_comments = re.sub(r"/\*.*?\*/", "", query, flags=re.DOTALL)++ # Remove -- comments (just split on \n and check each line)+ lines = no_block_comments.split("\n")+ for line in lines:+ # Find first non-whitespace character+ for char in line:+ if char.isspace():+ continue+ elif char == "-":+ if line.strip().startswith("--"):+ break # This line is a comment, continue to next line+ else:+ return False # Found non-comment content+ else:+ return False # Found non-comment content+ return True++ # If it doesn't start with comment markers, it's not empty+ return False++def extract_explain_content(df: Any) -> str:"""Extract all content from a DataFrame for EXPLAIN queries.diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yamlindex d907f430dd..a9a07e4538 100644--- a/packages/openapi/api.yaml+++ b/packages/openapi/api.yaml@@ -2033,6 +2033,7 @@ components:- $ref: '#/components/schemas/SQLTablePreview'- $ref: '#/components/schemas/SQLTableListPreview'- $ref: '#/components/schemas/DataSourceConnections'+ - $ref: '#/components/schemas/ValidateSQLResult'- $ref: '#/components/schemas/SecretKeysResult'- $ref: '#/components/schemas/FocusCell'- $ref: '#/components/schemas/UpdateCellCodes'@@ -2067,6 +2068,7 @@ components:startup-logs: '#/components/schemas/StartupLogs'update-cell-codes: '#/components/schemas/UpdateCellCodes'update-cell-ids: '#/components/schemas/UpdateCellIdsRequest'+ validate-sql-result: '#/components/schemas/ValidateSQLResult'variable-values: '#/components/schemas/VariableValues'variables: '#/components/schemas/Variables'propertyName: op@@ -3428,6 +3430,43 @@ components:- valuestitle: UpdateComponentValuesRequesttype: object+ ValidateSQLRequest:+ description: Validate an SQL query+ properties:+ engine:+ type: string+ query:+ type: string+ requestId:+ type: string+ required:+ - requestId+ - engine+ - query+ title: ValidateSQLRequest+ type: object+ ValidateSQLResult:+ properties:+ error:+ anyOf:+ - type: string+ - type: 'null'+ default: null+ op:+ enum:+ - validate-sql-result+ request_id:+ type: string+ result:+ anyOf:+ - {}+ - type: 'null'+ default: null+ required:+ - op+ - request_id+ title: ValidateSQLResult+ type: objectVariableContext:properties:name:@@ -4399,6 +4438,20 @@ paths:schema:$ref: '#/components/schemas/ListSecretKeysResponse'description: List all secret keys+ /api/sql/validate:+ post:+ requestBody:+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/ValidateSQLRequest'+ responses:+ '200':+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/SuccessResponse'+ description: Validate an SQL query/api/status:get:responses:diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.tsindex 99eb630272..513872f3ac 100644--- a/packages/openapi/src/api.ts+++ b/packages/openapi/src/api.ts@@ -2353,6 +2353,45 @@ export interface paths {patch?: never;trace?: never;};+ "/api/sql/validate": {+ parameters: {+ query?: never;+ header?: never;+ path?: never;+ cookie?: never;+ };+ get?: never;+ put?: never;+ post: {+ parameters: {+ query?: never;+ header?: never;+ path?: never;+ cookie?: never;+ };+ requestBody?: {+ content: {+ "application/json": components["schemas"]["ValidateSQLRequest"];+ };+ };+ responses: {+ /** @description Validate an SQL query */+ 200: {+ headers: {+ [name: string]: unknown;+ };+ content: {+ "application/json": components["schemas"]["SuccessResponse"];+ };+ };+ };+ };+ delete?: never;+ options?: never;+ head?: never;+ patch?: never;+ trace?: never;+ };"/api/status": {parameters: {query?: never;@@ -3656,6 +3695,7 @@ export interface components {| components["schemas"]["SQLTablePreview"]| components["schemas"]["SQLTableListPreview"]| components["schemas"]["DataSourceConnections"]+ | components["schemas"]["ValidateSQLResult"]| components["schemas"]["SecretKeysResult"]| components["schemas"]["FocusCell"]| components["schemas"]["UpdateCellCodes"]@@ -4353,6 +4393,25 @@ export interface components {objectIds: string[];values: unknown[];};+ /**+ * ValidateSQLRequest+ * @description Validate an SQL query+ */+ ValidateSQLRequest: {+ engine: string;+ query: string;+ requestId: string;+ };+ /** ValidateSQLResult */+ ValidateSQLResult: {+ /** @default null */+ error?: string | null;+ /** @enum {unknown} */+ op: "validate-sql-result";+ request_id: string;+ /** @default null */+ result?: unknown | null;+ };/** VariableContext */VariableContext: {name: string;diff --git a/pyproject.toml b/pyproject.tomlindex 389881b375..03cb8362da 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -543,6 +543,7 @@ format_on_save = truemulti_column = trueperformant_table_charts = truesql_linter = true+sql_mode = true[tool.marimo.display]dataframes = "rich"diff --git a/tests/_server/api/endpoints/test_sql_endpoints.py b/tests/_server/api/endpoints/test_sql_endpoints.pynew file mode 100644index 0000000000..e69509b79a--- /dev/null+++ b/tests/_server/api/endpoints/test_sql_endpoints.py@@ -0,0 +1,43 @@+# Copyright 2024 Marimo. All rights reserved.+from __future__ import annotations++from typing import TYPE_CHECKING++from tests._server.mocks import token_header, with_read_session, with_session++if TYPE_CHECKING:+ from starlette.testclient import TestClient++SESSION_ID = "session-123"+HEADERS = {+ "Marimo-Session-Id": SESSION_ID,+ **token_header("fake-token"),+}+++@with_session(SESSION_ID)+def test_validate_sql(client: TestClient) -> None:+ response = client.post(+ "/api/sql/validate",+ headers=HEADERS,+ json={+ "requestId": "test_request_id",+ "engine": "test_engine",+ "query": "SELECT * FROM test",+ },+ )+ assert response.status_code == 200, response.text+++@with_read_session(SESSION_ID)+def test_fails_in_read_mode(client: TestClient) -> None:+ response = client.post(+ "/api/sql/validate",+ headers=HEADERS,+ json={+ "requestId": "test_request_id",+ "engine": "test_engine",+ "query": "SELECT * FROM test",+ },+ )+ assert response.status_code == 401diff --git a/tests/_sql/test_engines.py b/tests/_sql/test_engines.pyindex 9035bd4f0b..dfca9277cb 100644--- a/tests/_sql/test_engines.py+++ b/tests/_sql/test_engines.py@@ -273,6 +273,45 @@ def test_duckdb_execute() -> None:duckdb_conn.close()+ not HAS_DUCKDB or not HAS_POLARS,+ reason="Duckdb and polars not installed",+)+def test_duckdb_execute_in_explain_mode():+ """Test DuckDBEngine execute in explain mode."""+ import duckdb++ duckdb_conn = duckdb.connect(":memory:")+ engine = DuckDBEngine(duckdb_conn)++ # Test with valid query+ result, error = engine.execute_in_explain_mode("SELECT 1, 2")+ assert result is not None+ assert error is None++ # Test with invalid queries+ result, error = engine.execute_in_explain_mode(+ "SELECT * FROM non_existent_table" # catalog error+ )+ assert result is None+ assert error is not None++ result, error = engine.execute_in_explain_mode("SELECT * FROM ")+ assert result is None+ assert error is not None++ # Test with empty queries+ result, error = engine.execute_in_explain_mode("")+ assert result is None+ assert error is None++ result, error = engine.execute_in_explain_mode("-- SELECT * FROM ")+ assert result is None+ assert error is None++ duckdb_conn.close()++@pytest.mark.skipif(not HAS_SQLALCHEMY or not (HAS_PANDAS or HAS_POLARS),reason="SQLAlchemy and either pandas or polars not installed",@@ -305,6 +344,40 @@ def test_sqlalchemy_execute() -> None:assert engine.execute("PRAGMA journal_mode=WAL") is not None[email protected](not HAS_SQLALCHEMY, reason="SQLAlchemy not installed")+def test_sqlalchemy_execute_in_explain_mode():+ """Test SQLAlchemyEngine execute in explain mode."""+ import sqlalchemy as sa++ sqlite_engine = sa.create_engine("sqlite:///:memory:")+ engine = SQLAlchemyEngine(sqlite_engine)++ # Test with valid query+ result, error = engine.execute_in_explain_mode("SELECT 1, 2")+ assert result is not None+ assert error is None++ # Test with invalid queries+ result, error = engine.execute_in_explain_mode(+ "SELECT * FROM non_existent_table" # catalog error+ )+ assert result is None+ assert error is not None++ result, error = engine.execute_in_explain_mode("SELECT * FROM ")+ assert result is None+ assert error is not None++ # Test with empty queries+ result, error = engine.execute_in_explain_mode("")+ assert result is None+ assert error is None++ result, error = engine.execute_in_explain_mode("-- SELECT * FROM ")+ assert result is None+ assert error is None++def test_sql_type_to_data_type() -> None:"""Test sql_type_to_data_type function."""# Now that the raise statement is removed, we can test directlydiff --git a/tests/_sql/test_sql.py b/tests/_sql/test_sql.pyindex 2861384714..3726358569 100644--- a/tests/_sql/test_sql.py+++ b/tests/_sql/test_sql.py@@ -13,7 +13,12 @@ from marimo._plugins import uifrom marimo._sql.engines.ibis import IbisEnginefrom marimo._sql.engines.sqlalchemy import SQLAlchemyEnginefrom marimo._sql.sql import _query_includes_limit, sql-from marimo._sql.utils import extract_explain_content, is_explain_query+from marimo._sql.utils import (+ extract_explain_content,+ is_explain_query,+ is_query_empty,+ wrap_query_with_explain,+)if TYPE_CHECKING:from collections.abc import Generator@@ -513,3 +518,53 @@ logical_plan# Clean upduckdb.sql("DROP TABLE test_explain")++ def test_wrap_query_with_explain(self):+ """Test wrap_query_with_explain function."""+ assert (+ wrap_query_with_explain("SELECT * FROM t")+ == "EXPLAIN SELECT * FROM t"+ )+ assert (+ wrap_query_with_explain("EXPLAIN SELECT * FROM t")+ == "EXPLAIN SELECT * FROM t"+ )+ assert (+ wrap_query_with_explain("EXPLAIN (FORMAT JSON) SELECT * FROM t")+ == "EXPLAIN (FORMAT JSON) SELECT * FROM t"+ )+ assert (+ wrap_query_with_explain("EXPLAIN ANALYZE SELECT * FROM t")+ == "EXPLAIN ANALYZE SELECT * FROM t"+ )+ assert (+ wrap_query_with_explain("EXPLAIN QUERY PLAN SELECT * FROM t")+ == "EXPLAIN QUERY PLAN SELECT * FROM t"+ )++ def test_is_query_empty(self):+ """Test is_query_empty function."""++ assert is_query_empty("SELECT * FROM t") is False+ assert is_query_empty("INSERT INTO t VALUES (1)") is False+ assert is_query_empty("UPDATE t SET col = 1") is False+ assert is_query_empty("DELETE FROM t") is False+ assert is_query_empty("CREATE TABLE t (id INT)") is False+ assert is_query_empty("") is True+ assert is_query_empty(" ") is True++ # Comments+ assert is_query_empty("-- SELECT * FROM t") is True+ assert is_query_empty("/* SELECT * FROM t */") is True+ assert (+ is_query_empty("""+ -- SELECT * FROM t+ /* SELECT * FROM t */+ """)+ is True+ )+ assert is_query_empty(" -- some query with space") is True++ # Invalid SQL+ assert is_query_empty("NOT A VALID SQL QUERY") is False+ assert is_query_empty("SELECT * FROM t WHERE x = '") is False