Skip to content

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.

fix sql validation errors not cleared on cell-language switch (#8388)

Shahmir Varqha679d14edc22d
diff --git a/frontend/src/core/codemirror/language/languages/sql/sql.ts b/frontend/src/core/codemirror/language/languages/sql/sql.ts
index 7acfce8283..ccf042fc6c 100644
--- a/frontend/src/core/codemirror/language/languages/sql/sql.ts
+++ b/frontend/src/core/codemirror/language/languages/sql/sql.ts
@@ -5,7 +5,12 @@ 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 { EditorView, keymap } from "@codemirror/view";
+import {
+ EditorView,
+ keymap,
+ ViewPlugin,
+ type ViewUpdate,
+} from "@codemirror/view";
import {
defaultSqlHoverTheme,
NodeSqlParser,
@@ -421,76 +426,85 @@ const SQL_VALIDATION_DEBOUNCE_MS = 300;
* Custom extension to run SQL queries in EXPLAIN mode on keypress.
*/
function sqlValidationExtension(): Extension {
- let debounceTimeout: number | undefined;
- let lastValidationRequest: string | null = null;
-
- return EditorView.updateListener.of((update) => {
- // Only run validation if the document has changed
- // The extension only runs on keypress, so we don't need to check for focus
- // This lets AI completions / external calls trigger validation
- if (!update.docChanged) {
- return;
- }
+ return ViewPlugin.define((view) => {
+ let debounceTimeout: number | undefined;
+ let lastValidationRequest: string | null = null;
+ const cellId = view.state.facet(cellIdState);
- const sqlMode = getSQLMode();
- if (sqlMode === "default") {
- return;
- }
+ return {
+ update(update: ViewUpdate) {
+ // Only run validation if the document has changed
+ // The extension only runs on keypress, so we don't need to check for focus
+ // This lets AI completions / external calls trigger validation
+ if (!update.docChanged) {
+ return;
+ }
- const metadata = getSQLMetadata(update.state);
- const connectionName = metadata.engine;
+ const sqlMode = getSQLMode();
+ if (sqlMode === "default") {
+ return;
+ }
- // Currently only DuckDB is supported
- if (!INTERNAL_SQL_ENGINES.has(connectionName)) {
- return;
- }
+ const metadata = getSQLMetadata(update.state);
+ const connectionName = metadata.engine;
- const doc = update.state.doc;
- const sqlContent = doc.toString();
+ // Currently only DuckDB is supported
+ if (!INTERNAL_SQL_ENGINES.has(connectionName)) {
+ return;
+ }
- // Clear existing timeout
- if (debounceTimeout) {
- window.clearTimeout(debounceTimeout);
- }
+ const sqlContent = update.state.doc.toString();
- // Debounce the validation call
- debounceTimeout = window.setTimeout(async () => {
- // Skip if content hasn't changed since last validation
- if (lastValidationRequest === sqlContent) {
- return;
- }
+ if (debounceTimeout) {
+ window.clearTimeout(debounceTimeout);
+ }
+
+ debounceTimeout = window.setTimeout(async () => {
+ // Skip if the SQL content has not changed
+ if (lastValidationRequest === sqlContent) {
+ return;
+ }
- lastValidationRequest = sqlContent;
- const cellId = update.view.state.facet(cellIdState);
+ lastValidationRequest = sqlContent;
- if (sqlContent === "") {
- clearSqlValidationError(cellId);
- return;
- }
-
- try {
- const dialect = connectionNameToParserDialect(connectionName);
- const result = await validateSQL(
- sqlContent,
- connectionName,
- dialect,
- sqlMode,
- );
- const validateResult = result.validate_result;
-
- if (validateResult?.error_message) {
- setSqlValidationError({
- cellId,
- errorMessage: validateResult.error_message,
- dialect,
- });
- } else {
- clearSqlValidationError(cellId);
+ if (sqlContent === "") {
+ clearSqlValidationError(cellId);
+ return;
+ }
+
+ try {
+ const dialect = connectionNameToParserDialect(connectionName);
+ const result = await validateSQL(
+ sqlContent,
+ connectionName,
+ dialect,
+ sqlMode,
+ );
+ const validateResult = result.validate_result;
+
+ if (validateResult?.error_message) {
+ setSqlValidationError({
+ cellId,
+ errorMessage: validateResult.error_message,
+ dialect,
+ });
+ } else {
+ clearSqlValidationError(cellId);
+ }
+ } catch (error) {
+ Logger.error("Failed to validate SQL", { error });
+ }
+ }, SQL_VALIDATION_DEBOUNCE_MS);
+ },
+
+ // Remove side-effects on plugin removal
+ destroy() {
+ if (debounceTimeout) {
+ window.clearTimeout(debounceTimeout);
}
- } catch (error) {
- Logger.error("Failed to validate SQL", { error });
- }
- }, SQL_VALIDATION_DEBOUNCE_MS);
+ clearSqlValidationError(cellId);
+ },
+ };
});
}
diff --git a/frontend/src/core/codemirror/language/panel/sql.tsx b/frontend/src/core/codemirror/language/panel/sql.tsx
index 8fb6654fac..a0a9cf40cb 100644
--- a/frontend/src/core/codemirror/language/panel/sql.tsx
+++ b/frontend/src/core/codemirror/language/panel/sql.tsx
@@ -5,7 +5,7 @@ import { useAtomValue } from "jotai";
import {
AlertCircle,
CircleHelpIcon,
- DatabaseBackup,
+ DatabaseZap,
SearchCheck,
} from "lucide-react";
import { getCellForDomProps } from "@/components/data-table/cell-utils";
@@ -160,7 +160,7 @@ export const SQLModeSelect: React.FC = () => {
return mode === "validate" ? (
<SearchCheck className="h-3 w-3" />
) : (
- <DatabaseBackup className="h-3 w-3" />
+ <DatabaseZap className="h-3 w-3" />
);
};
@@ -176,7 +176,7 @@ export const SQLModeSelect: React.FC = () => {
) : (
<div className="text-xs">
<div className="font-semibold mb-1 flex flex-row items-center gap-1">
- <DatabaseBackup className="h-3 w-3" />
+ <DatabaseZap className="h-3 w-3" />
Default Mode
</div>
<p>Standard editing</p>