Skip to content

fix(vscode): support aliased references #4309

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
May 6, 2025
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
21 changes: 14 additions & 7 deletions sqlmesh/lsp/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,20 @@ def get_model_definitions_for_a_path(
depends_on = model.depends_on

# Normalize the table reference
reference_name = table.sql(dialect=model.dialect)
normalized_reference_name = normalize_model_name(
reference_name,
default_catalog=lint_context.context.default_catalog,
dialect=model.dialect,
)
if normalized_reference_name not in depends_on:
unaliased = table.copy()
if unaliased.args.get("alias") is not None:
unaliased.set("alias", None)
reference_name = unaliased.sql(dialect=model.dialect)
try:
normalized_reference_name = normalize_model_name(
reference_name,
default_catalog=lint_context.context.default_catalog,
dialect=model.dialect,
)
if normalized_reference_name not in depends_on:
continue
except Exception:
# Skip references that cannot be normalized
continue

# Get the referenced model uri
Expand Down
17 changes: 17 additions & 0 deletions tests/lsp/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,20 @@ def test_reference() -> None:
referenced_text += read_file[line_num]
referenced_text += read_file[end_line][:end_character] # Last line up to end_character
assert referenced_text == "sushi.customers"


@pytest.mark.fast
def test_reference_with_alias() -> None:
context = Context(paths=["examples/sushi"])
lsp_context = LSPContext(context)

waiter_revenue_by_day_uri = next(
uri for uri, models in lsp_context.map.items() if "sushi.waiter_revenue_by_day" in models
)

references = get_model_definitions_for_a_path(lsp_context, waiter_revenue_by_day_uri)
assert len(references) == 3

assert references[0].uri.endswith("orders.py")
assert references[1].uri.endswith("order_items.py")
assert references[2].uri.endswith("items.py")