fix(sql): gate having alias visibility by dialect reject list
This commit is contained in:
1 parent
ac7fbaeb43
commit
1b6ff2ba54
5 files changed
+158
-13
No files matched your search
@@ -86,6 +86,34 @@ describe("sqlSemanticReferences shared consumers", () => {
|
||||
expect(diagnostics).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps flagging a HAVING alias for PostgreSQL, which rejects it", () => {
|
||||
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: "postgres",
|
||||
});
|
||||
|
||||
expect(diagnostics.map((diagnostic) => diagnostic.message)).toEqual(["Unknown column cnt"]);
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
@@ -1381,4 +1381,55 @@ describe("select alias visibility", () => {
|
||||
|
||||
expect(items.some((item) => item.label === "cnt")).toBe(true);
|
||||
});
|
||||
|
||||
it("does not prioritize HAVING aliases for dialects that reject them", () => {
|
||||
const sql = "SELECT COUNT(*) AS cnt, g FROM orders GROUP BY g HAVING cnt > ";
|
||||
for (const databaseType of ["postgres", "sqlserver", "db2", "oracle"] as const) {
|
||||
const context = getSqlCompletionContext(sql, sql.length, { databaseType });
|
||||
|
||||
expect(context.prioritizeSelectAliases, databaseType).toBe(false);
|
||||
expect(context.selectAliases, databaseType).toEqual([]);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps HAVING alias priority for SQLite-family and DuckDB dialects", () => {
|
||||
const sql = "SELECT COUNT(*) AS cnt, g FROM orders GROUP BY g HAVING cnt > ";
|
||||
for (const databaseType of ["duckdb", "sqlite", "doris", "bigquery"] as const) {
|
||||
const context = getSqlCompletionContext(sql, sql.length, { databaseType });
|
||||
|
||||
expect(context.prioritizeSelectAliases, databaseType).toBe(true);
|
||||
expect(context.selectAliases, databaseType).toContain("cnt");
|
||||
}
|
||||
});
|
||||
|
||||
it("does not anchor alias visibility on identifiers containing HAVING", () => {
|
||||
const sql = "SELECT id AS uid FROM orders WHERE having_count > 1";
|
||||
const context = getSqlCompletionContext(sql, sql.length, { databaseType: "mysql" });
|
||||
|
||||
expect(context.prioritizeSelectAliases).toBe(false);
|
||||
expect(context.selectAliases).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps alias visibility in ORDER BY after an identifier containing HAVING", () => {
|
||||
const sql = "SELECT id AS uid FROM orders WHERE having_count > 1 ORDER BY uid";
|
||||
const cursor = sql.length;
|
||||
const context = getSqlCompletionContext(sql, cursor, { databaseType: "mysql" });
|
||||
|
||||
expect(context.prioritizeSelectAliases).toBe(true);
|
||||
expect(context.selectAliases).toContain("uid");
|
||||
});
|
||||
|
||||
it("keeps HAVING alias priority for unlisted dialects (deny-list polarity)", () => {
|
||||
const sql = "SELECT COUNT(*) AS cnt, g FROM orders GROUP BY g HAVING cnt > ";
|
||||
for (const databaseType of ["spark", "databricks", "snowflake", "hive"] as const) {
|
||||
const context = getSqlCompletionContext(sql, sql.length, { databaseType });
|
||||
|
||||
expect(context.prioritizeSelectAliases, databaseType).toBe(true);
|
||||
expect(context.selectAliases, databaseType).toContain("cnt");
|
||||
}
|
||||
// No database type at all keeps the permissive legacy behavior.
|
||||
const untyped = getSqlCompletionContext(sql, sql.length, {});
|
||||
expect(untyped.prioritizeSelectAliases).toBe(true);
|
||||
expect(untyped.selectAliases).toContain("cnt");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user