This repository is currently being migrated. It's locked while the migration is in progress.
forked from kristeligt-dagblad/dbt_ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.sql
122 lines (101 loc) · 3.61 KB
/
model.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
{% macro drop_model(relation) %}
{{
adapter.dispatch(
macro_name='drop_model',
macro_namespace='dbt_ml'
)
(relation)
}}
{% endmacro %}
{% macro default__drop_model(relation) %}
{{ exceptions.raise_compiler_error("Dropping ML models is not implemented for the default adapter") }}
{% endmacro %}
{% macro bigquery__drop_model(relation) %}
{% call statement('drop_relation') -%}
drop {{ relation.type | default('model') }} if exists {{ relation }}
{%- endcall %}
{% endmacro %}
{% macro format_class_weights(class_weights) %}
{% set cw -%}
[{% for weight in class_weights %}
('{{ weight|first|string }}', {{ weight|last }}){{ ',' if not loop.last }}
{% endfor %}]
{%- endset %}
{%- do return(cw) -%}
{% endmacro %}
{% macro model_options(ml_config, labels) %}
{%- if labels -%}
{%- set label_list = [] -%}
{%- for label, value in labels.items() -%}
{%- do label_list.append((label, value)) -%}
{%- endfor -%}
{%- do ml_config.update({'labels': label_list}) -%}
{%- endif -%}
{% set options -%}
options({% for opt_key, opt_val in ml_config.items() %}
{{ opt_key }}={{ dbt_ml.format_class_weights(opt_val) if opt_key == 'class_weights' else (opt_val | tojson) if opt_val is string and not opt_val.startswith('HPARAM_') else opt_val if opt_val is number else (opt_val | safe) }}{{ ',' if not loop.last }}
{% endfor %})
{%- endset %}
{%- do return(options) -%}
{%- endmacro -%}
/*
{% macro transform(transform_config) %}
{% set transforms -%}
TRANSFORM(
{% for opt_key, opt_val in transform_config.items() %}
{%- if opt_key in ['label', '*'] -%}
{{ opt_key }}{{ ',' if not loop.last }}
{%- else -%}
{{ opt_val }} as {{ opt_key }}{{ ',' if not loop.last }}
{%- endif -%}
{% endfor %}
)
{%- endset %}
{%- do return(transforms) -%}
{%- endmacro -%}
*/
{% macro create_model_as(relation, sql) -%}
{{
adapter.dispatch(
macro_name='create_model_as',
macro_namespace='dbt_ml'
)
(relation, sql)
}}
{%- endmacro %}
{% macro default__create_model_as(relation, sql) %}
{{ exceptions.raise_compiler_error("ML model creation is not implemented for the default adapter") }}
{% endmacro %}
{% macro bigquery__create_model_as(relation, sql) %}
{%- set ml_config = config.get('ml_config', {}) -%}
--{%- set transform_config = config.get('transform_config', {}) -%}
{%- set raw_labels = config.get('labels', {}) -%}
{%- set sql_header = config.get('sql_header', none) -%}
{{ sql_header if sql_header is not none }}
create or replace model {{ relation }}
/*
{% if transform_config is not none %}
{{ dbt_ml.transform(
transform_config=transform_config
) }}
{% endif %}
*/
{{ dbt_ml.model_options(
ml_config=ml_config,
labels=raw_labels
) }}
as (
{{ sql }}
);
{% endmacro %}
{% materialization model, adapter='bigquery' -%}
{%- set identifier = model['alias'] -%}
{%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
{%- set target_relation = api.Relation.create(database=database, schema=schema, identifier=identifier) -%}
{{ run_hooks(pre_hooks) }}
{% call statement('main') -%}
{{ dbt_ml.create_model_as(target_relation, sql) }}
{% endcall -%}
{{ run_hooks(post_hooks) }}
{{ return({'relations': [target_relation]}) }}
{% endmaterialization %}