dbt-adapters 1.22.2__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.
Files changed (173) hide show
  1. dbt/adapters/__about__.py +1 -0
  2. dbt/adapters/__init__.py +8 -0
  3. dbt/adapters/base/README.md +13 -0
  4. dbt/adapters/base/__init__.py +16 -0
  5. dbt/adapters/base/column.py +173 -0
  6. dbt/adapters/base/connections.py +429 -0
  7. dbt/adapters/base/impl.py +2036 -0
  8. dbt/adapters/base/meta.py +150 -0
  9. dbt/adapters/base/plugin.py +32 -0
  10. dbt/adapters/base/query_headers.py +106 -0
  11. dbt/adapters/base/relation.py +648 -0
  12. dbt/adapters/cache.py +521 -0
  13. dbt/adapters/capability.py +63 -0
  14. dbt/adapters/catalogs/__init__.py +14 -0
  15. dbt/adapters/catalogs/_client.py +54 -0
  16. dbt/adapters/catalogs/_constants.py +1 -0
  17. dbt/adapters/catalogs/_exceptions.py +39 -0
  18. dbt/adapters/catalogs/_integration.py +113 -0
  19. dbt/adapters/clients/__init__.py +0 -0
  20. dbt/adapters/clients/jinja.py +24 -0
  21. dbt/adapters/contracts/__init__.py +0 -0
  22. dbt/adapters/contracts/connection.py +229 -0
  23. dbt/adapters/contracts/macros.py +11 -0
  24. dbt/adapters/contracts/relation.py +160 -0
  25. dbt/adapters/events/README.md +51 -0
  26. dbt/adapters/events/__init__.py +0 -0
  27. dbt/adapters/events/adapter_types_pb2.py +2 -0
  28. dbt/adapters/events/base_types.py +36 -0
  29. dbt/adapters/events/logging.py +83 -0
  30. dbt/adapters/events/types.py +436 -0
  31. dbt/adapters/exceptions/__init__.py +40 -0
  32. dbt/adapters/exceptions/alias.py +24 -0
  33. dbt/adapters/exceptions/cache.py +68 -0
  34. dbt/adapters/exceptions/compilation.py +269 -0
  35. dbt/adapters/exceptions/connection.py +16 -0
  36. dbt/adapters/exceptions/database.py +51 -0
  37. dbt/adapters/factory.py +264 -0
  38. dbt/adapters/protocol.py +150 -0
  39. dbt/adapters/py.typed +0 -0
  40. dbt/adapters/record/__init__.py +2 -0
  41. dbt/adapters/record/base.py +291 -0
  42. dbt/adapters/record/cursor/cursor.py +69 -0
  43. dbt/adapters/record/cursor/description.py +37 -0
  44. dbt/adapters/record/cursor/execute.py +39 -0
  45. dbt/adapters/record/cursor/fetchall.py +69 -0
  46. dbt/adapters/record/cursor/fetchmany.py +23 -0
  47. dbt/adapters/record/cursor/fetchone.py +23 -0
  48. dbt/adapters/record/cursor/rowcount.py +23 -0
  49. dbt/adapters/record/handle.py +55 -0
  50. dbt/adapters/record/serialization.py +115 -0
  51. dbt/adapters/reference_keys.py +39 -0
  52. dbt/adapters/relation_configs/README.md +25 -0
  53. dbt/adapters/relation_configs/__init__.py +12 -0
  54. dbt/adapters/relation_configs/config_base.py +46 -0
  55. dbt/adapters/relation_configs/config_change.py +26 -0
  56. dbt/adapters/relation_configs/config_validation.py +57 -0
  57. dbt/adapters/sql/__init__.py +2 -0
  58. dbt/adapters/sql/connections.py +263 -0
  59. dbt/adapters/sql/impl.py +286 -0
  60. dbt/adapters/utils.py +69 -0
  61. dbt/include/__init__.py +3 -0
  62. dbt/include/global_project/__init__.py +4 -0
  63. dbt/include/global_project/dbt_project.yml +7 -0
  64. dbt/include/global_project/docs/overview.md +43 -0
  65. dbt/include/global_project/macros/adapters/apply_grants.sql +167 -0
  66. dbt/include/global_project/macros/adapters/columns.sql +144 -0
  67. dbt/include/global_project/macros/adapters/freshness.sql +32 -0
  68. dbt/include/global_project/macros/adapters/indexes.sql +41 -0
  69. dbt/include/global_project/macros/adapters/metadata.sql +105 -0
  70. dbt/include/global_project/macros/adapters/persist_docs.sql +33 -0
  71. dbt/include/global_project/macros/adapters/relation.sql +84 -0
  72. dbt/include/global_project/macros/adapters/schema.sql +20 -0
  73. dbt/include/global_project/macros/adapters/show.sql +26 -0
  74. dbt/include/global_project/macros/adapters/timestamps.sql +52 -0
  75. dbt/include/global_project/macros/adapters/validate_sql.sql +10 -0
  76. dbt/include/global_project/macros/etc/datetime.sql +62 -0
  77. dbt/include/global_project/macros/etc/statement.sql +52 -0
  78. dbt/include/global_project/macros/generic_test_sql/accepted_values.sql +27 -0
  79. dbt/include/global_project/macros/generic_test_sql/not_null.sql +9 -0
  80. dbt/include/global_project/macros/generic_test_sql/relationships.sql +23 -0
  81. dbt/include/global_project/macros/generic_test_sql/unique.sql +12 -0
  82. dbt/include/global_project/macros/get_custom_name/get_custom_alias.sql +36 -0
  83. dbt/include/global_project/macros/get_custom_name/get_custom_database.sql +32 -0
  84. dbt/include/global_project/macros/get_custom_name/get_custom_schema.sql +60 -0
  85. dbt/include/global_project/macros/materializations/configs.sql +21 -0
  86. dbt/include/global_project/macros/materializations/functions/aggregate.sql +65 -0
  87. dbt/include/global_project/macros/materializations/functions/function.sql +20 -0
  88. dbt/include/global_project/macros/materializations/functions/helpers.sql +20 -0
  89. dbt/include/global_project/macros/materializations/functions/scalar.sql +69 -0
  90. dbt/include/global_project/macros/materializations/hooks.sql +35 -0
  91. dbt/include/global_project/macros/materializations/models/clone/can_clone_table.sql +7 -0
  92. dbt/include/global_project/macros/materializations/models/clone/clone.sql +67 -0
  93. dbt/include/global_project/macros/materializations/models/clone/create_or_replace_clone.sql +7 -0
  94. dbt/include/global_project/macros/materializations/models/incremental/column_helpers.sql +80 -0
  95. dbt/include/global_project/macros/materializations/models/incremental/incremental.sql +99 -0
  96. dbt/include/global_project/macros/materializations/models/incremental/is_incremental.sql +13 -0
  97. dbt/include/global_project/macros/materializations/models/incremental/merge.sql +120 -0
  98. dbt/include/global_project/macros/materializations/models/incremental/on_schema_change.sql +159 -0
  99. dbt/include/global_project/macros/materializations/models/incremental/strategies.sql +92 -0
  100. dbt/include/global_project/macros/materializations/models/materialized_view.sql +121 -0
  101. dbt/include/global_project/macros/materializations/models/table.sql +64 -0
  102. dbt/include/global_project/macros/materializations/models/view.sql +72 -0
  103. dbt/include/global_project/macros/materializations/seeds/helpers.sql +128 -0
  104. dbt/include/global_project/macros/materializations/seeds/seed.sql +60 -0
  105. dbt/include/global_project/macros/materializations/snapshots/helpers.sql +345 -0
  106. dbt/include/global_project/macros/materializations/snapshots/snapshot.sql +109 -0
  107. dbt/include/global_project/macros/materializations/snapshots/snapshot_merge.sql +34 -0
  108. dbt/include/global_project/macros/materializations/snapshots/strategies.sql +184 -0
  109. dbt/include/global_project/macros/materializations/tests/helpers.sql +44 -0
  110. dbt/include/global_project/macros/materializations/tests/test.sql +66 -0
  111. dbt/include/global_project/macros/materializations/tests/unit.sql +40 -0
  112. dbt/include/global_project/macros/materializations/tests/where_subquery.sql +15 -0
  113. dbt/include/global_project/macros/python_model/python.sql +114 -0
  114. dbt/include/global_project/macros/relations/column/columns_spec_ddl.sql +89 -0
  115. dbt/include/global_project/macros/relations/create.sql +23 -0
  116. dbt/include/global_project/macros/relations/create_backup.sql +17 -0
  117. dbt/include/global_project/macros/relations/create_intermediate.sql +17 -0
  118. dbt/include/global_project/macros/relations/drop.sql +41 -0
  119. dbt/include/global_project/macros/relations/drop_backup.sql +14 -0
  120. dbt/include/global_project/macros/relations/materialized_view/alter.sql +55 -0
  121. dbt/include/global_project/macros/relations/materialized_view/create.sql +10 -0
  122. dbt/include/global_project/macros/relations/materialized_view/drop.sql +14 -0
  123. dbt/include/global_project/macros/relations/materialized_view/refresh.sql +9 -0
  124. dbt/include/global_project/macros/relations/materialized_view/rename.sql +10 -0
  125. dbt/include/global_project/macros/relations/materialized_view/replace.sql +10 -0
  126. dbt/include/global_project/macros/relations/rename.sql +35 -0
  127. dbt/include/global_project/macros/relations/rename_intermediate.sql +14 -0
  128. dbt/include/global_project/macros/relations/replace.sql +50 -0
  129. dbt/include/global_project/macros/relations/schema.sql +8 -0
  130. dbt/include/global_project/macros/relations/table/create.sql +60 -0
  131. dbt/include/global_project/macros/relations/table/drop.sql +14 -0
  132. dbt/include/global_project/macros/relations/table/rename.sql +10 -0
  133. dbt/include/global_project/macros/relations/table/replace.sql +10 -0
  134. dbt/include/global_project/macros/relations/view/create.sql +27 -0
  135. dbt/include/global_project/macros/relations/view/drop.sql +14 -0
  136. dbt/include/global_project/macros/relations/view/rename.sql +10 -0
  137. dbt/include/global_project/macros/relations/view/replace.sql +66 -0
  138. dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql +107 -0
  139. dbt/include/global_project/macros/utils/any_value.sql +9 -0
  140. dbt/include/global_project/macros/utils/array_append.sql +8 -0
  141. dbt/include/global_project/macros/utils/array_concat.sql +7 -0
  142. dbt/include/global_project/macros/utils/array_construct.sql +12 -0
  143. dbt/include/global_project/macros/utils/bool_or.sql +9 -0
  144. dbt/include/global_project/macros/utils/cast.sql +7 -0
  145. dbt/include/global_project/macros/utils/cast_bool_to_text.sql +7 -0
  146. dbt/include/global_project/macros/utils/concat.sql +7 -0
  147. dbt/include/global_project/macros/utils/data_types.sql +129 -0
  148. dbt/include/global_project/macros/utils/date.sql +10 -0
  149. dbt/include/global_project/macros/utils/date_spine.sql +75 -0
  150. dbt/include/global_project/macros/utils/date_trunc.sql +7 -0
  151. dbt/include/global_project/macros/utils/dateadd.sql +14 -0
  152. dbt/include/global_project/macros/utils/datediff.sql +14 -0
  153. dbt/include/global_project/macros/utils/equals.sql +14 -0
  154. dbt/include/global_project/macros/utils/escape_single_quotes.sql +8 -0
  155. dbt/include/global_project/macros/utils/except.sql +9 -0
  156. dbt/include/global_project/macros/utils/generate_series.sql +53 -0
  157. dbt/include/global_project/macros/utils/hash.sql +7 -0
  158. dbt/include/global_project/macros/utils/intersect.sql +9 -0
  159. dbt/include/global_project/macros/utils/last_day.sql +15 -0
  160. dbt/include/global_project/macros/utils/length.sql +11 -0
  161. dbt/include/global_project/macros/utils/listagg.sql +30 -0
  162. dbt/include/global_project/macros/utils/literal.sql +7 -0
  163. dbt/include/global_project/macros/utils/position.sql +11 -0
  164. dbt/include/global_project/macros/utils/replace.sql +14 -0
  165. dbt/include/global_project/macros/utils/right.sql +12 -0
  166. dbt/include/global_project/macros/utils/safe_cast.sql +9 -0
  167. dbt/include/global_project/macros/utils/split_part.sql +26 -0
  168. dbt/include/global_project/tests/generic/builtin.sql +30 -0
  169. dbt/include/py.typed +0 -0
  170. dbt_adapters-1.22.2.dist-info/METADATA +124 -0
  171. dbt_adapters-1.22.2.dist-info/RECORD +173 -0
  172. dbt_adapters-1.22.2.dist-info/WHEEL +4 -0
  173. dbt_adapters-1.22.2.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,121 @@
