Skip to content

Commit

Permalink
Merge branch 'dev/wilt-chamberlain' into feature/stub-adapter-in-parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Beck committed Apr 30, 2019
2 parents ffceff7 + acca6a7 commit 7d66965
Show file tree
Hide file tree
Showing 22 changed files with 409 additions and 39 deletions.
2 changes: 1 addition & 1 deletion core/dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def string_size(self):

if self.dtype == 'text' or self.char_size is None:
# char_size should never be None. Handle it reasonably just in case
return 255
return 256
else:
return int(self.char_size)

Expand Down
41 changes: 23 additions & 18 deletions core/dbt/context/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,30 +232,33 @@ def __init__(self, model, context, overrides):
def pretty_dict(self, data):
return json.dumps(data, sort_keys=True, indent=4)

def get_missing_var(self, var_name):
pretty_vars = self.pretty_dict(self.local_vars)
msg = self.UndefinedVarError.format(
var_name, self.model_name, pretty_vars
)
dbt.exceptions.raise_compiler_error(msg, self.model)

def assert_var_defined(self, var_name, default):
if var_name not in self.local_vars and default is self._VAR_NOTSET:
pretty_vars = self.pretty_dict(self.local_vars)
dbt.exceptions.raise_compiler_error(
self.UndefinedVarError.format(
var_name, self.model_name, pretty_vars
),
self.model
)

def __call__(self, var_name, default=_VAR_NOTSET):
self.assert_var_defined(var_name, default)

if var_name not in self.local_vars:
return default
return self.get_missing_var(var_name)

def get_rendered_var(self, var_name):
raw = self.local_vars[var_name]

# if bool/int/float/etc are passed in, don't compile anything
if not isinstance(raw, basestring):
return raw

return dbt.clients.jinja.get_rendered(raw, self.context)

def __call__(self, var_name, default=_VAR_NOTSET):
if var_name in self.local_vars:
return self.get_rendered_var(var_name)
elif default is not self._VAR_NOTSET:
return default
else:
return self.get_missing_var(var_name)


def write(node, target_path, subdirectory):
def fn(payload):
Expand Down Expand Up @@ -388,7 +391,8 @@ def generate_base(model, model_dict, config, manifest, source_config,
return context


def modify_generated_context(context, model, model_dict, config, manifest):
def modify_generated_context(context, model, model_dict, config, manifest,
provider):
cli_var_overrides = config.cli_vars

context = _add_tracking(context)
Expand All @@ -401,7 +405,8 @@ def modify_generated_context(context, model, model_dict, config, manifest):

context["write"] = write(model_dict, config.target_path, 'run')
context["render"] = render(context, model_dict)
context["var"] = Var(model, context=context, overrides=cli_var_overrides)
context["var"] = provider.Var(model, context=context,
overrides=cli_var_overrides)
context['context'] = context

return context
Expand All @@ -420,7 +425,7 @@ def generate_execute_macro(model, config, manifest, provider):
provider)

return modify_generated_context(context, model, model_dict, config,
manifest)
manifest, provider)


def generate_model(model, config, manifest, source_config, provider):
Expand All @@ -441,7 +446,7 @@ def generate_model(model, config, manifest, source_config, provider):
})

return modify_generated_context(context, model, model_dict, config,
manifest)
manifest, provider)


def generate(model, config, manifest, source_config=None, provider=None):
Expand Down
6 changes: 6 additions & 0 deletions core/dbt/context/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ def __getattr__(self, name):
)


class Var(dbt.context.common.Var):
def get_missing_var(self, var_name):
# in the parser, just always return None.
return None


def generate(model, runtime_config, manifest, source_config):
# during parsing, we don't have a connection, but we might need one, so we
# have to acquire it.
Expand Down
4 changes: 4 additions & 0 deletions core/dbt/context/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ def __getattr__(self, name):
)


class Var(dbt.context.common.Var):
pass


def generate(model, runtime_config, manifest):
return dbt.context.common.generate(
model, runtime_config, manifest, None, dbt.context.runtime)
Expand Down
1 change: 1 addition & 0 deletions plugins/postgres/dbt/adapters/postgres/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# these are mostly just exports, #noqa them so flake8 will be happy
from dbt.adapters.postgres.connections import PostgresConnectionManager # noqa
from dbt.adapters.postgres.connections import PostgresCredentials
from dbt.adapters.postgres.relation import PostgresColumn # noqa
from dbt.adapters.postgres.impl import PostgresAdapter

