fix(sql): complete nested subquery sources

This commit is contained in:
t8y2 committed 2026-07-27 23:04:57 +08:00
1 parent 789f0118f5
commit 146dc672dd
5 files changed
+165 -10

No files matched your search

@@ -35,6 +35,30 @@ function semanticCompletion(markedSql: string, input: Partial<SqlCompletionProvi
}
describe("semantic SQL completion candidates", () => {
it("loads nested alias columns through the database-qualified metadata key", () => {
const { context, items } = semanticCompletion(
"SELECT * FROM aa.tb t WHERE EXISTS (SELECT 1 FROM aa.tb1 t1, aa.tb2 t2 WHERE t1.|)",
{
columnsByTable: new Map([["aa.tb1", [{ name: "id", table: "tb1", schema: "aa" }]]]),
},
{ databaseType: "mysql", dialect: "mysql" },
);
expect(context.referencedTables).toEqual(expect.arrayContaining([expect.objectContaining({ name: "tb1", schema: "aa", alias: "t1" })]));
expect(items.filter((item) => item.type === "column").map((item) => item.label)).toEqual(["id"]);
});
it("suggests nested tables after a qualified comma", () => {
const { context, items } = semanticCompletion(
"SELECT * FROM aa.tb t WHERE EXISTS (SELECT 1 FROM aa.tb1 t1, aa.|)",
{ tables: [{ name: "tb2", schema: "aa", type: "table" }] },
{ databaseType: "mysql", dialect: "mysql" },
);
expect(context.contextKind).toBe("table");
expect(items).toEqual(expect.arrayContaining([expect.objectContaining({ label: "tb2", type: "table" })]));
});
it.each([
["ordinary lowercase", "SELECT * FROM orders_alias a WHERE a.|", "ORDERS_ALIAS", false],
["quoted lowercase", 'SELECT * FROM "orders_alias" a WHERE a.|', "orders_alias", true],
@@ -80,6 +80,29 @@ describe("sqlSemanticModel baseline fixtures", () => {
expect(model.cursorIntent).toEqual(expect.objectContaining({ kind: "alias_column", qualifierParts: ["b"] }));
});
it("keeps nested EXISTS sources and outer correlated sources visible", () => {
const { sql, cursor } = sqlFixtureCursor("SELECT * FROM aa.tb t WHERE EXISTS (SELECT 1 FROM aa.tb1 t1, aa.tb2 t2 WHERE t1.|)");
const model = buildSqlSemanticModel(sql, cursor, { databaseType: "mysql", dialect: "mysql" });
expect(model.rowSources).toEqual(
expect.arrayContaining([
expect.objectContaining({ name: "tb1", alias: "t1", metadataTarget: { schema: "aa", table: "tb1" } }),
expect.objectContaining({ name: "tb2", alias: "t2", metadataTarget: { schema: "aa", table: "tb2" } }),
expect.objectContaining({ name: "tb", alias: "t", metadataTarget: { schema: "aa", table: "tb" } }),
]),
);
expect(model.cursorIntent).toEqual(expect.objectContaining({ kind: "alias_column", qualifierParts: ["t1"] }));
expect(model.cursorIntent.targetSourceId).toBe(model.rowSources.find((source) => source.alias === "t1")?.id);
});
it("classifies an incomplete nested database qualifier as a table context", () => {
const { sql, cursor } = sqlFixtureCursor("SELECT * FROM aa.tb t WHERE EXISTS (SELECT 1 FROM aa.tb1 t1, aa.|)");
const model = buildSqlSemanticModel(sql, cursor, { databaseType: "mysql", dialect: "mysql" });
expect(model.rowSources.some((source) => source.name === "aa")).toBe(false);
expect(model.cursorIntent).toEqual(expect.objectContaining({ kind: "table", qualifierParts: ["aa"], confidence: "high" }));
});
it("does not treat an Oracle FOR UPDATE clause as a table alias", () => {
const sql = "SELECT * FROM APP.USERS FOR UPDATE SKIP LOCKED";
const model = buildSqlSemanticModel(sql, sql.length, { databaseType: "oracle" });
@@ -100,6 +100,20 @@ describe("sqlCompletion quoted schema qualifiers", () => {
});
describe("sqlCompletion table targets", () => {
it("suggests tables after a database qualifier in an EXISTS table list", () => {
const sql = "SELECT * FROM aa.tb t WHERE EXISTS (SELECT 1 FROM aa.tb1 t1, aa.";
const context = getSqlCompletionContext(sql, sql.length);
const items = buildSqlCompletionItems(sql, sql.length, {
databaseType: "mysql",
tables: [{ name: "tb2", schema: "aa", type: "table" }],
columnsByTable: new Map(),
});
expect(context.qualifier).toBe("aa");
expect(context.suggestTables).toBe(true);
expect(items).toEqual(expect.arrayContaining([expect.objectContaining({ label: "tb2", type: "table" })]));
});
it("does not suggest aliases while completing an empty FROM target before LIMIT", () => {
const sql = "SELECT *\nFROM \nLIMIT 100;";
const cursor = "SELECT *\nFROM ".length;