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", () => {