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

Leverage generate_model_yaml to create LookML code #131

Closed
victorurbinajz opened this issue Jun 23, 2023 · 1 comment
Closed

Leverage generate_model_yaml to create LookML code #131

victorurbinajz opened this issue Jun 23, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@victorurbinajz
Copy link

victorurbinajz commented Jun 23, 2023

Describe the feature

  • Hi, everyone!
  • I'm using data contracts, and creating the code is not an easy task.
  • In order to make the process smoother, I've been using the macro generate_model_yaml
  • However, I also noticed that with some minor modifications, this macro can also be useful to create LookML code.

Describe alternatives you've considered

  • This is my suggestion for the new macro:
{% macro generate_column_looker(column, model_yaml, column_desc_dict, include_data_types, parent_column_name="") %}
    {% if parent_column_name %}
        {% set column_name = parent_column_name ~ "." ~ column.name %}
    {% else %}
        {% set column_name = column.name %}
    {% endif %}

    {% do model_yaml.append('  dimension: ' ~ column_name  | lower ~ ' {') %}
    {% if include_data_types %}
        {% set version = (dbt_version.split('.')[0] ~ '.' ~  dbt_version.split('.')[1]) | float %}
        {% if version < 1.5 %}
            {% set formatted = format_column(column) %}
        {% else %}
            {% set formatted = adapter.dispatch('format_column', 'dbt')(column) %}
        {% endif %}

        ---
        {% set data_type_name = formatted['data_type'] | lower %}
        {% if data_type_name in ['varchar', 'text'] %}
            {% set data_type_name = 'string' %}
        {% endif %}
        {% if data_type_name in ['timestamp_ltz', 'timestamp','timestamp_ltz'] %}
            {% set data_type_name = 'time' %}
        {% endif %}
        {% if data_type_name in ['boolean'] %}
            {% set data_type_name = 'yesno' %}
        {% endif %}
        {% if data_type_name in ['int','bigint','float8','float'] %}
            {% set data_type_name = 'number' %}
        {% endif %}
        ---

        {% do model_yaml.append('   type: ' ~ data_type_name | lower) %}
        {% do model_yaml.append('   sql: ${TABLE}.' ~ column_name  | lower ~ ' ;;') %}
        
    {% endif %}
    {% do model_yaml.append('  }') %}
    {% do model_yaml.append('') %}

    {% if column.fields|length > 0 %}
        {% for child_column in column.fields %}
            {% set model_yaml = codegen.generate_column_yaml(child_column, model_yaml, column_desc_dict, include_data_types, parent_column_name=column_name) %}
        {% endfor %}
    {% endif %}
    {% do return(model_yaml) %}
{% endmacro %}

{% macro generate_view_code_looker(model_names=[], upstream_descriptions=False, include_data_types=True) %}

    {% set model_yaml=[] %}
    {% do model_yaml.append('\n') %}    
    
    {% if model_names is string %}
        {{ exceptions.raise_compiler_error("The `model_names` argument must always be a list, even if there is only one model.") }}
    {% else %}
        {% for model in model_names %}

            {% do model_yaml.append('view: ' ~ model ~ ' {') %}
            {% do model_yaml.append('  sql_table_name: analytics.' ~ model ~ ' ;;') %}
            {% do model_yaml.append('\n') %}
            {% set relation=ref(model) %}
            {%- set columns = adapter.get_columns_in_relation(relation) -%}
            {% set column_desc_dict =  codegen.build_dict_column_descriptions(model) if upstream_descriptions else {} %}

            {% for column in columns %}
                {% set model_yaml = generate_column_looker(column, model_yaml, column_desc_dict, include_data_types) -%}
            {% endfor %}
        {% endfor %}
    {% endif %}
    {% do model_yaml.append('\n') %}
    {% do model_yaml.append('}') %}
    
{% if execute %}

    {% set joined = model_yaml | join ('\n') -%}
    {{ log(joined, info=True) }}
    {% do return(joined) %}

{% endif %}

{% endmacro %}
  • How to run this macro?
dbt run-operation generate_view_code_looker --args '{"model_names": ["model_name"]}'

Alternatively, the code can be put into a yml file

dbt run-operation generate_view_code_looker --args '{"model_names": ["model_name"]}' > sample_file.yml

Additional context

I'm using Snowflake, that's the only place where I've tested this macro.

Who will this benefit?

  • Use case: this macro can be used to create the LookML basic code to create Looker views. It's a good starting point because it already adds the data type and declares all the dimensions available.

Are you interested in contributing this feature?

  • For sure. I'd love to contribute.
@victorurbinajz victorurbinajz added the enhancement New feature or request label Jun 23, 2023
@graciegoheen
Copy link
Contributor

Hey! If you're adding contacts to your dbt models, you may want to check out the dbt-meshify package (specifically the add-contract command).

The scope of the dbt-codegen package is generating code for use within your dbt project. Since LookML code is separate from a dbt project, I'm going to close this issue for now.

@graciegoheen graciegoheen closed this as not planned Won't fix, can't repro, duplicate, stale Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants