Skip to content

Commit

Permalink
Draft implementation of metadata-based freshness
Browse files Browse the repository at this point in the history
  • Loading branch information
peterallenwebb committed Oct 9, 2023
1 parent 658e16e commit e39a6e7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20231008-195410.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Add support for checking table-last-modified by metadata
time: 2023-10-08T19:54:10.503476-04:00
custom:
Author: peterallenwebb
Issue: "785"
31 changes: 30 additions & 1 deletion dbt/adapters/snowflake/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

import agate

from dbt.adapters.base.impl import AdapterConfig, ConstraintSupport # type: ignore
from dbt.adapters.base.impl import (
AdapterConfig,
Capability,
CapabilitySupport,
ConstraintSupport,
Support,
) # type: ignore
from dbt.adapters.base.meta import available
from dbt.adapters.sql import SQLAdapter # type: ignore
from dbt.adapters.sql.impl import (
Expand Down Expand Up @@ -49,6 +55,17 @@ class SnowflakeAdapter(SQLAdapter):
ConstraintType.foreign_key: ConstraintSupport.NOT_ENFORCED,
}

_capabilities: Dict[Capability, CapabilitySupport] = {
Capability.TableLastModifiedMetadata: CapabilitySupport(
capability=Capability.TableLastModifiedMetadata,
support=Support.Full,
),
Capability.SchemaMetadataByRelations: CapabilitySupport(
capability=Capability.SchemaMetadataByRelations,
support=Support.NotImplemented,
),
}

@classmethod
def date_function(cls):
return "CURRENT_TIMESTAMP()"
Expand Down Expand Up @@ -259,3 +276,15 @@ def valid_incremental_strategies(self):
def debug_query(self):
"""Override for DebugTask method"""
self.execute("select 1 as id")

def capabilities(self) -> List[CapabilitySupport]:
return [c for c in self._capabilities.values()]

def capability_support(self, capability: Capability) -> CapabilitySupport:
if capability in self._capabilities:
return self._capabilities[capability]
else:
return CapabilitySupport(
capability=capability,
support=Support.Unknown,
)
19 changes: 19 additions & 0 deletions dbt/include/snowflake/macros/metadata.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% macro snowflake__get_relation_last_modified(information_schema, relations) -%}

{%- call statement('last_modified', fetch_result=True) -%}
select table_schema as schema,
table_name as identifier,
last_altered as last_modified,
{{ current_timestamp() }} as snapshotted_at
from {{ information_schema }}.tables
where (
{%- for relation in relations -%}
(upper(table_schema) = upper('{{ relation.schema }}') and
upper(table_name) = upper('{{ relation.identifier }}')){%- if not loop.last %} or {% endif -%}
{%- endfor -%}
)
{%- endcall -%}

{{ return(load_result('last_modified')) }}

{% endmacro %}

0 comments on commit e39a6e7

Please sign in to comment.