From e3087e09543561a2de84da1583f7915aef1e5376 Mon Sep 17 00:00:00 2001 From: vrustx <279631638@qq.com> Date: Sun, 7 Jun 2026 13:53:58 +0800 Subject: [PATCH] fix(sql): rank referenced-table columns above keywords in completion (#801) (#810) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When editing SQL with concrete tables already referenced (a FROM clause, a "table." qualifier, or an INSERT column list), column completions only carried `computeBoost + keyBoost (0/500)` — lower than keyword boosts (1200-1900) — so the table's own columns were interleaved among keywords instead of ranking at the top where the user expects them. Give columns a relevance boost (+2000) in these referenced-table contexts so they rank above plain keywords. Added a unit test asserting columns outrank keywords when a table is referenced. Co-authored-by: vrustx Co-authored-by: Claude Opus 4.8 (1M context) --- apps/desktop/src/lib/sqlCompletion.ts | 8 +++++++- packages/app-tests/sqlCompletion.test.ts | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/lib/sqlCompletion.ts b/apps/desktop/src/lib/sqlCompletion.ts index 415b84aa9..abb97e53b 100644 --- a/apps/desktop/src/lib/sqlCompletion.ts +++ b/apps/desktop/src/lib/sqlCompletion.ts @@ -2265,6 +2265,12 @@ function buildColumnItems( } } + // When the query already references concrete tables (or we are after a + // "table." qualifier / in an INSERT column list), the columns of those + // tables are what the user is most likely picking — boost them above plain + // keywords so they rank at the top instead of being interleaved. + const relevanceBoost = context.referencedTables.length > 0 || !!context.qualifier || !!context.insertTable ? 2000 : 0; + return uniqueColumns .filter((column) => matchesPrefix(column.displayLabel, context.prefix)) .map((column) => { @@ -2275,7 +2281,7 @@ function buildColumnItems( detail: buildColumnDetail(column), info: buildColumnInfo(column), apply: buildColumnApply(column, context, dialect), - boost: computeBoost(column.displayLabel, context.prefix) + keyBoost, + boost: computeBoost(column.displayLabel, context.prefix) + keyBoost + relevanceBoost, }; }) .sort(compareCompletionItems); diff --git a/packages/app-tests/sqlCompletion.test.ts b/packages/app-tests/sqlCompletion.test.ts index a5cb826eb..dd9da4b94 100644 --- a/packages/app-tests/sqlCompletion.test.ts +++ b/packages/app-tests/sqlCompletion.test.ts @@ -838,6 +838,21 @@ test("key columns get priority boost in column suggestions", () => { assert.ok(idItem.boost > nameItem.boost, "id column should have higher boost than name"); }); +test("referenced-table columns rank above keywords (#801)", () => { + const items = buildSqlCompletionItems("select from public.users u", "select ".length, { + tables, + columnsByTable, + }); + const column = items.find((item) => item.type === "column"); + assert.ok(column, "should suggest columns when a table is referenced"); + assert.ok(column.boost >= 2000, "referenced-table columns should be boosted above plain keywords"); + const columnIdx = items.findIndex((item) => item.type === "column"); + const keywordIdx = items.findIndex((item) => item.type === "keyword"); + if (keywordIdx >= 0) { + assert.ok(columnIdx < keywordIdx, "columns should appear before keywords in a referenced-table context"); + } +}); + // --- Schema name completion --- test("suggests schema names alongside tables in FROM context", () => {