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

include project macros in the manifest the adapter stores locally #2686

Merged
merged 2 commits into from
Aug 6, 2020

Conversation

beckjake
Copy link
Contributor

@beckjake beckjake commented Aug 5, 2020

resolves #2301

Description

Macros defined in core and called via the adapter's execute_macro method will now accept local overrides.

Previously, if you wanted to override a macro in dbt that was used for internal purposes (listing schemas, creating schemas, dropping schemas, getting columns in a relation, ...), the only way to do so was in an adapter plugin itself. This is because the adapter uses a special internal manifest that doesn't require any non-macros. Previously, the adapter's internal manifest only looked at macros defined in dbt core and any active adapter plugins. Now, we search the entire project (including dependencies) for macros and use that for the manifest.

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 Aug 5, 2020
@beckjake
Copy link
Contributor Author

beckjake commented Aug 5, 2020

@drewbanin this fixes an issue I bet exists in the 0.17.x RPC server as well: if you change a config and reload, it won't be reflected on the adapter level. I think that means quoting changes in dbt_project.yml might not be honored after a SIGHUP.

@beckjake beckjake force-pushed the feature/override-core-macros branch from bf7b1ef to 398aeb9 Compare August 5, 2020 20:42
Make sure "dbt deps" reloads the full manifest
Make sure commands that reload the dbt_project.yml properly reset the config (including adapters)
@beckjake beckjake force-pushed the feature/override-core-macros branch from 398aeb9 to 4274139 Compare August 5, 2020 20:51
@beckjake beckjake marked this pull request as ready for review August 6, 2020 13:08
@beckjake beckjake requested review from kwigley, gshank and jtcohen6 August 6, 2020 13:08
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.

Nice! This works as I'd expect.

Following on from the dispatch work, I decided to try something: If I'm running on postgres, and define in my local project:

  • get_columns_in_relation: overrides
  • postgres__get_columns_in_relation: overrides
  • default__get_columns_in_relation: does not override

That's true whether I call it as {{ get_columns_in_relation }} or {{ adapter.get_columns_in_relation }}. I'm fine with this behavior, as long as it doesn't surprise you.

@beckjake
Copy link
Contributor Author

beckjake commented Aug 6, 2020

Yeah. I don't think it's what I would dream of, but I also think it's probably one of the least-bad solutions right now.

If you define a macro with the name, dbt will just look that up (get_columns_in_relation).
If you define a macro without the name, dbt will look up get_columns_in_relation. That will call adapter.dispatch(), which will look for adaptertype__get_columns_in_relation in any package, followed by default__get_columns_in_relation in any package.

If you've defined adaptertype__get_columns_in_relation locally, that will win the lookup. but if you only define default__get_columns_in_relation locally, we'll find dbt_adaptertype.adaptertype__get_columns_in_relation first.

This is all a consequence of the fact that adapter-specific macros are just regular macros + how dbt builds the context.

I think the way to get this to behave how you'd expect is to have the adaptertype part of the macro definition (like materializations), and not put adaptertype macros into the context. In that world, default__get_columns_in_relation and adaptertype__get_columns_in_relation wouldn't be visible to the jinja context. They'd only see get_columns_in_relation, which would be resolved ahead of time to the appropriate underlying macro.

The takeaway here from a behavioral perspective is: don't try to override the things adapter.dispatch would call in your project, instead override the macro that would call adapter.dispatch.

@jtcohen6
Copy link
Contributor

jtcohen6 commented Aug 6, 2020

Got it! Thanks for the explanation. It's much more important that this override be possible, from the root project, in one way or another. And indeed:

The takeaway here from a behavioral perspective is: don't try to override the things adapter.dispatch would call in your project, instead override the macro that would call adapter.dispatch.

I don't see a need for packages to override adapter macros for use in the root project. As such, I don't feel passionately that we need all adapter macros to work with the level of rigor that materializations do. There may be a subset (e.g. schema tests) for which we decide it's necessary, someday, to wrap the adaptertype more tightly into the definition.

@beckjake beckjake merged commit fb8065d into dev/marian-anderson Aug 6, 2020
@beckjake beckjake deleted the feature/override-core-macros branch August 6, 2020 17:39
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.

Support overriding internal macros from root projects
4 participants