Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/sql/__tests__/alias-completion-source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ describe("aliasColumnCompletionSource", () => {
expect(labels(result)).toEqual(["id", "username"]);
});

it.each([
{
table: '"my.db".users',
schema: { "my.db": { users: ["quoted_column"] }, my: { db: { users: ["other_column"] } } },
},
{
table: '"my.table"',
schema: { "my.table": ["quoted_column"], my: { table: ["other_column"] } },
},
])("preserves quoted dots in aliased table $table", async ({ table, schema }) => {
const doc = `SELECT u. FROM ${table} u`;
const result = await complete(doc, { schema, pos: "SELECT u.".length });
expect(labels(result)).toEqual(["quoted_column"]);
});

it("offers CTE columns for a CTE alias", async () => {
const doc = "WITH recent AS (SELECT x, y FROM logs) SELECT r. FROM recent r";
const result = await complete(doc, { schema, pos: doc.indexOf("r. FROM") + 2 });
Expand Down
11 changes: 11 additions & 0 deletions src/sql/__tests__/query-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ describe("analyzeQueryContext", () => {
expect(context.aliases.get("u")).toBe("users");
});

it.each([
'SELECT u.id FROM "my.db"."user.table" u',
'SELECT u. FROM "my.db"."user.table" u',
])("preserves quoted path segments in %s", async (sql) => {
const context = await analyze(sql);
expect(context.tables).toEqual([
{ name: "user.table", path: ["my.db", "user.table"], alias: "u" },
]);
expect(context.aliases.get("u")).toBe("my.db.user.table");
});

it("records tables without aliases", async () => {
const context = await analyze("SELECT name FROM users");
expect(context.tables).toEqual([{ name: "users", path: ["users"] }]);
Expand Down
5 changes: 4 additions & 1 deletion src/sql/alias-completion-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ export function aliasColumnCompletionSource(config: AliasCompletionConfig = {}):
} else if (!cte) {
const schema = resolveSchema(config.schema, context);
if (schema != null) {
columns = findTableColumns(schema, target);
const table = queryContext.tables.find(
(table) => table.alias?.toLowerCase() === qualifier.toLowerCase(),
);
columns = findTableColumns(schema, table?.path ?? target);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/sql/query-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ function buildContextByRegex(rawSql: string): QueryContext {
const rawPath = match[1];
const rawAlias = match[2];
if (rawPath) {
const path = rawPath.split(".").map(stripIdentifierQuotes);
const path = (rawPath.match(new RegExp(IDENT, "g")) ?? []).map(stripIdentifierQuotes);
const name = path[path.length - 1] ?? "";
const alias =
rawAlias && !NON_ALIAS_KEYWORDS.has(rawAlias.toLowerCase())
Expand Down
Loading