Skip to content

Commit

Permalink
update to allow adapters to change model name resolution in py models (
Browse files Browse the repository at this point in the history
…#7115)

* update to allow adapters to change model name resolution in py models

* add changie

* fix newline adds

* move quoting into macro

* use single quotes
  • Loading branch information
colin-rogers-dbt authored Mar 8, 2023
1 parent 64b8a12 commit c02ddf8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 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"
20 changes: 14 additions & 6 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 | string | replace('"', '\"')}) -%}
{%- 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 | string | replace('"', '\"')}) -%}
{%- 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 Down Expand Up @@ -65,9 +73,9 @@ class this:
database = "{{ this.database }}"
schema = "{{ this.schema }}"
identifier = "{{ this.identifier }}"
{% set this_relation_name = this | string | replace('"', '\\"') %}
{% set this_relation_name = resolve_model_name(this) %}
def __repr__(self):
return "{{ this_relation_name }}"
return '{{ this_relation_name }}'


class dbtObj:
Expand Down

0 comments on commit c02ddf8

Please sign in to comment.