25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(sql): rank referenced table columns above sequences in WHERE completion (#12952)
Loc Nguyenaf661f31ad17
diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.tsindex 01604027ce..510cb176b4 100644--- a/apps/desktop/src/lib/sql/sqlCompletion.ts+++ b/apps/desktop/src/lib/sql/sqlCompletion.ts@@ -1871,7 +1871,7 @@ class SqlCompletionProvider {];if (this.databaseType === "mongodb") {- return dedupeAndSort(buildMongoCompletionItemsFromContext({ mode: "root", prefix: context.prefix, from: 0 }).map(mongoCompletionItemToSqlCompletionItem));+ return dedupeAndSort(buildMongoCompletionItemsFromContext({ mode: "root", prefix: context.prefix, from: 0 }).map(mongoCompletionItemToSqlCompletionItem), context);}const snippets = this.databaseType === "manticoresearch" ? [...(this.input.snippets ?? DEFAULT_SQL_SNIPPETS), ...MANTICORESEARCH_SQL_SNIPPETS] : (this.input.snippets ?? DEFAULT_SQL_SNIPPETS);@@ -1882,7 +1882,7 @@ class SqlCompletionProvider {this.databaseType,);if (context.preferredValueKeywords?.length) {- return dedupeAndSort([...customSnippetItems, ...buildPreferredKeywordItems(context.prefix, context.preferredValueKeywords, this.input.keywordCase)]);+ return dedupeAndSort([...customSnippetItems, ...buildPreferredKeywordItems(context.prefix, context.preferredValueKeywords, this.input.keywordCase)], context);}const preferReferencedColumns = hasMatchingReferencedColumnPrefix(context, this.columnsByTable);@@ -2010,7 +2010,7 @@ class SqlCompletionProvider {}}- return dedupeAndSort(this.items);+ return dedupeAndSort(this.items, context);}}@@ -4934,7 +4934,7 @@ function buildObjectItems(context: SqlCompletionContext, objects: SqlCompletionOconst signature = object.signature?.trim();const detail = [locationDetail, signature ? `(${signature})` : undefined, object.dataType ? `[${object.dataType}]` : undefined].filter(Boolean).join(" ");const schemaBoost = onlyFunctions ? Math.min(object.boost ?? 0, 1000) : (object.boost ?? 0);- const typeBoost = routineTypeBoost(object.type, prioritizeOracleFunctions && !onlyFunctions);+ const typeBoost = routineTypeBoost(object.type, prioritizeOracleFunctions && !onlyFunctions, onlyFunctions);const baseDedupeKey = object.applyName || (isOracleCompletionDatabase(databaseType) && object.schema) ? applyName : undefined;return {label: object.name,@@ -4948,7 +4948,7 @@ function buildObjectItems(context: SqlCompletionContext, objects: SqlCompletionOexactMatch: !!context.prefix && object.name.toLowerCase() === context.prefix.toLowerCase(),};})- .sort(compareCompletionItems)+ .sort((left, right) => compareCompletionItems(left, right, context)).slice(0, MAX_TABLE_COMPLETION_ITEMS);}@@ -5004,9 +5004,9 @@ function buildRoutineInfo(object: SqlCompletionObject): string | undefined {return parts.length > 1 ? parts.join("\n") : undefined;}-function routineTypeBoost(type: SqlCompletionObject["type"], prioritizeFunctions: boolean): number {+function routineTypeBoost(type: SqlCompletionObject["type"], prioritizeFunctions: boolean, columnsExpected = false): number {if (type === "package") return 1600;- if (type === "sequence") return 1500;+ if (type === "sequence") return columnsExpected ? 800 : 1500;if (type === "function") return prioritizeFunctions ? 1800 : 900;return prioritizeFunctions ? 900 : 1800;}@@ -6688,27 +6688,30 @@ function getHistoryBoost(label: string, type: string): number {return Math.min(count * 80, 500);}-function dedupeAndSort(items: SqlCompletionItem[]): SqlCompletionItem[] {+function dedupeAndSort(items: SqlCompletionItem[], context?: SqlCompletionContext): SqlCompletionItem[] {const seen = new Set<string>();- return items.sort(compareCompletionItems).filter((item) => {- const key = `${item.type}:${item.dedupeKey ?? item.label}`;- if (seen.has(key)) return false;- seen.add(key);- return true;- });+ return items+ .sort((left, right) => compareCompletionItems(left, right, context))+ .filter((item) => {+ const key = `${item.type}:${item.dedupeKey ?? item.label}`;+ if (seen.has(key)) return false;+ seen.add(key);+ return true;+ });}-function compareCompletionItems(left: SqlCompletionItem, right: SqlCompletionItem): number {- const leftSurfacesFirst = left.type === "variable" || left.type === "sequence";- const rightSurfacesFirst = right.type === "variable" || right.type === "sequence";+function compareCompletionItems(left: SqlCompletionItem, right: SqlCompletionItem, context?: SqlCompletionContext): number {+ const columnsExpected = !!context && context.suggestColumns && context.referencedTables.length > 0;+ const leftSurfacesFirst = left.type === "variable" || (!columnsExpected && left.type === "sequence");+ const rightSurfacesFirst = right.type === "variable" || (!columnsExpected && right.type === "sequence");if (leftSurfacesFirst !== rightSurfacesFirst) return leftSurfacesFirst ? -1 : 1;if (!left.exactMatch !== !right.exactMatch) return left.exactMatch ? -1 : 1;const leftBonus = getHistoryBoost(left.label, left.type);const rightBonus = getHistoryBoost(right.label, right.type);- return right.boost + rightBonus + getTypePriorityBoost(right.type) - (left.boost + leftBonus + getTypePriorityBoost(left.type));+ return right.boost + rightBonus + getTypePriorityBoost(right.type, columnsExpected) - (left.boost + leftBonus + getTypePriorityBoost(left.type, columnsExpected));}-function getTypePriorityBoost(type: SqlCompletionItem["type"]): number {+function getTypePriorityBoost(type: SqlCompletionItem["type"], columnsExpected = false): number {switch (type) {case "column":return 180;@@ -6719,7 +6722,7 @@ function getTypePriorityBoost(type: SqlCompletionItem["type"]): number {case "variable":return 220;case "sequence":- return 220;+ return columnsExpected ? 80 : 220;case "text":return 220;case "function":diff --git a/packages/app-tests/sqlCompletion.test.ts b/packages/app-tests/sqlCompletion.test.tsindex 07f172a390..e4d5c286ef 100644--- a/packages/app-tests/sqlCompletion.test.ts+++ b/packages/app-tests/sqlCompletion.test.ts@@ -3532,6 +3532,36 @@ test("referenced-table columns rank above keywords (#801)", () => {}});+test("referenced-table columns rank above sequences in WHERE clause (#12927)", () => {+ for (const databaseType of ["dameng", "oracle"] as const) {+ const sql = "select * from xx where col";+ const items = buildSqlCompletionItems(sql, sql.length, {+ tables: [{ name: "xx", type: "table" }],+ columnsByTable: new Map([["xx", [{ name: "col_value", table: "xx" }]]]),+ objects: [{ name: "col_seq", type: "sequence" }],+ databaseType,+ });++ const columnIdx = items.findIndex((item) => item.type === "column" && item.label === "col_value");+ const sequenceIdx = items.findIndex((item) => item.type === "sequence" && item.label === "col_seq");++ assert.ok(columnIdx >= 0, `column col_value should be suggested for ${databaseType}`);+ assert.ok(sequenceIdx >= 0, `sequence col_seq should be suggested for ${databaseType}`);+ assert.ok(columnIdx < sequenceIdx, `column col_value (index ${columnIdx}) should sort before sequence col_seq (index ${sequenceIdx}) for ${databaseType}`);++ // In contexts where columns are not expected, sequence still appears and retains priority+ const noTableSql = "select col";+ const noTableItems = buildSqlCompletionItems(noTableSql, noTableSql.length, {+ tables: [],+ columnsByTable: new Map(),+ objects: [{ name: "col_seq", type: "sequence" }],+ databaseType,+ });+ const noTableSeqIdx = noTableItems.findIndex((item) => item.type === "sequence" && item.label === "col_seq");+ assert.equal(noTableSeqIdx, 0, `sequence col_seq should rank first when columns are not expected for ${databaseType}`);+ }+});+// --- Schema name completion ---test("suggests schema names alongside tables in FROM context", () => {