Skip to content

Commit e09aa80

Browse files
authored
feat(vscode): add end point for column lineage (#4450)
1 parent 7ba0c10 commit e09aa80

3 files changed

Lines changed: 41 additions & 12 deletions

File tree

sqlmesh/lsp/api.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import typing as t
1111
from pydantic import field_validator
1212
from sqlmesh.utils.pydantic import PydanticModel
13-
from web.server.models import Model
13+
from web.server.models import LineageColumn, Model
1414

1515
API_FEATURE = "sqlmesh/api"
1616

@@ -59,3 +59,11 @@ class ApiResponseGetLineage(PydanticModel):
5959
"""
6060

6161
data: t.Dict[str, t.List[str]]
62+
63+
64+
class ApiResponseGetColumnLineage(PydanticModel):
65+
"""
66+
Response from the SQLMesh API for the get_column_lineage endpoint.
67+
"""
68+
69+
data: t.Dict[str, t.Dict[str, LineageColumn]]

sqlmesh/lsp/main.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import typing as t
66
from pathlib import Path
7+
import urllib.parse
78

89
from lsprotocol import types
910
from pygls.server import LanguageServer
@@ -14,6 +15,7 @@
1415
from sqlmesh.lsp.api import (
1516
API_FEATURE,
1617
ApiRequest,
18+
ApiResponseGetColumnLineage,
1719
ApiResponseGetLineage,
1820
ApiResponseGetModels,
1921
)
@@ -24,7 +26,7 @@
2426
get_references,
2527
)
2628
from sqlmesh.lsp.uri import URI
27-
from web.server.api.endpoints.lineage import model_lineage
29+
from web.server.api.endpoints.lineage import column_lineage, model_lineage
2830
from web.server.api.endpoints.models import get_models
2931

3032

@@ -92,18 +94,37 @@ def all_models(ls: LanguageServer, params: AllModelsRequest) -> AllModelsRespons
9294
@self.server.feature(API_FEATURE)
9395
def api(
9496
ls: LanguageServer, request: ApiRequest
95-
) -> t.Union[ApiResponseGetModels, ApiResponseGetLineage]:
97+
) -> t.Union[ApiResponseGetModels, ApiResponseGetLineage, ApiResponseGetColumnLineage]:
9698
ls.log_trace(f"API request: {request}")
9799
if self.lsp_context is None:
98100
raise RuntimeError("No context found")
99-
if request.url == "/api/models":
100-
response = ApiResponseGetModels(data=get_models(self.lsp_context.context))
101-
return response
102-
if request.url.startswith("/api/lineage"):
103-
name = request.url.split("/")[-1]
104-
lineage = model_lineage(name, self.lsp_context.context)
105-
non_set_lineage = {k: v for k, v in lineage.items() if v is not None}
106-
return ApiResponseGetLineage(data=non_set_lineage)
101+
102+
parsed_url = urllib.parse.urlparse(request.url)
103+
path_parts = parsed_url.path.strip("/").split("/")
104+
105+
if request.method == "GET":
106+
if path_parts == ["api", "models"]:
107+
# /api/models
108+
return ApiResponseGetModels(data=get_models(self.lsp_context.context))
109+
110+
if path_parts[:2] == ["api", "lineage"]:
111+
if len(path_parts) == 3:
112+
# /api/lineage/{model}
113+
model_name = urllib.parse.unquote(path_parts[2])
114+
lineage = model_lineage(model_name, self.lsp_context.context)
115+
non_set_lineage = {k: v for k, v in lineage.items() if v is not None}
116+
return ApiResponseGetLineage(data=non_set_lineage)
117+
118+
if len(path_parts) == 4:
119+
# /api/lineage/{model}/{column}
120+
model_name = urllib.parse.unquote(path_parts[2])
121+
column = urllib.parse.unquote(path_parts[3])
122+
logging.info(f"Column lineage request: {model_name} {column}")
123+
column_lineage_response = column_lineage(
124+
model_name, column, False, self.lsp_context.context
125+
)
126+
return ApiResponseGetColumnLineage(data=column_lineage_response)
127+
107128
raise NotImplementedError(f"API request not implemented: {request.url}")
108129

109130
@self.server.feature(types.TEXT_DOCUMENT_DID_OPEN)

web/server/api/endpoints/lineage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def create_models_only_lineage_adjacency_list(
126126

127127

128128
@router.get("/{model_name:str}/{column_name:str}")
129-
async def column_lineage(
129+
def column_lineage(
130130
model_name: str,
131131
column_name: str,
132132
models_only: bool = False,

0 commit comments

Comments
 (0)