함수 구성
함수 구성 (Function configurations)
함수(function)는 dbt가 웨어하우스에 만들어내는 사용자 정의 함수(UDF)예요. 이 문서에서는 함수 전용 구성과 일반 구성, 그리고 프로젝트·속성 파일별로 함수를 구성하는 방법을 다뤄요.
출처: 문서
본문
💡
dbt v1.11부터 또는 dbt "v1 Latest" 릴리즈 트랙에서 사용할 수 있어요.
사용 가능한 구성
함수 전용 구성 (Function-specific configurations)
리소스 전용 구성은 여러 리소스 타입이 아니라 dbt 리소스 타입 하나에만 적용돼요. 이런 설정은 프로젝트 파일(dbt_project.yml), 속성 파일(models/properties.yml, 다른 리소스도 동일), 또는 리소스 파일 안의 {{ config() }} 매크로에서 정의할 수 있어요.
다음 리소스 전용 구성은 Function에만 사용할 수 있어요:
dbt_project.yml
functions:
<resource-path>:
# Function-specific configs are defined in the properties YAML file
# See functions/schema.yml examples below
functions/schema.yml
functions:
- name: [<function-name>]
config:
type: scalar # optional, defaults to scalar. Eventually will include aggregate | table
volatility: deterministic | stable | non-deterministic # optional
runtime_version: <string> # required for Python UDFs
entry_point: <string> # required for Python UDFs
packages: [<string>] # optional, Python UDFs only
snowflake:
quote_args: true | false # optional, JavaScript UDFs on Snowflake only, available in 1.12+
# standard configs that apply to functions
database: <string>
schema: <string>
alias: <string>
tags: <string> | [<string>]
meta: {<dictionary>}
일반 구성 (General configurations)
일반 구성은 여러 리소스 타입에 걸쳐 적용되는 더 넓은 운영 설정을 제공해요. 리소스 전용 구성처럼 프로젝트 파일, 속성 파일, 또는 리소스 전용 파일에서 설정할 수 있어요.
Function은 모델처럼 database, schema, alias 구성을 지원해요. 이 구성들은 함수가 웨어하우스 어디에 생성될지 결정해요. 함수는 표준 dbt 구성 우선순위(구체적 구성 > 프로젝트 구성 > target 프로파일 기본값)를 따릅니다.
dbt_project.yml
functions:
<resource-path>:
+enabled: true | false
+tags: <string> | [<string>]
+database: <string>
+schema: <string>
+alias: <string>
+meta: {<dictionary>}
functions/schema.yml
functions:
- name: [<function-name>]
config:
enabled: true | false
tags: <string> | [<string>]
database: <string>
schema: <string>
alias: <string>
meta: {<dictionary>}
함수 구성하기
함수는 YAML 파일에서 구성해요 — dbt_project.yml이나 개별 함수의 YAML 속성 파일에서요. 함수 본문은 functions/ 디렉토리의 SQL 파일에 정의돼요.
함수 구성은 모델 구성처럼 계층적으로 적용돼요. 자세한 내용은 config inheritance를 참고하세요.
함수는 모델과 동일한 이름 생성 매크로를 따르는데, generate_database_name, generate_schema_name, generate_alias_name가 있어요.
예시
모든 함수에 schema 구성 적용하기
설치된 패키지의 함수까지 포함해 모든 함수에 구성을 적용하려면 functions 키 바로 아래에 중첩해요:
dbt_project.yml
functions:
+schema: udf_schema
프로젝트의 모든 함수에만 schema 구성 적용하기
프로젝트의 모든 함수(설치된 패키지의 함수 제외)에만 적용하려면 리소스 경로의 일부로 프로젝트 이름을 제공해요. 예를 들어 프로젝트 이름이 jaffle_shop이면:
dbt_project.yml
functions:
jaffle_shop:
+schema: udf_schema
마찬가지로 설치된 패키지의 이름을 사용해 그 패키지의 함수를 구성할 수도 있어요.
하나의 함수에만 schema 구성 적용하기
속성 파일에서 하나의 함수에만 구성을 적용하려면 함수의 config 블록에 지정해요:
functions/schema.yml
functions:
- name: is_positive_int
config:
schema: udf_schema
dbt_project.yml에서 하나의 함수에만 구성을 적용하려면 전체 리소스 경로(프로젝트 이름과 하위 디렉토리 포함)를 제공해요. 프로젝트 jaffle_shop에 functions/is_positive_int.sql 함수 파일이 있다면:
dbt_project.yml
functions:
jaffle_shop:
is_positive_int:
+schema: udf_schema
함수 구성 예시
다음 예시는 두 개의 함수 파일을 가진 jaffle_shop 프로젝트에서 함수를 구성하는 방법을 보여줘요:
functions/is_positive_int.sqlfunctions/marketing/clean_url.sql
dbt_project.yml
name: jaffle_shop
...
functions:
jaffle_shop:
+enabled: true
+schema: udf_schema
# This configures functions/is_positive_int.sql
is_positive_int:
+tags: ['validation']
marketing:
+schema: marketing_udfs # this will take precedence
functions/schema.yml
functions:
- name: is_positive_int
description: Determines if a string represents a positive integer
config:
type: scalar
volatility: deterministic
database: analytics
schema: udf_schema
arguments:
- name: a_string
data_type: string
description: The string to check
returns:
data_type: boolean
description: Returns true if the string represents a positive integer
더 알아보기 (Learn more)
- Function properties — 함수 속성 선언
- Config inheritance — 구성 상속
- Custom databases, Custom schemas, Custom aliases — 이름 생성 매크로