Skip to content

Commit

Permalink
Merge pull request #1689 from bastienboutonnet/feature/snowflake_clus…
Browse files Browse the repository at this point in the history
…tering

Implement Clustering for Snowflake
  • Loading branch information
drewbanin authored Aug 20, 2019
2 parents 0e897f7 + 90e8e75 commit e7a24a2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
23 changes: 13 additions & 10 deletions plugins/snowflake/dbt/adapters/snowflake/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class SnowflakeAdapter(SQLAdapter):
Relation = SnowflakeRelation
ConnectionManager = SnowflakeConnectionManager

AdapterSpecificConfigs = frozenset({"transient"})
AdapterSpecificConfigs = frozenset(
{"transient", "cluster_by", "automatic_clustering"}
)

@classmethod
def date_function(cls):
return 'CURRENT_TIMESTAMP()'
return "CURRENT_TIMESTAMP()"

@classmethod
def _catalog_filter_table(cls, table, manifest):
Expand All @@ -23,20 +25,21 @@ def _catalog_filter_table(cls, table, manifest):
lowered = table.rename(
column_names=[c.lower() for c in table.column_names]
)
return super(SnowflakeAdapter, cls)._catalog_filter_table(lowered,
manifest)
return super(SnowflakeAdapter, cls)._catalog_filter_table(
lowered, manifest
)

def _make_match_kwargs(self, database, schema, identifier):
quoting = self.config.quoting
if identifier is not None and quoting['identifier'] is False:
if identifier is not None and quoting["identifier"] is False:
identifier = identifier.upper()

if schema is not None and quoting['schema'] is False:
if schema is not None and quoting["schema"] is False:
schema = schema.upper()

if database is not None and quoting['database'] is False:
if database is not None and quoting["database"] is False:
database = database.upper()

return filter_null_values({'identifier': identifier,
'schema': schema,
'database': database})
return filter_null_values(
{"identifier": identifier, "schema": schema, "database": database}
)
39 changes: 31 additions & 8 deletions plugins/snowflake/dbt/include/snowflake/macros/adapters.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
{% macro snowflake__create_table_as(temporary, relation, sql) -%}
{%- set transient = config.get('transient', default=true) -%}
{%- set cluster_by_keys = config.get('cluster_by', default=none) -%}
{%- set enable_automatic_clustering = config.get('automatic_clustering', default=false) -%}
{%- if cluster_by_keys is not none and cluster_by_keys is string -%}
{%- set cluster_by_keys = [cluster_by_keys] -%}
{%- endif -%}
{%- if cluster_by_keys is not none -%}
{%- set cluster_by_string = cluster_by_keys|join(", ")-%}
{% else %}
{%- set cluster_by_string = none -%}
{%- endif -%}

create or replace {% if temporary -%}
temporary
{%- elif transient -%}
transient
{%- endif %} table {{ relation }}
as (
{%- if cluster_by_string is not none -%}
select * from(
{{ sql }}
) order by ({{ cluster_by_string }})
{%- else -%}
{{ sql }}
{%- endif %}
);
{% if cluster_by_string is not none and not temporary -%}
alter table {{relation}} cluster by ({{cluster_by_string}});
{%- endif -%}
{% if enable_automatic_clustering and cluster_by_string is not none and not temporary -%}
alter table {{relation}} resume recluster;
{%- endif -%}

create or replace {% if temporary -%}
temporary
{%- elif transient -%}
transient
{%- endif %} table {{ relation }}
as (
{{ sql }}
);
{% endmacro %}

{% macro snowflake__create_view_as(relation, sql) -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

{%- call statement('main') -%}
{{ create_table_as(false, target_relation, sql) }}

{%- endcall -%}

{%- else -%}
Expand Down

0 comments on commit e7a24a2

Please sign in to comment.