-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
this.is_view and this.is_table not working in BigQuery inside a hook #3529
Comments
@jmriego Thanks for opening! This is a sort of known issue (mentioned in #2938 as well): Following that, you've got two options:
{{
config(
post_hook=['ALTER {{"view" if model.config.materialized == "view" else "table"}} {{ this }} SET OPTIONS (description="second model...")'],
)
}}
-- Use the `ref` function to select from other models
select *
from {{ ref('my_first_dbt_model') }}
where id = 1
-- macros/alter_this.sql
{% macro alter_this(this) %}
{% set this_typed = adapter.get_relation(this.database, this.schema, this.name) %}
ALTER {{"view" if this_typed.is_view else "table"}} {{ this_typed }}
SET OPTIONS (description="second model...")
{% endmacro %} -- models/my_model.sql
{{
config(
post_hook=["{{ alter_this(this) }}"],
)
}}
-- Use the `ref` function to select from other models
select *
from {{ ref('my_first_dbt_model') }}
where id = 1 Note that the macro call must be nested in quotes + another set of curlies, in order to be re-parsed at execute time (see #2370). The question is, should dbt use one of those approaches by default to set I'm going to close this particular issue for now as a |
sure @jtcohen6 that all makes sense. Thanks for the explanation and the workaround!
does this mean that the post_hook config is parsed before the model has run and then that sql is ran at the end? |
This is the confusing behavior described in #2370:
Because of that double-rendering, these yield different results: post_hook=alter_this(this),
post_hook="{{ alter_this(this) }}", In the first case, "post-hook": [
{
"sql": "\n\n \n\n ALTER table None\n SET OPTIONS (description=\"second model...\")\n\n",
"transaction": true,
"index": null
}
], In the second case, parse-time Jinja rendering just removes the first set of curlies, leaving the main macro call in place to be re-rendered during execution: "post-hook": [
{
"sql": "{{ alter_this(this) }}",
"transaction": true,
"index": null
}
], |
Describe the bug
I'm trying to use the
.is_table
and.is_view
methods but they don't seem to be working in BigQuerySteps To Reproduce
Create a sample project with
dbt init
and modifymy_second_dbt_model.sql
to this:Run this model and you'll get an error. Actually, both
this.is_view
andthis.is_table
returnFalse
in JinjaExpected behavior
I would expect this to run successfully, but it doesn't render as expected.
Screenshots and log output
System information
Which database are you using dbt with?
The output of
dbt --version
:The operating system you're using:
Ubunt4 20.04
The output of
python --version
:Python 3.8.5
Additional context
Nothing else I can think of
The text was updated successfully, but these errors were encountered: