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,50 @@
1
+ {% macro get_replace_sql(existing_relation, target_relation, sql) %}
2
+ {{- log('Applying REPLACE to: ' ~ existing_relation) -}}
3
+ {{- adapter.dispatch('get_replace_sql', 'dbt')(existing_relation, target_relation, sql) -}}
4
+ {% endmacro %}
5
+
6
+
7
+ {% macro default__get_replace_sql(existing_relation, target_relation, sql) %}
8
+
9
+ {# /* use a create or replace statement if possible */ #}
10
+
11
+ {% set is_replaceable = existing_relation.type == target_relation.type and existing_relation.can_be_replaced %}
12
+
13
+ {% if is_replaceable and existing_relation.is_view %}
14
+ {{ get_replace_view_sql(target_relation, sql) }}
15
+
16
+ {% elif is_replaceable and existing_relation.is_table %}
17
+ {{ get_replace_table_sql(target_relation, sql) }}
18
+
19
+ {% elif is_replaceable and existing_relation.is_materialized_view %}
20
+ {{ get_replace_materialized_view_sql(target_relation, sql) }}
21
+
22
+ {# /* a create or replace statement is not possible, so try to stage and/or backup to be safe */ #}
23
+
24
+ {# /* create target_relation as an intermediate relation, then swap it out with the existing one using a backup */ #}
25
+ {%- elif target_relation.can_be_renamed and existing_relation.can_be_renamed -%}
26
+ {{ get_create_intermediate_sql(target_relation, sql) }};
27
+ {{ get_create_backup_sql(existing_relation) }};
28
+ {{ get_rename_intermediate_sql(target_relation) }};
29
+ {{ get_drop_backup_sql(existing_relation) }}
30
+
31
+ {# /* create target_relation as an intermediate relation, then swap it out with the existing one without using a backup */ #}
32
+ {%- elif target_relation.can_be_renamed -%}
33
+ {{ get_create_intermediate_sql(target_relation, sql) }};
34
+ {{ get_drop_sql(existing_relation) }};
35
+ {{ get_rename_intermediate_sql(target_relation) }}
36
+
37
+ {# /* create target_relation in place by first backing up the existing relation */ #}
38
+ {%- elif existing_relation.can_be_renamed -%}
39
+ {{ get_create_backup_sql(existing_relation) }};
40
+ {{ get_create_sql(target_relation, sql) }};
41
+ {{ get_drop_backup_sql(existing_relation) }}
42
+
43
+ {# /* no renaming is allowed, so just drop and create */ #}
44
+ {%- else -%}
45
+ {{ get_drop_sql(existing_relation) }};
46
+ {{ get_create_sql(target_relation, sql) }}
47
+
48
+ {%- endif -%}
49
+
50
+ {% endmacro %}
@@ -0,0 +1,8 @@
1
+ {% macro drop_schema_named(schema_name) %}
2
+ {{ return(adapter.dispatch('drop_schema_named', 'dbt') (schema_name)) }}
3
+ {% endmacro %}
4
+
5
+ {% macro default__drop_schema_named(schema_name) %}
6
+ {% set schema_relation = api.Relation.create(schema=schema_name) %}
7
+ {{ adapter.drop_schema(schema_relation) }}
8
+ {% endmacro %}
@@ -0,0 +1,60 @@
1
+ {% macro get_create_table_as_sql(temporary, relation, sql) -%}
2
+ {{ adapter.dispatch('get_create_table_as_sql', 'dbt')(temporary, relation, sql) }}
3
+ {%- endmacro %}
4
+
5
+ {% macro default__get_create_table_as_sql(temporary, relation, sql) -%}
6
+ {{ return(create_table_as(temporary, relation, sql)) }}
7
+ {% endmacro %}
8
+
9
+
10
+ /* {# keep logic under old macro name for backwards compatibility #} */
11
+ {% macro create_table_as(temporary, relation, compiled_code, language='sql') -%}
12
+ {# backward compatibility for create_table_as that does not support language #}
13
+ {% if language == "sql" %}
14
+ {{ adapter.dispatch('create_table_as', 'dbt')(temporary, relation, compiled_code)}}
15
+ {% else %}
16
+ {{ adapter.dispatch('create_table_as', 'dbt')(temporary, relation, compiled_code, language) }}
17
+ {% endif %}
18
+
19
+ {%- endmacro %}
20
+
21
+ {% macro default__create_table_as(temporary, relation, sql) -%}
22
+ {%- set sql_header = config.get('sql_header', none) -%}
23
+
24
+ {{ sql_header if sql_header is not none }}
25
+
26
+ create {% if temporary: -%}temporary{%- endif %} table
27
+ {{ relation.include(database=(not temporary), schema=(not temporary)) }}
28
+ {% set contract_config = config.get('contract') %}
29
+ {% if contract_config.enforced and (not temporary) %}
30
+ {{ get_assert_columns_equivalent(sql) }}
31
+ {{ get_table_columns_and_constraints() }}
32
+ {%- set sql = get_select_subquery(sql) %}
33
+ {% endif %}
34
+ as (
35
+ {{ sql }}
36
+ );
37
+ {%- endmacro %}
38
+
39
+
40
+ {% macro default__get_column_names() %}
41
+ {#- loop through user_provided_columns to get column names -#}
42
+ {%- set user_provided_columns = model['columns'] -%}
43
+ {%- for i in user_provided_columns %}
44
+ {%- set col = user_provided_columns[i] -%}
45
+ {%- set col_name = adapter.quote(col['name']) if col.get('quote') else col['name'] -%}
46
+ {{ col_name }}{{ ", " if not loop.last }}
47
+ {%- endfor -%}
48
+ {% endmacro %}
49
+
50
+
51
+ {% macro get_select_subquery(sql) %}
52
+ {{ return(adapter.dispatch('get_select_subquery', 'dbt')(sql)) }}
53
+ {% endmacro %}
54
+
55
+ {% macro default__get_select_subquery(sql) %}
56
+ select {{ adapter.dispatch('get_column_names', 'dbt')() }}
57
+ from (
58
+ {{ sql }}
59
+ ) as model_subq
60
+ {%- 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_table(relation) -%}
8
+ {{- adapter.dispatch('drop_table', 'dbt')(relation) -}}
9
+ {%- endmacro %}
10
+
11
+
12
+ {% macro default__drop_table(relation) -%}
13
+ drop table if exists {{ relation.render() }} cascade
14
+ {%- endmacro %}
@@ -0,0 +1,10 @@
1
+ {% macro get_rename_table_sql(relation, new_name) %}
2
+ {{- adapter.dispatch('get_rename_table_sql', 'dbt')(relation, new_name) -}}
3
+ {% endmacro %}
4
+
5
+
6
+ {% macro default__get_rename_table_sql(relation, new_name) %}
7
+ {{ exceptions.raise_compiler_error(
8
+ "`get_rename_table_sql` has not been implemented for this adapter."
9
+ ) }}
10
+ {% endmacro %}
@@ -0,0 +1,10 @@
1
+ {% macro get_replace_table_sql(relation, sql) %}
2
+ {{- adapter.dispatch('get_replace_table_sql', 'dbt')(relation, sql) -}}
3
+ {% endmacro %}
4
+
5
+
6
+ {% macro default__get_replace_table_sql(relation, sql) %}
7
+ {{ exceptions.raise_compiler_error(
8
+ "`get_replace_table_sql` has not been implemented for this adapter."
9
+ ) }}
10
+ {% endmacro %}
@@ -0,0 +1,27 @@
1
+ {% macro get_create_view_as_sql(relation, sql) -%}
2
+ {{ adapter.dispatch('get_create_view_as_sql', 'dbt')(relation, sql) }}
3
+ {%- endmacro %}
4
+
5
+ {% macro default__get_create_view_as_sql(relation, sql) -%}
6
+ {{ return(create_view_as(relation, sql)) }}
7
+ {% endmacro %}
8
+
9
+
10
+ /* {# keep logic under old name for backwards compatibility #} */
11
+ {% macro create_view_as(relation, sql) -%}
12
+ {{ adapter.dispatch('create_view_as', 'dbt')(relation, sql) }}
13
+ {%- endmacro %}
14
+
15
+ {% macro default__create_view_as(relation, sql) -%}
16
+ {%- set sql_header = config.get('sql_header', none) -%}
17
+
18
+ {{ sql_header if sql_header is not none }}
19
+ create view {{ relation.render() }}
20
+ {% set contract_config = config.get('contract') %}
21
+ {% if contract_config.enforced %}
22
+ {{ get_assert_columns_equivalent(sql) }}
23
+ {%- endif %}
24
+ as (
25
+ {{ sql }}
26
+ );
27
+ {%- 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_view(relation) -%}
8
+ {{- adapter.dispatch('drop_view', 'dbt')(relation) -}}
9
+ {%- endmacro %}
10
+
11
+
12
+ {% macro default__drop_view(relation) -%}
13
+ drop view if exists {{ relation.render() }} cascade
14
+ {%- endmacro %}
@@ -0,0 +1,10 @@
1
+ {% macro get_rename_view_sql(relation, new_name) %}
2
+ {{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}}
3
+ {% endmacro %}
4
+
5
+
6
+ {% macro default__get_rename_view_sql(relation, new_name) %}
7
+ {{ exceptions.raise_compiler_error(
8
+ "`get_rename_view_sql` has not been implemented for this adapter."
9
+ ) }}
10
+ {% endmacro %}
@@ -0,0 +1,66 @@
1
+ {% macro get_replace_view_sql(relation, sql) %}
2
+ {{- adapter.dispatch('get_replace_view_sql', 'dbt')(relation, sql) -}}
3
+ {% endmacro %}
4
+
5
+
6
+ {% macro default__get_replace_view_sql(relation, sql) %}
7
+ {{ exceptions.raise_compiler_error(
8
+ "`get_replace_view_sql` has not been implemented for this adapter."
9
+ ) }}
10
+ {% endmacro %}
11
+
12
+
13
+ /* {#
14
+ Core materialization implementation. BigQuery and Snowflake are similar
15
+ because both can use `create or replace view` where the resulting view's columns
16
+ are not necessarily the same as those of the existing view. On Redshift, this would
17
+ result in: ERROR: cannot change number of columns in view
18
+
19
+ This implementation is superior to the create_temp, swap_with_existing, drop_old
20
+ paradigm because transactions don't run DDL queries atomically on Snowflake. By using
21
+ `create or replace view`, the materialization becomes atomic in nature.
22
+ #} */
23
+
24
+ {% macro create_or_replace_view() %}
25
+ {%- set identifier = model['alias'] -%}
26
+
27
+ {%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
28
+ {%- set exists_as_view = (old_relation is not none and old_relation.is_view) -%}
29
+
30
+ {%- set target_relation = api.Relation.create(
31
+ identifier=identifier, schema=schema, database=database,
32
+ type='view') -%}
33
+ {% set grant_config = config.get('grants') %}
34
+
35
+ {{ run_hooks(pre_hooks) }}
36
+
37
+ -- If there's a table with the same name and we weren't told to full refresh,
38
+ -- that's an error. If we were told to full refresh, drop it. This behavior differs
39
+ -- for Snowflake and BigQuery, so multiple dispatch is used.
40
+ {%- if old_relation is not none and old_relation.is_table -%}
41
+ {{ handle_existing_table(should_full_refresh(), old_relation) }}
42
+ {%- endif -%}
43
+
44
+ -- build model
45
+ {% call statement('main') -%}
46
+ {{ get_create_view_as_sql(target_relation, sql) }}
47
+ {%- endcall %}
48
+
49
+ {% set should_revoke = should_revoke(exists_as_view, full_refresh_mode=True) %}
50
+ {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
51
+
52
+ {{ run_hooks(post_hooks) }}
53
+
54
+ {{ return({'relations': [target_relation]}) }}
55
+
56
+ {% endmacro %}
57
+
58
+
59
+ {% macro handle_existing_table(full_refresh, old_relation) %}
60
+ {{ adapter.dispatch('handle_existing_table', 'dbt')(full_refresh, old_relation) }}
61
+ {% endmacro %}
62
+
63
+ {% macro default__handle_existing_table(full_refresh, old_relation) %}
64
+ {{ log("Dropping relation " ~ old_relation.render() ~ " because it is of type " ~ old_relation.type) }}
65
+ {{ adapter.drop_relation(old_relation) }}
66
+ {% endmacro %}
@@ -0,0 +1,107 @@
1
+ {% macro get_fixture_sql(rows, column_name_to_data_types) %}
2
+ -- Fixture for {{ model.name }}
3
+ {% set default_row = {} %}
4
+
5
+ {%- if not column_name_to_data_types -%}
6
+ {#-- Use defer_relation IFF it is available in the manifest and 'this' is missing from the database --#}
7
+ {%- set this_or_defer_relation = defer_relation if (defer_relation and not load_relation(this)) else this -%}
8
+ {%- set columns_in_relation = adapter.get_columns_in_relation(this_or_defer_relation) -%}
9
+
10
+ {%- set column_name_to_data_types = {} -%}
11
+ {%- set column_name_to_quoted = {} -%}
12
+ {%- for column in columns_in_relation -%}
13
+
14
+ {#-- This needs to be a case-insensitive comparison --#}
15
+ {%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%}
16
+ {%- do column_name_to_quoted.update({column.name|lower: column.quoted}) -%}
17
+ {%- endfor -%}
18
+ {%- endif -%}
19
+
20
+ {%- if not column_name_to_data_types -%}
21
+ {{ exceptions.raise_compiler_error("Not able to get columns for unit test '" ~ model.name ~ "' from relation " ~ this ~ " because the relation doesn't exist") }}
22
+ {%- endif -%}
23
+
24
+ {%- for column_name, column_type in column_name_to_data_types.items() -%}
25
+ {%- do default_row.update({column_name: (safe_cast("null", column_type) | trim )}) -%}
26
+ {%- endfor -%}
27
+
28
+ {{ validate_fixture_rows(rows, row_number) }}
29
+
30
+ {%- for row in rows -%}
31
+ {%- set formatted_row = format_row(row, column_name_to_data_types) -%}
32
+ {%- set default_row_copy = default_row.copy() -%}
33
+ {%- do default_row_copy.update(formatted_row) -%}
34
+ select
35
+ {%- for column_name, column_value in default_row_copy.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%}, {%- endif %}
36
+ {%- endfor %}
37
+ {%- if not loop.last %}
38
+ union all
39
+ {% endif %}
40
+ {%- endfor -%}
41
+
42
+ {%- if (rows | length) == 0 -%}
43
+ select
44
+ {%- for column_name, column_value in default_row.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%},{%- endif %}
45
+ {%- endfor %}
46
+ limit 0
47
+ {%- endif -%}
48
+ {% endmacro %}
49
+
50
+
51
+ {% macro get_expected_sql(rows, column_name_to_data_types, column_name_to_quoted) %}
52
+
53
+ {%- if (rows | length) == 0 -%}
54
+ select * from dbt_internal_unit_test_actual
55
+ limit 0
56
+ {%- else -%}
57
+ {%- for row in rows -%}
58
+ {%- set formatted_row = format_row(row, column_name_to_data_types) -%}
59
+ select
60
+ {%- for column_name, column_value in formatted_row.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%}, {%- endif %}
61
+ {%- endfor %}
62
+ {%- if not loop.last %}
63
+ union all
64
+ {% endif %}
65
+ {%- endfor -%}
66
+ {%- endif -%}
67
+
68
+ {% endmacro %}
69
+
70
+ {%- macro format_row(row, column_name_to_data_types) -%}
71
+ {#-- generate case-insensitive formatted row --#}
72
+ {% set formatted_row = {} %}
73
+ {%- for column_name, column_value in row.items() -%}
74
+ {% set column_name = column_name|lower %}
75
+
76
+ {%- if column_name not in column_name_to_data_types %}
77
+ {#-- if user-provided row contains column name that relation does not contain, raise an error --#}
78
+ {% set fixture_name = "expected output" if model.resource_type == 'unit_test' else ("'" ~ model.name ~ "'") %}
79
+ {{ exceptions.raise_compiler_error(
80
+ "Invalid column name: '" ~ column_name ~ "' in unit test fixture for " ~ fixture_name ~ "."
81
+ "\nAccepted columns for " ~ fixture_name ~ " are: " ~ (column_name_to_data_types.keys()|list)
82
+ ) }}
83
+ {%- endif -%}
84
+
85
+ {%- set column_type = column_name_to_data_types[column_name] %}
86
+
87
+ {#-- sanitize column_value: wrap yaml strings in quotes, apply cast --#}
88
+ {%- set column_value_clean = column_value -%}
89
+ {%- if column_value is string -%}
90
+ {%- set column_value_clean = dbt.string_literal(dbt.escape_single_quotes(column_value)) -%}
91
+ {%- elif column_value is none -%}
92
+ {%- set column_value_clean = 'null' -%}
93
+ {%- endif -%}
94
+
95
+ {%- set row_update = {column_name: safe_cast(column_value_clean, column_type) } -%}
96
+ {%- do formatted_row.update(row_update) -%}
97
+ {%- endfor -%}
98
+ {{ return(formatted_row) }}
99
+ {%- endmacro -%}
100
+
101
+ {%- macro validate_fixture_rows(rows, row_number) -%}
102
+ {{ return(adapter.dispatch('validate_fixture_rows', 'dbt')(rows, row_number)) }}
103
+ {%- endmacro -%}
104
+
105
+ {%- macro default__validate_fixture_rows(rows, row_number) -%}
106
+ {# This is an abstract method for adapter overrides as needed #}
107
+ {%- endmacro -%}
@@ -0,0 +1,9 @@
1
+ {% macro any_value(expression) -%}
2
+ {{ return(adapter.dispatch('any_value', 'dbt') (expression)) }}
3
+ {% endmacro %}
4
+
5
+ {% macro default__any_value(expression) -%}
6
+
7
+ any_value({{ expression }})
8
+
9
+ {%- endmacro %}
@@ -0,0 +1,8 @@
1
+ {% macro array_append(array, new_element) -%}
2
+ {{ return(adapter.dispatch('array_append', 'dbt')(array, new_element)) }}
3
+ {%- endmacro %}
4
+
5
+ {# new_element must be the same data type as elements in array to match postgres functionality #}
6
+ {% macro default__array_append(array, new_element) -%}
7
+ array_append({{ array }}, {{ new_element }})
8
+ {%- endmacro %}
@@ -0,0 +1,7 @@
1
+ {% macro array_concat(array_1, array_2) -%}
2
+ {{ return(adapter.dispatch('array_concat', 'dbt')(array_1, array_2)) }}
3
+ {%- endmacro %}
4
+
5
+ {% macro default__array_concat(array_1, array_2) -%}
6
+ array_cat({{ array_1 }}, {{ array_2 }})
7
+ {%- endmacro %}
@@ -0,0 +1,12 @@
1
+ {% macro array_construct(inputs=[], data_type=api.Column.translate_type('integer')) -%}
2
+ {{ return(adapter.dispatch('array_construct', 'dbt')(inputs, data_type)) }}
3
+ {%- endmacro %}
4
+
5
+ {# all inputs must be the same data type to match postgres functionality #}
6
+ {% macro default__array_construct(inputs, data_type) -%}
7
+ {% if inputs|length > 0 %}
8
+ array[ {{ inputs|join(' , ') }} ]
9
+ {% else %}
10
+ array[]::{{data_type}}[]
11
+ {% endif %}
12
+ {%- endmacro %}
@@ -0,0 +1,9 @@
1
+ {% macro bool_or(expression) -%}
2
+ {{ return(adapter.dispatch('bool_or', 'dbt') (expression)) }}
3
+ {% endmacro %}
4
+
5
+ {% macro default__bool_or(expression) -%}
6
+
7
+ bool_or({{ expression }})
8
+
9
+ {%- endmacro %}
@@ -0,0 +1,7 @@
1
+ {% macro cast(field, type) %}
2
+ {{ return(adapter.dispatch('cast', 'dbt') (field, type)) }}
3
+ {% endmacro %}
4
+
5
+ {% macro default__cast(field, type) %}
6
+ cast({{field}} as {{type}})
7
+ {% endmacro %}
@@ -0,0 +1,7 @@
1
+ {% macro cast_bool_to_text(field) %}
2
+ {{ adapter.dispatch('cast_bool_to_text', 'dbt') (field) }}
3
+ {% endmacro %}
4
+
5
+ {% macro default__cast_bool_to_text(field) %}
6
+ cast({{ field }} as {{ api.Column.translate_type('string') }})
7
+ {% endmacro %}
@@ -0,0 +1,7 @@
1
+ {% macro concat(fields) -%}
2
+ {{ return(adapter.dispatch('concat', 'dbt')(fields)) }}
3
+ {%- endmacro %}
4
+
5
+ {% macro default__concat(fields) -%}
6
+ {{ fields|join(' || ') }}
7
+ {%- endmacro %}
@@ -0,0 +1,129 @@
1
+ {# string ------------------------------------------------- #}
2
+
3
+ {%- macro type_string() -%}
4
+ {{ return(adapter.dispatch('type_string', 'dbt')()) }}
5
+ {%- endmacro -%}
6
+
7
+ {% macro default__type_string() %}
8
+ {{ return(api.Column.translate_type("string")) }}
9
+ {% endmacro %}
10
+
11
+ -- This will return 'text' by default
12
+ -- On Postgres + Snowflake, that's equivalent to varchar (no size)
13
+ -- Redshift will treat that as varchar(256)
14
+
15
+
16
+ {# timestamp ------------------------------------------------- #}
17
+
18
+ {%- macro type_timestamp() -%}
19
+ {{ return(adapter.dispatch('type_timestamp', 'dbt')()) }}
20
+ {%- endmacro -%}
21
+
22
+ {% macro default__type_timestamp() %}
23
+ {{ return(api.Column.translate_type("timestamp")) }}
24
+ {% endmacro %}
25
+
26
+ /*
27
+ POSTGRES
28
+ https://www.postgresql.org/docs/current/datatype-datetime.html:
29
+ The SQL standard requires that writing just `timestamp`
30
+ be equivalent to `timestamp without time zone`, and
31
+ PostgreSQL honors that behavior.
32
+ `timestamptz` is accepted as an abbreviation for `timestamp with time zone`;
33
+ this is a PostgreSQL extension.
34
+
35
+ SNOWFLAKE
36
+ https://docs.snowflake.com/en/sql-reference/data-types-datetime.html#timestamp
37
+ The TIMESTAMP_* variation associated with TIMESTAMP is specified by the
38
+ TIMESTAMP_TYPE_MAPPING session parameter. The default is TIMESTAMP_NTZ.
39
+
40
+ BIGQUERY
41
+ TIMESTAMP means 'timestamp with time zone'
42
+ DATETIME means 'timestamp without time zone'
43
+ TODO: shouldn't this return DATETIME instead of TIMESTAMP, for consistency with other databases?
44
+ e.g. dateadd returns a DATETIME
45
+
46
+ /* Snowflake:
47
+ https://docs.snowflake.com/en/sql-reference/data-types-datetime.html#timestamp
48
+ The TIMESTAMP_* variation associated with TIMESTAMP is specified by the TIMESTAMP_TYPE_MAPPING session parameter. The default is TIMESTAMP_NTZ.
49
+ */
50
+
51
+
52
+ {# float ------------------------------------------------- #}
53
+
54
+ {%- macro type_float() -%}
55
+ {{ return(adapter.dispatch('type_float', 'dbt')()) }}
56
+ {%- endmacro -%}
57
+
58
+ {% macro default__type_float() %}
59
+ {{ return(api.Column.translate_type("float")) }}
60
+ {% endmacro %}
61
+
62
+ {# numeric ------------------------------------------------- #}
63
+
64
+ {%- macro type_numeric() -%}
65
+ {{ return(adapter.dispatch('type_numeric', 'dbt')()) }}
66
+ {%- endmacro -%}
67
+
68
+ /*
69
+ This one can't be just translate_type, since precision/scale make it a bit more complicated.
70
+
71
+ On most databases, the default (precision, scale) is something like:
72
+ Redshift: (18, 0)
73
+ Snowflake: (38, 0)
74
+ Postgres: (<=131072, 0)
75
+
76
+ https://www.postgresql.org/docs/current/datatype-numeric.html:
77
+ Specifying NUMERIC without any precision or scale creates an “unconstrained numeric”
78
+ column in which numeric values of any length can be stored, up to the implementation limits.
79
+ A column of this kind will not coerce input values to any particular scale,
80
+ whereas numeric columns with a declared scale will coerce input values to that scale.
81
+ (The SQL standard requires a default scale of 0, i.e., coercion to integer precision.
82
+ We find this a bit useless. If you're concerned about portability, always specify
83
+ the precision and scale explicitly.)
84
+ */
85
+
86
+ {% macro default__type_numeric() %}
87
+ {{ return(api.Column.numeric_type("numeric", 28, 6)) }}
88
+ {% endmacro %}
89
+
90
+
91
+ {# bigint ------------------------------------------------- #}
92
+
93
+ {%- macro type_bigint() -%}
94
+ {{ return(adapter.dispatch('type_bigint', 'dbt')()) }}
95
+ {%- endmacro -%}
96
+
97
+ -- We don't have a conversion type for 'bigint' in TYPE_LABELS,
98
+ -- so this actually just returns the string 'bigint'
99
+
100
+ {% macro default__type_bigint() %}
101
+ {{ return(api.Column.translate_type("bigint")) }}
102
+ {% endmacro %}
103
+
104
+ -- Good news: BigQuery now supports 'bigint' (and 'int') as an alias for 'int64'
105
+
106
+ {# int ------------------------------------------------- #}
107
+
108
+ {%- macro type_int() -%}
109
+ {{ return(adapter.dispatch('type_int', 'dbt')()) }}
110
+ {%- endmacro -%}
111
+
112
+ {%- macro default__type_int() -%}
113
+ {{ return(api.Column.translate_type("integer")) }}
114
+ {%- endmacro -%}
115
+
116
+ -- returns 'int' everywhere, except BigQuery, where it returns 'int64'
117
+ -- (but BigQuery also now accepts 'int' as a valid alias for 'int64')
118
+
119
+ {# bool ------------------------------------------------- #}
120
+
121
+ {%- macro type_boolean() -%}
122
+ {{ return(adapter.dispatch('type_boolean', 'dbt')()) }}
123
+ {%- endmacro -%}
124
+
125
+ {%- macro default__type_boolean() -%}
126
+ {{ return(api.Column.translate_type("boolean")) }}
127
+ {%- endmacro -%}
128
+
129
+ -- returns 'boolean' everywhere. BigQuery accepts 'boolean' as a valid alias for 'bool'
@@ -0,0 +1,10 @@
1
+ {% macro date(year, month, day) %}
2
+ {{ return(adapter.dispatch('date', 'dbt') (year, month, day)) }}
3
+ {% endmacro %}
4
+
5
+
6
+ {% macro default__date(year, month, day) -%}
7
+ {%- set dt = modules.datetime.date(year, month, day) -%}
8
+ {%- set iso_8601_formatted_date = dt.strftime('%Y-%m-%d') -%}
9
+ to_date('{{ iso_8601_formatted_date }}', 'YYYY-MM-DD')
10
+ {%- endmacro %}