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

better error message for ref inside conditional #376

Merged
merged 3 commits into from
Apr 12, 2017
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
2 changes: 2 additions & 0 deletions dbt/clients/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def __call__(self, *args, **kwargs):
if path not in self.node['depends_on']['macros']:
self.node['depends_on']['macros'].append(path)

return True
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cmcarthur curious what you think about this. By returning True instead of None here, dbt will evaluate the True branch of the conditional in the parsing stage. That means that we wouldn't throw an error for typical use cases like:

{% if already_exists(this.schema, this.table) %}
  select max(created_at) from {{ ref('other_model') }}
{% endif %}

The error would however occur if there was a ref in the else branch. Kind of a hack, but it feels a lot better to me. Could go either way here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, yeah, this feels good to me. nice.


return jinja2.sandbox.SandboxedEnvironment(
undefined=ParserMacroCapture)

Expand Down
23 changes: 10 additions & 13 deletions dbt/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,29 +194,26 @@ def do_ref(*args):
target_model_name,
target_model_package)

if target_model is None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

dbt.exceptions.ref_target_not_found(
model,
target_model_name,
target_model_package)

target_model_id = target_model.get('unique_id')

if target_model_id not in model.get('depends_on', {}).get('nodes'):
dbt.exceptions.ref_bad_context(model)
dbt.exceptions.ref_bad_context(model,
target_model_name,
target_model_package)

if get_materialization(target_model) == 'ephemeral':
model['extra_ctes'][target_model_id] = None
return '__dbt__CTE__{}'.format(target_model.get('name'))
else:
return '"{}"."{}"'.format(schema, target_model.get('name'))

def wrapped_do_ref(*args):
try:
return do_ref(*args)
except RuntimeError as e:
logger.info("Compiler error in {}".format(model.get('path')))
logger.info("Enabled models:")
for n, m in all_models.items():
if is_type(m, NodeType.Model):
logger.info(" - {}".format(m.get('unique_id')))
raise e

return wrapped_do_ref
return do_ref

def get_compiler_context(self, model, flat_graph):
context = self.project.context()
Expand Down
36 changes: 29 additions & 7 deletions dbt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,40 @@ def ref_invalid_args(model, args):
len(args)))


def ref_bad_context(model):
def ref_bad_context(model, target_model_name, target_model_package):
ref_string = "{{ ref('" + target_model_name + "}') }}}}"

if target_model_package is not None:
ref_string = ("{{ ref('" + target_model_package +
"', '" + target_model_name + "') }}")

base_error_msg = """dbt was unable to infer all dependencies for the model "{model_name}".
This typically happens when ref() is placed within a conditional block.

To fix this, add the following hint to the top of the model "{model_name}":

-- depends_on: {ref_string}"""
error_msg = base_error_msg.format(
model_name=model['name'],
model_path=model['path'],
ref_string=ref_string
)
raise_compiler_error(
model,
("ref() was used in an invalid context (probably in a "
"{% raw %} tag, or macro"))
model, error_msg)


def ref_target_not_found(model, target_model_name, target_model_package):
target_package_string = ''

if target_model_package is not None:
target_package_string = "in package '{}' ".format(target_model_package)

def ref_target_not_found(model, target_model_name):
raise_compiler_error(
model,
"Model '{}' depends on model '{}' which was not found."
.format(model.get('unique_id'), target_model_name))
"Model '{}' depends on model '{}' {}which was not found."
.format(model.get('unique_id'),
target_model_name,
target_package_string))


def ref_disabled_dependency(model, target_model):
Expand Down
4 changes: 2 additions & 2 deletions dbt/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def process_refs(flat_graph):
if target_model is None:
dbt.exceptions.ref_target_not_found(
node,
target_model_name)
target_model_name,
target_model_package)

if (dbt.utils.is_enabled(node) and
not dbt.utils.is_enabled(target_model)):
Expand Down Expand Up @@ -199,7 +200,6 @@ def parse_node(node, node_path, root_project_config, package_project_config,
context['config'] = __config(node, config)
context['target'] = property(lambda x: '', lambda x: x)
context['this'] = ''
context['already_exists'] = lambda *args: lambda *args: ''

context['var'] = Var(node, context)

Expand Down