dbt-labs / dbt-labs/dbt-adapters

[Bug] BigQuery incremental can inject _dbt_max_partition from comment text and fail first run

Open
#1,907 0 comments 0 reactions 0 assignees View on GitHub
triage:product
Dominant language
Python
Stars
233
Forks
362
Avg merge
3d 22h
Merged PRs (30d)
9

Description

### Is this a new bug?

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

### Which package is affected?

- `dbt-bigquery` (in `dbt-labs/dbt-adapters` monorepo)

### Current Behavior

For an incremental model on BigQuery (`insert_overwrite` + partitioned), if the literal token `_dbt_max_partition` appears **only inside SQL comments**, dbt still injects `_dbt_max_partition` script logic.

On a first run (target table does not yet exist), the injected block queries `{{ this }}` before creation and fails:

```sql
declare _dbt_max_partition date default (
select max(partition_col) from `project.dataset.my_incremental_model`
where partition_col is not null
);
```

Error:

```text
Not found: Table project:dataset.my_incremental_model was not found in location US
```

### Expected Behavior

`_dbt_max_partition` helper injection should not be triggered by occurrences inside comments (or string literals). It should be based on executable SQL references only.

### Steps To Reproduce

1. Create an incremental model:

```sql
{{
config(
materialized='incremental',
incremental_strategy='insert_overwrite',
partition_by={'field': 'partition_col', 'data_type': 'date', 'granularity': 'day'},
on_schema_change='append_new_columns'
)
}}

/* This text mentions _dbt_max_partition only in a comment */

select date('2026-04-30') as partition_col
```

2. Ensure target table does not exist.
3. Run `dbt run -s `.
4. Observe compiled script contains `declare _dbt_max_partition ... from ` and fails with table-not-found.
5. Remove/rename the comment token (without changing executable SQL), rerun, and model succeeds.

### Relevant notes

- Reproduced on `dbt-core==1.11.2` and `dbt-bigquery==1.11.0`.
- Dataset location was `US`; this does not appear to be a cross-region issue.
- Related but different historical issue: #583.

### Environment

- OS: macOS
- Python: 3.10.x
- dbt-core: 1.11.2
- dbt-bigquery: 1.11.0

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.