From b082fa1be3bd2d458e3ddc967a244f0bc8daf873 Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Tue, 19 Sep 2017 19:22:45 -0400 Subject: [PATCH 1/2] make ref work like this --- dbt/context/common.py | 6 +----- dbt/context/runtime.py | 8 ++------ dbt/utils.py | 26 +++++++++++++++++++------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/dbt/context/common.py b/dbt/context/common.py index 2a6037e2eec..7a23df8575a 100644 --- a/dbt/context/common.py +++ b/dbt/context/common.py @@ -285,11 +285,7 @@ def generate(model, project, flat_graph, provider=None): "sql_now": adapter.date_function(), "fromjson": fromjson(model), "target": target, - "this": dbt.utils.This( - model.get('schema', schema), - dbt.utils.model_immediate_name(model, dbt.flags.NON_DESTRUCTIVE), - model.get('name') - ) + "this": dbt.utils.Relation(adapter, model) }) context = _add_tracking(context) diff --git a/dbt/context/runtime.py b/dbt/context/runtime.py index 29e67a130a8..bac4f5c40b3 100644 --- a/dbt/context/runtime.py +++ b/dbt/context/runtime.py @@ -50,13 +50,9 @@ def do_ref(*args): if dbt.utils.get_materialization(target_model) == 'ephemeral': model['extra_ctes'][target_model_id] = None - return '__dbt__CTE__{}'.format(target_model.get('name')) - else: - adapter = get_adapter(profile) - table = target_model.get('name') - schema = target_model.get('schema') - return adapter.quote_schema_and_table(profile, schema, table) + adapter = get_adapter(profile) + return dbt.utils.Relation(adapter, target_model) return do_ref diff --git a/dbt/utils.py b/dbt/utils.py index a841328c82b..189f86e932d 100644 --- a/dbt/utils.py +++ b/dbt/utils.py @@ -3,6 +3,7 @@ import itertools import dbt.exceptions +import dbt.flags from dbt.include import GLOBAL_DBT_MODULES_PATH from dbt.compat import basestring @@ -31,17 +32,28 @@ class ExitCodes(object): UnhandledError = 2 -class This(object): - def __init__(self, schema, table, name): - self.schema = schema +class Relation(object): + def __init__(self, adapter, node): + self._adapter = adapter + + # This is the name of the relation within a transaction + table = model_immediate_name(node, dbt.flags.NON_DESTRUCTIVE) + + self.node = node + self.schema = node.get('schema') + self.name = node.get('name') self.table = table - self.name = table if name is None else name - def schema_table(self, schema, table): - return '"{}"."{}"'.format(schema, table) + self.materialized = get_materialization(node) + self.sql = node.get('injected_sql') def __repr__(self): - return self.schema_table(self.schema, self.table) + if self.materialized == 'ephemeral': + return '__dbt__CTE__{}'.format(self.name) + else: + return self._adapter.quote_schema_and_table(profile=None, + schema=self.schema, + table=self.table) def coalesce(*args): From c285ee470b627d8bf1d550b406beea0a5a84dd05 Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Tue, 19 Sep 2017 20:53:56 -0400 Subject: [PATCH 2/2] no tmp suffix for refs --- dbt/context/common.py | 2 +- dbt/utils.py | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/dbt/context/common.py b/dbt/context/common.py index 7a23df8575a..9524eb4ed5e 100644 --- a/dbt/context/common.py +++ b/dbt/context/common.py @@ -285,7 +285,7 @@ def generate(model, project, flat_graph, provider=None): "sql_now": adapter.date_function(), "fromjson": fromjson(model), "target": target, - "this": dbt.utils.Relation(adapter, model) + "this": dbt.utils.Relation(adapter, model, use_temp=True) }) context = _add_tracking(context) diff --git a/dbt/utils.py b/dbt/utils.py index 189f86e932d..cd2b32e73c0 100644 --- a/dbt/utils.py +++ b/dbt/utils.py @@ -33,27 +33,31 @@ class ExitCodes(object): class Relation(object): - def __init__(self, adapter, node): + def __init__(self, adapter, node, use_temp=False): self._adapter = adapter - # This is the name of the relation within a transaction - table = model_immediate_name(node, dbt.flags.NON_DESTRUCTIVE) - self.node = node self.schema = node.get('schema') self.name = node.get('name') - self.table = table + + if use_temp: + self.table = self._get_table_name(node) + else: + self.table = self.name self.materialized = get_materialization(node) self.sql = node.get('injected_sql') + def _get_table_name(self, node): + return model_immediate_name(node, dbt.flags.NON_DESTRUCTIVE) + def __repr__(self): if self.materialized == 'ephemeral': return '__dbt__CTE__{}'.format(self.name) else: return self._adapter.quote_schema_and_table(profile=None, - schema=self.schema, - table=self.table) + schema=self.schema, + table=self.table) def coalesce(*args):