-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 1.7.latest] Implementation of metadata-based freshness (#721)
* cherry pick pr#649 * turn off catalog by relations that was inadvertently turned on during the cherry pick
- Loading branch information
1 parent
7e3891f
commit bf80a11
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-12-19T12:05:33.784649-05:00 | ||
custom: | ||
Author: mikealfare | ||
Issue: "615" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
dbt/include/redshift/macros/metadata/relation_last_modified.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{% macro redshift__get_relation_last_modified(information_schema, relations) -%} | ||
|
||
{%- call statement('last_modified', fetch_result=True) -%} | ||
select | ||
ns.nspname as "schema", | ||
c.relname as identifier, | ||
max(qd.start_time) as last_modified, | ||
{{ current_timestamp() }} as snapshotted_at | ||
from pg_class c | ||
join pg_namespace ns | ||
on ns.oid = c.relnamespace | ||
join sys_query_detail qd | ||
on qd.table_id = c.oid | ||
where qd.step_name = 'insert' | ||
and ( | ||
{%- for relation in relations -%} | ||
( | ||
upper(ns.nspname) = upper('{{ relation.schema }}') | ||
and upper(c.relname) = upper('{{ relation.identifier }}') | ||
) | ||
{%- if not loop.last %} or {% endif -%} | ||
{%- endfor -%} | ||
) | ||
group by 1, 2, 4 | ||
{%- endcall -%} | ||
|
||
{{ return(load_result('last_modified')) }} | ||
|
||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
SCHEMA_YML = """version: 2 | ||
sources: | ||
- name: test_source | ||
freshness: | ||
warn_after: {count: 10, period: hour} | ||
error_after: {count: 1, period: day} | ||
schema: "{{ env_var('DBT_GET_LAST_RELATION_TEST_SCHEMA') }}" | ||
tables: | ||
- name: test_source_no_last_modified | ||
- name: test_source_last_modified | ||
loaded_at_field: last_modified | ||
""" | ||
|
||
SEED_TEST_SOURCE_NO_LAST_MODIFIED_CSV = """ | ||
id,name | ||
1,Martin | ||
2,Jeter | ||
3,Ruth | ||
4,Gehrig | ||
5,DiMaggio | ||
6,Torre | ||
7,Mantle | ||
8,Berra | ||
9,Maris | ||
""".strip() | ||
|
||
SEED_TEST_SOURCE_LAST_MODIFIED_CSV = """ | ||
id,name,last_modified | ||
1,Martin,2023-01-01 00:00:00 | ||
2,Jeter,2023-02-01 00:00:00 | ||
3,Ruth,2023-03-01 00:00:00 | ||
4,Gehrig,2023-04-01 00:00:00 | ||
5,DiMaggio,2023-05-01 00:00:00 | ||
6,Torre,2023-06-01 00:00:00 | ||
7,Mantle,2023-07-01 00:00:00 | ||
8,Berra,2023-08-01 00:00:00 | ||
9,Maris,2023-09-01 00:00:00 | ||
""".strip() |
42 changes: 42 additions & 0 deletions
42
tests/functional/adapter/sources_freshness_tests/test_get_relation_last_modified.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import os | ||
|
||
from dbt.tests.util import run_dbt | ||
import pytest | ||
|
||
from tests.functional.adapter.sources_freshness_tests import files | ||
|
||
|
||
class TestGetLastRelationModified: | ||
@pytest.fixture(scope="class") | ||
def seeds(self): | ||
return { | ||
"test_source_no_last_modified.csv": files.SEED_TEST_SOURCE_NO_LAST_MODIFIED_CSV, | ||
"test_source_last_modified.csv": files.SEED_TEST_SOURCE_LAST_MODIFIED_CSV, | ||
} | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return {"schema.yml": files.SCHEMA_YML} | ||
|
||
@pytest.fixture(scope="class", autouse=True) | ||
def setup(self, project): | ||
# we need the schema name for the sources section | ||
os.environ["DBT_GET_LAST_RELATION_TEST_SCHEMA"] = project.test_schema | ||
run_dbt(["seed"]) | ||
yield | ||
del os.environ["DBT_GET_LAST_RELATION_TEST_SCHEMA"] | ||
|
||
@pytest.mark.parametrize( | ||
"source,status,expect_pass", | ||
[ | ||
("test_source.test_source_no_last_modified", "pass", True), | ||
("test_source.test_source_last_modified", "error", False), # stale | ||
], | ||
) | ||
def test_get_last_relation_modified(self, project, source, status, expect_pass): | ||
results = run_dbt( | ||
["source", "freshness", "--select", f"source:{source}"], expect_pass=expect_pass | ||
) | ||
assert len(results) == 1 | ||
result = results[0] | ||
assert result.status == status |