dbt-adapters 0.1.0a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dbt-adapters might be problematic. Click here for more details.

Files changed (147) hide show
  1. dbt/__init__.py +0 -0
  2. dbt/adapters/__about__.py +1 -0
  3. dbt/adapters/__init__.py +7 -0
  4. dbt/adapters/base/README.md +13 -0
  5. dbt/adapters/base/__init__.py +15 -0
  6. dbt/adapters/base/column.py +166 -0
  7. dbt/adapters/base/connections.py +426 -0
  8. dbt/adapters/base/impl.py +1654 -0
  9. dbt/adapters/base/meta.py +131 -0
  10. dbt/adapters/base/plugin.py +32 -0
  11. dbt/adapters/base/query_headers.py +101 -0
  12. dbt/adapters/base/relation.py +471 -0
  13. dbt/adapters/cache.py +521 -0
  14. dbt/adapters/capability.py +52 -0
  15. dbt/adapters/clients/__init__.py +0 -0
  16. dbt/adapters/clients/jinja.py +24 -0
  17. dbt/adapters/contracts/__init__.py +0 -0
  18. dbt/adapters/contracts/connection.py +228 -0
  19. dbt/adapters/contracts/macros.py +11 -0
  20. dbt/adapters/contracts/relation.py +125 -0
  21. dbt/adapters/events/README.md +57 -0
  22. dbt/adapters/events/__init__.py +0 -0
  23. dbt/adapters/events/adapter_types.proto +517 -0
  24. dbt/adapters/events/adapter_types_pb2.py +208 -0
  25. dbt/adapters/events/base_types.py +40 -0
  26. dbt/adapters/events/logging.py +83 -0
  27. dbt/adapters/events/types.py +423 -0
  28. dbt/adapters/exceptions/__init__.py +40 -0
  29. dbt/adapters/exceptions/alias.py +24 -0
  30. dbt/adapters/exceptions/cache.py +68 -0
  31. dbt/adapters/exceptions/compilation.py +255 -0
  32. dbt/adapters/exceptions/connection.py +16 -0
  33. dbt/adapters/exceptions/database.py +51 -0
  34. dbt/adapters/factory.py +246 -0
  35. dbt/adapters/protocol.py +173 -0
  36. dbt/adapters/reference_keys.py +39 -0
  37. dbt/adapters/relation_configs/README.md +25 -0
  38. dbt/adapters/relation_configs/__init__.py +12 -0
  39. dbt/adapters/relation_configs/config_base.py +44 -0
  40. dbt/adapters/relation_configs/config_change.py +24 -0
  41. dbt/adapters/relation_configs/config_validation.py +57 -0
  42. dbt/adapters/sql/__init__.py +2 -0
  43. dbt/adapters/sql/connections.py +195 -0
  44. dbt/adapters/sql/impl.py +273 -0
  45. dbt/adapters/utils.py +69 -0
  46. dbt/include/global_project/__init__.py +4 -0
  47. dbt/include/global_project/dbt_project.yml +7 -0
  48. dbt/include/global_project/docs/overview.md +43 -0
  49. dbt/include/global_project/macros/adapters/apply_grants.sql +167 -0
  50. dbt/include/global_project/macros/adapters/columns.sql +137 -0
  51. dbt/include/global_project/macros/adapters/freshness.sql +16 -0
  52. dbt/include/global_project/macros/adapters/indexes.sql +41 -0
  53. dbt/include/global_project/macros/adapters/metadata.sql +96 -0
  54. dbt/include/global_project/macros/adapters/persist_docs.sql +33 -0
  55. dbt/include/global_project/macros/adapters/relation.sql +79 -0
  56. dbt/include/global_project/macros/adapters/schema.sql +20 -0
  57. dbt/include/global_project/macros/adapters/show.sql +22 -0
  58. dbt/include/global_project/macros/adapters/timestamps.sql +44 -0
  59. dbt/include/global_project/macros/adapters/validate_sql.sql +10 -0
  60. dbt/include/global_project/macros/etc/datetime.sql +62 -0
  61. dbt/include/global_project/macros/etc/statement.sql +52 -0
  62. dbt/include/global_project/macros/generic_test_sql/accepted_values.sql +27 -0
  63. dbt/include/global_project/macros/generic_test_sql/not_null.sql +9 -0
  64. dbt/include/global_project/macros/generic_test_sql/relationships.sql +23 -0
  65. dbt/include/global_project/macros/generic_test_sql/unique.sql +12 -0
  66. dbt/include/global_project/macros/get_custom_name/get_custom_alias.sql +36 -0
  67. dbt/include/global_project/macros/get_custom_name/get_custom_database.sql +32 -0
  68. dbt/include/global_project/macros/get_custom_name/get_custom_schema.sql +60 -0
  69. dbt/include/global_project/macros/materializations/configs.sql +21 -0
  70. dbt/include/global_project/macros/materializations/hooks.sql +35 -0
  71. dbt/include/global_project/macros/materializations/models/clone/can_clone_table.sql +7 -0
  72. dbt/include/global_project/macros/materializations/models/clone/clone.sql +67 -0
  73. dbt/include/global_project/macros/materializations/models/clone/create_or_replace_clone.sql +7 -0
  74. dbt/include/global_project/macros/materializations/models/incremental/column_helpers.sql +80 -0
  75. dbt/include/global_project/macros/materializations/models/incremental/incremental.sql +92 -0
  76. dbt/include/global_project/macros/materializations/models/incremental/is_incremental.sql +13 -0
  77. dbt/include/global_project/macros/materializations/models/incremental/merge.sql +131 -0
  78. dbt/include/global_project/macros/materializations/models/incremental/on_schema_change.sql +144 -0
  79. dbt/include/global_project/macros/materializations/models/incremental/strategies.sql +79 -0
  80. dbt/include/global_project/macros/materializations/models/materialized_view.sql +121 -0
  81. dbt/include/global_project/macros/materializations/models/table.sql +64 -0
  82. dbt/include/global_project/macros/materializations/models/view.sql +72 -0
  83. dbt/include/global_project/macros/materializations/seeds/helpers.sql +128 -0
  84. dbt/include/global_project/macros/materializations/seeds/seed.sql +60 -0
  85. dbt/include/global_project/macros/materializations/snapshots/helpers.sql +181 -0
  86. dbt/include/global_project/macros/materializations/snapshots/snapshot.sql +99 -0
  87. dbt/include/global_project/macros/materializations/snapshots/snapshot_merge.sql +25 -0
  88. dbt/include/global_project/macros/materializations/snapshots/strategies.sql +174 -0
  89. dbt/include/global_project/macros/materializations/tests/helpers.sql +14 -0
  90. dbt/include/global_project/macros/materializations/tests/test.sql +60 -0
  91. dbt/include/global_project/macros/materializations/tests/where_subquery.sql +15 -0
  92. dbt/include/global_project/macros/python_model/python.sql +103 -0
  93. dbt/include/global_project/macros/relations/column/columns_spec_ddl.sql +89 -0
  94. dbt/include/global_project/macros/relations/create.sql +23 -0
  95. dbt/include/global_project/macros/relations/create_backup.sql +17 -0
  96. dbt/include/global_project/macros/relations/create_intermediate.sql +17 -0
  97. dbt/include/global_project/macros/relations/drop.sql +41 -0
  98. dbt/include/global_project/macros/relations/drop_backup.sql +14 -0
  99. dbt/include/global_project/macros/relations/materialized_view/alter.sql +55 -0
  100. dbt/include/global_project/macros/relations/materialized_view/create.sql +10 -0
  101. dbt/include/global_project/macros/relations/materialized_view/drop.sql +14 -0
  102. dbt/include/global_project/macros/relations/materialized_view/refresh.sql +9 -0
  103. dbt/include/global_project/macros/relations/materialized_view/rename.sql +10 -0
  104. dbt/include/global_project/macros/relations/materialized_view/replace.sql +10 -0
  105. dbt/include/global_project/macros/relations/rename.sql +35 -0
  106. dbt/include/global_project/macros/relations/rename_intermediate.sql +14 -0
  107. dbt/include/global_project/macros/relations/replace.sql +50 -0
  108. dbt/include/global_project/macros/relations/schema.sql +8 -0
  109. dbt/include/global_project/macros/relations/table/create.sql +60 -0
  110. dbt/include/global_project/macros/relations/table/drop.sql +14 -0
  111. dbt/include/global_project/macros/relations/table/rename.sql +10 -0
  112. dbt/include/global_project/macros/relations/table/replace.sql +10 -0
  113. dbt/include/global_project/macros/relations/view/create.sql +27 -0
  114. dbt/include/global_project/macros/relations/view/drop.sql +14 -0
  115. dbt/include/global_project/macros/relations/view/rename.sql +10 -0
  116. dbt/include/global_project/macros/relations/view/replace.sql +66 -0
  117. dbt/include/global_project/macros/utils/any_value.sql +9 -0
  118. dbt/include/global_project/macros/utils/array_append.sql +8 -0
  119. dbt/include/global_project/macros/utils/array_concat.sql +7 -0
  120. dbt/include/global_project/macros/utils/array_construct.sql +12 -0
  121. dbt/include/global_project/macros/utils/bool_or.sql +9 -0
  122. dbt/include/global_project/macros/utils/cast_bool_to_text.sql +7 -0
  123. dbt/include/global_project/macros/utils/concat.sql +7 -0
  124. dbt/include/global_project/macros/utils/data_types.sql +129 -0
  125. dbt/include/global_project/macros/utils/date_spine.sql +75 -0
  126. dbt/include/global_project/macros/utils/date_trunc.sql +7 -0
  127. dbt/include/global_project/macros/utils/dateadd.sql +14 -0
  128. dbt/include/global_project/macros/utils/datediff.sql +14 -0
  129. dbt/include/global_project/macros/utils/escape_single_quotes.sql +8 -0
  130. dbt/include/global_project/macros/utils/except.sql +9 -0
  131. dbt/include/global_project/macros/utils/generate_series.sql +53 -0
  132. dbt/include/global_project/macros/utils/hash.sql +7 -0
  133. dbt/include/global_project/macros/utils/intersect.sql +9 -0
  134. dbt/include/global_project/macros/utils/last_day.sql +15 -0
  135. dbt/include/global_project/macros/utils/length.sql +11 -0
  136. dbt/include/global_project/macros/utils/listagg.sql +30 -0
  137. dbt/include/global_project/macros/utils/literal.sql +7 -0
  138. dbt/include/global_project/macros/utils/position.sql +11 -0
  139. dbt/include/global_project/macros/utils/replace.sql +14 -0
  140. dbt/include/global_project/macros/utils/right.sql +12 -0
  141. dbt/include/global_project/macros/utils/safe_cast.sql +9 -0
  142. dbt/include/global_project/macros/utils/split_part.sql +26 -0
  143. dbt/include/global_project/tests/generic/builtin.sql +30 -0
  144. dbt_adapters-0.1.0a1.dist-info/METADATA +81 -0
  145. dbt_adapters-0.1.0a1.dist-info/RECORD +147 -0
  146. dbt_adapters-0.1.0a1.dist-info/WHEEL +4 -0
  147. dbt_adapters-0.1.0a1.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,12 @@
