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.

enhancement: default new SQL cells to an available connection (#10826)

Shahmir Varqhab7f6cf22cdf6
diff --git a/frontend/src/core/codemirror/language/__tests__/sql.test.ts b/frontend/src/core/codemirror/language/__tests__/sql.test.ts
index 78daea258b..de08befa82 100644
--- a/frontend/src/core/codemirror/language/__tests__/sql.test.ts
+++ b/frontend/src/core/codemirror/language/__tests__/sql.test.ts
@@ -17,6 +17,7 @@ import type {
import type { DataSourceConnection } from "@/core/datasets/data-source-connections";
import {
dataSourceConnectionsAtom,
+ exportedForTesting as dataSourceTesting,
setLatestEngineSelected,
} from "@/core/datasets/data-source-connections";
import { type ConnectionName, DUCKDB_ENGINE } from "@/core/datasets/engines";
@@ -550,8 +551,8 @@ _df = mo.sql(
const getLatestEngine = () =>
store.get(dataSourceConnectionsAtom).latestEngineSelected;
- it("should use default engine initially", () => {
- expect(getLatestEngine()).toBe(DUCKDB_ENGINE);
+ it("should have no selected engine initially", () => {
+ expect(getLatestEngine()).toBeNull();
});
it("should persist the selected engine", () => {
@@ -622,6 +623,73 @@ _df = mo.sql(
});
describe("defaultCode", () => {
+ it("should prefer the newest usable connection until an engine is selected", () => {
+ const previousState = store.get(dataSourceConnectionsAtom);
+ const connection: DataSourceConnection = {
+ name: "first_connection" as ConnectionName,
+ source: "sqlite",
+ display_name: "SQLite",
+ dialect: "sqlite",
+ databases: [],
+ };
+ const newestConnection = {
+ ...connection,
+ name: "newest_connection" as ConnectionName,
+ };
+ const icebergConnection = {
+ ...connection,
+ name: "catalog" as ConnectionName,
+ source: "iceberg",
+ };
+ try {
+ store.set(dataSourceConnectionsAtom, {
+ latestEngineSelected: null,
+ connectionsMap: new Map(),
+ });
+ expect(adapter.defaultMetadata.engine).toBe(DUCKDB_ENGINE);
+ expect(adapter.defaultCode).toBe('_df = mo.sql(f"""SELECT * FROM """)');
+
+ store.set(dataSourceConnectionsAtom, {
+ latestEngineSelected: null,
+ connectionsMap: new Map([
+ [connection.name, connection],
+ [newestConnection.name, newestConnection],
+ [icebergConnection.name, icebergConnection],
+ [DUCKDB_ENGINE, { ...connection, name: DUCKDB_ENGINE }],
+ ]),
+ });
+ expect(adapter.defaultMetadata.engine).toBe(newestConnection.name);
+ expect(adapter.defaultCode).toContain("engine=newest_connection");
+ expect(
+ store.get(dataSourceConnectionsAtom).latestEngineSelected,
+ ).toBeNull();
+
+ const refreshConnection = (connection: DataSourceConnection) => {
+ store.set(
+ dataSourceConnectionsAtom,
+ dataSourceTesting.reducer(store.get(dataSourceConnectionsAtom), {
+ type: "addDataSourceConnection",
+ payload: { connections: [connection] },
+ }),
+ );
+ };
+ refreshConnection(connection);
+ expect(adapter.defaultMetadata.engine).toBe(connection.name);
+ expect(adapter.defaultCode).toContain("engine=first_connection");
+
+ setLatestEngineSelected(connection.name);
+ refreshConnection(newestConnection);
+ expect(adapter.defaultCode).toContain("engine=first_connection");
+
+ setLatestEngineSelected(DUCKDB_ENGINE);
+ refreshConnection(connection);
+ expect(adapter.defaultMetadata.engine).toBe(DUCKDB_ENGINE);
+ expect(adapter.defaultCode).toBe('_df = mo.sql(f"""SELECT * FROM """)');
+ } finally {
+ store.set(dataSourceConnectionsAtom, previousState);
+ }
+ });
+
it("should include engine in defaultCode when selected", () => {
const engine = "postgres_engine" as ConnectionName;
setLatestEngineSelected(engine);
diff --git a/frontend/src/core/codemirror/language/languages/sql/sql.ts b/frontend/src/core/codemirror/language/languages/sql/sql.ts
index afbd23f624..b5da61be3b 100644
--- a/frontend/src/core/codemirror/language/languages/sql/sql.ts
+++ b/frontend/src/core/codemirror/language/languages/sql/sql.ts
@@ -76,7 +76,22 @@ export interface SQLLanguageAdapterMetadata extends SQLMetadata {
}
function getLatestEngine(): ConnectionName {
- return store.get(dataSourceConnectionsAtom).latestEngineSelected;
+ const { latestEngineSelected, connectionsMap } = store.get(
+ dataSourceConnectionsAtom,
+ );
+ if (latestEngineSelected !== null) {
+ return latestEngineSelected;
+ }
+
+ // Map insertion order prefers the most recently discovered connection.
+ const connections = [...connectionsMap.values()].reverse();
+ return (
+ connections.find(
+ (connection) =>
+ !INTERNAL_SQL_ENGINES.has(connection.name) &&
+ connection.source !== "iceberg",
+ )?.name ?? DUCKDB_ENGINE
+ );
}
/**
diff --git a/frontend/src/core/datasets/__tests__/data-source.test.ts b/frontend/src/core/datasets/__tests__/data-source.test.ts
index afc12f6fc8..f89ae63dc4 100644
--- a/frontend/src/core/datasets/__tests__/data-source.test.ts
+++ b/frontend/src/core/datasets/__tests__/data-source.test.ts
@@ -106,6 +106,7 @@ describe("data source connections", () => {
let newState = addConnection(connections, state);
expect(newState.connectionsMap.size).toBe(defaultConnSize + 2);
+ newState = { ...newState, latestEngineSelected: connections[0].name };
newState = reducer(newState, {
type: "removeDataSourceConnection",
@@ -113,6 +114,7 @@ describe("data source connections", () => {
});
expect(newState.connectionsMap.size).toBe(defaultConnSize + 1);
expect(newState.connectionsMap.has("conn2" as ConnectionName)).toBe(true);
+ expect(newState.latestEngineSelected).toBeNull();
});
it("can clear all connections", () => {
diff --git a/frontend/src/core/datasets/data-source-connections.ts b/frontend/src/core/datasets/data-source-connections.ts
index 3c8d9beed5..e976b3e8d8 100644
--- a/frontend/src/core/datasets/data-source-connections.ts
+++ b/frontend/src/core/datasets/data-source-connections.ts
@@ -44,7 +44,8 @@ export interface DataSourceConnection extends Omit<
export type ConnectionsMap = ReadonlyMap<ConnectionName, DataSourceConnection>;
export interface DataSourceState {
- latestEngineSelected: ConnectionName;
+ // Null permits automatic selection; DuckDB can also be an explicit choice.
+ latestEngineSelected: ConnectionName | null;
connectionsMap: ConnectionsMap;
}
@@ -110,7 +111,7 @@ function tableSchemaPath(sqlTableContext: SQLTableContext): string[] {
function initialState(): DataSourceState {
return {
- latestEngineSelected: DUCKDB_ENGINE,
+ latestEngineSelected: null,
connectionsMap: initialConnections,
};
}
@@ -136,6 +137,8 @@ const {
// Backend will dedupe by connection name & keep the latest, so we use this as the key
const newMap = new Map(connectionsMap);
for (const conn of opts.connections) {
+ // Refresh insertion order so automatic selection follows discovery recency.
+ newMap.delete(conn.name);
newMap.set(conn.name, conn);
}
@@ -161,16 +164,17 @@ const {
}),
);
return {
- // If the latest engine selected is not in the new map, use the default engine
- latestEngineSelected: newMap.has(latestEngineSelected)
- ? latestEngineSelected
- : DUCKDB_ENGINE,
+ // Allow automatic selection again when the selected connection disappears.
+ latestEngineSelected:
+ latestEngineSelected && newMap.has(latestEngineSelected)
+ ? latestEngineSelected
+ : null,
connectionsMap: newMap,
};
},
clearDataSourceConnections: (): DataSourceState => ({
- latestEngineSelected: DUCKDB_ENGINE,
+ latestEngineSelected: null,
connectionsMap: new Map(),
}),
@@ -183,9 +187,10 @@ const {
const newMap = new Map(connectionsMap);
newMap.delete(connectionName);
return {
- latestEngineSelected: newMap.has(latestEngineSelected)
- ? latestEngineSelected
- : DUCKDB_ENGINE,
+ latestEngineSelected:
+ latestEngineSelected && newMap.has(latestEngineSelected)
+ ? latestEngineSelected
+ : null,
connectionsMap: newMap,
};
},