fix(sql): treat HAVING as alias-visible context

This commit is contained in:
dienaso authored and GitHub committed 2026-09-13 01:40:05 +08:00
1 parent 8ff56b4837
commit 4fe04c8bb9
3 files changed
+71 -2

No files matched your search

@@ -58,6 +58,34 @@ describe("sqlSemanticReferences shared consumers", () => {
expect(diagnostics).toEqual([]);
});
it("does not flag a SELECT alias used by MySQL HAVING", () => {
const sql = `SELECT COUNT(*) AS cnt, g FROM amounts GROUP BY g HAVING cnt > 1`;
const aliasStart = sql.indexOf("cnt", sql.indexOf("HAVING"));
const analysis: SqlReferenceAnalysis = {
tables: [
{
name: "amounts",
span: span(sql.indexOf("amounts") + 1, sql.indexOf("amounts") + "amounts".length),
},
],
columns: [
{
name: "cnt",
span: span(aliasStart + 1, aliasStart + "cnt".length),
},
],
};
const diagnostics = buildSqlSemanticDiagnostics(analysis, {
tables: [],
columnsByTable: new Map([["amounts", [{ name: "price" }, { name: "g" }]]]),
sql,
databaseType: "mysql",
});
expect(diagnostics).toEqual([]);
});
it("resolves navigation targets from subquery aliases and projected columns", () => {
const { sql, cursor } = sqlFixtureCursor("SELECT sq.user_| FROM (SELECT id, name AS user_name FROM users) sq");
const model = buildSqlSemanticModel(sql, cursor);