From d4365037629a2784f1ce9ec4cc72a486ca96ac28 Mon Sep 17 00:00:00 2001 From: Ben King <9087625+benfdking@users.noreply.github.com> Date: Sun, 4 May 2025 17:32:00 +0100 Subject: [PATCH 1/2] fix(vscode): support aliased references --- sqlmesh/lsp/reference.py | 5 ++++- tests/lsp/test_reference.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/sqlmesh/lsp/reference.py b/sqlmesh/lsp/reference.py index 06b88871b..271cb96d3 100644 --- a/sqlmesh/lsp/reference.py +++ b/sqlmesh/lsp/reference.py @@ -55,7 +55,10 @@ def get_model_definitions_for_a_path( depends_on = model.depends_on # Normalize the table reference - reference_name = table.sql(dialect=model.dialect) + unaliased = table.copy() + if unaliased.args.get("alias") is not None: + unaliased.set("alias", None) + reference_name = unaliased.sql(dialect=model.dialect) normalized_reference_name = normalize_model_name( reference_name, default_catalog=lint_context.context.default_catalog, diff --git a/tests/lsp/test_reference.py b/tests/lsp/test_reference.py index 4ef25107a..cf700420e 100644 --- a/tests/lsp/test_reference.py +++ b/tests/lsp/test_reference.py @@ -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") From 019a001827587dbc51b018d61d66c9efffe3d7cd Mon Sep 17 00:00:00 2001 From: Ben King <9087625+benfdking@users.noreply.github.com> Date: Tue, 6 May 2025 10:52:37 +0100 Subject: [PATCH 2/2] wrap in try catch to handle other circumstances --- sqlmesh/lsp/reference.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/sqlmesh/lsp/reference.py b/sqlmesh/lsp/reference.py index 271cb96d3..1a957d6d9 100644 --- a/sqlmesh/lsp/reference.py +++ b/sqlmesh/lsp/reference.py @@ -59,12 +59,16 @@ def get_model_definitions_for_a_path( if unaliased.args.get("alias") is not None: unaliased.set("alias", None) reference_name = unaliased.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: + 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