From abee51d62505931ec21ecc4c60df22500d9ee954 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Fri, 10 Jul 2026 15:48:56 +0800 Subject: [PATCH] fix(doris): use internal database for external catalogs --- apps/desktop/src/components/grid/DataGrid.vue | 11 +++--- .../src/components/layout/ContentArea.vue | 3 ++ .../src/composables/useDataGridExport.ts | 4 +- .../table/dataTabExecutionDatabase.spec.ts | 39 +++++++++++++++++++ .../src/lib/table/dataTabExecutionDatabase.ts | 15 +++++++ apps/desktop/src/stores/queryStore.ts | 23 +++++++---- 6 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 apps/desktop/src/lib/__tests__/table/dataTabExecutionDatabase.spec.ts create mode 100644 apps/desktop/src/lib/table/dataTabExecutionDatabase.ts diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue index 03ecef0080..f2cf7caf15 100644 --- a/apps/desktop/src/components/grid/DataGrid.vue +++ b/apps/desktop/src/components/grid/DataGrid.vue @@ -201,6 +201,7 @@ interface DataGridProps { databaseType?: DatabaseType; connectionId?: string; database?: string; + executionDatabase?: string; schema?: string; context?: "results" | "table-data"; sourceColumns?: Array; @@ -1353,7 +1354,7 @@ async function loadServerFilterValues(columnIndex: number, searchValue: string) limit: SERVER_COLUMN_FILTER_LIMIT, includeCounts: true, }); - const result = await api.executeQuery(props.connectionId, props.database ?? "", sql, tableMeta.schema ?? props.schema, undefined, { + const result = await api.executeQuery(props.connectionId, props.executionDatabase ?? props.database ?? "", sql, tableMeta.schema ?? props.schema, undefined, { maxRows: SERVER_COLUMN_FILTER_LIMIT, fetchSize: SERVER_COLUMN_FILTER_LIMIT, pageSize: SERVER_COLUMN_FILTER_LIMIT, @@ -3775,7 +3776,7 @@ async function lastPage() { const sql = countTarget?.sql; if (!sql) return; try { - const result = await api.executeQuery(props.connectionId, props.database ?? "", sql, countTarget.schema); + const result = await api.executeQuery(props.connectionId, props.executionDatabase ?? props.database ?? "", sql, countTarget.schema); const total = Number(result.rows?.[0]?.[0] ?? 0); if (total <= 0) return; const lastPageNum = Math.ceil(total / pageSize.value); @@ -3809,7 +3810,7 @@ async function calculateTotalRowCount() { try { const countTarget = await buildCurrentCountTarget(); if (!countTarget?.sql) return; - const result = await api.executeQuery(props.connectionId, props.database ?? "", countTarget.sql, countTarget.schema); + const result = await api.executeQuery(props.connectionId, props.executionDatabase ?? props.database ?? "", countTarget.sql, countTarget.schema); const total = Number(result.rows?.[0]?.[0] ?? 0); if (Number.isFinite(total) && total >= 0) { manualTotalRowCount.value = total; @@ -3867,7 +3868,7 @@ const editor = useDataGridEditor({ editable: computed(() => props.editable), databaseType: computed(() => props.databaseType), connectionId: computed(() => props.connectionId), - database: computed(() => props.database), + database: computed(() => props.executionDatabase ?? props.database), tableMeta: computed(() => props.tableMeta), sourceColumns: computed(() => props.sourceColumns), canEditExistingRows, @@ -6188,7 +6189,7 @@ const { copyInsertTargetLabel: computed(() => props.tableMeta?.tableName ?? props.customSaveHandler?.targetLabel), databaseType: computed(() => props.databaseType), connectionId: computed(() => props.connectionId), - database: computed(() => props.database), + database: computed(() => props.executionDatabase ?? props.database), context: computed(() => props.context), sourceColumns: visibleSourceColumns, columnTypes: visibleColumnTypes, diff --git a/apps/desktop/src/components/layout/ContentArea.vue b/apps/desktop/src/components/layout/ContentArea.vue index d3925ebfa3..7c64b79a0c 100644 --- a/apps/desktop/src/components/layout/ContentArea.vue +++ b/apps/desktop/src/components/layout/ContentArea.vue @@ -63,6 +63,7 @@ import { defaultQueryResultArchiveFileName } from "@/lib/query/queryResultArchiv import { saveQueryResultArchiveFile } from "@/lib/query/queryResultArchiveFile"; import { isTableDataEditable } from "@/lib/table/tableEditing"; import { tableMetaForDataTab } from "@/lib/table/tableDataTabMeta"; +import { dataTabExecutionDatabase } from "@/lib/table/dataTabExecutionDatabase"; import { formatShortcut } from "@/lib/editor/shortcutRegistry"; import { effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect"; import { chartableColumnIndexes } from "@/lib/dataGrid/chartData"; @@ -197,6 +198,7 @@ const objectBrowserRef = ref(); const activeTableMeta = computed(() => props.activeTab.tableMeta); const activeDataTabTableMeta = computed(() => tableMetaForDataTab(props.activeTab)); const activeEffectiveDatabaseType = computed(() => effectiveDatabaseTypeForConnection(props.activeConnection)); +const activeDataTabExecutionDatabase = computed(() => dataTabExecutionDatabase(props.activeConnection, props.activeTab.database, activeDataTabTableMeta.value?.catalog)); function findNodeInTree(nodes: TreeNode[], id: string): TreeNode | undefined { for (const node of nodes) { @@ -1388,6 +1390,7 @@ defineExpose({ focusSearch, refreshData, handleModRTarget, requestQueryEditorExe :database-type="activeEffectiveDatabaseType" :connection-id="activeTab.connectionId" :database="activeTab.database" + :execution-database="activeDataTabExecutionDatabase" :table-meta="activeDataTabTableMeta" :table-info-tab="activeTab.tableInfoTab" :page-offset="activeTab.resultPageOffset" diff --git a/apps/desktop/src/composables/useDataGridExport.ts b/apps/desktop/src/composables/useDataGridExport.ts index 3fc1aa01a7..3a106acf78 100644 --- a/apps/desktop/src/composables/useDataGridExport.ts +++ b/apps/desktop/src/composables/useDataGridExport.ts @@ -886,7 +886,9 @@ export function useDataGridExport(options: UseDataGridExportOptions) { async function exportFullTableDataViaBackend(format: "csv" | "xlsx" | "json" | "markdown" | "sql", rowIds?: number[]): Promise { const meta = tableMeta.value; - if (rowIds !== undefined || context.value !== "table-data" || !meta || !connectionId.value || !database.value) { + // The backend table exporter currently builds two-part table names. External + // Doris/StarRocks catalogs need the data-tab paginator's three-part SQL. + if (rowIds !== undefined || context.value !== "table-data" || !meta || meta.catalog || !connectionId.value || !database.value) { return false; } diff --git a/apps/desktop/src/lib/__tests__/table/dataTabExecutionDatabase.spec.ts b/apps/desktop/src/lib/__tests__/table/dataTabExecutionDatabase.spec.ts new file mode 100644 index 0000000000..ea788155fd --- /dev/null +++ b/apps/desktop/src/lib/__tests__/table/dataTabExecutionDatabase.spec.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from "vitest"; +import { dataTabExecutionDatabase } from "@/lib/table/dataTabExecutionDatabase"; +import type { ConnectionConfig } from "@/types/database"; + +function connection(overrides: Partial): ConnectionConfig { + return { + id: "connection-1", + name: "Test", + db_type: "mysql", + host: "localhost", + port: 3306, + username: "root", + password: "", + ...overrides, + }; +} + +describe("dataTabExecutionDatabase", () => { + it("uses the configured database for a Doris external catalog", () => { + expect(dataTabExecutionDatabase(connection({ db_type: "doris", database: "yunye" }), "default", "iceberg_catalog")).toBe("yunye"); + }); + + it("allows no session database for a Doris external catalog", () => { + expect(dataTabExecutionDatabase(connection({ db_type: "doris" }), "default", "iceberg_catalog")).toBe(""); + }); + + it("uses the configured database for a StarRocks external catalog", () => { + expect(dataTabExecutionDatabase(connection({ db_type: "starrocks", database: "analytics" }), "default", "iceberg_catalog")).toBe("analytics"); + }); + + it("recognizes Doris JDBC connections", () => { + expect(dataTabExecutionDatabase(connection({ db_type: "jdbc", connection_string: "jdbc:mysql://localhost:9030/db?product=doris", database: "internal_db" }), "default", "iceberg_catalog")).toBe("internal_db"); + }); + + it("keeps the tab database for internal and ordinary tables", () => { + expect(dataTabExecutionDatabase(connection({ db_type: "doris", database: "configured" }), "internal_db")).toBe("internal_db"); + expect(dataTabExecutionDatabase(connection({ db_type: "mysql", database: "configured" }), "tenant_db", "catalog_like_value")).toBe("tenant_db"); + }); +}); diff --git a/apps/desktop/src/lib/table/dataTabExecutionDatabase.ts b/apps/desktop/src/lib/table/dataTabExecutionDatabase.ts new file mode 100644 index 0000000000..f47d01c0d5 --- /dev/null +++ b/apps/desktop/src/lib/table/dataTabExecutionDatabase.ts @@ -0,0 +1,15 @@ +import type { ConnectionConfig } from "@/types/database"; +import { connectionIsDorisFamilyCatalogCapable } from "@/lib/database/databaseFeatureSupport"; +import { effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect"; + +export function dataTabExecutionDatabase(connection: ConnectionConfig | undefined, tabDatabase: string, catalog?: string): string { + if (!catalog?.trim()) return tabDatabase; + + const databaseType = effectiveDatabaseTypeForConnection(connection); + const usesExternalCatalog = connectionIsDorisFamilyCatalogCapable(connection) || databaseType === "doris" || databaseType === "starrocks"; + if (!usesExternalCatalog) return tabDatabase; + + // External catalog namespaces qualify the SQL object name, but they are not + // valid MySQL-protocol session databases. Keep the configured internal DB. + return connection?.database ?? ""; +} diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts index 30c03cfef2..ee2a9c726d 100644 --- a/apps/desktop/src/stores/queryStore.ts +++ b/apps/desktop/src/stores/queryStore.ts @@ -30,6 +30,7 @@ import { usesAgentCursorForQuery } from "@/lib/database/databaseDriverManifest"; import { canUseKeylessRowPredicate } from "@/lib/table/tableEditing"; import { TABLE_DATA_EXPORT_PAGE_SIZE } from "@/lib/table/tableDataExport"; import { tableMetaForDataTab } from "@/lib/table/tableDataTabMeta"; +import { dataTabExecutionDatabase } from "@/lib/table/dataTabExecutionDatabase"; import { tableOpenPageLimit } from "@/lib/table/tableOpenPageLimit"; import { loadTableMetadata } from "@/lib/metadata/tableMetadataCache"; import { quoteTableIdentifier } from "@/lib/table/tableSelectSql"; @@ -349,7 +350,10 @@ export const useQueryStore = defineStore("query", () => { const sessionId = tab?.resultSessionId ?? tab?.result?.session_id; if (!tab || !sessionId || sessionId === preserveSessionId) return; try { - await api.closeQuerySession(tab.connectionId, tab.database, sessionId, tab.id); + const catalog = tab.mode === "data" ? tab.tableMeta?.catalog : undefined; + const connection = catalog ? useConnectionStore().getConfig(tab.connectionId) : undefined; + const executionDatabase = dataTabExecutionDatabase(connection, tab.database, catalog); + await api.closeQuerySession(tab.connectionId, executionDatabase, sessionId, tab.id); } catch (error) { console.warn("[DBX][query-session:close:error]", { tabId: tab.id, sessionId, error }); } finally { @@ -368,9 +372,12 @@ export const useQueryStore = defineStore("query", () => { async function closeClientConnectionSession(tab: QueryTab | undefined) { if (!tab?.connectionId) return; + const catalog = tab.mode === "data" ? tab.tableMeta?.catalog : undefined; + const connection = catalog ? useConnectionStore().getConfig(tab.connectionId) : undefined; + const executionDatabase = dataTabExecutionDatabase(connection, tab.database, catalog); const clientSessionIds = [tabClientSessionId(tab), ...BACKGROUND_CLIENT_SESSION_SUFFIXES.map((suffix) => tabClientSessionId(tab, suffix))]; for (const clientSessionId of clientSessionIds) { - await closeClientSessionId(tab.connectionId, tab.database, clientSessionId, { tabId: tab.id }); + await closeClientSessionId(tab.connectionId, executionDatabase, clientSessionId, { tabId: tab.id }); } } @@ -2231,6 +2238,7 @@ export const useQueryStore = defineStore("query", () => { mongoCommands = splitMongoCommandRanges(sql); } const effectiveDbType = effectiveDatabaseTypeForConnection(conn); + const executionDatabase = dataTabExecutionDatabase(conn, tab.database, tab.mode === "data" ? tab.tableMeta?.catalog : undefined); const useAgentCursor = usesAgentCursorForQuery(conn?.db_type); const queryTimeoutSecs = queryTimeoutSecsForConnection(conn); const settingsStore = useSettingsStore(); @@ -2548,11 +2556,11 @@ export const useQueryStore = defineStore("query", () => { if (tab.autoCommit === false) { if (!tab.txnSessionId) { console.info("[DBX][executeTabSql:begin-manual-txn:start]", { traceId, elapsed: elapsed() }); - tab.txnSessionId = await api.beginManualTransaction(tab.connectionId, tab.database, executionSchema); + tab.txnSessionId = await api.beginManualTransaction(tab.connectionId, executionDatabase, executionSchema); console.info("[DBX][executeTabSql:begin-manual-txn:done]", { traceId, txnSessionId: tab.txnSessionId, elapsed: elapsed() }); } console.info("[DBX][executeTabSql:execute-in-txn:invoke]", { traceId, txnSessionId: tab.txnSessionId, elapsed: elapsed() }); - executionPromise = api.executeInManualTransaction(tab.txnSessionId, sqlToExecute, tab.database, executionSchema, pageLimit); + executionPromise = api.executeInManualTransaction(tab.txnSessionId, sqlToExecute, executionDatabase, executionSchema, pageLimit); } else { console.info("[DBX][executeTabSql:execute-multi:start]", { traceId, elapsed: elapsed() }); // Query and data tabs use a tab-scoped pool so repeated executions keep @@ -2578,7 +2586,7 @@ export const useQueryStore = defineStore("query", () => { optionKeys: Object.keys(executionOptions), clientSession: Boolean(clientSessionId), }); - executionPromise = api.executeMulti(tab.connectionId, tab.database, sqlToExecute, executionSchema, executionId, executionOptions); + executionPromise = api.executeMulti(tab.connectionId, executionDatabase, sqlToExecute, executionSchema, executionId, executionOptions); } const results = annotateQueryResultSources(markQueryResultsRowsRaw(await withFrontendQueryTimeout(executionPromise, frontendTimeoutSecs, t("editor.queryTimeoutError", { seconds: frontendTimeoutSecs }))), queryBaseSql, sourceLabelDatabase, effectiveDbType); console.info("[DBX][executeTabSql:execute-multi:done]", { @@ -3098,6 +3106,7 @@ export const useQueryStore = defineStore("query", () => { const sortOrder = tab.resultSortColumn && tab.resultSortDirection ? `${quoteTableIdentifier(effectiveDbType, tab.resultSortColumn)} ${tab.resultSortDirection.toUpperCase()}` : undefined; const orderBy = tab.orderByInput?.trim() || sortOrder; const queryTimeoutSecs = queryTimeoutSecsForConnection(conn); + const executionDatabase = dataTabExecutionDatabase(conn, tab.database, tableMeta.catalog); const rows: QueryResult["rows"] = []; let columns: string[] = []; let executionTimeMs = 0; @@ -3120,7 +3129,7 @@ export const useQueryStore = defineStore("query", () => { limit: pageLimit, offset, }); - const results = await api.executeMulti(tab.connectionId, tab.database, sql, undefined, exportExecutionId, { + const results = await api.executeMulti(tab.connectionId, executionDatabase, sql, undefined, exportExecutionId, { maxRows: pageLimit, fetchSize: pageLimit, clientSessionId, @@ -3136,7 +3145,7 @@ export const useQueryStore = defineStore("query", () => { offset += result.rows.length; } } finally { - void closeClientSessionId(tab.connectionId, tab.database, clientSessionId, { tabId: tab.id }); + void closeClientSessionId(tab.connectionId, executionDatabase, clientSessionId, { tabId: tab.id }); } return {