adding config option for table and incremental materializaions #1313
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
resolves #
docs dbt-labs/docs.getdbt.com/#
Problem
BigQuery generates inefficient execution plans when doing comparisons on nested queries. For example;
BigQuery also generates inefficient execution plans using a CTE approach. For example;
BigQuery variables are unable to be used as they require declaration at the top of a script. The existing
pre_hook
options are not suitable as they run in a separate script.So the current best practices to solve this in dbt is to use a
run_query
macro to set a dbt variable from a SQL query. This results in two separate SQL scripts being generated. For example;This approach executes these two separate scripts;
The current solution has a few drawbacks;
TIMESTAMP
which is converted to a string in the dbt variable and thenCAST
back to aTIMESTAMP
in the subsequent query.Solution
This macro and materialization update enables running SQL code at the beginning of a BigQuery SQL script. Which allows dbt models to
DECLARE
andSET
BigQuery variables before theCREATE OR REPLACE TABLE
statement.Using this approach, what would have required two separate scripts to produce an efficient execution plan in BigQuery can now be achieved in a single script.
Alternative approaches considered;
Having a
bq_varables
option that accepted a list of dicts with information about the variable type and defaults. I decided against this as there may be other use cases for running SQL within a single script at the beginning of a model.Not making these changes and only using dbt variables instead of BigQuery variables. I decided against this as setting dbt variables from a SQL query currently requires executing the
run_query()
macro which generates a separate SQL script. As mentioned above, this can result in a single model creating dozens of separate SQL scripts to run.Checklist