fix(spark): apply selected database to queries

This commit is contained in:
t8y2 committed 2026-07-10 21:45:13 +08:00
1 parent 982247c077
commit 53f2a4f215
4 files changed
+91 -3

No files matched your search

+54
View File
@@ -57,6 +57,14 @@ function sqlServerConn(id: string): ConnectionConfig {
};
}
function sparkConn(id: string): ConnectionConfig {
return {
...conn(id),
db_type: "spark",
port: 10000,
};
}
function withConnectionHealthMock(handler: typeof fetch): typeof fetch {
return async (input, init) => {
if (String(input) === "/api/connection/check-health") {
@@ -3693,6 +3701,52 @@ test("query execution is scoped to the tab client session", async () => {
}
});
test("Spark query execution applies the selected database as schema context", async () => {
const restoreStorage = installMemoryStorage();
setActivePinia(createPinia());
const connectionStore = useConnectionStore();
const store = useQueryStore();
const originalFetch = globalThis.fetch;
connectionStore.addEphemeralConnection(sparkConn("spark-1"));
const tabId = store.createTab("spark-1", "ai_test", "Query");
let executeBody: any;
globalThis.fetch = withConnectionHealthMock(async (input, init) => {
const url = String(input);
if (url === "/api/query/prepare-pagination-plan") {
return new Response(JSON.stringify({ sqlToExecute: "select * from user_orders_v2", useAgentResultSession: false }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
if (url === "/api/query/execute-multi") {
executeBody = JSON.parse(String(init?.body ?? "{}"));
return new Response(JSON.stringify([{ columns: ["order_id"], rows: [["ORD001"]], affected_rows: 0, execution_time_ms: 1 }]), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
if (url === "/api/query/analyze-editability") {
return new Response(JSON.stringify({ editable: false, reason: "complex-source" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
return new Response("unexpected request", { status: 500 });
});
try {
await store.executeTabSql(tabId, "select * from user_orders_v2");
assert.equal(executeBody.database, "ai_test");
assert.equal(executeBody.schema, "ai_test");
} finally {
globalThis.fetch = originalFetch;
restoreStorage();
}
});
test("data tab execution uses a tab-scoped client session", async () => {
const restoreStorage = installMemoryStorage();
setActivePinia(createPinia());