fix(sql): rank referenced-table columns above keywords in completion (#801) (#810)

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 <vrustx@vrustxdeMac-mini.local>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
vrustx 2026-06-07 13:53:58 +08:00 committed by GitHub
parent ff32e4b00f
commit e3087e0954
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View File

@ -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);

View File

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