dbt-labs / dbt-codegen

Macros that generate dbt code
https://hub.getdbt.com/dbt-labs/codegen/latest/
Apache License 2.0
480 stars 106 forks source link

Feature request: Option to alphabetize columns in generate_base_model output #117

Closed insuhpak closed 1 year ago

insuhpak commented 1 year ago

Describe the feature

Add a feature to alphabetize columns in the output of generate_base_model.sql.

Describe alternatives you've considered

I was manually alphabetizing the columns after the output was generated by the generate_base_model.

Now, I have a customized version of the generate_base_model.sql where line 6

{% set column_names=columns | map(attribute='name') %}

is replaced by

    {# Custom #}
    {% if alphabetize %}
        {%- set column_names=columns | map(attribute='name') | sort -%}
    {% else %}
        {%- set column_names=columns | map(attribute='name') -%}
    {% endif %}
    {# EndCustom #} 

where alphabetize is a new boolean argument to the macro.

Line 1 is also replaced with

 {% macro stg_ucp__effdt_tables(source_name, table_name, partition_columns, alphabetize=False) %}

alphabetize is set to false by default so if users do not specify the new alphabetize argument when calling generate_base_model the output will still be what they expect.

Additional context

Link to builtin filter jinja-filters.sort

The reason I am requesting this as a feature is to avoid managing our own in-house version of codegen macros especially against new versions of codegen that are released. Therefore, even though we know of a solution we are hesitant to use it widely across our models.

Who will this benefit?

Anyone who wants/needs their columns sort alphabetically in their base models.

This enhancement was also opened in issue #47.

Are you interested in contributing this feature?

Yes! But I would need a hand getting started.

github-actions[bot] commented 1 year ago

This issue has been marked as Stale because it has been open for 180 days with no activity. If you would like the issue to remain open, please comment on the issue or else it will be closed in 7 days.

github-actions[bot] commented 1 year ago

Although we are closing this issue as stale, it's not gone forever. Issues can be reopened if there is renewed community interest. Just add a comment to notify the maintainers.