Skip to content

Commit

Permalink
Fix listing saved queries (#9736)
Browse files Browse the repository at this point in the history
* Add tests to check that saved queries show in `dbt list`

* Update `list` task to support saved queries

This is built off of @jtcohen6 work in d6e7cda on jerco/fix-9532.
I didn't directly cherry pick because there was more work to do as
well as merge conflicts. That is to say @jtcohen6 should be credited
with some of the work.

* Update error message when iterating over nodes during list command errors

This was originally suggested by @jtcohen6 in d6e7cda of jerco/fix-9532.
This commit just makes sure the change gets included because I didn't
cherry-pick that commit into this work.
  • Loading branch information
QMalcolm authored Mar 11, 2024
1 parent 7072a53 commit 8a395e9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20240307-142459.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Support saved queries in `dbt list`
time: 2024-03-07T14:24:59.530072-05:00
custom:
Author: QMalcolm jtcohen6
Issue: "9532"
1 change: 1 addition & 0 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@
[
"metric",
"semantic_model",
"saved_query",
"source",
"analysis",
"model",
Expand Down
10 changes: 9 additions & 1 deletion core/dbt/task/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Exposure,
SourceDefinition,
Metric,
SavedQuery,
SemanticModel,
UnitTestDefinition,
)
Expand Down Expand Up @@ -31,6 +32,7 @@ class ListTask(GraphRunnableTask):
NodeType.Source,
NodeType.Exposure,
NodeType.Metric,
NodeType.SavedQuery,
NodeType.SemanticModel,
NodeType.Unit,
)
Expand Down Expand Up @@ -83,10 +85,12 @@ def _iterate_selected_nodes(self):
yield self.manifest.semantic_models[unique_id]
elif unique_id in self.manifest.unit_tests:
yield self.manifest.unit_tests[unique_id]
elif unique_id in self.manifest.saved_queries:
yield self.manifest.saved_queries[unique_id]
else:
raise DbtRuntimeError(
f'Got an unexpected result from node selection: "{unique_id}"'
f"Expected a source or a node!"
f"Listing this node type is not yet supported!"
)

def generate_selectors(self):
Expand All @@ -106,6 +110,10 @@ def generate_selectors(self):
# metrics are searched for by pkg.metric_name
metric_selector = ".".join([node.package_name, node.name])
yield f"metric:{metric_selector}"
elif node.resource_type == NodeType.SavedQuery:
assert isinstance(node, SavedQuery)
saved_query_selector = ".".join([node.package_name, node.name])
yield f"saved_query:{saved_query_selector}"
elif node.resource_type == NodeType.SemanticModel:
assert isinstance(node, SemanticModel)
semantic_model_selector = ".".join([node.package_name, node.name])
Expand Down
24 changes: 24 additions & 0 deletions tests/functional/list/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@
"""


saved_queries__sq_yml = """
saved_queries:
- name: my_saved_query
label: My Saved Query
query_params:
metrics:
- total_outer
group_by:
- "Dimension('my_entity__created_at')"
exports:
- name: my_export
config:
alias: my_export_alias
export_as: table
schema: my_export_schema_name
"""


@pytest.fixture(scope="class")
def snapshots():
return {"snapshot.sql": snapshots__snapshot_sql}
Expand All @@ -164,6 +182,7 @@ def models():
"docs.md": models__docs_md,
"outer.sql": models__outer_sql,
"metricflow_time_spine.sql": models__metric_flow,
"sq.yml": saved_queries__sq_yml,
"sm.yml": semantic_models__sm_yml,
"m.yml": metrics__m_yml,
"sub": {"inner.sql": models__sub__inner_sql},
Expand Down Expand Up @@ -195,6 +214,11 @@ def metrics():
return {"m.yml": metrics__m_yml}


@pytest.fixture(scope="class")
def saved_queries():
return {"sq.yml": saved_queries__sq_yml}


@pytest.fixture(scope="class")
def project_files(
project_root,
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/list/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
analyses,
semantic_models,
metrics,
saved_queries,
project_files,
)

Expand Down Expand Up @@ -596,6 +597,7 @@ def expect_all_output(self):
"test.t",
"semantic_model:test.my_sm",
"metric:test.total_outer",
"saved_query:test.my_saved_query",
}
# analyses have their type inserted into their fqn like tests
expected_all = expected_default | {"test.analysis.a"}
Expand Down Expand Up @@ -626,6 +628,9 @@ def expect_select(self):
results = self.run_dbt_ls(["--resource-type", "metric"])
assert set(results) == {"metric:test.total_outer"}

results = self.run_dbt_ls(["--resource-type", "saved_query"])
assert set(results) == {"saved_query:test.my_saved_query"}

results = self.run_dbt_ls(["--resource-type", "model", "--select", "outer+"])
assert set(results) == {"test.outer", "test.sub.inner"}

Expand Down

0 comments on commit 8a395e9

Please sign in to comment.