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

Add necessary macros to schema test context namespace #3272

Merged
merged 1 commit into from
Apr 20, 2021

Conversation

gshank
Copy link
Contributor

@gshank gshank commented Apr 16, 2021

resolves #3229, #3240

Description

Ensure TestMacroNamespace is loaded in the TestContext and that necessary macros for rendering tests are loaded.

Checklist

  • I have signed the CLA
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • I have updated the CHANGELOG.md and added information about my change to the "dbt next" section.

@cla-bot cla-bot bot added the cla:yes label Apr 16, 2021
Copy link
Contributor

@jtcohen6 jtcohen6 left a comment

Choose a reason for hiding this comment

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

This is amazing!

I'm so excited by the potential here. Over the next few months, I could see us harnessing depends_on.macros to power up partial parsing and in stateful node selection (state:modified):

  • Could we extend statically_extract_macro_calls to also capture calls to ref(), source(), config()? We could mark these as "dirty macros" that have implications at parse-time.
  • Could we also capture calls to var(), env_var()? (Values too?) Then we'd know whether a node depends on a given variable, and/or whether that node's file needs to be re-parsed.

In the much shorter term, this neatly resolves the regressions we saw popping up in v0.19.1. After merging, how big of a lift would it be to cherry-pick this onto 0.19.latest and fold it into a v0.19.2 bugfix release? Or are the changes here too foundational / the git histories too far diverged?

@gshank
Copy link
Contributor Author

gshank commented Apr 16, 2021

Some of the code has moved so I'd have to hand update the changes in core/dbt/parser/manifest.py, but the rest of it should apply pretty cleanly. I'd say 2-4 hours to backport.

We could certainly look at capturing some of those other calls. We'd have to do some experiments. There's more work to do with adapter.dispatch too.

Comment on lines +657 to +661
standard_calls = {
'source': [],
'ref': [],
'config': [],
}
Copy link
Contributor

Choose a reason for hiding this comment

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

are the empty lists from a previous approach? can standard_calls just be a set of strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That structure was there from an experiment that Drew wrote. It could be a set of strings right now, but I think I'll leave it like this as a connection to that previous experiment and to leave space for a possible future enhancement.

Comment on lines 62 to 78
def _build_macros_by_name(self):
macros_by_name = {}
# search root package macros
for macro in self.root_package_macros.values():

# all internal packages (already in the right order)
for macro in self.internal_packages_namespace.values():
macros_by_name[macro.name] = macro
# search miscellaneous non-internal packages

# non-internal packages
for fnamespace in self.packages.values():
for macro in fnamespace.values():
macros_by_name[macro.name] = macro
# search all internal packages
for macro in self.internal_packages_namespace.values():

# root package macros
for macro in self.root_package_macros.values():
macros_by_name[macro.name] = macro

self.macros_by_name = macros_by_name
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'm having a little trouble following this change, was this ordering wrong before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. It never actually worked, so we didn't notice. It needs to be built from lowest precedence to highest, and it was the opposite before.

@@ -1408,7 +1408,12 @@ def __init__(
self.macro_resolver = macro_resolver
self.thread_ctx = MacroStack()
super().__init__(model, config, manifest, provider, context_config)
self._build_test_namespace
Copy link
Contributor

Choose a reason for hiding this comment

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

oof! we missed this line before

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. One of the reasons that it didn't work :-)

@gshank gshank merged commit 33dc970 into develop Apr 20, 2021
@gshank gshank deleted the test_context_regression branch April 20, 2021 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0.19.1 unable to infer dependencies for custom tests
3 participants