Skip to content

Commit 7e09d18

Browse files
authored
fix(vscode): support aliased references (#4309)
1 parent 03a07f1 commit 7e09d18

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

sqlmesh/lsp/reference.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,20 @@ def get_model_definitions_for_a_path(
5555
depends_on = model.depends_on
5656

5757
# Normalize the table reference
58-
reference_name = table.sql(dialect=model.dialect)
59-
normalized_reference_name = normalize_model_name(
60-
reference_name,
61-
default_catalog=lint_context.context.default_catalog,
62-
dialect=model.dialect,
63-
)
64-
if normalized_reference_name not in depends_on:
58+
unaliased = table.copy()
59+
if unaliased.args.get("alias") is not None:
60+
unaliased.set("alias", None)
61+
reference_name = unaliased.sql(dialect=model.dialect)
62+
try:
63+
normalized_reference_name = normalize_model_name(
64+
reference_name,
65+
default_catalog=lint_context.context.default_catalog,
66+
dialect=model.dialect,
67+
)
68+
if normalized_reference_name not in depends_on:
69+
continue
70+
except Exception:
71+
# Skip references that cannot be normalized
6572
continue
6673

6774
# Get the referenced model uri

tests/lsp/test_reference.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,20 @@ def test_reference() -> None:
4646
referenced_text += read_file[line_num]
4747
referenced_text += read_file[end_line][:end_character] # Last line up to end_character
4848
assert referenced_text == "sushi.customers"
49+
50+
51+
@pytest.mark.fast
52+
def test_reference_with_alias() -> None:
53+
context = Context(paths=["examples/sushi"])
54+
lsp_context = LSPContext(context)
55+
56+
waiter_revenue_by_day_uri = next(
57+
uri for uri, models in lsp_context.map.items() if "sushi.waiter_revenue_by_day" in models
58+
)
59+
60+
references = get_model_definitions_for_a_path(lsp_context, waiter_revenue_by_day_uri)
61+
assert len(references) == 3
62+
63+
assert references[0].uri.endswith("orders.py")
64+
assert references[1].uri.endswith("order_items.py")
65+
assert references[2].uri.endswith("items.py")

0 commit comments

Comments
 (0)