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

dbt-materialize: support generate_cluster_name #27159

Closed
chaas opened this issue May 17, 2024 · 5 comments · Fixed by #27303
Closed

dbt-materialize: support generate_cluster_name #27159

chaas opened this issue May 17, 2024 · 5 comments · Fixed by #27303
Assignees
Labels
T-dbt Theme: dbt adapter

Comments

@chaas
Copy link
Contributor

chaas commented May 17, 2024

Outcome: support generate_cluster_name so users can prefix a cluster name per dbt target. E.g. so that when running in dev, the dev_ cluster is used and in prod, the prod_ cluster is used.

Context - slack thread:

Introduce a macro called generate_cluster_name, akin to generate_schema_name, that was invoked on every model’s cluster with the following default definition:

{% macro generate_cluster_name(custom_cluster_name, node) -%}

    {%- set default_cluster = target.cluster -%}
    {%- if custom_cluster_name is none -%}

        {{ default_cluster }}

    {%- else -%}

        {{ custom_cluster_name }}

    {%- endif -%}

{%- endmacro %}

We should automatically call generate_cluster_name(model.cluster) on each model’s cluster to determine the true cluster name to use.
The user can plug in their own version of the macro, like one that automatically prefixes each cluster with the target name:

{% macro generate_cluster_name(custom_cluster_name, node) -%}

    {%- set default_cluster = target.cluster -%}
    {%- if custom_cluster_name is none -%}

        {{ default_cluster }}

    {%- else -%}

        {{target.name}}_{{ custom_cluster_name }}

    {%- endif -%}

{%- endmacro %}
@chaas chaas added the T-dbt Theme: dbt adapter label May 17, 2024
@sjwiesman
Copy link
Contributor

Just as an FYI, we already have a macro called generate_cluster_name that is invoked in all the places described in this ticket.

https://github.com/MaterializeInc/materialize/blob/main/misc/dbt-materialize/dbt/include/materialize/macros/utils/generate_cluster_name.sql

@chaas
Copy link
Contributor Author

chaas commented May 20, 2024

Just as an FYI, we already have a macro called generate_cluster_name that is invoked in all the places described in this ticket.

Ah ok thanks, Seth, so then we should just update the macro to support this new option?

@bobbyiliev
Copy link
Contributor

bobbyiliev commented May 22, 2024

I was just looking into doing this after the dbt adapter migration to v1.8.0 is done.

Just to make sure that I have the full picture here, as we already have the generate_cluster_name macro that is invoked in all of the required places, there is technically no work on the adapter side that has to be done as users should already be able to override the macro and extend it in their own projects, eg. they could use something like this:

{% macro generate_cluster_name(custom_cluster_name) -%}
    {%- set default_cluster = target.cluster -%}
    {%- set deploy_suffix = "_dbt_deploy" if var('deploy', False) else "" -%}

    {%- if custom_cluster_name is none -%}
        {{ default_cluster }}{{ deploy_suffix }}
    {%- else -%}
        {{ target.name }}_{{ custom_cluster_name | trim }}{{ deploy_suffix }}
    {%- endif -%}
{%- endmacro %}

That way users who want to override the logic, will be able to do that, but also users who prefer the default behavior (eg. just using the default cluster name without prefixing it with the target name) would still have that.

Or do we prefer to actually extend the macro? Maybe introduce another var that users could pass to prefix cluster names?

@sjwiesman
Copy link
Contributor

sjwiesman commented May 22, 2024

Because of the away dependency resolution works, I think you'll need to update the callers to use the adapter dispatch method 1.

And then more broadly, there's a question of if the deploy suffix handling should happen inside this macro or not. If users override it, they will break blue/green if they don't copy the suffix handling exactly.

Maybe what we should do it create an internal macro that handles deploy suffixes and calls generate_cluster_name.

{% macro generate_cluster_name_internal(custom_cluster_name) -%}
      {%- set cluster_name = adapter.dispatch('generate_cluster_name')(custom_cluster_name) -%}
      {%- set deploy_suffix = "_dbt_deploy" if var('deploy', False) else "" -%}
      {{ cluster_name }}{{ deploy_suffix }}
{%- endmacro %}

Footnotes

  1. https://docs.getdbt.com/reference/dbt-jinja-functions/dispatch

@bobbyiliev
Copy link
Contributor

Thanks @sjwiesman this is very helpful!

I started putting together a PR for this.

bobbyiliev added a commit that referenced this issue Jun 7, 2024
<!--
Describe the contents of the PR briefly but completely.

If you write detailed commit messages, it is acceptable to copy/paste
them
here, or write "see commit messages for details." If there is only one
commit
in the PR, GitHub will have already added its commit message above.
-->

### Motivation

Fixes #27159

This PR introduces a couple of things:
* A new `generate_deploy_cluster_name` macro responsible for appending
the `_dbt_deploy` suffix for the blue/green deployments.
* A new `generate_final_cluster_name` adapter method.
* Users can define their own `generate_cluster_name` macro in their dbt
project to override the cluster name generation logic, like one that
automatically prefixes each cluster with the target name:

```jinja
{% macro generate_cluster_name(custom_cluster_name, node) -%}

    {%- set default_cluster = target.cluster -%}
    {%- if custom_cluster_name is none -%}

        {{ default_cluster }}

    {%- else -%}

        {{target.name}}_{{ custom_cluster_name }}

    {%- endif -%}

{%- endmacro %}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-dbt Theme: dbt adapter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants