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 after a complete JOIN ON predicate
李子凡a548f9769b73
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.tsindex aba7053a8b..eecb2be81b 100644--- a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts+++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts@@ -337,6 +337,75 @@ describe("sqlCompletion keyword snippets", () => {expect(items[0]?.type).toBe("function");});});++ describe("clause keywords after a completed JOIN ... ON predicate (#13283)", () => {+ const objects = ["wms_handle_order", "WhereUsed", "web_hook", "wh_sync"].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: "orders" }, { name: "users" }],+ columnsByTable: new Map([+ [+ "orders",+ [+ { name: "user_id", table: "orders" },+ { name: "warehouse_id", table: "orders" },+ ],+ ],+ ["users", [{ name: "id", table: "users" }]],+ ]),+ objects,+ keywordCase: "upper",+ });+ }++ it.each([+ ["SELECT * FROM orders LEFT JOIN users ON users.id = orders.user_id w", "WHERE"],+ ["SELECT * FROM orders LEFT JOIN users ON users.id = orders.user_id wh", "WHERE"],+ ["SELECT * FROM orders JOIN users ON users.id = orders.user_id AND users.id > 0 w", "WHERE"],+ ["SELECT * FROM orders LEFT JOIN users ON users.id = orders.user_id gr", "GROUP"],+ ])("ranks the clause keyword first for %s", (sql, keyword) => {+ for (const databaseType of ["mysql", "postgres", "sqlserver"] as const) {+ const items = rankedItems(sql, databaseType);+ expect(items[0]?.type, `${databaseType} ${sql}`).toBe("keyword");+ expect(items[0]?.label, `${databaseType} ${sql}`).toBe(keyword);+ }+ });++ it("keeps columns ahead of keywords while the ON predicate is still open", () => {+ const items = rankedItems("SELECT * FROM orders LEFT JOIN users ON users.id = orders.user_", "mysql");++ expect(items[0]?.label).toBe("user_id");+ });++ it.each([+ ["SELECT * FROM orders LEFT JOIN users ON users.id = orders.user_id GROUP BY w", "orders.warehouse_id"],+ ["SELECT * FROM orders LEFT JOIN users ON users.id = orders.user_id ORDER BY w", "orders.warehouse_id"],+ ])("keeps the typed column first after a later clause boundary for %s", (sql, expected) => {+ for (const databaseType of ["mysql", "postgres", "sqlserver"] as const) {+ const items = rankedItems(sql, databaseType);+ expect(items[0]?.label, `${databaseType} ${sql}`).toBe(expected);+ }+ });++ it.each([+ ["... LIMIT 10 w", "SELECT * FROM orders LEFT JOIN users ON users.id = orders.user_id LIMIT 10 w", "SELECT * FROM orders LIMIT 10 w"],+ ["... UNION w", "SELECT * FROM orders LEFT JOIN users ON users.id = orders.user_id UNION w", "SELECT * FROM orders UNION w"],+ ])("ranks the same items with and without a preceding join predicate for %s", (_label, withJoin, withoutJoin) => {+ for (const databaseType of ["mysql", "postgres", "sqlserver"] as const) {+ const joined = rankedItems(withJoin, databaseType);+ const plain = rankedItems(withoutJoin, databaseType);+ expect(joined[0]?.label, `${databaseType} ${withJoin}`).toBe(plain[0]?.label);+ expect(joined[0]?.type, `${databaseType} ${withJoin}`).toBe(plain[0]?.type);+ }+ });+ });});describe("SQL Server datepart completion", () => {diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.tsindex 0a944b419f..73591b9043 100644--- a/apps/desktop/src/lib/sql/sqlCompletion.ts+++ b/apps/desktop/src/lib/sql/sqlCompletion.ts@@ -1777,6 +1777,8 @@ export interface SqlCompletionContext {dataTypeContext: boolean;/** The cursor follows a complete FROM/JOIN table reference, where the next word is an alias or a clause keyword. */afterTableReference?: boolean;+ /** The cursor follows a complete `JOIN … ON` predicate, where a clause keyword is the next expected word. */+ afterJoinCondition?: boolean;}const SQL_COMPLETION_CLOSING_QUOTES: Readonly<Record<string, SqlCompletionClosingQuote>> = {@@ -3050,6 +3052,7 @@ export function getSqlCompletionContext(sql: string, cursor: number, options: SqcontextKind,dataTypeContext,afterTableReference: !dataTypeContext && !qualifier && !exclusiveTableSuggestions && !suggestColumns && !insertInfo && isAfterSelectBodyExpression(beforeToken, options.databaseType),+ afterJoinCondition: !dataTypeContext && !qualifier && !exclusiveTableSuggestions && !insertInfo && isAfterJoinOnCondition(beforeToken, options.databaseType),};}@@ -3722,6 +3725,40 @@ function isAfterSelectBodyExpression(beforeToken: string, databaseType: Databasereturn true;}+/**+ * A complete `ON a = b` predicate also ends a condition where a clause keyword can+ * come next. `isInColumnContext` treats the operators in that predicate as "a column+ * goes here", which kept `suggestColumns` true and silenced the keyword ranking after+ * `LEFT JOIN … ON …` (#13283).+ */+function isAfterJoinOnCondition(beforeToken: string, databaseType: DatabaseType | undefined): boolean {+ const cleaned = maskSqlLiteralsAndComments(beforeToken, databaseType).trimEnd();+ // Only SELECT/WITH statements: in `UPDATE … JOIN … ON …` a top-level ON also+ // exists, but the boost is not wanted while SET assignments continue.+ if (!/^\s*(select|with)\b/i.test(cleaned)) return false;+ const lastOn = lastTopLevelKeywordIndex(cleaned, "on");+ if (lastOn < 0) return false;+ // Once a later clause boundary follows the join predicate, the cursor is in+ // that clause (WHERE column, GROUP BY expression, LIMIT number, UNION branch),+ // not at a clause-keyword position.+ const lastClauseBoundary = Math.max(+ lastTopLevelKeywordIndex(cleaned, "where"),+ lastTopLevelKeywordIndex(cleaned, "group"),+ lastTopLevelKeywordIndex(cleaned, "order"),+ lastTopLevelKeywordIndex(cleaned, "having"),+ lastTopLevelKeywordIndex(cleaned, "limit"),+ lastTopLevelKeywordIndex(cleaned, "offset"),+ lastTopLevelKeywordIndex(cleaned, "union"),+ lastTopLevelKeywordIndex(cleaned, "intersect"),+ lastTopLevelKeywordIndex(cleaned, "except"),+ );+ if (lastClauseBoundary > lastOn) return false;+ const lastKeyword = /\b([A-Za-z_][\w$]*)\s*$/.exec(cleaned)?.[1]?.toLowerCase();+ if (lastKeyword && CONDITION_INCOMPLETE_TAIL_KEYWORDS.has(lastKeyword)) return false;+ if (lastKeyword && SELECT_BODY_INCOMPLETE_TAIL_KEYWORDS.has(lastKeyword) && !JOIN_MODIFIERS.has(lastKeyword)) return false;+ return isExpressionTailComplete(cleaned);+}+const SELECT_BODY_INCOMPLETE_TAIL_KEYWORDS = new Set(["where", "and", "or", "not", "having", "group", "order", "by", "on", "is", "in", "like", "between", ...JOIN_MODIFIERS]);const CONDITION_INCOMPLETE_TAIL_KEYWORDS = new Set(["where", "and", "or", "not", "having", "on", "is", "in", "like", "between", "exists"]);@@ -6670,7 +6707,9 @@ function buildKeywordItems(prefix: string, context: SqlCompletionContext, databa// 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;+ // `afterJoinCondition` implies a finished predicate, so `suggestColumns` being+ // true there only reflects the `=` the predicate already used.+ const clauseKeywordExpected = !!prefix && !context.suggestTables && (!!context.afterTableReference || !!context.afterJoinCondition);const normalizedPrefix = prefix.toLowerCase();return activeSqlKeywords(databaseType, driverProfile)