-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathcreate_table_as.sql
More file actions
122 lines (105 loc) · 4.74 KB
/
create_table_as.sql
File metadata and controls
122 lines (105 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
{% macro edr_create_table_as(
temporary, relation, sql_query, drop_first=false, should_commit=false
) %}
{# This macro contains a simplified implementation that replaces our usage of
dbt.create_table_as and serves our needs.
This version also runs the query rather than return the SQL.
#}
{% if drop_first %} {% do dbt.drop_relation_if_exists(relation) %} {% endif %}
{% set create_query = elementary.edr_get_create_table_as_sql(
temporary, relation, sql_query
) %}
{% do elementary.run_query(create_query) %}
{% if should_commit %} {% do adapter.commit() %} {% endif %}
{% endmacro %}
{% macro edr_get_create_table_as_sql(temporary, relation, sql_query) %}
{{
return(
adapter.dispatch("edr_get_create_table_as_sql", "elementary")(
temporary, relation, sql_query
)
)
}}
{% endmacro %}
{% macro default__edr_get_create_table_as_sql(temporary, relation, sql_query) %}
{{ dbt.get_create_table_as_sql(temporary, relation, sql_query) }}
{% endmacro %}
{# Simplified versions for dbt-fusion supported adapters as the original dbt macro
no longer works outside of the scope of a model's materialization #}
{% macro snowflake__edr_get_create_table_as_sql(temporary, relation, sql_query) %}
create or replace {% if temporary %} temporary {% endif %} table {{ relation }}
as {{ sql_query }}
{% endmacro %}
{% macro bigquery__edr_get_create_table_as_sql(temporary, relation, sql_query) %}
create or replace table {{ relation }}
{% if temporary %}
options (expiration_timestamp=TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 1 hour))
{% endif %}
as {{ sql_query }}
{% endmacro %}
{% macro postgres__edr_get_create_table_as_sql(temporary, relation, sql_query) %}
create {% if temporary %} temporary {% endif %} table {{ relation.include(database=(not temporary), schema=(not temporary)) }}
as {{ sql_query }}
{% endmacro %}
{% macro redshift__edr_get_create_table_as_sql(temporary, relation, sql_query) %}
{% if temporary and elementary.is_dbt_fusion() %}
{# dbt-fusion uses connection pooling - temp tables created in one session
aren't visible in other sessions. Create regular tables instead.
These are cleaned up by Elementary's normal cleanup logic. #}
create table {{ relation }}
as {{ sql_query }}
{% else %}
create {% if temporary %} temporary {% endif %} table {{ relation.include(database=(not temporary), schema=(not temporary)) }}
as {{ sql_query }}
{% endif %}
{% endmacro %}
{% macro databricks__edr_get_create_table_as_sql(temporary, relation, sql_query) %}
{% if temporary %}
{% if elementary.is_dbt_fusion() %}
{#
dbt fusion does not run Databricks statements in the same session, so we can't use temp
views.
(the view will be dropped later as has_temp_table_support returns False for Databricks)
More details here - https://github.com/dbt-labs/dbt-fusion/blob/fa78a4099553a805af7629ac80be55e23e24bb4c/crates/dbt-loader/src/dbt_macro_assets/dbt-databricks/macros/relations/table/create.sql#L54
#}
{% set relation_type = "view" %}
{% else %} {% set relation_type = "temporary view" %}
{% endif %}
{% else %} {% set relation_type = "table" %}
{% endif %}
create or replace {{ relation_type }} {{ relation }}
as {{ sql_query }}
{% endmacro %}
{% macro clickhouse__edr_get_create_table_as_sql(temporary, relation, sql_query) %}
{# ClickHouse does not support database-scoped temporary tables, so we force temporary to be false. #}
{{ dbt.get_create_table_as_sql(false, relation, sql_query) }}
{% endmacro %}
{% macro duckdb__edr_get_create_table_as_sql(temporary, relation, sql_query) %}
create or replace {% if temporary %} temporary {% endif %} table {{ relation }}
as {{ sql_query }}
{% endmacro %}
{% macro trino__edr_get_create_table_as_sql(temporary, relation, sql_query) %}
{# dbt-trino's create_table_as accesses model.config which fails when called
outside a model context (e.g. from edr_create_table_as). Use simplified SQL. #}
create table {{ relation }}
as {{ sql_query }}
{% endmacro %}
{% macro spark__edr_get_create_table_as_sql(temporary, relation, sql_query) %}
{# Spark: use a temporary view for temp tables, regular table otherwise #}
{% if temporary %}
create or replace temporary view {{ relation }}
as {{ sql_query }}
{% else %}
create table {{ relation }}
as {{ sql_query }}
{% endif %}
{% endmacro %}
{% macro fabricspark__edr_get_create_table_as_sql(temporary, relation, sql_query) %}
{{
return(
elementary.spark__edr_get_create_table_as_sql(
temporary, relation, sql_query
)
)
}}
{% endmacro %}