from dbt.adapters.base import AdapterPlugin
Expand Down
2 changes: 2 additions & 0 deletions plugins/postgres/dbt/adapters/postgres/impl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dbt.adapters.base.meta import available
from dbt.adapters.sql import SQLAdapter
from dbt.adapters.postgres import PostgresConnectionManager
from dbt.adapters.postgres import PostgresColumn
import dbt.compat
import dbt.exceptions

Expand All @@ -11,6 +12,7 @@

class PostgresAdapter(SQLAdapter):
ConnectionManager = PostgresConnectionManager
Column = PostgresColumn

@classmethod
def date_function(cls):
Expand Down
10 changes: 10 additions & 0 deletions plugins/postgres/dbt/adapters/postgres/relation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from dbt.adapters.base import Column


class PostgresColumn(Column):
@property
def data_type(self):
# on postgres, do not convert 'text' to 'varchar()'
if self.dtype.lower() == 'text':
return self.dtype
return super(PostgresColumn, self).data_type
1 change: 1 addition & 0 deletions plugins/redshift/dbt/adapters/redshift/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dbt.adapters.redshift.connections import RedshiftConnectionManager # noqa
from dbt.adapters.redshift.connections import RedshiftCredentials
from dbt.adapters.redshift.relation import RedshiftColumn # noqa
from dbt.adapters.redshift.impl import RedshiftAdapter


Expand Down
2 changes: 2 additions & 0 deletions plugins/redshift/dbt/adapters/redshift/impl.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from dbt.adapters.postgres import PostgresAdapter
from dbt.adapters.redshift import RedshiftConnectionManager
from dbt.adapters.redshift import RedshiftColumn
from dbt.logger import GLOBAL_LOGGER as logger # noqa


class RedshiftAdapter(PostgresAdapter):
ConnectionManager = RedshiftConnectionManager
Column = RedshiftColumn

AdapterSpecificConfigs = frozenset({"sort_type", "dist", "sort", "bind"})

Expand Down
5 changes: 5 additions & 0 deletions plugins/redshift/dbt/adapters/redshift/relation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from dbt.adapters.base import Column


class RedshiftColumn(Column):
pass # redshift does not inherit from postgres here
8 changes: 4 additions & 4 deletions test/integration/004_simple_archive_test/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ create table {database}.{schema}.archive_expected (
updated_at TIMESTAMP WITHOUT TIME ZONE,
dbt_valid_from TIMESTAMP WITHOUT TIME ZONE,
dbt_valid_to TIMESTAMP WITHOUT TIME ZONE,
dbt_scd_id VARCHAR(255),
dbt_scd_id VARCHAR(256),
dbt_updated_at TIMESTAMP WITHOUT TIME ZONE
);

Expand Down Expand Up @@ -93,7 +93,7 @@ create table {database}.{schema}.archive_castillo_expected (
updated_at TIMESTAMP WITHOUT TIME ZONE,
dbt_valid_from TIMESTAMP WITHOUT TIME ZONE,
dbt_valid_to TIMESTAMP WITHOUT TIME ZONE,
dbt_scd_id VARCHAR(255),
dbt_scd_id VARCHAR(256),
dbt_updated_at TIMESTAMP WITHOUT TIME ZONE
);

Expand Down Expand Up @@ -139,7 +139,7 @@ create table {database}.{schema}.archive_alvarez_expected (
updated_at TIMESTAMP WITHOUT TIME ZONE,
dbt_valid_from TIMESTAMP WITHOUT TIME ZONE,
dbt_valid_to TIMESTAMP WITHOUT TIME ZONE,
dbt_scd_id VARCHAR(255),
dbt_scd_id VARCHAR(256),
dbt_updated_at TIMESTAMP WITHOUT TIME ZONE
);

Expand Down Expand Up @@ -185,7 +185,7 @@ create table {database}.{schema}.archive_kelly_expected (
updated_at TIMESTAMP WITHOUT TIME ZONE,
dbt_valid_from TIMESTAMP WITHOUT TIME ZONE,
dbt_valid_to TIMESTAMP WITHOUT TIME ZONE,
dbt_scd_id VARCHAR(255),
dbt_scd_id VARCHAR(256),
dbt_updated_at TIMESTAMP WITHOUT TIME ZONE
);

Expand Down
9 changes: 9 additions & 0 deletions test/integration/004_simple_archive_test/seed_longtext.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
create table {database}.{schema}.super_long (
id INTEGER,
longstring TEXT,
updated_at TIMESTAMP WITHOUT TIME ZONE
);

insert into {database}.{schema}.super_long (id, longstring, updated_at) VALUES
(1, 'short', current_timestamp),
(2, repeat('a', 500), current_timestamp);
Loading

0 comments on commit 7d66965

Please sign in to comment.