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.

improvement: add completion info to SQL table completions (#6502)

Myles Scolnickff2ca9a96c01
diff --git a/frontend/src/core/codemirror/language/__tests__/sql.test.ts b/frontend/src/core/codemirror/language/__tests__/sql.test.ts
index 8da977601a..48124cddb1 100644
--- a/frontend/src/core/codemirror/language/__tests__/sql.test.ts
+++ b/frontend/src/core/codemirror/language/__tests__/sql.test.ts
@@ -735,16 +735,130 @@ describe("tablesCompletionSource", () => {
expect(completionSource?.schema).toMatchInlineSnapshot(`
{
"public": {
- "orders": [
- "order_id",
- "user_id",
- "total",
- ],
- "users": [
- "id",
- "name",
- "email",
- ],
+ "children": {
+ "orders": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "order_id",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "user_id",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "total",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "orders",
+ "type": "table",
+ },
+ },
+ "users": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "id",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "name",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "email",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "users",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "public",
+ "info": [Function],
+ "label": "public",
+ "type": "schema",
+ },
+ },
+ "test_db": {
+ "children": {
+ "public": {
+ "children": {
+ "orders": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "order_id",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "user_id",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "total",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "orders",
+ "type": "table",
+ },
+ },
+ "users": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "id",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "name",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "email",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "users",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db.public",
+ "info": [Function],
+ "label": "public",
+ "type": "schema",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db",
+ "info": [Function],
+ "label": "test_db",
+ "type": "database",
+ },
},
}
`);
@@ -829,17 +943,71 @@ describe("tablesCompletionSource", () => {
expect(completionSource?.schema).toMatchInlineSnapshot(`
{
"db1": {
- "schema1": {
- "table1": [
- "col1",
- ],
+ "children": {
+ "schema1": {
+ "children": {
+ "table1": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "table1",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "db1.schema1",
+ "info": [Function],
+ "label": "schema1",
+ "type": "schema",
+ },
+ },
+ },
+ "self": {
+ "detail": "db1",
+ "info": [Function],
+ "label": "db1",
+ "type": "database",
},
},
"db2": {
- "schema2": {
- "table2": [
- "col2",
- ],
+ "children": {
+ "schema2": {
+ "children": {
+ "table2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col2",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "table2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "db2.schema2",
+ "info": [Function],
+ "label": "schema2",
+ "type": "schema",
+ },
+ },
+ },
+ "self": {
+ "detail": "db2",
+ "info": [Function],
+ "label": "db2",
+ "type": "database",
},
},
}
@@ -975,29 +1143,179 @@ describe("tablesCompletionSource", () => {
);
expect(completionSource?.schema).toMatchInlineSnapshot(`
{
+ "db1": {
+ "children": {
+ "schema1": {
+ "children": {
+ "table1": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "table1",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "db1.schema1",
+ "info": [Function],
+ "label": "schema1",
+ "type": "schema",
+ },
+ },
+ "schema2": {
+ "children": {
+ "table2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col2",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "table2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "db1.schema2",
+ "info": [Function],
+ "label": "schema2",
+ "type": "schema",
+ },
+ },
+ },
+ "self": {
+ "detail": "db1",
+ "info": [Function],
+ "label": "db1",
+ "type": "database",
+ },
+ },
"db2": {
- "schema2": {
- "table2": [
- "col2",
- ],
+ "children": {
+ "schema2": {
+ "children": {
+ "table2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col2",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "table2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "db2.schema2",
+ "info": [Function],
+ "label": "schema2",
+ "type": "schema",
+ },
+ },
+ },
+ "self": {
+ "detail": "db2",
+ "info": [Function],
+ "label": "db2",
+ "type": "database",
},
},
"db3": {
- "schema2": {
- "table2": [
- "col2",
- ],
+ "children": {
+ "schema2": {
+ "children": {
+ "table2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col2",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "table2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "db3.schema2",
+ "info": [Function],
+ "label": "schema2",
+ "type": "schema",
+ },
+ },
+ },
+ "self": {
+ "detail": "db3",
+ "info": [Function],
+ "label": "db3",
+ "type": "database",
},
},
"schema1": {
- "table1": [
- "col1",
- ],
+ "children": {
+ "table1": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "table1",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "schema1",
+ "info": [Function],
+ "label": "schema1",
+ "type": "schema",
+ },
},
"schema2": {
- "table2": [
- "col2",
- ],
+ "children": {
+ "table2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col2",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "table2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "schema2",
+ "info": [Function],
+ "label": "schema2",
+ "type": "schema",
+ },
},
}
`);
@@ -1066,11 +1384,82 @@ describe("tablesCompletionSource", () => {
expect(completionSource?.schema).toMatchInlineSnapshot(`
{
"public": {
- "users": [
- "id",
- "name",
- "email",
- ],
+ "children": {
+ "users": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "id",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "name",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "email",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "users",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "public",
+ "info": [Function],
+ "label": "public",
+ "type": "schema",
+ },
+ },
+ "test_db": {
+ "children": {
+ "public": {
+ "children": {
+ "users": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "id",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "name",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "email",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "users",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db.public",
+ "info": [Function],
+ "label": "public",
+ "type": "schema",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db",
+ "info": [Function],
+ "label": "test_db",
+ "type": "database",
+ },
},
}
`);
@@ -1196,16 +1585,55 @@ describe("tablesCompletionSource", () => {
expect(completionSource?.dialect).toBe(PostgreSQL);
expect(completionSource?.schema).toMatchInlineSnapshot(`
{
+ "test_db": {
+ "children": {},
+ "self": {
+ "detail": "test_db",
+ "info": [Function],
+ "label": "test_db",
+ "type": "database",
+ },
+ },
"test_db2": {
- "orders": [
- "order_id",
+ "children": {
+ "orders": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "order_id",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "orders",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db2",
+ "info": [Function],
+ "label": "test_db2",
+ "type": "database",
+ },
+ },
+ "users": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "id",
+ "type": "column",
+ },
],
+ "self": {
+ "info": [Function],
+ "label": "users",
+ "type": "table",
+ },
},
- "users": [
- "id",
- ],
}
- `);
+ `);
});
it("should return local tables", () => {
@@ -1534,14 +1962,82 @@ describe("tablesCompletionSource", () => {
const completionSource = completionStore.getCompletionSource(TEST_ENGINE);
expect(completionSource?.schema).toMatchInlineSnapshot(`
{
- "dataset1": [
- "col1",
- "col2",
- ],
- "test_schema": {
- "dataset2": [
- "col1",
+ "dataset1": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "col2",
+ "type": "column",
+ },
],
+ "self": {
+ "info": [Function],
+ "label": "dataset1",
+ "type": "table",
+ },
+ },
+ "test_db": {
+ "children": {
+ "test_schema": {
+ "children": {
+ "dataset2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "dataset2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db.test_schema",
+ "info": [Function],
+ "label": "test_schema",
+ "type": "schema",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db",
+ "info": [Function],
+ "label": "test_db",
+ "type": "database",
+ },
+ },
+ "test_schema": {
+ "children": {
+ "dataset2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "dataset2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_schema",
+ "info": [Function],
+ "label": "test_schema",
+ "type": "schema",
+ },
},
}
`);
@@ -1555,13 +2051,66 @@ describe("tablesCompletionSource", () => {
const completionSource = completionStore.getCompletionSource(TEST_ENGINE);
expect(completionSource?.schema).toMatchInlineSnapshot(`
- {
- "test_schema": {
- "dataset2": [
- "col1",
- ],
- },
- }
+ {
+ "test_db": {
+ "children": {
+ "test_schema": {
+ "children": {
+ "dataset2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "dataset2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db.test_schema",
+ "info": [Function],
+ "label": "test_schema",
+ "type": "schema",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db",
+ "info": [Function],
+ "label": "test_db",
+ "type": "database",
+ },
+ },
+ "test_schema": {
+ "children": {
+ "dataset2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "dataset2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_schema",
+ "info": [Function],
+ "label": "test_schema",
+ "type": "schema",
+ },
+ },
+ }
`);
const newConnection: DataSourceConnection = {
@@ -1593,17 +2142,85 @@ describe("tablesCompletionSource", () => {
mockStore.set(datasetsAtom, { tables: testDatasets } as DatasetsState);
const completionSource = completionStore.getCompletionSource(TEST_ENGINE);
expect(completionSource?.schema).toMatchInlineSnapshot(`
- {
- "dataset1": [
- "col1",
- "col2",
- ],
- "test_schema": {
- "dataset2": [
- "col1",
- ],
- },
- }
+ {
+ "dataset1": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "col2",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "dataset1",
+ "type": "table",
+ },
+ },
+ "test_db": {
+ "children": {
+ "test_schema": {
+ "children": {
+ "dataset2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "dataset2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db.test_schema",
+ "info": [Function],
+ "label": "test_schema",
+ "type": "schema",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db",
+ "info": [Function],
+ "label": "test_db",
+ "type": "database",
+ },
+ },
+ "test_schema": {
+ "children": {
+ "dataset2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "dataset2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_schema",
+ "info": [Function],
+ "label": "test_schema",
+ "type": "schema",
+ },
+ },
+ }
`);
const newTestDatasets = [
@@ -1620,17 +2237,85 @@ describe("tablesCompletionSource", () => {
const newCompletionSource =
completionStore.getCompletionSource(TEST_ENGINE);
expect(newCompletionSource?.schema).toMatchInlineSnapshot(`
- {
- "dataset3": [
- "col1",
- "col2",
- ],
- "test_schema": {
- "dataset2": [
- "col1",
- ],
- },
- }
+ {
+ "dataset3": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ {
+ "info": [Function],
+ "label": "col2",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "dataset3",
+ "type": "table",
+ },
+ },
+ "test_db": {
+ "children": {
+ "test_schema": {
+ "children": {
+ "dataset2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "dataset2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db.test_schema",
+ "info": [Function],
+ "label": "test_schema",
+ "type": "schema",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_db",
+ "info": [Function],
+ "label": "test_db",
+ "type": "database",
+ },
+ },
+ "test_schema": {
+ "children": {
+ "dataset2": {
+ "children": [
+ {
+ "info": [Function],
+ "label": "col1",
+ "type": "column",
+ },
+ ],
+ "self": {
+ "info": [Function],
+ "label": "dataset2",
+ "type": "table",
+ },
+ },
+ },
+ "self": {
+ "detail": "test_schema",
+ "info": [Function],
+ "label": "test_schema",
+ "type": "schema",
+ },
+ },
+ }
`);
});
});
diff --git a/frontend/src/core/codemirror/language/languages/sql/completion-builder.ts b/frontend/src/core/codemirror/language/languages/sql/completion-builder.ts
new file mode 100644
index 0000000000..eadc4e5945
--- /dev/null
+++ b/frontend/src/core/codemirror/language/languages/sql/completion-builder.ts
@@ -0,0 +1,160 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import type { Completion } from "@codemirror/autocomplete";
+import type { SQLNamespace } from "@codemirror/lang-sql";
+import { createRoot } from "react-dom/client";
+import type {
+ Database,
+ DatabaseSchema,
+ DataTable,
+ DataTableColumn,
+} from "@/core/kernel/messages";
+import {
+ renderColumnInfo,
+ renderDatabaseInfo,
+ renderSchemaInfo,
+ renderTableInfo,
+} from "./renderers";
+
+/**
+ * Simple builder for SQL completion schemas.
+ */
+export class CompletionBuilder {
+ private schema: Record<string, SQLNamespace> = {};
+
+ /**
+ * Add a table with its columns at the specified path
+ */
+ addTable(path: string[], table: DataTable): this {
+ const tableNamespace: SQLNamespace = {
+ self: tableToCompletion({
+ table: table,
+ }),
+ children: table.columns.map((col) =>
+ columnToCompletion({
+ column: col,
+ }),
+ ),
+ };
+
+ this.setAt([...path, table.name], tableNamespace);
+ return this;
+ }
+
+ /**
+ * Add a schema at the specified path
+ */
+ addSchema(path: string[], schema: DatabaseSchema): this {
+ const schemaObject: SQLNamespace = {
+ self: schemaToCompletion({
+ namespace: schema,
+ path: path,
+ }),
+ children: {},
+ };
+
+ this.setAt(path, schemaObject);
+ return this;
+ }
+
+ /**
+ * Add a database at the specified path
+ */
+ addDatabase(path: string[], database: Database): this {
+ const databaseObject: SQLNamespace = {
+ self: databaseToCompletion({
+ namespace: database,
+ path: path,
+ }),
+ children: {},
+ };
+ this.setAt(path, databaseObject);
+ return this;
+ }
+
+ /**
+ * Set a value at a nested path, creating intermediate objects as needed
+ */
+ private setAt(path: string[], value: SQLNamespace): void {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ let current: any = this.schema;
+ for (const key of path.slice(0, -1)) {
+ if (!current[key]) {
+ current[key] = { children: {} };
+ }
+ current = current[key].children;
+ }
+ current[path[path.length - 1]] = value;
+ }
+
+ /**
+ * Build the final schema
+ */
+ build(): SQLNamespace {
+ return this.schema;
+ }
+
+ /**
+ * Reset for reuse
+ */
+ reset(): this {
+ this.schema = {};
+ return this;
+ }
+}
+
+function columnToCompletion(opts: { column: DataTableColumn }): Completion {
+ return {
+ label: opts.column.name,
+ type: "column",
+ info: () => {
+ const dom = document.createElement("div");
+ createRoot(dom).render(renderColumnInfo(opts.column));
+ return { dom: dom };
+ },
+ };
+}
+
+function tableToCompletion(opts: { table: DataTable }): Completion {
+ return {
+ label: opts.table.name,
+ type: "table",
+ info: () => {
+ const dom = document.createElement("div");
+ createRoot(dom).render(renderTableInfo(opts.table));
+ return { dom: dom };
+ },
+ };
+}
+
+function schemaToCompletion(opts: {
+ namespace: DatabaseSchema;
+ path: string[];
+}): Completion {
+ return {
+ label: opts.namespace.name,
+ detail: opts.path.join("."),
+ type: "schema",
+ info: () => {
+ const dom = document.createElement("div");
+ createRoot(dom).render(renderSchemaInfo(opts.namespace));
+ return { dom: dom };
+ },
+ };
+}
+
+function databaseToCompletion(opts: {
+ namespace: Database;
+ path: string[];
+}): Completion {
+ return {
+ label: opts.namespace.name,
+ detail: opts.path.join("."),
+ type: "database",
+ info: () => {
+ const dom = document.createElement("div");
+ createRoot(dom).render(renderDatabaseInfo(opts.namespace));
+ return { dom: dom };
+ },
+ };
+}
diff --git a/frontend/src/core/codemirror/language/languages/sql/completion-sources.tsx b/frontend/src/core/codemirror/language/languages/sql/completion-sources.tsx
index 5a279b250d..5f4c292635 100644
--- a/frontend/src/core/codemirror/language/languages/sql/completion-sources.tsx
+++ b/frontend/src/core/codemirror/language/languages/sql/completion-sources.tsx
@@ -7,6 +7,7 @@ import {
} from "@codemirror/lang-sql";
import type { EditorState } from "@codemirror/state";
import { DefaultSqlTooltipRenders } from "@marimo-team/codemirror-sql";
+import { once } from "@/utils/once";
import { languageMetadataField } from "../../metadata";
import { SCHEMA_CACHE } from "./completion-store";
import type { SQLLanguageAdapterMetadata } from "./sql";
@@ -28,7 +29,12 @@ export function tablesCompletionSource(): CompletionSource {
return null;
}
- return schemaCompletionSource(config)(ctx);
+ const completions = schemaCompletionSource(config)(ctx);
+ if (!completions) {
+ return null;
+ }
+
+ return completions;
};
}
@@ -84,7 +90,7 @@ export function customKeywordCompletionSource(): CompletionSource {
}
// e.g. lazily load keyword docs
-const getKeywordDocs = async (): Promise<Record<string, unknown>> => {
+const getKeywordDocs = once(async (): Promise<Record<string, unknown>> => {
const keywords = await import(
"@marimo-team/codemirror-sql/data/common-keywords.json"
);
@@ -96,4 +102,4 @@ const getKeywordDocs = async (): Promise<Record<string, unknown>> => {
...keywords.default.keywords,
...duckdbKeywords.default.keywords,
};
-};
+});
diff --git a/frontend/src/core/codemirror/language/languages/sql/completion-store.ts b/frontend/src/core/codemirror/language/languages/sql/completion-store.ts
index 80d8424db1..5aed29b6c4 100644
--- a/frontend/src/core/codemirror/language/languages/sql/completion-store.ts
+++ b/frontend/src/core/codemirror/language/languages/sql/completion-store.ts
@@ -1,6 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import type { SQLConfig, SQLDialect } from "@codemirror/lang-sql";
+import { atom } from "jotai";
import { isSchemaless } from "@/components/datasources/utils";
import { dataConnectionsMapAtom } from "@/core/datasets/data-source-connections";
import type { ConnectionName } from "@/core/datasets/engines";
@@ -8,14 +9,22 @@ import { datasetTablesAtom } from "@/core/datasets/state";
import type { DataSourceConnection } from "@/core/kernel/messages";
import { store } from "@/core/state/jotai";
import { LRUCache } from "@/utils/lru";
+import { CompletionBuilder } from "./completion-builder";
import { guessDialect, ModifiedStandardSQL } from "./utils";
-type TableToCols = Record<string, string[]>;
-type Schemas = Record<string, TableToCols>;
type CachedSchema = Pick<SQLConfig, "schema" | "defaultSchema"> & {
shouldAddLocalTables: boolean;
};
+const datasetTableCompletionsAtom = atom((get) => {
+ const tables = get(datasetTablesAtom);
+ const builder = new CompletionBuilder();
+ for (const table of tables) {
+ builder.addTable([], table);
+ }
+ return builder.build();
+});
+
class SQLCompletionStore {
private cache: LRUCache<DataSourceConnection, CachedSchema>;
@@ -33,81 +42,74 @@ class SQLCompletionStore {
}
private getConnectionSchema(connection: DataSourceConnection): CachedSchema {
- const schemaMap: Record<string, TableToCols> = {};
- const databaseMap: Record<string, Schemas> = {};
+ const { default_database, databases, default_schema } = connection;
+ const builder = new CompletionBuilder();
// When there is only one database, it is the default
- const defaultDb = connection.databases.find(
- (db) =>
- db.name === connection.default_database ||
- connection.databases.length === 1,
+ const defaultDb = databases.find(
+ (db) => db.name === default_database || databases.length === 1,
);
- const dbToVerify = defaultDb ?? connection.databases[0];
+ const dbToVerify = defaultDb ?? databases[0];
const isSchemalessDb =
dbToVerify?.schemas.some((schema) => isSchemaless(schema.name)) ?? false;
// For schemaless databases, treat databases as schemas
if (isSchemalessDb) {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- const dbToTablesMap: Record<string, any> = {};
-
- for (const db of connection.databases) {
+ for (const db of databases) {
const isDefaultDb = db.name === defaultDb?.name;
-
- for (const schema of db.schemas) {
- for (const table of schema.tables) {
- const columns = table.columns.map((col) => col.name);
-
- if (isDefaultDb) {
- // For default database, add tables directly to top level
- dbToTablesMap[table.name] = columns;
- } else {
- // Otherwise nest under database name
- dbToTablesMap[db.name] = dbToTablesMap[db.name] || {};
- dbToTablesMap[db.name][table.name] = columns;
- }
+ const tables = db.schemas.flatMap((schema) => schema.tables);
+ builder.addDatabase([db.name], db);
+
+ for (const table of tables) {
+ if (isDefaultDb) {
+ // For default database, add tables directly to top level
+ builder.addTable([], table);
+ } else {
+ // Otherwise nest under database name
+ builder.addTable([db.name], table);
}
}
}
return {
shouldAddLocalTables: false,
- schema: dbToTablesMap,
+ schema: builder.build(),
defaultSchema: defaultDb?.name,
};
}
- // For default db, we can use the schema name directly
- for (const schema of defaultDb?.schemas ?? []) {
- schemaMap[schema.name] = {};
- for (const table of schema.tables) {
- const columns = table.columns.map((col) => col.name);
- schemaMap[schema.name][table.name] = columns;
+ // For default db, we can use the schema name directly so add them to the top level
+ if (defaultDb) {
+ for (const schema of defaultDb.schemas) {
+ builder.addSchema([schema.name], schema);
+
+ for (const table of schema.tables) {
+ builder.addTable([schema.name], table);
+ }
}
}
// Otherwise, we need to use the fully qualified name
- for (const database of connection.databases) {
- if (database.name === defaultDb?.name) {
- continue;
- }
- databaseMap[database.name] = {};
+ for (const database of databases) {
+ // We still want to add the default database here in case
+ // users want fully qualified names for completions
+
+ builder.addDatabase([database.name], database);
for (const schema of database.schemas) {
- databaseMap[database.name][schema.name] = {};
+ builder.addSchema([database.name, schema.name], schema);
for (const table of schema.tables) {
- const columns = table.columns.map((col) => col.name);
- databaseMap[database.name][schema.name][table.name] = columns;
+ builder.addTable([database.name, schema.name], table);
}
}
}
return {
shouldAddLocalTables: true,
- schema: { ...databaseMap, ...schemaMap },
- defaultSchema: connection.default_schema ?? undefined,
+ schema: builder.build(),
+ defaultSchema: default_schema ?? undefined,
};
}
@@ -142,15 +144,9 @@ class SQLCompletionStore {
}
const getTablesMap = () => {
- const localTables = store.get(datasetTablesAtom);
// If there is a conflict with connection tables,
// the engine will prioritize the connection tables without special handling
- const tablesMap: TableToCols = {};
- for (const table of localTables) {
- const tableColumns = table.columns.map((col) => col.name);
- tablesMap[table.name] = tableColumns;
- }
- return tablesMap;
+ return store.get(datasetTableCompletionsAtom);
};
const schema = this.cache.getOrCreate(connection);
diff --git a/frontend/src/core/codemirror/language/languages/sql/renderers.tsx b/frontend/src/core/codemirror/language/languages/sql/renderers.tsx
new file mode 100644
index 0000000000..5e385a10fc
--- /dev/null
+++ b/frontend/src/core/codemirror/language/languages/sql/renderers.tsx
@@ -0,0 +1,485 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import {
+ ColumnsIcon,
+ DatabaseIcon,
+ EyeIcon,
+ HashIcon,
+ InfoIcon,
+ KeyIcon,
+ LayersIcon,
+ TableIcon,
+} from "lucide-react";
+import type React from "react";
+import { DATA_TYPE_ICON } from "@/components/datasets/icons";
+import { Badge } from "@/components/ui/badge";
+import type {
+ Database,
+ DatabaseSchema,
+ DataTable,
+ DataTableColumn,
+ DataType,
+} from "@/core/kernel/messages";
+
+// Configuration constants
+const PREVIEW_ITEM_LIMIT = 5;
+
+// Color mappings for data types (Tailwind-safe)
+const DATA_TYPE_COLORS: Record<DataType, string> = {
+ boolean: "bg-[var(--orange-4)] text-[var(--orange-11)]",
+ date: "bg-[var(--grass-4)] text-[var(--grass-11)]",
+ time: "bg-[var(--grass-4)] text-[var(--grass-11)]",
+ datetime: "bg-[var(--grass-4)] text-[var(--grass-11)]",
+ number: "bg-[var(--purple-4)] text-[var(--purple-11)]",
+ integer: "bg-[var(--purple-4)] text-[var(--purple-11)]",
+ string: "bg-[var(--blue-4)] text-[var(--blue-11)]",
+ unknown: "bg-[var(--slate-4)] text-[var(--slate-11)]",
+};
+
+// Source type colors
+const SOURCE_TYPE_COLORS = {
+ local: "bg-[var(--blue-4)] text-[var(--blue-11)]",
+ duckdb: "bg-[var(--amber-4)] text-[var(--amber-11)]",
+ connection: "bg-[var(--green-4)] text-[var(--green-11)]",
+ catalog: "bg-[var(--purple-4)] text-[var(--purple-11)]",
+} as const;
+
+const CONTAINER_STYLES = "p-3 min-w-[250px] flex flex-col divide-y";
+
+// Helper components and functions
+const SectionHeader: React.FC<{
+ icon: React.ReactNode;
+ title: string;
+ badge?: React.ReactNode;
+}> = ({ icon, title, badge }) => (
+ <div className="flex items-center gap-2 pb-2">
+ {icon}
+ <h3 className="font-semibold text-sm">{title}</h3>
+ {badge}
+ </div>
+);
+
+const MetadataRow: React.FC<{
+ label: string;
+ value: React.ReactNode;
+}> = ({ label, value }) => (
+ <div className="flex items-center justify-between text-xs">
+ <span className="text-[var(--slate-11)]">{label}:</span>
+ {value}
+ </div>
+);
+
+const StatisticItem: React.FC<{
+ icon: React.ReactNode;
+ text: string;
+}> = ({ icon, text }) => (
+ <div className="flex items-center gap-1">
+ {icon}
+ <span className="text-xs text-[var(--slate-11)]">{text}</span>
+ </div>
+);
+
+const PreviewList: React.FC<{
+ title?: string;
+ items: React.ReactNode[];
+ totalCount: number;
+ limit?: number;
+}> = ({ title = "", items, totalCount, limit = PREVIEW_ITEM_LIMIT }) => {
+ const visibleItems = items.slice(0, limit);
+ const hasMore = totalCount > limit;
+
+ return (
+ <div className="py-2">
+ {title && (
+ <h4 className="text-xs font-medium text-[var(--slate-11)] mb-2">
+ {title}:
+ </h4>
+ )}
+ <div className="flex flex-col gap-1 overflow-y-auto">
+ {visibleItems}
+ {hasMore && (
+ <div className="text-xs text-[var(--slate-10)] text-center py-1">
+ ... and {totalCount - limit} more
+ </div>
+ )}
+ </div>
+ </div>
+ );
+};
+
+const getDataTypeColorClass = (dataType: DataType): string => {
+ return DATA_TYPE_COLORS[dataType] || DATA_TYPE_COLORS.unknown;
+};
+
+export const renderTableInfo = (table: DataTable): React.ReactNode => {
+ const tableIcon =
+ table.type === "view" ? (
+ <EyeIcon className="w-4 h-4 text-[var(--blue-9)]" />
+ ) : (
+ <TableIcon className="w-4 h-4 text-[var(--green-9)]" />
+ );
+
+ const typeBadge = (
+ <Badge
+ variant="secondary"
+ className={`text-xs ${
+ table.type === "view"
+ ? "bg-[var(--blue-4)] text-[var(--blue-11)]"
+ : "bg-[var(--green-4)] text-[var(--green-11)]"
+ }`}
+ >
+ {table.type}
+ </Badge>
+ );
+
+ const columnItems = table.columns.map((column) => {
+ const TypeIcon = DATA_TYPE_ICON[column.type];
+ return (
+ <div
+ key={column.name}
+ className="flex items-center justify-between text-xs rounded"
+ >
+ <div className="flex items-center gap-2">
+ <TypeIcon className="w-3 h-3 text-[var(--slate-9)]" />
+ <span className="font-mono">{column.name}</span>
+ </div>
+ <Badge
+ variant="outline"
+ className={`text-xs ${getDataTypeColorClass(column.type)}`}
+ >
+ {column.type}
+ </Badge>
+ </div>
+ );
+ });
+
+ const hasPrimaryKeys = table.primary_keys && table.primary_keys.length > 0;
+ const hasIndexes = table.indexes && table.indexes.length > 0;
+
+ return (
+ <div className={`${CONTAINER_STYLES} min-w-[300px]`}>
+ <SectionHeader icon={tableIcon} title={table.name} badge={typeBadge} />
+
+ {/* Metadata */}
+ <div className="flex flex-col gap-2 py-2">
+ <MetadataRow
+ label="Source"
+ value={
+ <Badge
+ variant="outline"
+ className={`text-xs ${SOURCE_TYPE_COLORS[table.source_type]}`}
+ >
+ {table.source}
+ </Badge>
+ }
+ />
+
+ {table.variable_name && (
+ <MetadataRow
+ label="Variable"
+ value={
+ <code className="text-xs bg-[var(--slate-4)] px-1 rounded">
+ {table.variable_name}
+ </code>
+ }
+ />
+ )}
+
+ {table.engine && (
+ <MetadataRow
+ label="Engine"
+ value={
+ <code className="text-xs bg-[var(--slate-4)] px-1 rounded">
+ {table.engine}
+ </code>
+ }
+ />
+ )}
+ </div>
+
+ {/* Statistics */}
+ {(table.num_columns != null || table.num_rows != null) && (
+ <div className="grid grid-cols-2 gap-2 py-2">
+ {table.num_columns != null && (
+ <StatisticItem
+ icon={<ColumnsIcon className="w-3 h-3 text-[var(--slate-9)]" />}
+ text={`${table.num_columns} columns`}
+ />
+ )}
+ {table.num_rows != null && (
+ <StatisticItem
+ icon={<HashIcon className="w-3 h-3 text-[var(--slate-9)]" />}
+ text={`${table.num_rows} rows`}
+ />
+ )}
+ </div>
+ )}
+
+ {/* Empty Info */}
+ {table.columns.length === 0 && renderEmptyInfo("column")}
+
+ {/* Primary Keys & Indexes */}
+ {(hasPrimaryKeys || hasIndexes) && (
+ <div className="flex flex-col gap-2 py-2">
+ {hasPrimaryKeys && (
+ <div className="flex flex-row gap-1">
+ <div className="flex items-center gap-1">
+ <KeyIcon className="w-3 h-3 text-[var(--amber-9)]" />
+ <span className="text-xs font-medium text-[var(--slate-11)]">
+ Primary Keys:
+ </span>
+ </div>
+ {table.primary_keys?.map((key) => (
+ <Badge
+ key={key}
+ variant="outline"
+ className="text-xs text-[var(--slate-11)]"
+ >
+ {key}
+ </Badge>
+ ))}
+ </div>
+ )}
+
+ {hasIndexes && (
+ <div className="flex flex-row gap-1">
+ <div className="flex items-center gap-1 mb-1">
+ <LayersIcon className="w-3 h-3 text-[var(--purple-9)]" />
+ <span className="text-xs font-medium text-[var(--slate-11)]">
+ Indexes:
+ </span>
+ </div>
+ {table.indexes?.map((index) => (
+ <Badge
+ key={index}
+ variant="outline"
+ className="text-xs text-[var(--slate-11)]"
+ >
+ {index}
+ </Badge>
+ ))}
+ </div>
+ )}
+ </div>
+ )}
+
+ {/* Sample Columns Preview */}
+ {table.columns.length > 0 && (
+ <PreviewList items={columnItems} totalCount={table.columns.length} />
+ )}
+ </div>
+ );
+};
+
+export const renderColumnInfo = (column: DataTableColumn): React.ReactNode => {
+ const TypeIcon = DATA_TYPE_ICON[column.type];
+
+ const typeBadge = (
+ <Badge
+ variant="outline"
+ className={`text-xs ${getDataTypeColorClass(column.type)}`}
+ >
+ {column.type}
+ </Badge>
+ );
+
+ const sampleItems =
+ column.sample_values?.map((value, index) => (
+ <div
+ key={index}
+ className="text-xs bg-[var(--slate-3)] rounded font-mono"
+ >
+ {value === null || value === undefined ? "null" : String(value)}
+ </div>
+ )) || [];
+
+ return (
+ <div className={CONTAINER_STYLES}>
+ <SectionHeader
+ icon={<TypeIcon className="w-4 h-4 text-[var(--slate-9)]" />}
+ title={column.name}
+ badge={typeBadge}
+ />
+
+ {/* Type Information */}
+ <div className="flex flex-col gap-2 mt-2">
+ <MetadataRow
+ label="Type"
+ value={<span className="font-medium">{column.type}</span>}
+ />
+ <MetadataRow
+ label="External Type"
+ value={
+ <code className="text-xs bg-[var(--slate-4)] px-1 rounded">
+ {column.external_type}
+ </code>
+ }
+ />
+ </div>
+
+ {/* Sample Values */}
+ {column.sample_values && column.sample_values.length > 0 && (
+ <PreviewList
+ title="Sample Values"
+ items={sampleItems}
+ totalCount={column.sample_values.length}
+ />
+ )}
+ </div>
+ );
+};
+
+export const renderDatabaseInfo = (database: Database): React.ReactNode => {
+ const dialectBadge = (
+ <Badge
+ variant="outline"
+ className="text-xs bg-[var(--blue-4)] text-[var(--blue-11)]"
+ >
+ {database.dialect}
+ </Badge>
+ );
+
+ const schemaItems = database.schemas.map((schema) => (
+ <div
+ key={schema.name}
+ className="flex items-center justify-between text-xs rounded hover:bg-[var(--slate-3)]"
+ >
+ <div className="flex items-center gap-2">
+ <LayersIcon className="w-3 h-3 text-[var(--slate-9)]" />
+ <span>{schema.name}</span>
+ </div>
+ <Badge variant="outline" className="text-xs">
+ {schema.tables.length} tables
+ </Badge>
+ </div>
+ ));
+
+ return (
+ <div className={CONTAINER_STYLES}>
+ <SectionHeader
+ icon={<DatabaseIcon className="w-4 h-4 text-[var(--blue-9)]" />}
+ title={database.name}
+ badge={dialectBadge}
+ />
+ {/* Metadata */}
+ <div className="flex flex-col gap-2 py-2">
+ <MetadataRow
+ label="Dialect"
+ value={<span className="font-medium">{database.dialect}</span>}
+ />
+
+ {database.engine && (
+ <MetadataRow
+ label="Engine"
+ value={
+ <code className="text-xs bg-[var(--slate-4)] px-1 rounded">
+ {database.engine}
+ </code>
+ }
+ />
+ )}
+ </div>
+ {/* Schema Statistics */}
+ <div className="py-2">
+ <StatisticItem
+ icon={<LayersIcon className="w-3 h-3 text-[var(--slate-9)]" />}
+ text={`${database.schemas.length} schema${database.schemas.length === 1 ? "" : "s"}`}
+ />
+ </div>
+ {/* Empty Info */}
+ {database.schemas.length === 0 && renderEmptyInfo("schema")}
+
+ {/* Schema Preview */}
+ {database.schemas.length > 0 && (
+ <PreviewList
+ title="Schemas"
+ items={schemaItems}
+ totalCount={database.schemas.length}
+ />
+ )}
+ </div>
+ );
+};
+
+export const renderSchemaInfo = (schema: DatabaseSchema): React.ReactNode => {
+ const schemaBadge = (
+ <Badge
+ variant="outline"
+ className="text-xs bg-[var(--green-4)] text-[var(--green-11)]"
+ >
+ Schema
+ </Badge>
+ );
+
+ const tableItems = schema.tables.map((table) => (
+ <div
+ key={table.name}
+ className="flex items-center justify-between text-xs rounded hover:bg-[var(--slate-3)]"
+ >
+ <div className="flex items-center gap-2">
+ {table.type === "view" ? (
+ <EyeIcon className="w-3 h-3 text-[var(--blue-9)]" />
+ ) : (
+ <TableIcon className="w-3 h-3 text-[var(--green-9)]" />
+ )}
+ <span>{table.name}</span>
+ </div>
+ <Badge
+ variant="outline"
+ className={`text-xs ${
+ table.type === "view"
+ ? "bg-[var(--blue-4)] text-[var(--blue-11)]"
+ : "bg-[var(--green-4)] text-[var(--green-11)]"
+ }`}
+ >
+ {table.type}
+ </Badge>
+ </div>
+ ));
+
+ return (
+ <div className={CONTAINER_STYLES}>
+ <SectionHeader
+ icon={<LayersIcon className="w-4 h-4 text-[var(--green-9)]" />}
+ title={schema.name}
+ badge={schemaBadge}
+ />
+
+ {/* Table Statistics */}
+ <div className="py-2">
+ <StatisticItem
+ icon={<TableIcon className="w-3 h-3 text-[var(--slate-9)]" />}
+ text={`${schema.tables.length} table${schema.tables.length === 1 ? "" : "s"}`}
+ />
+ </div>
+
+ {/* Empty Info */}
+ {schema.tables.length === 0 && renderEmptyInfo("table")}
+
+ {/* Table Preview */}
+ {schema.tables.length > 0 && (
+ <PreviewList
+ title="Tables"
+ items={tableItems}
+ totalCount={schema.tables.length}
+ />
+ )}
+ </div>
+ );
+};
+
+export const renderEmptyInfo = (
+ type: "column" | "table" | "schema" | "database",
+) => {
+ return (
+ <div className="flex items-start gap-2 mt-3">
+ <InfoIcon size={10} className="mt-1 text-[var(--slate-10)] shrink-0" />
+ <span className="text-xs text-[var(--slate-11)]">
+ No {type} information available.{" \n"}
+ <span className="text-[var(--blue-10)]">
+ Introspect to see more details.
+ </span>
+ </span>
+ </div>
+ );
+};
diff --git a/frontend/src/core/codemirror/language/languages/sql/utils.ts b/frontend/src/core/codemirror/language/languages/sql/utils.ts
index b85e18a327..80143e69f9 100644
--- a/frontend/src/core/codemirror/language/languages/sql/utils.ts
+++ b/frontend/src/core/codemirror/language/languages/sql/utils.ts
@@ -18,8 +18,11 @@ import {
} from "@marimo-team/codemirror-sql/dialects";
import type { DataSourceConnection } from "@/core/kernel/messages";
+/**
+ * Guess the CodeMirror SQL dialect from the backend connection dialect.
+ */
export function guessDialect(
- connection: DataSourceConnection,
+ connection: Pick<DataSourceConnection, "dialect">,
): SQLDialect | undefined {
switch (connection.dialect) {
case "postgresql":