Skip to content

Commit 91874ba

Browse files
committed
Fix: Exclude non-model references from the selector's output (#2068)
1 parent 7515990 commit 91874ba

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

sqlmesh/core/selector.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,16 @@ def _get_value_and_dependency_inclusion(value: str) -> t.Tuple[str, bool, bool]:
134134
return value, include_upstream, include_downstream
135135

136136
def _get_models(
137-
self, model_name: str, include_upstream: bool, include_downstream: bool
137+
self,
138+
model_name: str,
139+
include_upstream: bool,
140+
include_downstream: bool,
141+
models: t.Optional[t.Dict[str, Model]] = None,
138142
) -> t.Set[str]:
139143
result = {model_name}
140144
if include_upstream:
141-
result.update(self._dag.upstream(model_name))
145+
models = models or self._models
146+
result.update([u for u in self._dag.upstream(model_name) if u in models])
142147
if include_downstream:
143148
result.update(self._dag.downstream(model_name))
144149
return result
@@ -222,6 +227,8 @@ def expand_model_selections(
222227
logger.warning(f"Expression '{selection}' doesn't match any models.")
223228

224229
for model_fqn in matched_models:
225-
results.update(self._get_models(model_fqn, include_upstream, include_downstream))
230+
results.update(
231+
self._get_models(model_fqn, include_upstream, include_downstream, models=models)
232+
)
226233

227234
return results

tests/core/test_selector.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,36 @@ def test_expand_model_tags(mocker: MockerFixture, make_snapshot, tags, tag_selec
349349
assert selector.expand_model_selections(tag_selections) == output
350350

351351

352+
def test_select_models_with_external_parent(mocker: MockerFixture):
353+
default_catalog = "test_catalog"
354+
added_model = SqlModel(
355+
name="db.added_model",
356+
query=d.parse_one("SELECT 1 AS a FROM external"),
357+
default_catalog=default_catalog,
358+
tags=["tag1"],
359+
)
360+
361+
env_name = "test_env"
362+
363+
state_reader_mock = mocker.Mock()
364+
state_reader_mock.get_environment.return_value = Environment(
365+
name=env_name,
366+
snapshots=[],
367+
start_at="2023-01-01",
368+
end_at="2023-02-01",
369+
plan_id="test_plan_id",
370+
)
371+
state_reader_mock.get_snapshots.return_value = {}
372+
373+
local_models: UniqueKeyDict[str, Model] = UniqueKeyDict("models")
374+
local_models[added_model.fqn] = added_model
375+
376+
selector = Selector(state_reader_mock, local_models, default_catalog=default_catalog)
377+
378+
expanded_selections = selector.expand_model_selections(["+*added_model*"])
379+
assert expanded_selections == {added_model.fqn}
380+
381+
352382
def _assert_models_equal(actual: t.Dict[str, Model], expected: t.Dict[str, Model]) -> None:
353383
assert set(actual) == set(expected)
354384
for name, model in actual.items():

0 commit comments

Comments
 (0)