fix(spark): apply selected database to queries
This commit is contained in:
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 { effectiveDatabaseTypeForConnection, inferJdbcDialect } from "@/lib/database/jdbcDialect";
|
||||
import { connectionQueryExecutionSchema, effectiveDatabaseTypeForConnection, inferJdbcDialect } from "@/lib/database/jdbcDialect";
|
||||
|
||||
describe("jdbc dialect inference", () => {
|
||||
it("detects InterSystems IRIS and Caché JDBC connections", () => {
|
||||
@@ -47,3 +47,25 @@ describe("jdbc dialect inference", () => {
|
||||
).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">>;
|
||||
|
||||
const DATABASE_AS_EXECUTION_SCHEMA_TYPES = new Set<DatabaseType>(["hive", "spark"]);
|
||||
|
||||
const JDBC_DIALECT_MATCHERS: Array<{ type: DatabaseType; patterns: RegExp[] }> = [
|
||||
{ type: "databend", patterns: [/jdbc:databend:/i, /com\.databend\.jdbc\.DatabendDriver/i, /databend-jdbc/i] },
|
||||
{ type: "starrocks", patterns: [/starrocks/i] },
|
||||
@@ -56,6 +58,16 @@ export function connectionUsesSchemaExecutionContext(connection?: Pick<Connectio
|
||||
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 {
|
||||
if (connection?.db_type === "jdbc" && inferJdbcDialect(connection) === "databend") return schema || database;
|
||||
if (connectionUsesDatabaseObjectTreeMode(connection)) return "";
|
||||
|
||||
Reference in new issue
Block a user