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,44 @@
1
+ {% macro get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
2
+ {{ adapter.dispatch('get_test_sql', 'dbt')(main_sql, fail_calc, warn_if, error_if, limit) }}
3
+ {%- endmacro %}
4
+
5
+ {% macro default__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
6
+ select
7
+ {{ fail_calc }} as failures,
8
+ {{ fail_calc }} {{ warn_if }} as should_warn,
9
+ {{ fail_calc }} {{ error_if }} as should_error
10
+ from (
11
+ {{ main_sql }}
12
+ {{ "limit " ~ limit if limit != none }}
13
+ ) dbt_internal_test
14
+ {%- endmacro %}
15
+
16
+
17
+
18
+
19
+ {% macro get_unit_test_sql(main_sql, expected_fixture_sql, expected_column_names) -%}
20
+ {{ adapter.dispatch('get_unit_test_sql', 'dbt')(main_sql, expected_fixture_sql, expected_column_names) }}
21
+ {%- endmacro %}
22
+
23
+ {% macro default__get_unit_test_sql(main_sql, expected_fixture_sql, expected_column_names) -%}
24
+ -- Build actual result given inputs
25
+ with dbt_internal_unit_test_actual as (
26
+ select
27
+ {% for expected_column_name in expected_column_names %}{{expected_column_name}}{% if not loop.last -%},{% endif %}{%- endfor -%}, {{ dbt.string_literal("actual") }} as {{ adapter.quote("actual_or_expected") }}
28
+ from (
29
+ {{ main_sql }}
30
+ ) _dbt_internal_unit_test_actual
31
+ ),
32
+ -- Build expected result
33
+ dbt_internal_unit_test_expected as (
34
+ select
35
+ {% for expected_column_name in expected_column_names %}{{expected_column_name}}{% if not loop.last -%}, {% endif %}{%- endfor -%}, {{ dbt.string_literal("expected") }} as {{ adapter.quote("actual_or_expected") }}
36
+ from (
37
+ {{ expected_fixture_sql }}
38
+ ) _dbt_internal_unit_test_expected
39
+ )
40
+ -- Union actual and expected results
41
+ select * from dbt_internal_unit_test_actual
42
+ union all
43
+ select * from dbt_internal_unit_test_expected
44
+ {%- endmacro %}
@@ -0,0 +1,66 @@
1
+ {%- materialization test, default -%}
2
+
3
+ {% set relations = [] %}
4
+ {% set limit = config.get('limit') %}
5
+
6
+ {% set sql_with_limit %}
7
+ {{ get_limit_subquery_sql(sql, limit) }}
8
+ {% endset %}
9
+
10
+ {% if should_store_failures() %}
11
+
12
+ {% set identifier = model['alias'] %}
13
+ {% set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) %}
14
+
15
+ {% set store_failures_as = config.get('store_failures_as') %}
16
+ -- if `--store-failures` is invoked via command line and `store_failures_as` is not set,
17
+ -- config.get('store_failures_as', 'table') returns None, not 'table'
18
+ {% if store_failures_as == none %}{% set store_failures_as = 'table' %}{% endif %}
19
+ {% if store_failures_as not in ['table', 'view'] %}
20
+ {{ exceptions.raise_compiler_error(
21
+ "'" ~ store_failures_as ~ "' is not a valid value for `store_failures_as`. "
22
+ "Accepted values are: ['ephemeral', 'table', 'view']"
23
+ ) }}
24
+ {% endif %}
25
+
26
+ {% set target_relation = api.Relation.create(
27
+ identifier=identifier, schema=schema, database=database, type=store_failures_as) -%} %}
28
+
29
+ {% if old_relation %}
30
+ {% do adapter.drop_relation(old_relation) %}
31
+ {% endif %}
32
+
33
+ {% call statement(auto_begin=True) %}
34
+ {{ get_create_sql(target_relation, sql_with_limit) }}
35
+ {% endcall %}
36
+
37
+ {% do relations.append(target_relation) %}
38
+
39
+ {# Since the test failures have already been saved to the database, reuse that result rather than querying again #}
40
+ {% set main_sql %}
41
+ select *
42
+ from {{ target_relation }}
43
+ {% endset %}
44
+
45
+ {{ adapter.commit() }}
46
+
47
+ {% else %}
48
+
49
+ {% set main_sql = sql_with_limit %}
50
+
51
+ {% endif %}
52
+
53
+ {% set fail_calc = config.get('fail_calc') %}
54
+ {% set warn_if = config.get('warn_if') %}
55
+ {% set error_if = config.get('error_if') %}
56
+
57
+ {% call statement('main', fetch_result=True) -%}
58
+
59
+ {# The limit has already been included above, and we do not want to duplicate it again. We also want to be safe for macro overrides treating `limit` as a required parameter. #}
60
+ {{ get_test_sql(main_sql, fail_calc, warn_if, error_if, limit=none)}}
61
+
62
+ {%- endcall %}
63
+
64
+ {{ return({'relations': relations}) }}
65
+
66
+ {%- endmaterialization -%}
@@ -0,0 +1,40 @@
1
+ {%- materialization unit, default -%}
2
+
3
+ {% set relations = [] %}
4
+
5
+ {% set expected_rows = config.get('expected_rows') %}
6
+ {% set expected_sql = config.get('expected_sql') %}
7
+ {% set tested_expected_column_names = expected_rows[0].keys() if (expected_rows | length ) > 0 else get_columns_in_query(sql) %}
8
+
9
+ {%- set target_relation = this.incorporate(type='table') -%}
10
+ {%- set temp_relation = make_temp_relation(target_relation)-%}
11
+ {% do run_query(get_create_table_as_sql(True, temp_relation, get_empty_subquery_sql(sql))) %}
12
+ {%- set columns_in_relation = adapter.get_columns_in_relation(temp_relation) -%}
13
+ {%- set column_name_to_data_types = {} -%}
14
+ {%- set column_name_to_quoted = {} -%}
15
+ {%- for column in columns_in_relation -%}
16
+ {%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%}
17
+ {%- do column_name_to_quoted.update({column.name|lower: column.quoted}) -%}
18
+ {%- endfor -%}
19
+
20
+ {%- set expected_column_names_quoted = [] -%}
21
+ {%- for column_name in tested_expected_column_names -%}
22
+ {%- do expected_column_names_quoted.append(column_name_to_quoted[column_name|lower]) -%}
23
+ {%- endfor -%}
24
+
25
+ {% if not expected_sql %}
26
+ {% set expected_sql = get_expected_sql(expected_rows, column_name_to_data_types, column_name_to_quoted) %}
27
+ {% endif %}
28
+ {% set unit_test_sql = get_unit_test_sql(sql, expected_sql, expected_column_names_quoted) %}
29
+
30
+ {% call statement('main', fetch_result=True) -%}
31
+
32
+ {{ unit_test_sql }}
33
+
34
+ {%- endcall %}
35
+
36
+ {% do adapter.drop_relation(temp_relation) %}
37
+
38
+ {{ return({'relations': relations}) }}
39
+
40
+ {%- endmaterialization -%}
@@ -0,0 +1,15 @@
1
+ {% macro get_where_subquery(relation) -%}
2
+ {% do return(adapter.dispatch('get_where_subquery', 'dbt')(relation)) %}
3
+ {%- endmacro %}
4
+
5
+ {% macro default__get_where_subquery(relation) -%}
6
+ {% set where = config.get('where', '') %}
7
+ {% if where %}
8
+ {%- set filtered -%}
9
+ (select * from {{ relation }} where {{ where }}) dbt_subquery
10
+ {%- endset -%}
11
+ {% do return(filtered) %}
12
+ {%- else -%}
13
+ {% do return(relation) %}
14
+ {%- endif -%}
15
+ {%- endmacro %}
@@ -0,0 +1,114 @@
1
+ {% macro resolve_model_name(input_model_name) %}
2
+ {{ return(adapter.dispatch('resolve_model_name', 'dbt')(input_model_name)) }}
3
+ {% endmacro %}
4
+
5
+ {%- macro default__resolve_model_name(input_model_name) -%}
6
+ {{ input_model_name | string | replace('"', '\"') }}
7
+ {%- endmacro -%}
8
+
9
+ {% macro build_ref_function(model) %}
10
+
11
+ {%- set ref_dict = {} -%}
12
+ {%- for _ref in model.refs -%}
13
+ {% set _ref_args = [_ref.get('package'), _ref['name']] if _ref.get('package') else [_ref['name'],] %}
14
+ {%- set resolved = ref(*_ref_args, v=_ref.get('version')) -%}
15
+
16
+ {#
17
+ We want to get the string of the returned relation by calling .render() in order to skip sample/empty
18
+ mode rendering logic. However, people override the default ref macro, and often return a string instead
19
+ of a relation (like the ref macro does by default). Thus, to make sure we dont blow things up, we have
20
+ to ensure the resolved relation has a .render() method.
21
+ #}
22
+ {%- if resolved.render is defined and resolved.render is callable -%}
23
+ {%- set resolved = resolved.render() -%}
24
+ {%- endif -%}
25
+
26
+ {%- if _ref.get('version') -%}
27
+ {% do _ref_args.extend(["v" ~ _ref['version']]) %}
28
+ {%- endif -%}
29
+ {%- do ref_dict.update({_ref_args | join('.'): resolve_model_name(resolved)}) -%}
30
+ {%- endfor -%}
31
+
32
+ def ref(*args, **kwargs):
33
+ refs = {{ ref_dict | tojson }}
34
+ key = '.'.join(args)
35
+ version = kwargs.get("v") or kwargs.get("version")
36
+ if version:
37
+ key += f".v{version}"
38
+ dbt_load_df_function = kwargs.get("dbt_load_df_function")
39
+ return dbt_load_df_function(refs[key])
40
+
41
+ {% endmacro %}
42
+
43
+ {% macro build_source_function(model) %}
44
+
45
+ {%- set source_dict = {} -%}
46
+ {%- for _source in model.sources -%}
47
+ {%- set resolved = source(*_source) -%}
48
+ {%- do source_dict.update({_source | join('.'): resolve_model_name(resolved)}) -%}
49
+ {%- endfor -%}
50
+
51
+ def source(*args, dbt_load_df_function):
52
+ sources = {{ source_dict | tojson }}
53
+ key = '.'.join(args)
54
+ return dbt_load_df_function(sources[key])
55
+
56
+ {% endmacro %}
57
+
58
+ {% macro build_config_dict(model) %}
59
+ {%- set config_dict = {} -%}
60
+ {% set config_dbt_used = zip(model.config.config_keys_used, model.config.config_keys_defaults) | list %}
61
+ {%- for key, default in config_dbt_used -%}
62
+ {# weird type testing with enum, would be much easier to write this logic in Python! #}
63
+ {%- if key == "language" -%}
64
+ {%- set value = "python" -%}
65
+ {%- endif -%}
66
+ {%- set value = model.config.get(key, default) -%}
67
+ {%- do config_dict.update({key: value}) -%}
68
+ {%- endfor -%}
69
+ config_dict = {{ config_dict }}
70
+ {% endmacro %}
71
+
72
+ {% macro py_script_postfix(model) %}
73
+ # This part is user provided model code
74
+ # you will need to copy the next section to run the code
75
+ # COMMAND ----------
76
+ # this part is dbt logic for get ref work, do not modify
77
+
78
+ {{ build_ref_function(model ) }}
79
+ {{ build_source_function(model ) }}
80
+ {{ build_config_dict(model) }}
81
+
82
+ class config:
83
+ def __init__(self, *args, **kwargs):
84
+ pass
85
+
86
+ @staticmethod
87
+ def get(key, default=None):
88
+ return config_dict.get(key, default)
89
+
90
+ class this:
91
+ """dbt.this() or dbt.this.identifier"""
92
+ database = "{{ this.database }}"
93
+ schema = "{{ this.schema }}"
94
+ identifier = "{{ this.identifier }}"
95
+ {% set this_relation_name = resolve_model_name(this) %}
96
+ def __repr__(self):
97
+ return '{{ this_relation_name }}'
98
+
99
+
100
+ class dbtObj:
101
+ def __init__(self, load_df_function) -> None:
102
+ self.source = lambda *args: source(*args, dbt_load_df_function=load_df_function)
103
+ self.ref = lambda *args, **kwargs: ref(*args, **kwargs, dbt_load_df_function=load_df_function)
104
+ self.config = config
105
+ self.this = this()
106
+ self.is_incremental = {{ is_incremental() }}
107
+
108
+ # COMMAND ----------
109
+ {{py_script_comment()}}
110
+ {% endmacro %}
111
+
112
+ {#-- entry point for add instuctions for running compiled_code --#}
113
+ {%macro py_script_comment()%}
114
+ {%endmacro%}
@@ -0,0 +1,89 @@
1
+ {%- macro get_table_columns_and_constraints() -%}
2
+ {{ adapter.dispatch('get_table_columns_and_constraints', 'dbt')() }}
3
+ {%- endmacro -%}
4
+
5
+ {% macro default__get_table_columns_and_constraints() -%}
6
+ {{ return(table_columns_and_constraints()) }}
7
+ {%- endmacro %}
8
+
9
+ {% macro table_columns_and_constraints() %}
10
+ {# loop through user_provided_columns to create DDL with data types and constraints #}
11
+ {%- set raw_column_constraints = adapter.render_raw_columns_constraints(raw_columns=model['columns']) -%}
12
+ {%- set raw_model_constraints = adapter.render_raw_model_constraints(raw_constraints=model['constraints']) -%}
13
+ (
14
+ {% for c in raw_column_constraints -%}
15
+ {{ c }}{{ "," if not loop.last or raw_model_constraints }}
16
+ {% endfor %}
17
+ {% for c in raw_model_constraints -%}
18
+ {{ c }}{{ "," if not loop.last }}
19
+ {% endfor -%}
20
+ )
21
+ {% endmacro %}
22
+
23
+ {%- macro get_assert_columns_equivalent(sql) -%}
24
+ {{ adapter.dispatch('get_assert_columns_equivalent', 'dbt')(sql) }}
25
+ {%- endmacro -%}
26
+
27
+ {% macro default__get_assert_columns_equivalent(sql) -%}
28
+ {{ return(assert_columns_equivalent(sql)) }}
29
+ {%- endmacro %}
30
+
31
+ {#
32
+ Compares the column schema provided by a model's sql file to the column schema provided by a model's schema file.
33
+ If any differences in name, data_type or number of columns exist between the two schemas, raises a compiler error
34
+ #}
35
+ {% macro assert_columns_equivalent(sql) %}
36
+
37
+ {#-- First ensure the user has defined 'columns' in yaml specification --#}
38
+ {%- set user_defined_columns = model['columns'] -%}
39
+ {%- if not user_defined_columns -%}
40
+ {{ exceptions.raise_contract_error([], []) }}
41
+ {%- endif -%}
42
+
43
+ {#-- Obtain the column schema provided by sql file. #}
44
+ {%- set sql_file_provided_columns = get_column_schema_from_query(sql, config.get('sql_header', none)) -%}
45
+ {#--Obtain the column schema provided by the schema file by generating an 'empty schema' query from the model's columns. #}
46
+ {%- set schema_file_provided_columns = get_column_schema_from_query(get_empty_schema_sql(user_defined_columns)) -%}
47
+
48
+ {#-- create dictionaries with name and formatted data type and strings for exception #}
49
+ {%- set sql_columns = format_columns(sql_file_provided_columns) -%}
50
+ {%- set yaml_columns = format_columns(schema_file_provided_columns) -%}
51
+
52
+ {%- if sql_columns|length != yaml_columns|length -%}
53
+ {%- do exceptions.raise_contract_error(yaml_columns, sql_columns) -%}
54
+ {%- endif -%}
55
+
56
+ {%- for sql_col in sql_columns -%}
57
+ {%- set yaml_col = [] -%}
58
+ {%- for this_col in yaml_columns -%}
59
+ {%- if this_col['name'] == sql_col['name'] -%}
60
+ {%- do yaml_col.append(this_col) -%}
61
+ {%- break -%}
62
+ {%- endif -%}
63
+ {%- endfor -%}
64
+ {%- if not yaml_col -%}
65
+ {#-- Column with name not found in yaml #}
66
+ {%- do exceptions.raise_contract_error(yaml_columns, sql_columns) -%}
67
+ {%- endif -%}
68
+ {%- if sql_col['formatted'] != yaml_col[0]['formatted'] -%}
69
+ {#-- Column data types don't match #}
70
+ {%- do exceptions.raise_contract_error(yaml_columns, sql_columns) -%}
71
+ {%- endif -%}
72
+ {%- endfor -%}
73
+
74
+ {% endmacro %}
75
+
76
+ {% macro format_columns(columns) %}
77
+ {% set formatted_columns = [] %}
78
+ {% for column in columns %}
79
+ {%- set formatted_column = adapter.dispatch('format_column', 'dbt')(column) -%}
80
+ {%- do formatted_columns.append(formatted_column) -%}
81
+ {% endfor %}
82
+ {{ return(formatted_columns) }}
83
+ {% endmacro %}
84
+
85
+ {% macro default__format_column(column) -%}
86
+ {% set data_type = column.dtype %}
87
+ {% set formatted = column.column.lower() ~ " " ~ data_type %}
88
+ {{ return({'name': column.name, 'data_type': data_type, 'formatted': formatted}) }}
89
+ {%- endmacro -%}
@@ -0,0 +1,23 @@
1
+ {%- macro get_create_sql(relation, sql) -%}
2
+ {{- log('Applying CREATE to: ' ~ relation) -}}
3
+ {{- adapter.dispatch('get_create_sql', 'dbt')(relation, sql) -}}
4
+ {%- endmacro -%}
5
+
6
+
7
+ {%- macro default__get_create_sql(relation, sql) -%}
8
+
9
+ {%- if relation.is_view -%}
10
+ {{ get_create_view_as_sql(relation, sql) }}
11
+
12
+ {%- elif relation.is_table -%}
13
+ {{ get_create_table_as_sql(False, relation, sql) }}
14
+
15
+ {%- elif relation.is_materialized_view -%}
16
+ {{ get_create_materialized_view_as_sql(relation, sql) }}
17
+
18
+ {%- else -%}
19
+ {{- exceptions.raise_compiler_error("`get_create_sql` has not been implemented for: " ~ relation.type ) -}}
20
+
21
+ {%- endif -%}
22
+
23
+ {%- endmacro -%}
@@ -0,0 +1,17 @@
1
+ {%- macro get_create_backup_sql(relation) -%}
2
+ {{- log('Applying CREATE BACKUP to: ' ~ relation) -}}
3
+ {{- adapter.dispatch('get_create_backup_sql', 'dbt')(relation) -}}
4
+ {%- endmacro -%}
5
+
6
+
7
+ {%- macro default__get_create_backup_sql(relation) -%}
8
+
9
+ -- get the standard backup name
10
+ {% set backup_relation = make_backup_relation(relation, relation.type) %}
11
+
12
+ -- drop any pre-existing backup
13
+ {{ get_drop_sql(backup_relation) }};
14
+
15
+ {{ get_rename_sql(relation, backup_relation.identifier) }}
16
+
17
+ {%- endmacro -%}
@@ -0,0 +1,17 @@
1
+ {%- macro get_create_intermediate_sql(relation, sql) -%}
2
+ {{- log('Applying CREATE INTERMEDIATE to: ' ~ relation) -}}
3
+ {{- adapter.dispatch('get_create_intermediate_sql', 'dbt')(relation, sql) -}}
4
+ {%- endmacro -%}
5
+
6
+
7
+ {%- macro default__get_create_intermediate_sql(relation, sql) -%}
8
+
9
+ -- get the standard intermediate name
10
+ {% set intermediate_relation = make_intermediate_relation(relation) %}
11
+
12
+ -- drop any pre-existing intermediate
13
+ {{ get_drop_sql(intermediate_relation) }};
14
+
15
+ {{ get_create_sql(intermediate_relation, sql) }}
16
+
17
+ {%- endmacro -%}
@@ -0,0 +1,41 @@
1
+ {%- macro get_drop_sql(relation) -%}
2
+ {{- log('Applying DROP to: ' ~ relation) -}}
3
+ {{- adapter.dispatch('get_drop_sql', 'dbt')(relation) -}}
4
+ {%- endmacro -%}
5
+
6
+
7
+ {%- macro default__get_drop_sql(relation) -%}
8
+
9
+ {%- if relation.is_view -%}
10
+ {{ drop_view(relation) }}
11
+
12
+ {%- elif relation.is_table -%}
13
+ {{ drop_table(relation) }}
14
+
15
+ {%- elif relation.is_materialized_view -%}
16
+ {{ drop_materialized_view(relation) }}
17
+
18
+ {%- else -%}
19
+ drop {{ relation.type }} if exists {{ relation.render() }} cascade
20
+
21
+ {%- endif -%}
22
+
23
+ {%- endmacro -%}
24
+
25
+
26
+ {% macro drop_relation(relation) -%}
27
+ {{ return(adapter.dispatch('drop_relation', 'dbt')(relation)) }}
28
+ {% endmacro %}
29
+
30
+ {% macro default__drop_relation(relation) -%}
31
+ {% call statement('drop_relation', auto_begin=False) -%}
32
+ {{ get_drop_sql(relation) }}
33
+ {%- endcall %}
34
+ {% endmacro %}
35
+
36
+
37
+ {% macro drop_relation_if_exists(relation) %}
38
+ {% if relation is not none %}
39
+ {{ adapter.drop_relation(relation) }}
40
+ {% endif %}
41
+ {% endmacro %}
@@ -0,0 +1,14 @@
1
+ {%- macro get_drop_backup_sql(relation) -%}
2
+ {{- log('Applying DROP BACKUP to: ' ~ relation) -}}
3
+ {{- adapter.dispatch('get_drop_backup_sql', 'dbt')(relation) -}}
4
+ {%- endmacro -%}
5
+
6
+
7
+ {%- macro default__get_drop_backup_sql(relation) -%}
8
+
9
+ -- get the standard backup name
10
+ {% set backup_relation = make_backup_relation(relation, relation.type) %}
11
+
12
+ {{ get_drop_sql(backup_relation) }}
13
+
14
+ {%- endmacro -%}
@@ -0,0 +1,55 @@
1
+ {% macro get_alter_materialized_view_as_sql(
2
+ relation,
3
+ configuration_changes,
4
+ sql,
5
+ existing_relation,
6
+ backup_relation,
7
+ intermediate_relation
8
+ ) %}
9
+ {{- log('Applying ALTER to: ' ~ relation) -}}
10
+ {{- adapter.dispatch('get_alter_materialized_view_as_sql', 'dbt')(
11
+ relation,
12
+ configuration_changes,
13
+ sql,
14
+ existing_relation,
15
+ backup_relation,
16
+ intermediate_relation
17
+ ) -}}
18
+ {% endmacro %}
19
+
20
+
21
+ {% macro default__get_alter_materialized_view_as_sql(
22
+ relation,
23
+ configuration_changes,
24
+ sql,
25
+ existing_relation,
26
+ backup_relation,
27
+ intermediate_relation
28
+ ) %}
29
+ {{ exceptions.raise_compiler_error("Materialized views have not been implemented for this adapter.") }}
30
+ {% endmacro %}
31
+
32
+
33
+ {% macro get_materialized_view_configuration_changes(existing_relation, new_config) %}
34
+ /* {#
35
+ It's recommended that configuration changes be formatted as follows:
36
+ {"<change_category>": [{"action": "<name>", "context": ...}]}
37
+
38
+ For example:
39
+ {
40
+ "indexes": [
41
+ {"action": "drop", "context": "index_abc"},
42
+ {"action": "create", "context": {"columns": ["column_1", "column_2"], "type": "hash", "unique": True}},
43
+ ],
44
+ }
45
+
46
+ Either way, `get_materialized_view_configuration_changes` needs to align with `get_alter_materialized_view_as_sql`.
47
+ #} */
48
+ {{- log('Determining configuration changes on: ' ~ existing_relation) -}}
49
+ {%- do return(adapter.dispatch('get_materialized_view_configuration_changes', 'dbt')(existing_relation, new_config)) -%}
50
+ {% endmacro %}
51
+
52
+
53
+ {% macro default__get_materialized_view_configuration_changes(existing_relation, new_config) %}
54
+ {{ exceptions.raise_compiler_error("Materialized views have not been implemented for this adapter.") }}
55
+ {% endmacro %}
@@ -0,0 +1,10 @@
1
+ {% macro get_create_materialized_view_as_sql(relation, sql) -%}
2
+ {{- adapter.dispatch('get_create_materialized_view_as_sql', 'dbt')(relation, sql) -}}
3
+ {%- endmacro %}
4
+
5
+
6
+ {% macro default__get_create_materialized_view_as_sql(relation, sql) -%}
7
+ {{ exceptions.raise_compiler_error(
8
+ "`get_create_materialized_view_as_sql` has not been implemented for this adapter."
9
+ ) }}
10
+ {% endmacro %}
@@ -0,0 +1,14 @@
1
+ {# /*
2
+ This was already implemented. Instead of creating a new macro that aligns with the standard,
3
+ this was reused and the default was maintained. This gets called by `drop_relation`, which
4
+ actually executes the drop, and `get_drop_sql`, which returns the template.
5
+ */ #}
6
+
7
+ {% macro drop_materialized_view(relation) -%}
8
+ {{- adapter.dispatch('drop_materialized_view', 'dbt')(relation) -}}
9
+ {%- endmacro %}
10
+
11
+
12
+ {% macro default__drop_materialized_view(relation) -%}
13
+ drop materialized view if exists {{ relation.render() }} cascade
14
+ {%- endmacro %}
@@ -0,0 +1,9 @@
1
+ {% macro refresh_materialized_view(relation) %}
2
+ {{- log('Applying REFRESH to: ' ~ relation) -}}
3
+ {{- adapter.dispatch('refresh_materialized_view', 'dbt')(relation) -}}
4
+ {% endmacro %}
5
+
6
+
7
+ {% macro default__refresh_materialized_view(relation) %}
8
+ {{ exceptions.raise_compiler_error("`refresh_materialized_view` has not been implemented for this adapter.") }}
9
+ {% endmacro %}
@@ -0,0 +1,10 @@
1
+ {% macro get_rename_materialized_view_sql(relation, new_name) %}
2
+ {{- adapter.dispatch('get_rename_materialized_view_sql', 'dbt')(relation, new_name) -}}
3
+ {% endmacro %}
4
+
5
+
6
+ {% macro default__get_rename_materialized_view_sql(relation, new_name) %}
7
+ {{ exceptions.raise_compiler_error(
8
+ "`get_rename_materialized_view_sql` has not been implemented for this adapter."
9
+ ) }}
10
+ {% endmacro %}
@@ -0,0 +1,10 @@
1
+ {% macro get_replace_materialized_view_sql(relation, sql) %}
2
+ {{- adapter.dispatch('get_replace_materialized_view_sql', 'dbt')(relation, sql) -}}
3
+ {% endmacro %}
4
+
5
+
6
+ {% macro default__get_replace_materialized_view_sql(relation, sql) %}
7
+ {{ exceptions.raise_compiler_error(
8
+ "`get_replace_materialized_view_sql` has not been implemented for this adapter."
9
+ ) }}
10
+ {% endmacro %}
@@ -0,0 +1,35 @@
1
+ {%- macro get_rename_sql(relation, new_name) -%}
2
+ {{- log('Applying RENAME to: ' ~ relation) -}}
3
+ {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}}
4
+ {%- endmacro -%}
5
+
6
+
7
+ {%- macro default__get_rename_sql(relation, new_name) -%}
8
+
9
+ {%- if relation.is_view -%}
10
+ {{ get_rename_view_sql(relation, new_name) }}
11
+
12
+ {%- elif relation.is_table -%}
13
+ {{ get_rename_table_sql(relation, new_name) }}
14
+
15
+ {%- elif relation.is_materialized_view -%}
16
+ {{ get_rename_materialized_view_sql(relation, new_name) }}
17
+
18
+ {%- else -%}
19
+ {{- exceptions.raise_compiler_error("`get_rename_sql` has not been implemented for: " ~ relation.type ) -}}
20
+
21
+ {%- endif -%}
22
+
23
+ {%- endmacro -%}
24
+
25
+
26
+ {% macro rename_relation(from_relation, to_relation) -%}
27
+ {{ return(adapter.dispatch('rename_relation', 'dbt')(from_relation, to_relation)) }}
28
+ {% endmacro %}
29
+
30
+ {% macro default__rename_relation(from_relation, to_relation) -%}
31
+ {% set target_name = adapter.quote_as_configured(to_relation.identifier, 'identifier') %}
32
+ {% call statement('rename_relation') -%}
33
+ alter table {{ from_relation.render() }} rename to {{ target_name }}
34
+ {%- endcall %}
35
+ {% endmacro %}
@@ -0,0 +1,14 @@
1
+ {%- macro get_rename_intermediate_sql(relation) -%}
2
+ {{- log('Applying RENAME INTERMEDIATE to: ' ~ relation) -}}
3
+ {{- adapter.dispatch('get_rename_intermediate_sql', 'dbt')(relation) -}}
4
+ {%- endmacro -%}
5
+
6
+
7
+ {%- macro default__get_rename_intermediate_sql(relation) -%}
8
+
9
+ -- get the standard intermediate name
10
+ {% set intermediate_relation = make_intermediate_relation(relation) %}
11
+
12
+ {{ get_rename_sql(intermediate_relation, relation.identifier) }}
13
+
14
+ {%- endmacro -%}