Skip to content
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

feat(python): allows to use prj. vars #339

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20241031-160641.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: allows to use prj. vars in python models
time: 2024-10-31T16:06:41.214334214-03:00
custom:
Author: devmessias
Issue: 5617 10914
26 changes: 26 additions & 0 deletions dbt/include/global_project/macros/python_model/python.sql
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ def source(*args, dbt_load_df_function):
config_dict = {{ config_dict }}
{% endmacro %}


{% macro build_var_dict(model, context) %}
{%- set var_dict = {} -%}
{% set var_dbt_used = zip(model.config.var_keys_used, model.config.var_keys_defaults) | list %}
{%- for key, default in var_dbt_used -%}
{# weird type testing with enum, would be much easier to write this logic in Python! #}
{%- if key == "language" -%}
{%- set value = "python" -%}
{%- endif -%}
{%- set value = context.var(key, default) -%}
{%- do var_dict.update({key: value}) -%}
{%- endfor -%}
var_dict = {{ var_dict }}
{% endmacro %}

{% macro py_script_postfix(model) %}
# This part is user provided model code
# you will need to copy the next section to run the code
Expand All @@ -67,6 +82,7 @@ config_dict = {{ config_dict }}
{{ build_ref_function(model ) }}
{{ build_source_function(model ) }}
{{ build_config_dict(model) }}
{{ build_var_dict(model, context) }}

class config:
def __init__(self, *args, **kwargs):
Expand All @@ -76,6 +92,15 @@ class config:
def get(key, default=None):
return config_dict.get(key, default)

class DbtVar:
def __init__(self, *args, **kwargs):
pass

@staticmethod
def get(key, default=None):
return var_dict.get(key, default)


class this:
"""dbt.this() or dbt.this.identifier"""
database = "{{ this.database }}"
Expand All @@ -91,6 +116,7 @@ class dbtObj:
self.source = lambda *args: source(*args, dbt_load_df_function=load_df_function)
self.ref = lambda *args, **kwargs: ref(*args, **kwargs, dbt_load_df_function=load_df_function)
self.config = config
self.var = DbtVar
self.this = this()
self.is_incremental = {{ is_incremental() }}

Expand Down