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: add completions from local dfs (#4444)

Shahmir Varqha115ef86f4944
diff --git a/frontend/src/core/codemirror/language/__tests__/sql.test.ts b/frontend/src/core/codemirror/language/__tests__/sql.test.ts
index db62be4d1c..14fa6111d5 100644
--- a/frontend/src/core/codemirror/language/__tests__/sql.test.ts
+++ b/frontend/src/core/codemirror/language/__tests__/sql.test.ts
@@ -9,6 +9,8 @@ import {
} from "@/core/datasets/data-source-connections";
import type { DataSourceConnection } from "@/core/kernel/messages";
import { PostgreSQL } from "@codemirror/lang-sql";
+import { datasetsAtom } from "@/core/datasets/state";
+import type { DatasetsState } from "@/core/datasets/types";
const adapter = new SQLLanguageAdapter();
@@ -600,6 +602,10 @@ describe("tablesCompletionSource", () => {
beforeEach(() => {
// Reset the adapter engine
adapter.engine = DUCKDB_ENGINE;
+ // reset the datasets state
+ mockStore.set(datasetsAtom, {
+ tables: [],
+ } as unknown as DatasetsState);
});
it("should return null if connection not found", () => {
@@ -1172,4 +1178,77 @@ describe("tablesCompletionSource", () => {
}
`);
});
+
+ it("should return local tables", () => {
+ const testDatasets = [
+ {
+ name: "dataset1",
+ columns: [
+ { name: "col1", type: "number" },
+ { name: "col2", type: "string" },
+ ],
+ },
+ ];
+ mockStore.set(datasetsAtom, { tables: testDatasets } as DatasetsState);
+
+ const mockConnection: DataSourceConnection = {
+ name: "test_engine",
+ dialect: "duckdb",
+ display_name: "duckdb",
+ default_database: "test_db",
+ default_schema: "test_schema",
+ source: "duckdb",
+ databases: [
+ {
+ dialect: "duckdb",
+ name: "test_db",
+ schemas: [
+ {
+ name: "test_schema",
+ tables: [
+ {
+ name: "dataset2",
+ source: "duckdb",
+ source_type: "local",
+ type: "table",
+ columns: [
+ {
+ name: "col1",
+ external_type: "string",
+ type: "string",
+ sample_values: [],
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ };
+ mockStore.set(dataSourceConnectionsAtom, {
+ connectionsMap: new Map([
+ ["test_engine" as ConnectionName, mockConnection],
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ ]) as any,
+ latestEngineSelected: "test_engine" as ConnectionName,
+ });
+
+ const completionSource = completionStore.getCompletionSource(
+ "test_engine" as ConnectionName,
+ );
+ expect(completionSource?.schema).toMatchInlineSnapshot(`
+ {
+ "dataset1": [
+ "col1",
+ "col2",
+ ],
+ "test_schema": {
+ "dataset2": [
+ "col1",
+ ],
+ },
+ }
+ `);
+ });
});
diff --git a/frontend/src/core/codemirror/language/sql.ts b/frontend/src/core/codemirror/language/sql.ts
index ed092fc19f..1eefb2d416 100644
--- a/frontend/src/core/codemirror/language/sql.ts
+++ b/frontend/src/core/codemirror/language/sql.ts
@@ -39,6 +39,7 @@ import {
import { LRUCache } from "@/utils/lru";
import type { DataSourceConnection } from "@/core/kernel/messages";
import { isSchemaless } from "@/components/datasources/utils";
+import { datasetTablesAtom } from "@/core/datasets/state";
/**
* Language adapter for SQL.
@@ -202,6 +203,16 @@ export class SQLCompletionStore {
return null;
}
+ 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;
+ }
+
let cacheConfig: SQLConfig | undefined = this.cache.get(connection);
if (!cacheConfig) {
const schemaMap: Record<string, TableToCols> = {};
@@ -286,7 +297,7 @@ export class SQLCompletionStore {
cacheConfig = {
...baseConfig,
- schema: { ...databaseMap, ...schemaMap },
+ schema: { ...databaseMap, ...schemaMap, ...tablesMap },
defaultSchema: connection.default_schema ?? undefined,
};
this.cache.set(connection, cacheConfig);
diff --git a/frontend/src/core/datasets/data-source-connections.ts b/frontend/src/core/datasets/data-source-connections.ts
index 9eff20cb0a..658f2d8c65 100644
--- a/frontend/src/core/datasets/data-source-connections.ts
+++ b/frontend/src/core/datasets/data-source-connections.ts
@@ -261,8 +261,10 @@ export const exportedForTesting = {
initialState,
};
-// If you need to get table names from all connections & local datasets, use this atom
-// When a table name is used in multiple connections, we need to use a more qualified name
+/**
+ * If you need to get table names from all connections & local datasets, use this atom.
+ * Uses a more qualified name if there are collisions.
+ */
export const allTablesAtom = atom((get) => {
const datasets = store.get(datasetTablesAtom);
const connections = get(dataSourceConnectionsAtom).connectionsMap;