Skip to content

Commit

Permalink
backport 7115 (#7152)
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-rogers-dbt authored Mar 10, 2023
1 parent d0a6ea9 commit 14f966c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230303-112519.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: allow adapters to change model name resolution in py models
time: 2023-03-03T11:25:19.276637-08:00
custom:
Author: colin-rogers-dbt
Issue: "7114"
29 changes: 19 additions & 10 deletions core/dbt/include/global_project/macros/python_model/python.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
{% macro resolve_model_name(input_model_name) %}
{{ return(adapter.dispatch('resolve_model_name', 'dbt')(input_model_name)) }}
{% endmacro %}

{%- macro default__resolve_model_name(input_model_name) -%}
{{ input_model_name | string | replace('"', '\"') }}
{%- endmacro -%}

{% macro build_ref_function(model) %}

{%- set ref_dict = {} -%}
{%- for _ref in model.refs -%}
{%- set resolved = ref(*_ref) -%}
{%- do ref_dict.update({_ref | join("."): resolved.quote(database=False, schema=False, identifier=False) | string}) -%}
{%- do ref_dict.update({_ref | join('.'): resolve_model_name(resolved)}) -%}
{%- endfor -%}

def ref(*args,dbt_load_df_function):
refs = {{ ref_dict | tojson }}
key = ".".join(args)
key = '.'.join(args)
return dbt_load_df_function(refs[key])

{% endmacro %}
Expand All @@ -18,12 +26,12 @@ def ref(*args,dbt_load_df_function):
{%- set source_dict = {} -%}
{%- for _source in model.sources -%}
{%- set resolved = source(*_source) -%}
{%- do source_dict.update({_source | join("."): resolved.quote(database=False, schema=False, identifier=False) | string}) -%}
{%- do source_dict.update({_source | join('.'): resolve_model_name(resolved)}) -%}
{%- endfor -%}

def source(*args, dbt_load_df_function):
sources = {{ source_dict | tojson }}
key = ".".join(args)
key = '.'.join(args)
return dbt_load_df_function(sources[key])

{% endmacro %}
Expand All @@ -33,8 +41,8 @@ def source(*args, dbt_load_df_function):
{% set config_dbt_used = zip(model.config.config_keys_used, model.config.config_keys_defaults) | list %}
{%- for key, default in config_dbt_used -%}
{# weird type testing with enum, would be much easier to write this logic in Python! #}
{%- if key == 'language' -%}
{%- set value = 'python' -%}
{%- if key == "language" -%}
{%- set value = "python" -%}
{%- endif -%}
{%- set value = model.config.get(key, default) -%}
{%- do config_dict.update({key: value}) -%}
Expand Down Expand Up @@ -62,11 +70,12 @@ class config:

class this:
"""dbt.this() or dbt.this.identifier"""
database = '{{ this.database }}'
schema = '{{ this.schema }}'
identifier = '{{ this.identifier }}'
database = "{{ this.database }}"
schema = "{{ this.schema }}"
identifier = "{{ this.identifier }}"
{% set this_relation_name = resolve_model_name(this) %}
def __repr__(self):
return '{{ this }}'
return '{{ this_relation_name }}'


class dbtObj:
Expand Down

0 comments on commit 14f966c

Please sign in to comment.