fix(sql): complete nested subquery sources
This commit is contained in:
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" });
|
||||
|
||||
Reference in new issue
Block a user