feat(completion): improve SQL function suggestions

This commit is contained in:
zipg authored and GitHub committed 2026-07-20 01:55:52 +08:00
1 parent 21652bc797
commit f5cdbb8662
12 files changed
+412 -40

No files matched your search

@@ -35,6 +35,37 @@ function semanticCompletion(markedSql: string, input: Partial<SqlCompletionProvi
}
describe("semantic SQL completion candidates", () => {
it("keeps matching functions available in column expressions", () => {
const columnsByTable = new Map<string, SqlCompletionColumn[]>([
[
"routes",
[
{ name: "start_sid", table: "routes" },
{ name: "start_dept", table: "routes" },
],
],
]);
const { context, items } = semanticCompletion(
"SELECT * FROM routes WHERE st_|",
{
columnsByTable,
objects: [
{ name: "st_area", type: "function", dataType: "double precision" },
{ name: "st_refresh", type: "procedure" },
],
},
{ databaseType: "postgres", dialect: "postgres" },
);
expect(context.contextKind).toBe("column");
expect(context.suggestColumns).toBe(true);
expect(context.suggestRoutines).toBe(true);
expect(context.exclusiveRoutineSuggestions).toBe(false);
expect(items.some((item) => item.label === "st_area" && item.type === "function")).toBe(true);
expect(items.some((item) => item.label === "st_refresh")).toBe(false);
});
it("keeps alias-qualified column completion scoped to one row source", () => {
const columnsByTable = new Map<string, SqlCompletionColumn[]>([
["users", ["id", "name", "email"].map((name) => ({ name, table: "users" }))],
@@ -285,7 +285,7 @@ describe("sqlCompletion scoped context classification", () => {
expect(context.prefix).toBe("userc");
expect(context.referencedTables).toEqual(expect.arrayContaining([expect.objectContaining({ name: "A1User" })]));
expect(context.suggestColumns).toBe(true);
expect(context.suggestRoutines).toBe(false);
expect(context.suggestRoutines).toBe(true);
});
it("auto-opens column completion after WHERE whitespace before LIMIT", () => {
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { resolveSqlCompletionTableLookupTarget } from "@/lib/sql/sqlCompletionLookupTarget";
import { getSqlCompletionContext } from "@/lib/sql/sqlCompletion";
import { resolveSqlCompletionRoutineLookupTarget, resolveSqlCompletionTableLookupTarget } from "@/lib/sql/sqlCompletionLookupTarget";
describe("sqlCompletionLookupTarget", () => {
it("treats qualified table completion as a database lookup for MySQL-compatible engines", () => {
@@ -75,4 +76,23 @@ describe("sqlCompletionLookupTarget", () => {
filter: "ord",
});
});
it.each([
["SELECT dbo.fn_", "dbo", "fn_"],
["SELECT public.st_", "public", "st_"],
])("separates a routine schema from its name mask for %s", (sql, schema, mask) => {
const completionContext = getSqlCompletionContext(sql, sql.length);
expect(resolveSqlCompletionRoutineLookupTarget({ currentSchema: "fallback", completionContext })).toEqual({ schema, mask });
});
it("uses the current schema for an unqualified routine mask", () => {
const sql = "SELECT st_";
const completionContext = getSqlCompletionContext(sql, sql.length);
expect(resolveSqlCompletionRoutineLookupTarget({ currentSchema: "public", completionContext })).toEqual({
schema: "public",
mask: "st_",
});
});
});