1
+ {% materialization materialized_view, default %}
2
+ {% set existing_relation = load_cached_relation(this) %}
3
+ {% set target_relation = this.incorporate(type=this.MaterializedView) %}
4
+ {% set intermediate_relation = make_intermediate_relation(target_relation) %}
5
+ {% set backup_relation_type = target_relation.MaterializedView if existing_relation is none else existing_relation.type %}
6
+ {% set backup_relation = make_backup_relation(target_relation, backup_relation_type) %}
7
+
8
+ {{ materialized_view_setup(backup_relation, intermediate_relation, pre_hooks) }}
9
+
10
+ {% set build_sql = materialized_view_get_build_sql(existing_relation, target_relation, backup_relation, intermediate_relation) %}
11
+
12
+ {% if build_sql == '' %}
13
+ {{ materialized_view_execute_no_op(target_relation) }}
14
+ {% else %}
15
+ {{ materialized_view_execute_build_sql(build_sql, existing_relation, target_relation, post_hooks) }}
16
+ {% endif %}
17
+
18
+ {{ materialized_view_teardown(backup_relation, intermediate_relation, post_hooks) }}
19
+
20
+ {{ return({'relations': [target_relation]}) }}
21
+
22
+ {% endmaterialization %}
23
+
24
+
25
+ {% macro materialized_view_setup(backup_relation, intermediate_relation, pre_hooks) %}
26
+
27
+ -- backup_relation and intermediate_relation should not already exist in the database
28
+ -- it's possible these exist because of a previous run that exited unexpectedly
29
+ {% set preexisting_backup_relation = load_cached_relation(backup_relation) %}
30
+ {% set preexisting_intermediate_relation = load_cached_relation(intermediate_relation) %}
31
+
32
+ -- drop the temp relations if they exist already in the database
33
+ {{ drop_relation_if_exists(preexisting_backup_relation) }}
34
+ {{ drop_relation_if_exists(preexisting_intermediate_relation) }}
35
+
36
+ {{ run_hooks(pre_hooks, inside_transaction=False) }}
37
+
38
+ {% endmacro %}
39
+
40
+
41
+ {% macro materialized_view_teardown(backup_relation, intermediate_relation, post_hooks) %}
42
+
43
+ -- drop the temp relations if they exist to leave the database clean for the next run
44
+ {{ drop_relation_if_exists(backup_relation) }}
45
+ {{ drop_relation_if_exists(intermediate_relation) }}
46
+
47
+ {{ run_hooks(post_hooks, inside_transaction=False) }}
48
+
49
+ {% endmacro %}
50
+
51
+
52
+ {% macro materialized_view_get_build_sql(existing_relation, target_relation, backup_relation, intermediate_relation) %}
53
+
54
+ {% set full_refresh_mode = should_full_refresh() %}
55
+
56
+ -- determine the scenario we're in: create, full_refresh, alter, refresh data
57
+ {% if existing_relation is none %}
58
+ {% set build_sql = get_create_materialized_view_as_sql(target_relation, sql) %}
59
+ {% elif full_refresh_mode or not existing_relation.is_materialized_view %}
60
+ {% set build_sql = get_replace_sql(existing_relation, target_relation, sql) %}
61
+ {% else %}
62
+
63
+ -- get config options
64
+ {% set on_configuration_change = config.get('on_configuration_change') %}
65
+ {% set configuration_changes = get_materialized_view_configuration_changes(existing_relation, config) %}
66
+
67
+ {% if configuration_changes is none %}
68
+ {% set build_sql = refresh_materialized_view(target_relation) %}
69
+
70
+ {% elif on_configuration_change == 'apply' %}
71
+ {% set build_sql = get_alter_materialized_view_as_sql(target_relation, configuration_changes, sql, existing_relation, backup_relation, intermediate_relation) %}
72
+ {% elif on_configuration_change == 'continue' %}
73
+ {% set build_sql = '' %}
74
+ {{ exceptions.warn("Configuration changes were identified and `on_configuration_change` was set to `continue` for `" ~ target_relation.render() ~ "`") }}
75
+ {% elif on_configuration_change == 'fail' %}
76
+ {{ exceptions.raise_fail_fast_error("Configuration changes were identified and `on_configuration_change` was set to `fail` for `" ~ target_relation.render() ~ "`") }}
77
+
78
+ {% else %}
79
+ -- this only happens if the user provides a value other than `apply`, 'skip', 'fail'
80
+ {{ exceptions.raise_compiler_error("Unexpected configuration scenario") }}
81
+
82
+ {% endif %}
83
+
84
+ {% endif %}
85
+
86
+ {% do return(build_sql) %}
87
+
88
+ {% endmacro %}
89
+
90
+
91
+ {% macro materialized_view_execute_no_op(target_relation) %}
92
+ {% do store_raw_result(
93
+ name="main",
94
+ message="skip " ~ target_relation,
95
+ code="skip",
96
+ rows_affected="-1"
97
+ ) %}
98
+ {% endmacro %}
99
+
100
+
101
+ {% macro materialized_view_execute_build_sql(build_sql, existing_relation, target_relation, post_hooks) %}
102
+
103
+ -- `BEGIN` happens here:
104
+ {{ run_hooks(pre_hooks, inside_transaction=True) }}
105
+
106
+ {% set grant_config = config.get('grants') %}
107
+
108
+ {% call statement(name="main") %}
109
+ {{ build_sql }}
110
+ {% endcall %}
111
+
112
+ {% set should_revoke = should_revoke(existing_relation, full_refresh_mode=True) %}
113
+ {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
114
+
115
+ {% do persist_docs(target_relation, model) %}
116
+
117
+ {{ run_hooks(post_hooks, inside_transaction=True) }}
118
+
119
+ {{ adapter.commit() }}
120
+
121
+ {% endmacro %}
@@ -0,0 +1,64 @@
1
+ {% materialization table, default %}
2
+
3
+ {%- set existing_relation = load_cached_relation(this) -%}
4
+ {%- set target_relation = this.incorporate(type='table') %}
5
+ {%- set intermediate_relation = make_intermediate_relation(target_relation) -%}
6
+ -- the intermediate_relation should not already exist in the database; get_relation
7
+ -- will return None in that case. Otherwise, we get a relation that we can drop
8
+ -- later, before we try to use this name for the current operation
9
+ {%- set preexisting_intermediate_relation = load_cached_relation(intermediate_relation) -%}
10
+ /*
11
+ See ../view/view.sql for more information about this relation.
12
+ */
13
+ {%- set backup_relation_type = 'table' if existing_relation is none else existing_relation.type -%}
14
+ {%- set backup_relation = make_backup_relation(target_relation, backup_relation_type) -%}
15
+ -- as above, the backup_relation should not already exist
16
+ {%- set preexisting_backup_relation = load_cached_relation(backup_relation) -%}
17
+ -- grab current tables grants config for comparision later on
18
+ {% set grant_config = config.get('grants') %}
19
+
20
+ -- drop the temp relations if they exist already in the database
21
+ {{ drop_relation_if_exists(preexisting_intermediate_relation) }}
22
+ {{ drop_relation_if_exists(preexisting_backup_relation) }}
23
+
24
+ {{ run_hooks(pre_hooks, inside_transaction=False) }}
25
+
26
+ -- `BEGIN` happens here:
27
+ {{ run_hooks(pre_hooks, inside_transaction=True) }}
28
+
29
+ -- build model
30
+ {% call statement('main') -%}
31
+ {{ get_create_table_as_sql(False, intermediate_relation, sql) }}
32
+ {%- endcall %}
33
+
34
+ {% do create_indexes(intermediate_relation) %}
35
+
36
+ -- cleanup
37
+ {% if existing_relation is not none %}
38
+ /* Do the equivalent of rename_if_exists. 'existing_relation' could have been dropped
39
+ since the variable was first set. */
40
+ {% set existing_relation = load_cached_relation(existing_relation) %}
41
+ {% if existing_relation is not none %}
42
+ {{ adapter.rename_relation(existing_relation, backup_relation) }}
43
+ {% endif %}
44
+ {% endif %}
45
+
46
+ {{ adapter.rename_relation(intermediate_relation, target_relation) }}
47
+
48
+ {{ run_hooks(post_hooks, inside_transaction=True) }}
49
+
50
+ {% set should_revoke = should_revoke(existing_relation, full_refresh_mode=True) %}
51
+ {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
52
+
53
+ {% do persist_docs(target_relation, model) %}
54
+
55
+ -- `COMMIT` happens here
56
+ {{ adapter.commit() }}
57
+
58
+ -- finally, drop the existing/backup relation after the commit
59
+ {{ drop_relation_if_exists(backup_relation) }}
60
+
61
+ {{ run_hooks(post_hooks, inside_transaction=False) }}
62
+
63
+ {{ return({'relations': [target_relation]}) }}
64
+ {% endmaterialization %}
@@ -0,0 +1,72 @@
1
+ {%- materialization view, default -%}
2
+
3
+ {%- set existing_relation = load_cached_relation(this) -%}
4
+ {%- set target_relation = this.incorporate(type='view') -%}
5
+ {%- set intermediate_relation = make_intermediate_relation(target_relation) -%}
6
+
7
+ -- the intermediate_relation should not already exist in the database; get_relation
8
+ -- will return None in that case. Otherwise, we get a relation that we can drop
9
+ -- later, before we try to use this name for the current operation
10
+ {%- set preexisting_intermediate_relation = load_cached_relation(intermediate_relation) -%}
11
+ /*
12
+ This relation (probably) doesn't exist yet. If it does exist, it's a leftover from
13
+ a previous run, and we're going to try to drop it immediately. At the end of this
14
+ materialization, we're going to rename the "existing_relation" to this identifier,
15
+ and then we're going to drop it. In order to make sure we run the correct one of:
16
+ - drop view ...
17
+ - drop table ...
18
+
19
+ We need to set the type of this relation to be the type of the existing_relation, if it exists,
20
+ or else "view" as a sane default if it does not. Note that if the existing_relation does not
21
+ exist, then there is nothing to move out of the way and subsequentally drop. In that case,
22
+ this relation will be effectively unused.
23
+ */
24
+ {%- set backup_relation_type = 'view' if existing_relation is none else existing_relation.type -%}
25
+ {%- set backup_relation = make_backup_relation(target_relation, backup_relation_type) -%}
26
+ -- as above, the backup_relation should not already exist
27
+ {%- set preexisting_backup_relation = load_cached_relation(backup_relation) -%}
28
+ -- grab current tables grants config for comparision later on
29
+ {% set grant_config = config.get('grants') %}
30
+
31
+ {{ run_hooks(pre_hooks, inside_transaction=False) }}
32
+
33
+ -- drop the temp relations if they exist already in the database
34
+ {{ drop_relation_if_exists(preexisting_intermediate_relation) }}
35
+ {{ drop_relation_if_exists(preexisting_backup_relation) }}
36
+
37
+ -- `BEGIN` happens here:
38
+ {{ run_hooks(pre_hooks, inside_transaction=True) }}
39
+
40
+ -- build model
41
+ {% call statement('main') -%}
42
+ {{ get_create_view_as_sql(intermediate_relation, sql) }}
43
+ {%- endcall %}
44
+
45
+ -- cleanup
46
+ -- move the existing view out of the way
47
+ {% if existing_relation is not none %}
48
+ /* Do the equivalent of rename_if_exists. 'existing_relation' could have been dropped
49
+ since the variable was first set. */
50
+ {% set existing_relation = load_cached_relation(existing_relation) %}
51
+ {% if existing_relation is not none %}
52
+ {{ adapter.rename_relation(existing_relation, backup_relation) }}
53
+ {% endif %}
54
+ {% endif %}
55
+ {{ adapter.rename_relation(intermediate_relation, target_relation) }}
56
+
57
+ {% set should_revoke = should_revoke(existing_relation, full_refresh_mode=True) %}
58
+ {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
59
+
60
+ {% do persist_docs(target_relation, model) %}
61
+
62
+ {{ run_hooks(post_hooks, inside_transaction=True) }}
63
+
64
+ {{ adapter.commit() }}
65
+
66
+ {{ drop_relation_if_exists(backup_relation) }}
67
+
68
+ {{ run_hooks(post_hooks, inside_transaction=False) }}
69
+
70
+ {{ return({'relations': [target_relation]}) }}
71
+
72
+ {%- endmaterialization -%}
@@ -0,0 +1,128 @@
1
+
2
+ {% macro create_csv_table(model, agate_table) -%}
3
+ {{ adapter.dispatch('create_csv_table', 'dbt')(model, agate_table) }}
4
+ {%- endmacro %}
5
+
6
+ {% macro default__create_csv_table(model, agate_table) %}
7
+ {%- set column_override = model['config'].get('column_types', {}) -%}
8
+ {%- set quote_seed_column = model['config'].get('quote_columns', None) -%}
9
+
10
+ {% set sql %}
11
+ create table {{ this.render() }} (
12
+ {%- for col_name in agate_table.column_names -%}
13
+ {%- set inferred_type = adapter.convert_type(agate_table, loop.index0) -%}
14
+ {%- set type = column_override.get(col_name, inferred_type) -%}
15
+ {%- set column_name = (col_name | string) -%}
16
+ {{ adapter.quote_seed_column(column_name, quote_seed_column) }} {{ type }} {%- if not loop.last -%}, {%- endif -%}
17
+ {%- endfor -%}
18
+ )
19
+ {% endset %}
20
+
21
+ {% call statement('_') -%}
22
+ {{ sql }}
23
+ {%- endcall %}
24
+
25
+ {{ return(sql) }}
26
+ {% endmacro %}
27
+
28
+
29
+ {% macro reset_csv_table(model, full_refresh, old_relation, agate_table) -%}
30
+ {{ adapter.dispatch('reset_csv_table', 'dbt')(model, full_refresh, old_relation, agate_table) }}
31
+ {%- endmacro %}
32
+
33
+ {% macro default__reset_csv_table(model, full_refresh, old_relation, agate_table) %}
34
+ {% set sql = "" %}
35
+ {% if full_refresh %}
36
+ {{ adapter.drop_relation(old_relation) }}
37
+ {% set sql = create_csv_table(model, agate_table) %}
38
+ {% else %}
39
+ {{ adapter.truncate_relation(old_relation) }}
40
+ {% set sql = "truncate table " ~ old_relation.render() %}
41
+ {% endif %}
42
+
43
+ {{ return(sql) }}
44
+ {% endmacro %}
45
+
46
+
47
+ {% macro get_csv_sql(create_or_truncate_sql, insert_sql) %}
48
+ {{ adapter.dispatch('get_csv_sql', 'dbt')(create_or_truncate_sql, insert_sql) }}
49
+ {% endmacro %}
50
+
51
+ {% macro default__get_csv_sql(create_or_truncate_sql, insert_sql) %}
52
+ {{ create_or_truncate_sql }};
53
+ -- dbt seed --
54
+ {{ insert_sql }}
55
+ {% endmacro %}
56
+
57
+
58
+ {% macro get_binding_char() -%}
59
+ {{ adapter.dispatch('get_binding_char', 'dbt')() }}
60
+ {%- endmacro %}
61
+
62
+ {% macro default__get_binding_char() %}
63
+ {{ return('%s') }}
64
+ {% endmacro %}
65
+
66
+
67
+ {% macro get_batch_size() -%}
68
+ {{ return(adapter.dispatch('get_batch_size', 'dbt')()) }}
69
+ {%- endmacro %}
70
+
71
+ {% macro default__get_batch_size() %}
72
+ {{ return(10000) }}
73
+ {% endmacro %}
74
+
75
+
76
+ {% macro get_seed_column_quoted_csv(model, column_names) %}
77
+ {%- set quote_seed_column = model['config'].get('quote_columns', None) -%}
78
+ {% set quoted = [] %}
79
+ {% for col in column_names -%}
80
+ {%- do quoted.append(adapter.quote_seed_column(col, quote_seed_column)) -%}
81
+ {%- endfor %}
82
+
83
+ {%- set dest_cols_csv = quoted | join(', ') -%}
84
+ {{ return(dest_cols_csv) }}
85
+ {% endmacro %}
86
+
87
+
88
+ {% macro load_csv_rows(model, agate_table) -%}
89
+ {{ adapter.dispatch('load_csv_rows', 'dbt')(model, agate_table) }}
90
+ {%- endmacro %}
91
+
92
+ {% macro default__load_csv_rows(model, agate_table) %}
93
+
94
+ {% set batch_size = get_batch_size() %}
95
+
96
+ {% set cols_sql = get_seed_column_quoted_csv(model, agate_table.column_names) %}
97
+ {% set bindings = [] %}
98
+
99
+ {% set statements = [] %}
100
+
101
+ {% for chunk in agate_table.rows | batch(batch_size) %}
102
+ {% set bindings = [] %}
103
+
104
+ {% for row in chunk %}
105
+ {% do bindings.extend(row) %}
106
+ {% endfor %}
107
+
108
+ {% set sql %}
109
+ insert into {{ this.render() }} ({{ cols_sql }}) values
110
+ {% for row in chunk -%}
111
+ ({%- for column in agate_table.column_names -%}
112
+ {{ get_binding_char() }}
113
+ {%- if not loop.last%},{%- endif %}
114
+ {%- endfor -%})
115
+ {%- if not loop.last%},{%- endif %}
116
+ {%- endfor %}
117
+ {% endset %}
118
+
119
+ {% do adapter.add_query(sql, bindings=bindings, abridge_sql_log=True) %}
120
+
121
+ {% if loop.index0 == 0 %}
122
+ {% do statements.append(sql) %}
123
+ {% endif %}
124
+ {% endfor %}
125
+
126
+ {# Return SQL so we can render it out into the compiled files #}
127
+ {{ return(statements[0]) }}
128
+ {% endmacro %}
@@ -0,0 +1,60 @@
1
+ {% materialization seed, default %}
2
+
3
+ {%- set identifier = model['alias'] -%}
4
+ {%- set full_refresh_mode = (should_full_refresh()) -%}
5
+
6
+ {%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
7
+
8
+ {%- set exists_as_table = (old_relation is not none and old_relation.is_table) -%}
9
+ {%- set exists_as_view = (old_relation is not none and old_relation.is_view) -%}
10
+
11
+ {%- set grant_config = config.get('grants') -%}
12
+ {%- set agate_table = load_agate_table() -%}
13
+ -- grab current tables grants config for comparison later on
14
+
15
+ {%- do store_result('agate_table', response='OK', agate_table=agate_table) -%}
16
+
17
+ {{ run_hooks(pre_hooks, inside_transaction=False) }}
18
+
19
+ -- `BEGIN` happens here:
20
+ {{ run_hooks(pre_hooks, inside_transaction=True) }}
21
+
22
+ -- build model
23
+ {% set create_table_sql = "" %}
24
+ {% if exists_as_view %}
25
+ {{ exceptions.raise_compiler_error("Cannot seed to '{}', it is a view".format(old_relation.render())) }}
26
+ {% elif exists_as_table %}
27
+ {% set create_table_sql = reset_csv_table(model, full_refresh_mode, old_relation, agate_table) %}
28
+ {% else %}
29
+ {% set create_table_sql = create_csv_table(model, agate_table) %}
30
+ {% endif %}
31
+
32
+ {% set code = 'CREATE' if full_refresh_mode else 'INSERT' %}
33
+ {% set rows_affected = (agate_table.rows | length) %}
34
+ {% set sql = load_csv_rows(model, agate_table) %}
35
+
36
+ {% call noop_statement('main', code ~ ' ' ~ rows_affected, code, rows_affected) %}
37
+ {{ get_csv_sql(create_table_sql, sql) }};
38
+ {% endcall %}
39
+
40
+ {% set target_relation = this.incorporate(type='table') %}
41
+
42
+ {% set should_revoke = should_revoke(old_relation, full_refresh_mode) %}
43
+ {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
44
+
45
+ {% do persist_docs(target_relation, model) %}
46
+
47
+ {% if full_refresh_mode or not exists_as_table %}
48
+ {% do create_indexes(target_relation) %}
49
+ {% endif %}
50
+
51
+ {{ run_hooks(post_hooks, inside_transaction=True) }}
52
+
53
+ -- `COMMIT` happens here
54
+ {{ adapter.commit() }}
55
+
56
+ {{ run_hooks(post_hooks, inside_transaction=False) }}
57
+
58
+ {{ return({'relations': [target_relation]}) }}
59
+
60
+ {% endmaterialization %}