columns

columns

columns는 리소스 자체가 아니라 다른 리소스 타입의 자식 속성이에요. 모델·소스·시드·스냅샷·분석(analysis) 등에 컬럼별 설명, 데이터 타입, 데이터 테스트, quote 여부를 정의할 수 있어요.

출처: 문서

본문

models/.yml

models:
  - name: <model_name>
    columns:
      - name: <column_name>
        data_type: <string>
        description: <markdown_string>
        quote: true | false
        data_tests: ...
        config:
          tags: ...
          meta: ...
      - name: <another_column>
        ...

models/.yml

sources:
  - name: <source_name>
    tables:
    - name: <table_name>
      columns:
        - name: <column_name>
          description: <markdown_string>
          data_type: <string>
          quote: true | false
          data_tests: ...
          config:
            tags: ...
            meta: ...
        - name: <another_column>
          ...

seeds/.yml

seeds:
  - name: <seed_name>
    columns:
      - name: <column_name>
        description: <markdown_string>
        data_type: <string>
        quote: true | false
        data_tests: ...
        config:
          tags: ...
          meta: ...
      - name: <another_column>
            ...

snapshots/.yml

snapshots:
  - name: <snapshot_name>
    columns:
      - name: <column_name>
        description: <markdown_string>
        data_type: <string>
        quote: true | false
        data_tests: ...
        config:
          tags: ...
          meta: ...
      - name: <another_column>

analyses/.yml

analyses:
  - name: <analysis_name>
    columns:
      - name: <column_name>
        description: <markdown_string>
        data_type: <string>
      - name: <another_column>

컬럼은 그 자체로 리소스가 아니에요. 대신 다른 리소스 타입의 자식 속성이며, 리소스 레벨에서 정의한 속성과 유사한 하위 속성을 정의할 수 있어요.

  • tags
  • meta
  • data_tests
  • description

컬럼은 리소스가 아니기 때문에, tagsmeta 속성은 config 블록 아래에 중첩돼 있어도 진짜 configuration은 아니에요. 부모 리소스의 tagsmeta 값을 상속하지 않아요. 다만 컬럼에 적용된 태그나 최상위 리소스 태그를 사용해 컬럼에 정의된 generic 테스트를 선택할 수 있어요. 테스트 선택 예시를 참고하세요.

컬럼은 선택적으로 data_type을 정의할 수 있으며, 다음에 필요해요.

  • 모델 contract를 강제할 때
  • external 소스 속성, dbt-external-tables 같은 다른 패키지·플러그인에서 사용할 때

quote

quote 필드는 컬럼 이름의 quoting(인용)을 활성화하거나 비활성화하는 데 쓰여요.

models/schema.yml

models:
  - name: model_name
    columns:
      - name: column_name
        quote: true | false

models/schema.yml

sources:
  - name: source_name
    tables:
      - name: table_name
        columns:
          - name: column_name
            quote: true | false

seeds/schema.yml

seeds:
  - name: seed_name
    columns:
      - name: column_name
        quote: true | false

snapshots/schema.yml

snapshots:
  - name: snapshot_name
    columns:
      - name: column_name
        quote: true | false

analysis/schema.yml

analyses:
  - name: analysis_name
    columns:
      - name: column_name
        quote: true | false

Default

기본 quoting 값은 false예요.

Explanation

이 속성은 quoting이 특히 까다로울 수 있는 Snowflake 사용자에게 특히 관련이 있어요.

이 속성은 다음과 같은 경우에 유용해요.

  • 소스 테이블에 선택하기 위해 quoting이 필요한 컬럼이 있을 때. 예: 컬럼 대소문자를 보존하려는 경우
  • Snowflake에서 quote_columns: true(문서)로 만든 시드
  • 모델이 SQL에서 quoting을 사용해 예약어를 우회할 때
select user_group as "group"

quote: true를 설정하지 않으면:

  • 이 컬럼에 적용된 데이터 테스트가 잘못된 SQL로 실패할 수 있어요.
  • 문서가 제대로 렌더링되지 않을 수 있어요. 예: group"group"이 같은 컬럼 이름으로 매칭되지 않을 수 있어요.

Example

소스 테이블의 quoted 컬럼에 데이터 테스트 추가하기

Snowflake를 쓴다면 특히 관련이 있어요.

sources:
  - name: stripe
    tables:
      - name: payment
        columns:
          - name: orderID
            quote: true
            data_tests:
              - not_null

quote: true가 없으면 다음과 같은 오류가 나요.

$ dbt test -s source:stripe.*
Running with dbt=0.16.1
Found 7 models, 22 tests, 0 snapshots, 0 analyses, 130 macros, 0 operations, 0 seed files, 4 sources

13:33:37 | Concurrency: 4 threads (target='learn')
13:33:37 |
13:33:37 | 1 of 1 START test source_not_null_stripe_payment_order_id............ [RUN]
13:33:39 | 1 of 1 ERROR source_not_null_stripe_payment_order_id................. [ERROR in 1.89s]
13:33:39 |
13:33:39 | Finished running 1 tests in 6.43s.

Completed with 1 error and 0 warnings:

Database Error in test source_not_null_stripe_payment_order_id (models/staging/stripe/src_stripe.yml)
  000904 (42000): SQL compilation error: error line 3 at position 6
  invalid identifier 'ORDERID'
  compiled SQL at target/compiled/jaffle_shop/schema_test/source_not_null_stripe_payment_orderID.sql

이건 dbt가 다음 쿼리를 실행하려 했기 때문이에요.

select count(*)
from raw.stripe.payment
where orderID is null

대신 이렇게 실행해야 해요.

select count(*)
from raw.stripe.payment
where "orderID" is null

더 알아보기 (Learn more)