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(completion): rank clause keywords above routines after FROM
Abel Yagubyan116035f0a912
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.tsindex 2d3581d0ed..aba7053a8b 100644--- a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts+++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts@@ -292,6 +292,51 @@ describe("sqlCompletion keyword snippets", () => {expect(items.some((item) => item.label === "QUALIFY")).toBe(false);});++ describe("clause keywords after a FROM table reference (#13120)", () => {+ const routineNames = ["wms_handle_order", "WriteHistoryEntry", "WhereUsed", "web_hook", "w_h_e_r", "wh_sync", "group_hist", "order_sync"];+ const objects = routineNames.flatMap((name) => [+ { name, schema: "app", type: "procedure" as const, signature: "" },+ { name: `${name}_fn`, schema: "app", type: "function" as const, signature: "" },+ ]);++ function rankedItems(sql: string, databaseType: "mysql" | "postgres" | "sqlserver") {+ const legacy = getSqlCompletionContext(sql, sql.length, { databaseType });+ const context = sqlCompletionContextFromSemantic(buildSqlSemanticModel(sql, sql.length, { databaseType }), legacy);+ return buildSqlCompletionItemsFromContext(context, {+ databaseType,+ tables: [{ name: "users" }],+ columnsByTable: new Map([["users", [{ name: "id", table: "users" }]]]),+ objects,+ keywordCase: "upper",+ });+ }++ it.each([+ ["SELECT * FROM users w", "WHERE"],+ ["SELECT * FROM users wh", "WHERE"],+ ["SELECT * FROM users whe", "WHERE"],+ ["SELECT * FROM users wher", "WHERE"],+ ["SELECT * FROM users u wh", "WHERE"],+ ["SELECT * FROM users AS u gr", "GROUP"],+ ["SELECT * FROM users ord", "ORDER"],+ ])("ranks the clause keyword above routines for %s", (sql, keyword) => {+ for (const databaseType of ["mysql", "postgres", "sqlserver"] as const) {+ const items = rankedItems(sql, databaseType);+ const keywordIndex = items.findIndex((item) => item.type === "keyword" && item.label === keyword);+ const firstRoutineIndex = items.findIndex((item) => item.type === "function");+ expect(keywordIndex, `${databaseType} ${sql}`).toBeGreaterThanOrEqual(0);+ expect(items[0]?.type, `${databaseType} ${sql}`).toBe("keyword");+ if (firstRoutineIndex >= 0) expect(keywordIndex, `${databaseType} ${sql}`).toBeLessThan(firstRoutineIndex);+ }+ });++ it("keeps routines ahead of keywords where an expression is expected", () => {+ const items = rankedItems("SELECT * FROM users WHERE id = wh", "mysql");++ expect(items[0]?.type).toBe("function");+ });+ });});describe("SQL Server datepart completion", () => {diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.tsindex 8dcca71c4d..0a944b419f 100644--- a/apps/desktop/src/lib/sql/sqlCompletion.ts+++ b/apps/desktop/src/lib/sql/sqlCompletion.ts@@ -1775,6 +1775,8 @@ export interface SqlCompletionContext {followingKeyword?: string;contextKind: SqlCompletionContextKind;dataTypeContext: boolean;+ /** The cursor follows a complete FROM/JOIN table reference, where the next word is an alias or a clause keyword. */+ afterTableReference?: boolean;}const SQL_COMPLETION_CLOSING_QUOTES: Readonly<Record<string, SqlCompletionClosingQuote>> = {@@ -3047,6 +3049,7 @@ export function getSqlCompletionContext(sql: string, cursor: number, options: SqfollowingKeyword: getFollowingKeywordAfterCursor(sql, cursor, statementSpan.end),contextKind,dataTypeContext,+ afterTableReference: !dataTypeContext && !qualifier && !exclusiveTableSuggestions && !suggestColumns && !insertInfo && isAfterSelectBodyExpression(beforeToken, options.databaseType),};}@@ -6664,6 +6667,11 @@ function buildKeywordItems(prefix: string, context: SqlCompletionContext, databaconst isDml = context.statementKind === "select" || context.statementKind === "insert" || context.statementKind === "update" || context.statementKind === "delete";const showDdl = !isDml || context.suggestTables || context.dataTypeContext;const functionSignatures = activeFunctionSignatures(databaseType, driverProfile);+ // After `FROM t`, a routine name cannot come next, but a clause keyword can.+ // Without this, initials and fuzzy matches on hundreds of procedures pushed+ // WHERE out of the list for `wh`, `whe` and `wher` (#13120).+ const clauseKeywordExpected = !!prefix && !!context.afterTableReference && !context.suggestColumns && !context.suggestTables;+ const normalizedPrefix = prefix.toLowerCase();return activeSqlKeywords(databaseType, driverProfile).filter((keyword) => {@@ -6680,15 +6688,20 @@ function buildKeywordItems(prefix: string, context: SqlCompletionContext, databaconst freqBoost = !context.dataTypeContext && HIGH_FREQUENCY_KEYWORDS.has(keyword) ? 100 : 0;const typeBoost = context.dataTypeContext && isDataType ? 2500 : 0;const followingBoost = context.followingKeyword && isPairPrefixKeywordForFollowing(keyword, context.followingKeyword) ? 1300 : 0;+ const clauseBoost = clauseKeywordExpected && TABLE_REFERENCE_CLAUSE_KEYWORDS.has(keyword) && keyword.toLowerCase().startsWith(normalizedPrefix) ? TABLE_REFERENCE_CLAUSE_KEYWORD_BOOST : 0;return {label: applySqlKeywordCase(keyword, keywordCase),type: "keyword" as const,detail: isDataType ? "data type" : undefined,- boost: base + freqBoost + typeBoost + followingBoost,+ boost: base + freqBoost + typeBoost + followingBoost + clauseBoost,};});}+const TABLE_REFERENCE_CLAUSE_KEYWORDS = new Set(["WHERE", "GROUP", "GROUP BY", "ORDER", "ORDER BY", "HAVING", "UNION", "INTERSECT", "EXCEPT"]);+// Enough to outrank a routine's initials match plus the procedure type boost.+const TABLE_REFERENCE_CLAUSE_KEYWORD_BOOST = 3000;+function shouldOfferKeywordPrefixContinuations(context: SqlCompletionContext, pendingJoinKeyword: boolean): boolean {return !!context.prefix && !pendingJoinKeyword && !context.qualifier && !context.exclusiveTableSuggestions && !context.exclusiveColumnSuggestions;}