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

added macro for wh selection #115

Conversation

javiCalvo
Copy link
Contributor

@javiCalvo javiCalvo commented Mar 21, 2022

resolves #103 Macro for warehouse selection

Description

The change is minimal to call a macro that gets the actual / default warehouse for the model and returns the same but can be overriden for more complex logics (ie. select a different warehouse depending on the environment). Test are missing as I don't know how to start with them but the change is working.

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-snowflake next" section.

@cla-bot cla-bot bot added the cla:yes label Mar 21, 2022
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.

@javiCalvo Looping back after a long delay — let us know if this is something you'd still be interested in contributing. My interest here is around making the _use_warehouse behavior a bit more consistent with how other method-macro handoffs happen for dbt adapters.

Comment on lines 91 to 95
warehouse = self.execute_macro(
SNOWFLAKE_WAREHOUSE_MACRO_NAME,
kwargs=kwargs
)
self.execute('use warehouse {}'.format(warehouse))
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than a macro that returns the name of the warehouse, and then templating + executing use warehouse ... right from within the Python context, I think it would be better to have the Jinja macro template out the actual SQL we want to run (use warehouse {{ warehouse }}) and execute that SQL, via statement or run_query

That way, self.execute_macro accomplishes the full intended functionality, and we don't need the implicit call to self.execute (a.k.a. adapter.execute).

The macro could be named use_warehouse or use_snowflake_warehouse. To accomplish your intended functionality, you could still override that macro, adding complex conditional rules to determine the warehouse name, as long as the macro still templates out use warehouse ... and executes that SQL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comment, I'm not sure if I'm getting the idea 100%. You suggest to implement a macro like this:

{% macro use_warehouse(warehouse) -%}
  {% call statement('use_warehouse') -%}
    use warehouse {{ warehouse }}
  {%- endcall %}
{%- endmacro %}

And then the _use_warehouse method will be like:

    def _use_warehouse(self, warehouse: str):
        """Use the given warehouse. Quotes are never applied."""
        kwargs = {
            'warehouse': warehouse
        }
        self.execute_macro(
            SNOWFLAKE_USE_WAREHOUSE_MACRO_NAME,
            kwargs=kwargs
        )

If I'm getting this right then the macro override will have to make the call statement also? for example:

{% macro use_warehouse(warehouse) -%}

{% set warehouse = warehouse | replace('_X_', '_' + {'stg': 'D', 'tst': 'Q', 'prd': 'P'}[target.name] + '_') %}

{% call statement('use_warehouse') -%}
    use warehouse {{ warehouse }}
{%- endcall %}

{%- endmacro %}

Because I think the previous implementation is simpler in terms of macro override, but maybe is more inconsistent in the adapter implementation I don't know.

Copy link
Contributor

Choose a reason for hiding this comment

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

@javiCalvo Sorry for the delayed response on my end!

Fair point. It is in closer keeping with how other adapter methods/macros work in dbt today, but I also appreciate wanting a narrower macro that just templates the desired SQL.

Another way we could factor this:

{% macro get_use_warehouse_sql(warehouse) -%}
    use warehouse {{ warehouse }}
{%- endmacro %}
    def _use_warehouse(self, warehouse: str):
        """Use the given warehouse. Quotes are never applied."""
        kwargs = {
            'warehouse': warehouse
        }
        use_warehouse_sql = self.execute_macro(
            SNOWFLAKE_USE_WAREHOUSE_MACRO_NAME,
            kwargs=kwargs
        )
        self.execute(use_warehouse_sql)

Then your override can be just this (returning a string, no call, much easier to test):

{% macro get_use_warehouse_sql(warehouse) -%}
   {% set warehouse = warehouse | replace('_X_', '_' + {'stg': 'D', 'tst': 'Q', 'prd': 'P'}[target.name] + '_') %}
    use warehouse {{ warehouse }}
{%- endmacro %}

@jtcohen6 jtcohen6 added the ready_for_review Externally contributed PR has functional approval, ready for code review from Core engineering label Jul 18, 2022
@McKnight-42 McKnight-42 reopened this Jul 25, 2022
@McKnight-42
Copy link
Contributor

@javiCalvo this is looking very good, might need you to rerun pre-commit commands locally tried to fix a conflict we were having between main and guess pre-commit didn't like that (so sorry about that).

@McKnight-42 McKnight-42 requested a review from jtcohen6 August 12, 2022 19:12
@McKnight-42 McKnight-42 requested a review from a team as a code owner March 1, 2023 21:49
@McKnight-42 McKnight-42 mentioned this pull request Mar 7, 2023
6 tasks
@McKnight-42
Copy link
Contributor

@javiCalvo going to close this in favor of #503 to get your change ready for merger thank you for all your work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:yes ready_for_review Externally contributed PR has functional approval, ready for code review from Core engineering
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CT-333] Macro for warehouse selection
4 participants