dbt-labs / dbt-labs/dbt-adapters

[Bug] `--cache-selected-only` is a no-op for `dbt test`: empty `cache_schemas` treated as `None`

Open
#2,126 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
233
Forks
362
Avg merge
3d 22h
Merged PRs (30d)
9

Description

### Is this a new bug in dbt-adapters?

- [x] I believe this is a new bug in dbt-adapters
- [x] I have searched the existing issues, and I could not find an existing issue for this bug

### Current Behavior

`BaseAdapter._relations_cache_for_schemas` treats an empty `cache_schemas` set the same as `cache_schemas=None`:

https://github.com/dbt-labs/dbt-adapters/blob/main/dbt-adapters/src/dbt/adapters/base/impl.py#L593-L602

```python
def _relations_cache_for_schemas(
self,
relation_configs: Iterable[RelationConfig],
cache_schemas: Optional[Set[BaseRelation]] = None,
) -> None:
"""Populate the relations cache for the given schemas. Returns an
iterable of the schemas populated, as strings.
"""
if not cache_schemas:
cache_schemas = self._get_cache_schemas(relation_configs)
...
```

For `dbt test`, dbt-core's `get_model_schemas` legitimately returns an *empty set*, because test nodes aren't `is_relational`. With `--cache-selected-only` on, that empty set means "no schemas are needed for this selection" — but `if not cache_schemas:` is truthy for `set()`, so it falls through to `_get_cache_schemas(relation_configs)`, which re-expands to every schema in the project. The flag becomes a no-op for `dbt test`.

### Expected Behavior

An explicitly empty `cache_schemas` set should be respected (introspect nothing), and the "compute the default schemas" fallback should only trigger when `cache_schemas is None`.

### Steps To Reproduce

Reported downstream in dbt-labs/dbt-core#12944 (see [this comment](https://github.com/dbt-labs/dbt-core/issues/12944#issuecomment-5222202417)):

1. A project with models spread across many schemas, `cache_selected_only` enabled.
2. `dbt test -s `
3. Expected: only the selected model's schema is introspected for the relation cache.
4. Actual: every schema in the project is introspected, because `get_model_schemas` returns an empty set for the test selection and `_relations_cache_for_schemas` treats that as "compute the default (all schemas)".

### Relevant log output

_No response_

### Environment

- dbt-adapters: 1.24.5 (also present on `main`)

### Which database adapter are you using with dbt?

other (adapter-agnostic — logic lives in `BaseAdapter`)

### Additional Context

Proposed fix (from the linked dbt-core issue comment): change the falsy check to an explicit `is None` check:

```python
if cache_schemas is None:
cache_schemas = self._get_cache_schemas(relation_configs)
```

This is a companion issue to dbt-labs/dbt-core#12944, which covers the same `--cache-selected-only` gap for `compile`/`show`/`docs generate` (fixed in dbt-labs/dbt-core#15915). This one is being tracked separately because the fix here changes `dbt test` behavior and was flagged as needing a maintainer's call rather than a drive-by fix.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.