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

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { effectiveDatabaseTypeForConnection, inferJdbcDialect } from "@/lib/database/jdbcDialect"; import { connectionQueryExecutionSchema, effectiveDatabaseTypeForConnection, inferJdbcDialect } from "@/lib/database/jdbcDialect";
describe("jdbc dialect inference", () => { describe("jdbc dialect inference", () => {
it("detects InterSystems IRIS and Caché JDBC connections", () => { it("detects InterSystems IRIS and Caché JDBC connections", () => {
@@ -47,3 +47,25 @@ describe("jdbc dialect inference", () => {
).toBe("sqlserver"); ).toBe("sqlserver");
}); });
}); });
describe("query execution schema", () => {
it.each(["spark", "hive"] as const)("uses the selected database as the %s execution schema", (dbType) => {
expect(connectionQueryExecutionSchema({ db_type: dbType }, "ai_test", undefined, false)).toBe("ai_test");
});
it("prefers an explicit schema for PostgreSQL", () => {
expect(connectionQueryExecutionSchema({ db_type: "postgres" }, "app", "reporting", false)).toBe("reporting");
});
it("does not send a schema for MySQL database context", () => {
expect(connectionQueryExecutionSchema({ db_type: "mysql" }, "app", undefined, false)).toBeUndefined();
});
it("does not change data-tab execution context", () => {
expect(connectionQueryExecutionSchema({ db_type: "spark" }, "ai_test", undefined, true)).toBeUndefined();
});
it("keeps generic JDBC Databend schema fallback", () => {
expect(connectionQueryExecutionSchema({ db_type: "jdbc", connection_string: "jdbc:databend://localhost:8000/default" }, "analytics", undefined, false)).toBe("analytics");
});
});
@@ -3,6 +3,8 @@ import { isSchemaAware, usesDatabaseObjectTreeMode, usesTreeSchemaMode } from "@
type JdbcDialectConnection = Pick<ConnectionConfig, "db_type"> & Partial<Pick<ConnectionConfig, "driver_profile" | "connection_string" | "jdbc_driver_class" | "jdbc_driver_paths">>; type JdbcDialectConnection = Pick<ConnectionConfig, "db_type"> & Partial<Pick<ConnectionConfig, "driver_profile" | "connection_string" | "jdbc_driver_class" | "jdbc_driver_paths">>;
const DATABASE_AS_EXECUTION_SCHEMA_TYPES = new Set<DatabaseType>(["hive", "spark"]);
const JDBC_DIALECT_MATCHERS: Array<{ type: DatabaseType; patterns: RegExp[] }> = [ const JDBC_DIALECT_MATCHERS: Array<{ type: DatabaseType; patterns: RegExp[] }> = [
{ type: "databend", patterns: [/jdbc:databend:/i, /com\.databend\.jdbc\.DatabendDriver/i, /databend-jdbc/i] }, { type: "databend", patterns: [/jdbc:databend:/i, /com\.databend\.jdbc\.DatabendDriver/i, /databend-jdbc/i] },
{ type: "starrocks", patterns: [/starrocks/i] }, { type: "starrocks", patterns: [/starrocks/i] },
@@ -56,6 +58,16 @@ export function connectionUsesSchemaExecutionContext(connection?: Pick<Connectio
return connection?.db_type === "jdbc" && inferJdbcDialect(connection) === "databend"; return connection?.db_type === "jdbc" && inferJdbcDialect(connection) === "databend";
} }
export function connectionQueryExecutionSchema(connection: JdbcDialectConnection | undefined, database: string | undefined, schema: string | undefined, dataMode: boolean): string | undefined {
if (connectionUsesSchemaExecutionContext(connection)) return schema || database || undefined;
if (dataMode || connectionUsesDatabaseObjectTreeMode(connection)) return undefined;
if (schema) return schema;
const type = effectiveDatabaseTypeForConnection(connection);
// Hive and Spark display their SQL namespace in the database selector, but
// their agents switch it with USE through the schema execution parameter.
return type && DATABASE_AS_EXECUTION_SCHEMA_TYPES.has(type) ? database || undefined : undefined;
}
export function connectionObjectTreeQuerySchema(connection: JdbcDialectConnection | undefined, database: string, schema?: string): string { export function connectionObjectTreeQuerySchema(connection: JdbcDialectConnection | undefined, database: string, schema?: string): string {
if (connection?.db_type === "jdbc" && inferJdbcDialect(connection) === "databend") return schema || database; if (connection?.db_type === "jdbc" && inferJdbcDialect(connection) === "databend") return schema || database;
if (connectionUsesDatabaseObjectTreeMode(connection)) return ""; if (connectionUsesDatabaseObjectTreeMode(connection)) return "";
+2 -2
View File
@@ -35,7 +35,7 @@ import { dataTabExecutionDatabase } from "@/lib/table/dataTabExecutionDatabase";
import { tableOpenPageLimit } from "@/lib/table/tableOpenPageLimit"; import { tableOpenPageLimit } from "@/lib/table/tableOpenPageLimit";
import { loadTableMetadata } from "@/lib/metadata/tableMetadataCache"; import { loadTableMetadata } from "@/lib/metadata/tableMetadataCache";
import { quoteTableIdentifier } from "@/lib/table/tableSelectSql"; import { quoteTableIdentifier } from "@/lib/table/tableSelectSql";
import { connectionUsesDatabaseObjectTreeMode, connectionUsesSchemaExecutionContext, effectiveDatabaseTypeForConnection, metadataSchemaForConnection } from "@/lib/database/jdbcDialect"; import { connectionQueryExecutionSchema, effectiveDatabaseTypeForConnection, metadataSchemaForConnection } from "@/lib/database/jdbcDialect";
import { frontendQueryTimeoutSecsForSql, queryTimeoutSecsForConnection } from "@/lib/sql/queryTimeout"; import { frontendQueryTimeoutSecsForSql, queryTimeoutSecsForConnection } from "@/lib/sql/queryTimeout";
import { queryResultSourceLabel } from "@/lib/sql/queryResultSource"; import { queryResultSourceLabel } from "@/lib/sql/queryResultSource";
import { sortDataGridRows, type DataGridSortDirection } from "@/lib/dataGrid/dataGridSort"; import { sortDataGridRows, type DataGridSortDirection } from "@/lib/dataGrid/dataGridSort";
@@ -2670,7 +2670,7 @@ export const useQueryStore = defineStore("query", () => {
pageOffset = options?.pagination?.offset ?? 0; pageOffset = options?.pagination?.offset ?? 0;
} }
const executionSchema = connectionUsesSchemaExecutionContext(conn) ? tab.schema || tab.database : tab.mode === "data" || connectionUsesDatabaseObjectTreeMode(conn) ? undefined : tab.schema; const executionSchema = connectionQueryExecutionSchema(conn, tab.database, tab.schema, tab.mode === "data");
const frontendTimeoutSecs = frontendQueryTimeoutSecsForSql(sqlToExecute, effectiveDbType, queryTimeoutSecs); const frontendTimeoutSecs = frontendQueryTimeoutSecsForSql(sqlToExecute, effectiveDbType, queryTimeoutSecs);
const sourceLabelDatabase = tab.database || conn?.database; const sourceLabelDatabase = tab.database || conn?.database;
+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 { function withConnectionHealthMock(handler: typeof fetch): typeof fetch {
return async (input, init) => { return async (input, init) => {
if (String(input) === "/api/connection/check-health") { 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 () => { test("data tab execution uses a tab-scoped client session", async () => {
const restoreStorage = installMemoryStorage(); const restoreStorage = installMemoryStorage();
setActivePinia(createPinia()); setActivePinia(createPinia());