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

update to allow adapters to change model name resolution in py models #7115

Merged
merged 5 commits into from
Mar 8, 2023
Merged
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/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