Skip to content

Commit 6fc3011

Browse files
authored
Merge pull request #193 from analyst-collective/fix/incremental-type-expansion
Fix/incremental type expansion
2 parents c8c049e + 11d9a3d commit 6fc3011

File tree

6 files changed

+46
-39
lines changed

6 files changed

+46
-39
lines changed

dbt/project.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
'modules-path': 'dbt_modules'
2121
}
2222

23-
default_profiles = {
24-
'user': {}
25-
}
23+
default_profiles = {}
2624

2725
class DbtProjectError(Exception):
2826
def __init__(self, message, project):
@@ -43,7 +41,7 @@ def __init__(self, cfg, profiles, profile_to_load=None):
4341
self.profile_to_load = self.cfg['profile']
4442

4543
if self.profile_to_load is None:
46-
self.profile_to_load = 'user'
44+
raise DbtProjectError("No profile was supplied in the dbt_project.yml file, or the command line", self)
4745

4846
if self.profile_to_load in self.profiles:
4947
self.cfg.update(self.profiles[self.profile_to_load])

dbt/runner.py

+1
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ def __init__(self, project, target_path, graph_type, threads):
283283
"invocation_id" : dbt.tracking.invocation_id,
284284
"get_columns_in_table" : self.schema.get_columns_in_table,
285285
"get_missing_columns" : self.schema.get_missing_columns,
286+
"already_exists" : self.schema.table_exists,
286287
}
287288

288289

dbt/schema.py

+17
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def __init__(self, column, dtype, char_size):
2828
def name(self):
2929
return self.column
3030

31+
@property
32+
def quoted(self):
33+
return '"{}"'.format(self.column)
34+
3135
@property
3236
def data_type(self):
3337
if self.is_string():
@@ -69,6 +73,7 @@ def __init__(self, project, target):
6973
self.logger = logging.getLogger(__name__)
7074

7175
self.schema_cache = {}
76+
self.runtime_existing = self.query_for_existing(self.target.schema)
7277

7378
def cache_table_columns(self, schema, table, columns):
7479
tid = (schema, table)
@@ -285,3 +290,15 @@ def expand_column_types_if_needed(self, temp_table, to_schema, to_table):
285290
self.logger.debug("Changing col type from %s to %s in table %s.%s", dest_column.data_type, new_type, to_schema, to_table)
286291
self.alter_column_type(to_schema, to_table, column_name, new_type)
287292

293+
# update these cols in the cache! This is a hack to fix broken incremental models for type expansion. TODO
294+
self.cache_table_columns(to_schema, to_table, source_columns)
295+
296+
def table_exists(self, schema, table):
297+
if schema == self.target.schema:
298+
exists = self.runtime_existing.get(table) is not None
299+
return exists
300+
else:
301+
tables = self.query_for_existing(schema)
302+
exists = tables.get(table) is not None
303+
return exists
304+

dbt/task/deps.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def __pull_deps_recursive(self, repos, processed_repos = set(), i=0):
101101
dep_project = project.read_project(
102102
os.path.join(self.project['modules-path'],
103103
dep_folder,
104-
'dbt_project.yml')
104+
'dbt_project.yml'), profile_to_load=self.project.profile_to_load
105105
)
106106
processed_repos.add(dep_folder)
107107
self.__pull_deps_recursive(dep_project['repositories'], processed_repos, i+1)

dbt/templates.py

+19-33
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ class BaseCreateTemplate(object):
55
{query}
66
);"""
77

8-
# Distribution style, sort keys,BACKUP, and NULL properties are inherited by LIKE tables,
9-
# but you cannot explicitly set them in the CREATE TABLE ... LIKE statement.
10-
# via http://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html
118
incremental_template = """
12-
create temporary table "{identifier}__dbt_incremental_empty_tmp" {dist_qualifier} {sort_qualifier} as (
13-
select * from (
14-
{query}
15-
) as tmp limit 0
9+
{{% if not already_exists("{schema}", "{identifier}") %}}
10+
11+
create table "{schema}"."{identifier}" {dist_qualifier} {sort_qualifier} as (
12+
{query}
1613
);
1714
18-
create table if not exists "{schema}"."{identifier}" (like "{identifier}__dbt_incremental_empty_tmp");
15+
16+
{{% else %}}
1917
2018
create temporary table "{identifier}__dbt_incremental_tmp" as (
2119
with dbt_incr_sbq as (
@@ -27,12 +25,20 @@ class BaseCreateTemplate(object):
2725
2826
-- DBT_OPERATION {{ function: expand_column_types_if_needed, args: {{ temp_table: "{identifier}__dbt_incremental_tmp", to_schema: "{schema}", to_table: "{identifier}"}} }}
2927
28+
{{% set dest_columns = get_columns_in_table("{schema}", "{identifier}") %}}
29+
{{% set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') %}}
30+
3031
{incremental_delete_statement}
3132
32-
insert into "{schema}"."{identifier}" (
33-
select * from "{identifier}__dbt_incremental_tmp"
33+
insert into "{schema}"."{identifier}" ({{{{ dest_cols_csv }}}})
34+
(
35+
select {{{{ dest_cols_csv }}}}
36+
from "{identifier}__dbt_incremental_tmp"
3437
);
35-
"""
38+
39+
{{% endif %}}
40+
41+
"""
3642

3743
incremental_delete_template = """
3844
delete from "{schema}"."{identifier}" where ({unique_key}) in (
@@ -110,30 +116,10 @@ class DryCreateTemplate(object):
110116

111117

112118
incremental_template = """
113-
create temporary table "{identifier}__dbt_incremental_empty_tmp" {dist_qualifier} {sort_qualifier} as (
119+
create table "{schema}"."{identifier}" {dist_qualifier} {sort_qualifier} as (
114120
select * from (
115121
{query}
116-
) as tmp limit 0
117-
);
118-
119-
120-
create table if not exists "{schema}"."{identifier}" (like "{identifier}__dbt_incremental_empty_tmp");
121-
122-
create temporary table "{identifier}__dbt_incremental_tmp" as (
123-
with dbt_incr_sbq as (
124-
{query}
125-
)
126-
select * from dbt_incr_sbq
127-
where ({sql_where}) or ({sql_where}) is null
128-
limit 0
129-
);
130-
131-
-- DBT_OPERATION {{ function: expand_column_types_if_needed, args: {{ temp_table: "{identifier}__dbt_incremental_tmp", to_schema: "{schema}", to_table: "{identifier}"}} }}
132-
133-
{incremental_delete_statement}
134-
135-
insert into "{schema}"."{identifier}" (
136-
select * from "{identifier}__dbt_incremental_tmp"
122+
) s limit 0
137123
);
138124
"""
139125

dbt/utils.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dbt.project
44
import pprint
55
import json
6+
import dbt.project
67

78
DBTConfigKeys = [
89
'enabled',
@@ -81,7 +82,11 @@ def dependency_projects(project):
8182
for obj in os.listdir(project['modules-path']):
8283
full_obj = os.path.join(project['modules-path'], obj)
8384
if os.path.isdir(full_obj):
84-
yield dbt.project.read_project(os.path.join(full_obj, 'dbt_project.yml'))
85+
try:
86+
yield dbt.project.read_project(os.path.join(full_obj, 'dbt_project.yml'), profile_to_load=project.profile_to_load)
87+
except dbt.project.DbtProjectError as e:
88+
print("Error reading dependency project at {}".format(full_obj))
89+
print(str(e))
8590

8691
def split_path(path):
8792
norm = os.path.normpath(path)

0 commit comments

Comments
 (0)