feat(completion): show table metadata comments in SQL completions

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-24 20:36:58 +08:00
1 parent f45fcdb69d
commit 8d23216d60
7 files changed
+104 -49

No files matched your search

@@ -41,6 +41,7 @@ describe("completionTreeIndex", () => {
connectionId: "conn",
database: "hive",
schema: "sales_analytics",
comment: "Daily revenue summary",
},
{
id: "conn:hive:sales_analytics:__tables:daily_revenue_view",
@@ -70,7 +71,7 @@ describe("completionTreeIndex", () => {
expect(completionSchemasFromTree(tree, "conn", "hive")).toEqual(["sales_analytics"]);
const expected = [
{ name: "daily_revenue", schema: "sales_analytics", type: "table" },
{ name: "daily_revenue", schema: "sales_analytics", type: "table", detail: "→ Daily revenue summary" },
{ name: "daily_revenue_view", schema: "sales_analytics", type: "view" },
{ name: "daily_revenue_mv", schema: "sales_analytics", type: "materialized_view" },
];
@@ -801,6 +801,52 @@ describe("sqlCompletion scoped context classification", () => {
});
describe("sqlCompletion scoped metadata ranking", () => {
it("shows a table metadata detail in the completion item", () => {
const sql = "SELECT * FROM orders";
const items = buildSqlCompletionItems(sql, sql.length, {
tables: [{ name: "orders", type: "table", detail: "→ Customer orders" }],
columnsByTable: new Map(),
}).filter((item) => item.type === "table");
expect(items).toEqual([expect.objectContaining({ label: "orders", detail: "→ Customer orders" })]);
});
it("hides the redundant table type while preserving view and schema details", () => {
const simpleItems = buildSqlCompletionItems("SELECT * FROM orders", "SELECT * FROM orders".length, {
tables: [{ name: "orders", schema: "public", type: "table" }],
columnsByTable: new Map(),
}).filter((item) => item.type === "table");
expect(simpleItems.find((item) => item.label === "orders")?.detail).toBeUndefined();
const viewItems = buildSqlCompletionItems("SELECT * FROM order_view", "SELECT * FROM order_view".length, {
tables: [{ name: "order_view", schema: "public", type: "view" }],
columnsByTable: new Map(),
}).filter((item) => item.type === "table");
expect(viewItems.find((item) => item.label === "order_view")?.detail).toBe("view");
const duplicateItems = buildSqlCompletionItems("SELECT * FROM orders", "SELECT * FROM orders".length, {
tables: [
{ name: "orders", schema: "archive", type: "table" },
{ name: "orders", schema: "sales", type: "table" },
],
columnsByTable: new Map(),
}).filter((item) => item.type === "table");
expect(duplicateItems.map((item) => item.detail).sort()).toEqual(["archive.orders", "sales.orders"]);
const annotatedDuplicateItems = buildSqlCompletionItems("SELECT * FROM orders", "SELECT * FROM orders".length, {
tables: [
{ name: "orders", schema: "archive", type: "table", detail: "→ Archived orders" },
{ name: "orders", schema: "sales", type: "table", detail: "→ Current orders" },
],
columnsByTable: new Map(),
}).filter((item) => item.type === "table");
expect(annotatedDuplicateItems.map((item) => item.detail).sort()).toEqual(["archive.orders → Archived orders", "sales.orders → Current orders"]);
});
it("ranks exact and prefix table matches ahead of contains/fuzzy matches", () => {
const sql = "SELECT * FROM Temp";
const items = buildSqlCompletionItems(sql, sql.length, {
@@ -4,6 +4,11 @@ import { isTdengineStableTableType } from "@/lib/table/tableEditing";
const TABLE_NODE_TYPES = new Set(["table", "view", "materialized_view"]);
function completionTableDetail(comment: string | null | undefined): string | undefined {
const normalized = comment?.trim();
return normalized ? `→ ${normalized}` : undefined;
}
export function completionSchemasFromTree(nodes: readonly TreeNode[], connectionId: string, database: string): string[] {
const seen = new Set<string>();
const schemas: string[] = [];
@@ -27,11 +32,13 @@ export function completionTablesFromTree(nodes: readonly TreeNode[], connectionI
// the internal catalog. Keep the two metadata scopes isolated.
if ((node.catalog?.toLowerCase() ?? undefined) !== preferredCatalog) return;
if (preferredSchema && node.schema?.toLowerCase() !== preferredSchema) return;
const detail = completionTableDetail(node.comment);
tables.push({
name: node.tableName || node.label,
catalog: node.catalog,
schema: node.schema,
type: node.type === "materialized_view" ? "materialized_view" : node.type === "view" ? "view" : "table",
...(detail ? { detail } : {}),
...(isTdengineStableTableType(node.tableType) ? { tableType: node.tableType } : {}),
});
});
+3 -1
View File
@@ -3372,10 +3372,12 @@ function buildTableItems(
const suppliedApplyNameIsQualified = suppliedApplyName?.includes(".") === true;
const applyName = qualifiedByContext ? quoteCompletionApplyIdentifier(table.name, dialect) : ambiguousTableName && !!table.schema && (!suppliedApplyName || !suppliedApplyNameIsQualified) ? defaultApplyName : (suppliedApplyName ?? defaultApplyName);
const alias = autoAliasTables ? generateTableCompletionAlias(table.name, existingAliases) : "";
const schemaDetail = ambiguousTableName && table.schema ? `${table.schema}.${table.name}` : undefined;
const detail = table.detail && schemaDetail ? `${schemaDetail} ${table.detail}` : (table.detail ?? schemaDetail ?? (table.type === "table" ? undefined : table.type));
return {
label: table.name,
type: "table" as const,
detail: table.detail ?? (table.schema ? `${table.schema}.${table.name}` : table.type),
detail,
apply: formatTableAliasApply(applyName, alias, databaseType, keywordCase),
boost: computeBoost(table.name, prefix) + 1000 + (table.boost ?? 0),
dedupeKey: table.applyName || ambiguousTableName || (databaseType === "oracle" && table.schema) ? applyName : undefined,