Repository: dbt-labs/dbt-core

Issue:
[CT-2680] [Bug] Allow referencing private models within `dbt show` "inline" queries
Currently, dbt will raise a `DbtReferenceError` when trying to preview an `--inline` query that `ref`s a private model, because the inline query is not part of the same model `group` as the private model.

In my opinion:
- It should be possible to preview a private model, or a query that references that model
- If you then save that query in a model in a different `group`, you'll start seeing the reference error — which makes sense!

### Reproduction case

```yml
groups:
  - name: my_cool_group
    owner:
      name: Me

models:
  - name: my_model
    access: private
    config:
      group: my_cool_group
```

```bash
$ dbt show --inline "select * from {{ ref('my_model') }}"
22:51:45  Running with dbt=1.6.0-b2
22:51:45  Encountered an error:
Parsing Error
  Node sql operation.my_dbt_project.inline_query attempted to reference node model.my_dbt_project.my_model, which is not allowed because the referenced node is private to the my_cool_group group.
```

(While here: let's change the string rendering of `sql operation` to something without spaces - `sqloperation`)

### Looms

Users running into this error in the dbt Cloud IDE:
- https://www.loom.com/share/763aa5e9161e424c9f01736dc1459792
- https://www.loom.com/share/6b222e10e19d460e922bd4ee51f6fe1e


Discussion:

Resolving patch:
diff --git a/core/dbt/context/providers.py b/core/dbt/context/providers.py
--- a/core/dbt/context/providers.py
+++ b/core/dbt/context/providers.py
@@ -502,6 +502,9 @@ def resolve(
         elif (
             target_model.resource_type == NodeType.Model
             and target_model.access == AccessType.Private
+            # don't raise this reference error for ad hoc 'preview' queries
+            and self.model.resource_type != NodeType.SqlOperation
+            and self.model.resource_type != NodeType.RPCCall  # TODO: rm
         ):
             if not self.model.group or self.model.group != target_model.group:
                 raise DbtReferenceError(
diff --git a/core/dbt/parser/manifest.py b/core/dbt/parser/manifest.py
--- a/core/dbt/parser/manifest.py
+++ b/core/dbt/parser/manifest.py
@@ -1683,8 +1683,13 @@ def _process_refs_for_node(manifest: Manifest, current_project: str, node: Manif
             )
             continue
 
-        # Handle references to models that are private
-        elif isinstance(target_model, ModelNode) and target_model.access == AccessType.Private:
+        # Handle references to models that are private, unless this is an 'ad hoc' query (SqlOperation, RPCCall)
+        elif (
+            isinstance(target_model, ModelNode)
+            and target_model.access == AccessType.Private
+            and node.resource_type != NodeType.SqlOperation
+            and node.resource_type != NodeType.RPCCall  # TODO: rm
+        ):
             if not node.group or node.group != target_model.group:
                 raise dbt.exceptions.DbtReferenceError(
                     unique_id=node.unique_id,


Test patch:
diff --git a/tests/functional/show/fixtures.py b/tests/functional/show/fixtures.py
--- a/tests/functional/show/fixtures.py
+++ b/tests/functional/show/fixtures.py
@@ -17,6 +17,18 @@
 select current_setting('timezone') as timezone
 """
 
+private_model_yml = """
+groups:
+  - name: my_cool_group
+    owner: {name: me}
+
+models:
+  - name: private_model
+    access: private
+    config:
+      group: my_cool_group
+"""
+
 
 schema_yml = """
 models:
diff --git a/tests/functional/show/test_show.py b/tests/functional/show/test_show.py
--- a/tests/functional/show/test_show.py
+++ b/tests/functional/show/test_show.py
@@ -10,6 +10,7 @@
     models__ephemeral_model,
     schema_yml,
     models__sql_header,
+    private_model_yml,
 )
 
 
@@ -137,3 +138,20 @@ def test_none(self, project):
         (results, log_output) = run_dbt_and_capture(["show", "--select", "sample_model.v2"])
         assert "Previewing node 'sample_model.v1'" not in log_output
         assert "Previewing node 'sample_model.v2'" in log_output
+
+
+class TestShowPrivateModel:
+    @pytest.fixture(scope="class")
+    def models(self):
+        return {
+            "schema.yml": private_model_yml,
+            "private_model.sql": models__sample_model,
+        }
+
+    @pytest.fixture(scope="class")
+    def seeds(self):
+        return {"sample_seed.csv": seeds__sample_seed}
+
+    def test_version_unspecified(self, project):
+        run_dbt(["build"])
+        run_dbt(["show", "--inline", "select * from {{ ref('private_model') }}"])