dbt-labs / dbt-codegen

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

Alias nested sub-structs for bigquery #98

Closed dbeatty10 closed 1 year ago

dbeatty10 commented 1 year ago

The following was originally reported and described here by @Zatte

Describe the bug

Output will be ambiguous in the case of multiple sub-structs (nested columns) with the same name.

Steps to reproduce

Use the dbt-bigquery adapter and the following files:

packages.yml

packages:
  - package: dbt-labs/codegen
    version: 0.9.0

models/model_struct.sql

select 
   struct(
      "a1" as a, 
      "b1" as b
   ) as substruct1, 
   struct(
      "a2" as a, 
      "b2" as b
   ) as substruct2

Then run the following:

dbt deps
dbt run-operation generate_model_yaml --args '{"model_names": ["model_struct"]}'

Expected results

version: 2

models:
  - name: model_struct
    description: ""
    columns:
      - name: substruct1
        description: ""

      - name: substruct1.a
        description: ""

      - name: substruct1.b
        description: ""

      - name: substruct2
        description: ""

      - name: substruct2.a
        description: ""

      - name: substruct2.b
        description: ""

Actual results

version: 2

models:
  - name: model_struct
    description: ""
    columns:
      - name: substruct1
        description: ""

      - name: a
        description: ""

      - name: b
        description: ""

      - name: substruct2
        description: ""

      - name: a
        description: ""

      - name: b
        description: ""

System information

Which database are you using dbt with?

The output of dbt --version:

 $ dbt --version
Core:
  - installed: 1.3.1
  - latest:    1.3.1 - Up to date!

Plugins:
  - bigquery: 1.3.0 - Up to date!

Are you interested in contributing the fix?

I'll open a PR for this. The most time-consuming part will be modifying the integration tests.

Simple fix is described here:

-     {% do model_yaml.append('      - name: ' ~ column.name | lower ) %}
+     {% do model_yaml.append('      - name: ' ~ column_name  | lower ) %}

This fix is verified to work as described in my local environment.