1
+ {% macro default__test_unique(model, column_name) %}
2
+
3
+ select
4
+ {{ column_name }} as unique_field,
5
+ count(*) as n_records
6
+
7
+ from {{ model }}
8
+ where {{ column_name }} is not null
9
+ group by {{ column_name }}
10
+ having count(*) > 1
11
+
12
+ {% endmacro %}
@@ -0,0 +1,36 @@
1
+
2
+ {#
3
+ Renders a alias name given a custom alias name. If the custom
4
+ alias name is none, then the resulting alias is just the filename of the
5
+ model. If an alias override is specified, then that is used.
6
+
7
+ This macro can be overriden in projects to define different semantics
8
+ for rendering a alias name.
9
+
10
+ Arguments:
11
+ custom_alias_name: The custom alias name specified for a model, or none
12
+ node: The available node that an alias is being generated for, or none
13
+
14
+ #}
15
+
16
+ {% macro generate_alias_name(custom_alias_name=none, node=none) -%}
17
+ {% do return(adapter.dispatch('generate_alias_name', 'dbt')(custom_alias_name, node)) %}
18
+ {%- endmacro %}
19
+
20
+ {% macro default__generate_alias_name(custom_alias_name=none, node=none) -%}
21
+
22
+ {%- if custom_alias_name -%}
23
+
24
+ {{ custom_alias_name | trim }}
25
+
26
+ {%- elif node.version -%}
27
+
28
+ {{ return(node.name ~ "_v" ~ (node.version | replace(".", "_"))) }}
29
+
30
+ {%- else -%}
31
+
32
+ {{ node.name }}
33
+
34
+ {%- endif -%}
35
+
36
+ {%- endmacro %}
@@ -0,0 +1,32 @@
1
+ {#
2
+ Renders a database name given a custom database name. If the custom
3
+ database name is none, then the resulting database is just the "database"
4
+ value in the specified target. If a database override is specified, then
5
+ the custom database name is used instead of the default "database" value.
6
+
7
+ This macro can be overriden in projects to define different semantics
8
+ for rendering a database name.
9
+
10
+ Arguments:
11
+ custom_database_name: The custom database name specified for a model, or none
12
+ node: The node the database is being generated for
13
+
14
+ #}
15
+
16
+ {% macro generate_database_name(custom_database_name=none, node=none) -%}
17
+ {% do return(adapter.dispatch('generate_database_name', 'dbt')(custom_database_name, node)) %}
18
+ {%- endmacro %}
19
+
20
+ {% macro default__generate_database_name(custom_database_name=none, node=none) -%}
21
+ {%- set default_database = target.database -%}
22
+ {%- if custom_database_name is none -%}
23
+
24
+ {{ default_database }}
25
+
26
+ {%- else -%}
27
+
28
+ {{ custom_database_name }}
29
+
30
+ {%- endif -%}
31
+
32
+ {%- endmacro %}
@@ -0,0 +1,60 @@
1
+
2
+ {#
3
+ Renders a schema name given a custom schema name. If the custom
4
+ schema name is none, then the resulting schema is just the "schema"
5
+ value in the specified target. If a schema override is specified, then
6
+ the resulting schema is the default schema concatenated with the
7
+ custom schema.
8
+
9
+ This macro can be overriden in projects to define different semantics
10
+ for rendering a schema name.
11
+
12
+ Arguments:
13
+ custom_schema_name: The custom schema name specified for a model, or none
14
+ node: The node the schema is being generated for
15
+
16
+ #}
17
+ {% macro generate_schema_name(custom_schema_name=none, node=none) -%}
18
+ {{ return(adapter.dispatch('generate_schema_name', 'dbt')(custom_schema_name, node)) }}
19
+ {% endmacro %}
20
+
21
+ {% macro default__generate_schema_name(custom_schema_name, node) -%}
22
+
23
+ {%- set default_schema = target.schema -%}
24
+ {%- if custom_schema_name is none -%}
25
+
26
+ {{ default_schema }}
27
+
28
+ {%- else -%}
29
+
30
+ {{ default_schema }}_{{ custom_schema_name | trim }}
31
+
32
+ {%- endif -%}
33
+
34
+ {%- endmacro %}
35
+
36
+
37
+ {#
38
+ Renders a schema name given a custom schema name. In production, this macro
39
+ will render out the overriden schema name for a model. Otherwise, the default
40
+ schema specified in the active target is used.
41
+
42
+ Arguments:
43
+ custom_schema_name: The custom schema name specified for a model, or none
44
+ node: The node the schema is being generated for
45
+
46
+ #}
47
+ {% macro generate_schema_name_for_env(custom_schema_name, node) -%}
48
+
49
+ {%- set default_schema = target.schema -%}
50
+ {%- if target.name == 'prod' and custom_schema_name is not none -%}
51
+
52
+ {{ custom_schema_name | trim }}
53
+
54
+ {%- else -%}
55
+
56
+ {{ default_schema }}
57
+
58
+ {%- endif -%}
59
+
60
+ {%- endmacro %}
@@ -0,0 +1,21 @@
1
+ {% macro set_sql_header(config) -%}
2
+ {{ config.set('sql_header', caller()) }}
3
+ {%- endmacro %}
4
+
5
+
6
+ {% macro should_full_refresh() %}
7
+ {% set config_full_refresh = config.get('full_refresh') %}
8
+ {% if config_full_refresh is none %}
9
+ {% set config_full_refresh = flags.FULL_REFRESH %}
10
+ {% endif %}
11
+ {% do return(config_full_refresh) %}
12
+ {% endmacro %}
13
+
14
+
15
+ {% macro should_store_failures() %}
16
+ {% set config_store_failures = config.get('store_failures') %}
17
+ {% if config_store_failures is none %}
18
+ {% set config_store_failures = flags.STORE_FAILURES %}
19
+ {% endif %}
20
+ {% do return(config_store_failures) %}
21
+ {% endmacro %}
@@ -0,0 +1,35 @@
1
+ {% macro run_hooks(hooks, inside_transaction=True) %}
2
+ {% for hook in hooks | selectattr('transaction', 'equalto', inside_transaction) %}
3
+ {% if not inside_transaction and loop.first %}
4
+ {% call statement(auto_begin=inside_transaction) %}
5
+ commit;
6
+ {% endcall %}
7
+ {% endif %}
8
+ {% set rendered = render(hook.get('sql')) | trim %}
9
+ {% if (rendered | length) > 0 %}
10
+ {% call statement(auto_begin=inside_transaction) %}
11
+ {{ rendered }}
12
+ {% endcall %}
13
+ {% endif %}
14
+ {% endfor %}
15
+ {% endmacro %}
16
+
17
+
18
+ {% macro make_hook_config(sql, inside_transaction) %}
19
+ {{ tojson({"sql": sql, "transaction": inside_transaction}) }}
20
+ {% endmacro %}
21
+
22
+
23
+ {% macro before_begin(sql) %}
24
+ {{ make_hook_config(sql, inside_transaction=False) }}
25
+ {% endmacro %}
26
+
27
+
28
+ {% macro in_transaction(sql) %}
29
+ {{ make_hook_config(sql, inside_transaction=True) }}
30
+ {% endmacro %}
31
+
32
+
33
+ {% macro after_commit(sql) %}
34
+ {{ make_hook_config(sql, inside_transaction=False) }}
35
+ {% endmacro %}
@@ -0,0 +1,7 @@
1
+ {% macro can_clone_table() %}
2
+ {{ return(adapter.dispatch('can_clone_table', 'dbt')()) }}
3
+ {% endmacro %}
4
+
5
+ {% macro default__can_clone_table() %}
6
+ {{ return(False) }}
7
+ {% endmacro %}
@@ -0,0 +1,67 @@
1
+ {%- materialization clone, default -%}
2
+
3
+ {%- set relations = {'relations': []} -%}
4
+
5
+ {%- if not defer_relation -%}
6
+ -- nothing to do
7
+ {{ log("No relation found in state manifest for " ~ model.unique_id, info=True) }}
8
+ {{ return(relations) }}
9
+ {%- endif -%}
10
+
11
+ {%- set existing_relation = load_cached_relation(this) -%}
12
+
13
+ {%- if existing_relation and not flags.FULL_REFRESH -%}
14
+ -- noop!
15
+ {{ log("Relation " ~ existing_relation ~ " already exists", info=True) }}
16
+ {{ return(relations) }}
17
+ {%- endif -%}
18
+
19
+ {%- set other_existing_relation = load_cached_relation(defer_relation) -%}
20
+
21
+ -- If this is a database that can do zero-copy cloning of tables, and the other relation is a table, then this will be a table
22
+ -- Otherwise, this will be a view
23
+
24
+ {% set can_clone_table = can_clone_table() %}
25
+
26
+ {%- if other_existing_relation and other_existing_relation.type == 'table' and can_clone_table -%}
27
+
28
+ {%- set target_relation = this.incorporate(type='table') -%}
29
+ {% if existing_relation is not none and not existing_relation.is_table %}
30
+ {{ log("Dropping relation " ~ existing_relation ~ " because it is of type " ~ existing_relation.type) }}
31
+ {{ drop_relation_if_exists(existing_relation) }}
32
+ {% endif %}
33
+
34
+ -- as a general rule, data platforms that can clone tables can also do atomic 'create or replace'
35
+ {% call statement('main') %}
36
+ {% if target_relation and defer_relation and target_relation == defer_relation %}
37
+ {{ log("Target relation and defer relation are the same, skipping clone for relation: " ~ target_relation) }}
38
+ {% else %}
39
+ {{ create_or_replace_clone(target_relation, defer_relation) }}
40
+ {% endif %}
41
+
42
+ {% endcall %}
43
+
44
+ {% set should_revoke = should_revoke(existing_relation, full_refresh_mode=True) %}
45
+ {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
46
+ {% do persist_docs(target_relation, model) %}
47
+
48
+ {{ return({'relations': [target_relation]}) }}
49
+
50
+ {%- else -%}
51
+
52
+ {%- set target_relation = this.incorporate(type='view') -%}
53
+
54
+ -- reuse the view materialization
55
+ -- TODO: support actual dispatch for materialization macros
56
+ -- Tracking ticket: https://github.com/dbt-labs/dbt-core/issues/7799
57
+ {% set search_name = "materialization_view_" ~ adapter.type() %}
58
+ {% if not search_name in context %}
59
+ {% set search_name = "materialization_view_default" %}
60
+ {% endif %}
61
+ {% set materialization_macro = context[search_name] %}
62
+ {% set relations = materialization_macro() %}
63
+ {{ return(relations) }}
64
+
65
+ {%- endif -%}
66
+
67
+ {%- endmaterialization -%}
@@ -0,0 +1,7 @@
1
+ {% macro create_or_replace_clone(this_relation, defer_relation) %}
2
+ {{ return(adapter.dispatch('create_or_replace_clone', 'dbt')(this_relation, defer_relation)) }}
3
+ {% endmacro %}
4
+
5
+ {% macro default__create_or_replace_clone(this_relation, defer_relation) %}
6
+ create or replace table {{ this_relation }} clone {{ defer_relation }}
7
+ {% endmacro %}
@@ -0,0 +1,80 @@
1
+ /* {#
2
+ Helper macros for internal use with incremental materializations.
3
+ Use with care if calling elsewhere.
4
+ #} */
5
+
6
+
7
+ {% macro get_quoted_csv(column_names) %}
8
+
9
+ {% set quoted = [] %}
10
+ {% for col in column_names -%}
11
+ {%- do quoted.append(adapter.quote(col)) -%}
12
+ {%- endfor %}
13
+
14
+ {%- set dest_cols_csv = quoted | join(', ') -%}
15
+ {{ return(dest_cols_csv) }}
16
+
17
+ {% endmacro %}
18
+
19
+
20
+ {% macro diff_columns(source_columns, target_columns) %}
21
+
22
+ {% set result = [] %}
23
+ {% set source_names = source_columns | map(attribute = 'column') | list %}
24
+ {% set target_names = target_columns | map(attribute = 'column') | list %}
25
+
26
+ {# --check whether the name attribute exists in the target - this does not perform a data type check #}
27
+ {% for sc in source_columns %}
28
+ {% if sc.name not in target_names %}
29
+ {{ result.append(sc) }}
30
+ {% endif %}
31
+ {% endfor %}
32
+
33
+ {{ return(result) }}
34
+
35
+ {% endmacro %}
36
+
37
+
38
+ {% macro diff_column_data_types(source_columns, target_columns) %}
39
+
40
+ {% set result = [] %}
41
+ {% for sc in source_columns %}
42
+ {% set tc = target_columns | selectattr("name", "equalto", sc.name) | list | first %}
43
+ {% if tc %}
44
+ {% if sc.data_type != tc.data_type and not sc.can_expand_to(other_column=tc) %}
45
+ {{ result.append( { 'column_name': tc.name, 'new_type': sc.data_type } ) }}
46
+ {% endif %}
47
+ {% endif %}
48
+ {% endfor %}
49
+
50
+ {{ return(result) }}
51
+
52
+ {% endmacro %}
53
+
54
+ {% macro get_merge_update_columns(merge_update_columns, merge_exclude_columns, dest_columns) %}
55
+ {{ return(adapter.dispatch('get_merge_update_columns', 'dbt')(merge_update_columns, merge_exclude_columns, dest_columns)) }}
56
+ {% endmacro %}
57
+
58
+ {% macro default__get_merge_update_columns(merge_update_columns, merge_exclude_columns, dest_columns) %}
59
+ {%- set default_cols = dest_columns | map(attribute="quoted") | list -%}
60
+
61
+ {%- if merge_update_columns and merge_exclude_columns -%}
62
+ {{ exceptions.raise_compiler_error(
63
+ 'Model cannot specify merge_update_columns and merge_exclude_columns. Please update model to use only one config'
64
+ )}}
65
+ {%- elif merge_update_columns -%}
66
+ {%- set update_columns = merge_update_columns -%}
67
+ {%- elif merge_exclude_columns -%}
68
+ {%- set update_columns = [] -%}
69
+ {%- for column in dest_columns -%}
70
+ {% if column.column | lower not in merge_exclude_columns | map("lower") | list %}
71
+ {%- do update_columns.append(column.quoted) -%}
72
+ {% endif %}
73
+ {%- endfor -%}
74
+ {%- else -%}
75
+ {%- set update_columns = default_cols -%}
76
+ {%- endif -%}
77
+
78
+ {{ return(update_columns) }}
79
+
80
+ {% endmacro %}
@@ -0,0 +1,92 @@
1
+
2
+ {% materialization incremental, default -%}
3
+
4
+ -- relations
5
+ {%- set existing_relation = load_cached_relation(this) -%}
6
+ {%- set target_relation = this.incorporate(type='table') -%}
7
+ {%- set temp_relation = make_temp_relation(target_relation)-%}
8
+ {%- set intermediate_relation = make_intermediate_relation(target_relation)-%}
9
+ {%- set backup_relation_type = 'table' if existing_relation is none else existing_relation.type -%}
10
+ {%- set backup_relation = make_backup_relation(target_relation, backup_relation_type) -%}
11
+
12
+ -- configs
13
+ {%- set unique_key = config.get('unique_key') -%}
14
+ {%- set full_refresh_mode = (should_full_refresh() or existing_relation.is_view) -%}
15
+ {%- set on_schema_change = incremental_validate_on_schema_change(config.get('on_schema_change'), default='ignore') -%}
16
+
17
+ -- the temp_ and backup_ relations should not already exist in the database; get_relation
18
+ -- will return None in that case. Otherwise, we get a relation that we can drop
19
+ -- later, before we try to use this name for the current operation. This has to happen before
20
+ -- BEGIN, in a separate transaction
21
+ {%- set preexisting_intermediate_relation = load_cached_relation(intermediate_relation)-%}
22
+ {%- set preexisting_backup_relation = load_cached_relation(backup_relation) -%}
23
+ -- grab current tables grants config for comparision later on
24
+ {% set grant_config = config.get('grants') %}
25
+ {{ drop_relation_if_exists(preexisting_intermediate_relation) }}
26
+ {{ drop_relation_if_exists(preexisting_backup_relation) }}
27
+
28
+ {{ run_hooks(pre_hooks, inside_transaction=False) }}
29
+
30
+ -- `BEGIN` happens here:
31
+ {{ run_hooks(pre_hooks, inside_transaction=True) }}
32
+
33
+ {% set to_drop = [] %}
34
+
35
+ {% if existing_relation is none %}
36
+ {% set build_sql = get_create_table_as_sql(False, target_relation, sql) %}
37
+ {% elif full_refresh_mode %}
38
+ {% set build_sql = get_create_table_as_sql(False, intermediate_relation, sql) %}
39
+ {% set need_swap = true %}
40
+ {% else %}
41
+ {% do run_query(get_create_table_as_sql(True, temp_relation, sql)) %}
42
+ {% do adapter.expand_target_column_types(
43
+ from_relation=temp_relation,
44
+ to_relation=target_relation) %}
45
+ {#-- Process schema changes. Returns dict of changes if successful. Use source columns for upserting/merging --#}
46
+ {% set dest_columns = process_schema_changes(on_schema_change, temp_relation, existing_relation) %}
47
+ {% if not dest_columns %}
48
+ {% set dest_columns = adapter.get_columns_in_relation(existing_relation) %}
49
+ {% endif %}
50
+
51
+ {#-- Get the incremental_strategy, the macro to use for the strategy, and build the sql --#}
52
+ {% set incremental_strategy = config.get('incremental_strategy') or 'default' %}
53
+ {% set incremental_predicates = config.get('predicates', none) or config.get('incremental_predicates', none) %}
54
+ {% set strategy_sql_macro_func = adapter.get_incremental_strategy_macro(context, incremental_strategy) %}
55
+ {% set strategy_arg_dict = ({'target_relation': target_relation, 'temp_relation': temp_relation, 'unique_key': unique_key, 'dest_columns': dest_columns, 'incremental_predicates': incremental_predicates }) %}
56
+ {% set build_sql = strategy_sql_macro_func(strategy_arg_dict) %}
57
+
58
+ {% endif %}
59
+
60
+ {% call statement("main") %}
61
+ {{ build_sql }}
62
+ {% endcall %}
63
+
64
+ {% if need_swap %}
65
+ {% do adapter.rename_relation(target_relation, backup_relation) %}
66
+ {% do adapter.rename_relation(intermediate_relation, target_relation) %}
67
+ {% do to_drop.append(backup_relation) %}
68
+ {% endif %}
69
+
70
+ {% set should_revoke = should_revoke(existing_relation, full_refresh_mode) %}
71
+ {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
72
+
73
+ {% do persist_docs(target_relation, model) %}
74
+
75
+ {% if existing_relation is none or existing_relation.is_view or should_full_refresh() %}
76
+ {% do create_indexes(target_relation) %}
77
+ {% endif %}
78
+
79
+ {{ run_hooks(post_hooks, inside_transaction=True) }}
80
+
81
+ -- `COMMIT` happens here
82
+ {% do adapter.commit() %}
83
+
84
+ {% for rel in to_drop %}
85
+ {% do adapter.drop_relation(rel) %}
86
+ {% endfor %}
87
+
88
+ {{ run_hooks(post_hooks, inside_transaction=False) }}
89
+
90
+ {{ return({'relations': [target_relation]}) }}
91
+
92
+ {%- endmaterialization %}
@@ -0,0 +1,13 @@
1
+
2
+ {% macro is_incremental() %}
3
+ {#-- do not run introspective queries in parsing #}
4
+ {% if not execute %}
5
+ {{ return(False) }}
6
+ {% else %}
7
+ {% set relation = adapter.get_relation(this.database, this.schema, this.table) %}
8
+ {{ return(relation is not none
9
+ and relation.type == 'table'
10
+ and model.config.materialized == 'incremental'
11
+ and not should_full_refresh()) }}
12
+ {% endif %}
13
+ {% endmacro %}
@@ -0,0 +1,131 @@
1
+ {% macro get_merge_sql(target, source, unique_key, dest_columns, incremental_predicates=none) -%}
2
+ -- back compat for old kwarg name
3
+ {% set incremental_predicates = kwargs.get('predicates', incremental_predicates) %}
4
+ {{ adapter.dispatch('get_merge_sql', 'dbt')(target, source, unique_key, dest_columns, incremental_predicates) }}
5
+ {%- endmacro %}
6
+
7
+ {% macro default__get_merge_sql(target, source, unique_key, dest_columns, incremental_predicates=none) -%}
8
+ {%- set predicates = [] if incremental_predicates is none else [] + incremental_predicates -%}
9
+ {%- set dest_cols_csv = get_quoted_csv(dest_columns | map(attribute="name")) -%}
10
+ {%- set merge_update_columns = config.get('merge_update_columns') -%}
11
+ {%- set merge_exclude_columns = config.get('merge_exclude_columns') -%}
12
+ {%- set update_columns = get_merge_update_columns(merge_update_columns, merge_exclude_columns, dest_columns) -%}
13
+ {%- set sql_header = config.get('sql_header', none) -%}
14
+
15
+ {% if unique_key %}
16
+ {% if unique_key is sequence and unique_key is not mapping and unique_key is not string %}
17
+ {% for key in unique_key %}
18
+ {% set this_key_match %}
19
+ DBT_INTERNAL_SOURCE.{{ key }} = DBT_INTERNAL_DEST.{{ key }}
20
+ {% endset %}
21
+ {% do predicates.append(this_key_match) %}
22
+ {% endfor %}
23
+ {% else %}
24
+ {% set unique_key_match %}
25
+ DBT_INTERNAL_SOURCE.{{ unique_key }} = DBT_INTERNAL_DEST.{{ unique_key }}
26
+ {% endset %}
27
+ {% do predicates.append(unique_key_match) %}
28
+ {% endif %}
29
+ {% else %}
30
+ {% do predicates.append('FALSE') %}
31
+ {% endif %}
32
+
33
+ {{ sql_header if sql_header is not none }}
34
+
35
+ merge into {{ target }} as DBT_INTERNAL_DEST
36
+ using {{ source }} as DBT_INTERNAL_SOURCE
37
+ on {{"(" ~ predicates | join(") and (") ~ ")"}}
38
+
39
+ {% if unique_key %}
40
+ when matched then update set
41
+ {% for column_name in update_columns -%}
42
+ {{ column_name }} = DBT_INTERNAL_SOURCE.{{ column_name }}
43
+ {%- if not loop.last %}, {%- endif %}
44
+ {%- endfor %}
45
+ {% endif %}
46
+
47
+ when not matched then insert
48
+ ({{ dest_cols_csv }})
49
+ values
50
+ ({{ dest_cols_csv }})
51
+
52
+ {% endmacro %}
53
+
54
+
55
+ {% macro get_delete_insert_merge_sql(target, source, unique_key, dest_columns, incremental_predicates) -%}
56
+ {{ adapter.dispatch('get_delete_insert_merge_sql', 'dbt')(target, source, unique_key, dest_columns, incremental_predicates) }}
57
+ {%- endmacro %}
58
+
59
+ {% macro default__get_delete_insert_merge_sql(target, source, unique_key, dest_columns, incremental_predicates) -%}
60
+
61
+ {%- set dest_cols_csv = get_quoted_csv(dest_columns | map(attribute="name")) -%}
62
+
63
+ {% if unique_key %}
64
+ {% if unique_key is sequence and unique_key is not string %}
65
+ delete from {{target }}
66
+ using {{ source }}
67
+ where (
68
+ {% for key in unique_key %}
69
+ {{ source }}.{{ key }} = {{ target }}.{{ key }}
70
+ {{ "and " if not loop.last}}
71
+ {% endfor %}
72
+ {% if incremental_predicates %}
73
+ {% for predicate in incremental_predicates %}
74
+ and {{ predicate }}
75
+ {% endfor %}
76
+ {% endif %}
77
+ );
78
+ {% else %}
79
+ delete from {{ target }}
80
+ where (
81
+ {{ unique_key }}) in (
82
+ select ({{ unique_key }})
83
+ from {{ source }}
84
+ )
85
+ {%- if incremental_predicates %}
86
+ {% for predicate in incremental_predicates %}
87
+ and {{ predicate }}
88
+ {% endfor %}
89
+ {%- endif -%};
90
+
91
+ {% endif %}
92
+ {% endif %}
93
+
94
+ insert into {{ target }} ({{ dest_cols_csv }})
95
+ (
96
+ select {{ dest_cols_csv }}
97
+ from {{ source }}
98
+ )
99
+
100
+ {%- endmacro %}
101
+
102
+
103
+ {% macro get_insert_overwrite_merge_sql(target, source, dest_columns, predicates, include_sql_header=false) -%}
104
+ {{ adapter.dispatch('get_insert_overwrite_merge_sql', 'dbt')(target, source, dest_columns, predicates, include_sql_header) }}
105
+ {%- endmacro %}
106
+
107
+ {% macro default__get_insert_overwrite_merge_sql(target, source, dest_columns, predicates, include_sql_header) -%}
108
+ {#-- The only time include_sql_header is True: --#}
109
+ {#-- BigQuery + insert_overwrite strategy + "static" partitions config --#}
110
+ {#-- We should consider including the sql header at the materialization level instead --#}
111
+
112
+ {%- set predicates = [] if predicates is none else [] + predicates -%}
113
+ {%- set dest_cols_csv = get_quoted_csv(dest_columns | map(attribute="name")) -%}
114
+ {%- set sql_header = config.get('sql_header', none) -%}
115
+
116
+ {{ sql_header if sql_header is not none and include_sql_header }}
117
+
118
+ merge into {{ target }} as DBT_INTERNAL_DEST
119
+ using {{ source }} as DBT_INTERNAL_SOURCE
120
+ on FALSE
121
+
122
+ when not matched by source
123
+ {% if predicates %} and {{ predicates | join(' and ') }} {% endif %}
124
+ then delete
125
+
126
+ when not matched then insert
127
+ ({{ dest_cols_csv }})
128
+ values
129
+ ({{ dest_cols_csv }})
130
+
131
+ {% endmacro %}