question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

using `{{ ref('anything') }}` in a `set_sql_header` call resolves to the current model and not 'anything'

See original GitHub issue

Describe the bug

Here’s a minimal reproducible example (running on BigQuery).

-- test_tmp_1.sql
{{
    config(materialized="table")
}}

select a.* from unnest([
    struct(1 as key, 1 as value)
]) as a
-- test_tmp_2.sql
{{
    config(materialized="table")
}}

{% call set_sql_header(config) %}
    select * from {{ ref('test_tmp_1') }};
{% endcall %}

select * from {{ ref('test_tmp_1') }}

When I dbt run this project, I get an error when test_tmp_2 is run saying something along the lines of “test_tmp_2 does not exist”.

The output SQL for test_tmp_2 after a dbt run shows:


    select * from `project`.`dataset`.`test_tmp_2`;


  create or replace table `project`.`dataset`.`test_tmp_2`
  
  
  OPTIONS()
  as (
    



select * from `project`.`dataset`.`test_tmp_1`
  );
    

Expected behavior

The ref('test_tmp_1') call in the call set_sql_header(config) block should resolve to test_tmp_1.

Instead it resolves to test_tmp_2.

System information

Which database are you using dbt with?

  • postgres
  • redshift
  • bigquery
  • snowflake
  • other (specify: ____________)

The output of dbt --version:

installed version: 0.18.0
   latest version: 0.18.0

Up to date!

Plugins:
  - bigquery: 0.18.0
  - snowflake: 0.18.0
  - redshift: 0.18.0
  - postgres: 0.18.0

The operating system you’re using:

macOS 10.15.6

The output of python --version:

Python 3.8.1

Additional context

N/A

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
gmailMichaelShacommented, Apr 8, 2022

I am seeing this issue when I am trying to create a temporary table using set_sql_header with a source. Using snowflake database

{{
    config(
        materialized='table',
    )
}}
{%- call set_sql_header(config) -%}
    CREATE OR REPLACE TEMPORARY TABLE {{ target.database }}.{{target.schema}}.temp_tbl_1 AS
    SELECT * FROM {{ source('source_1', 'world_cities') }};
{%- endcall -%}

SELECT *
FROM {{ target.database ~ "." ~ target.schema ~ ".temp_tbl_1" }}
0reactions
elyobocommented, Oct 30, 2022

Another use case: setting a variable to the result of part of a query, so that it can be referenced in multiple places. BigQuery reevaluates CTEs if they’re queried more than once (I assume there’s some rational behaviour for this, I guess due to parallelisation?) and so the recommended approach is to set a variable to the result and reference it instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A complete guide to React refs - LogRocket Blog
Learn how to use React refs, and why it's important to use them only when React can't handle a function call through its...
Read more >
Everything You Need to Know About Refs in React
In this post, you will learn how to properly use refs, how to use the current API, and decide when to approach one...
Read more >
React: why is that changing the current value of ref from ...
Short answer, no. The only things that cause a re-render in React are the following: A state change within the component (via the...
Read more >
Referencing Values with Refs - React Docs Beta
You can access the current value of that ref through the ref.current property. This value is intentionally mutable, meaning you can both read...
Read more >
Azure Private Endpoint DNS configuration | Microsoft Learn
Existing Private DNS Zones tied to a single service should not be ... you can adjust the model using the following reference: Azure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found