Skip to content

Commit

Permalink
Merge branch 'main' into pre-commit/2024-09-06T23_35_40
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealfare authored Oct 11, 2024
2 parents 3869fcf + 4df6e54 commit d14192c
Show file tree
Hide file tree
Showing 29 changed files with 458 additions and 83 deletions.
1 change: 1 addition & 0 deletions .changes/1.10.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## dbt-adapters 1.10.0 - September 12, 2024
1 change: 1 addition & 0 deletions .changes/1.10.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## dbt-adapters 1.10.1 - September 16, 2024
5 changes: 5 additions & 0 deletions .changes/1.10.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## dbt-adapters 1.10.2 - October 01, 2024

### Under the Hood

- dbt-tests-adapters: Add required begin to microbatch model config to BaseMicrobatch test ([#315](https://github.com/dbt-labs/dbt-adapters/issues/315))
11 changes: 11 additions & 0 deletions .changes/1.5.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## dbt-adapters 1.5.0 - September 10, 2024

### Features

- Compare 'snapshot_get_time' and snapshot 'updated_at' data types ([#242](https://github.com/dbt-labs/dbt-adapters/issues/242))
- Add Behavior Flag framework ([#281](https://github.com/dbt-labs/dbt-adapters/issues/281))
- Add EventTimeFilter to BaseRelation, which renders a filtered relation when start or end is set ([#294](https://github.com/dbt-labs/dbt-adapters/issues/294))

### Dependencies

- Update dbt-common pin to >=1.8 ([#299](https://github.com/dbt-labs/dbt-adapters/pull/299))
5 changes: 5 additions & 0 deletions .changes/1.6.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## dbt-adapters 1.6.0 - September 12, 2024

### Features

- Default microbatch strategy implementation and base tests ([#302](https://github.com/dbt-labs/dbt-adapters/issues/302))
1 change: 1 addition & 0 deletions .changes/1.6.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## dbt-adapters 1.6.1 - September 16, 2024
5 changes: 5 additions & 0 deletions .changes/1.7.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## dbt-adapters 1.7.0 - September 19, 2024

### Features

- Allow configuring of snapshot column names ([#289](https://github.com/dbt-labs/dbt-adapters/issues/289))
6 changes: 0 additions & 6 deletions .changes/unreleased/Features-20240621-143024.yaml

This file was deleted.

6 changes: 0 additions & 6 deletions .changes/unreleased/Features-20240818-005131.yaml

This file was deleted.

6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240927-134248.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Enable setting current value of dbt_valid_to
time: 2024-09-27T13:42:48.654556-04:00
custom:
Author: gshank
Issue: "320"
4 changes: 3 additions & 1 deletion .github/workflows/precommit-autoupdate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ concurrency:
jobs:
precommit-autoupdate:
name: "Run pre-commit autoupdate"
uses: dbt-labs/actions/.github/workflows/pre-commit-autoupdate.yml@pre-commit-auto-update
uses: dbt-labs/actions/.github/workflows/pre-commit-autoupdate.yml
secrets:
TOKEN: ${{ secrets.FISHTOWN_BOT_PAT }}
SLACK_WEBHOOK_PR_URL: ${{ secrets.SLACK_DEV_ADAPTER_PULL_REQUESTS }}
SLACK_WEBHOOK_ALERTS_URL: ${{ secrets.SLACK_DEV_ADAPTER_ALERTS }}
39 changes: 38 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
and is generated by [Changie](https://github.com/miniscruff/changie).

## dbt-adapters 1.10.2 - October 01, 2024

### Under the Hood

- dbt-tests-adapters: Add required begin to microbatch model config to BaseMicrobatch test ([#315](https://github.com/dbt-labs/dbt-adapters/issues/315))



## dbt-adapters 1.10.1 - September 16, 2024

## dbt-adapters 1.10.0 - September 12, 2024

## dbt-adapters 1.7.0 - September 19, 2024

### Features

- Allow configuring of snapshot column names ([#289](https://github.com/dbt-labs/dbt-adapters/issues/289))

## dbt-adapters 1.6.1 - September 16, 2024

## dbt-adapters 1.6.0 - September 12, 2024

### Features

- Default microbatch strategy implementation and base tests ([#302](https://github.com/dbt-labs/dbt-adapters/issues/302))

## dbt-adapters 1.5.0 - September 10, 2024

### Features

- Compare 'snapshot_get_time' and snapshot 'updated_at' data types ([#242](https://github.com/dbt-labs/dbt-adapters/issues/242))
- Add Behavior Flag framework ([#281](https://github.com/dbt-labs/dbt-adapters/issues/281))
- Add EventTimeFilter to BaseRelation, which renders a filtered relation when start or end is set ([#294](https://github.com/dbt-labs/dbt-adapters/issues/294))

### Dependencies

- Update dbt-common pin to >=1.8 ([#299](https://github.com/dbt-labs/dbt-adapters/pull/299))

## dbt-adapters 1.4.1 - August 09, 2024

### Fixes
Expand All @@ -19,7 +57,6 @@ and is generated by [Changie](https://github.com/miniscruff/changie).
- [@jeancochrane](https://github.com/jeancochrane) ([#5273](https://github.com/dbt-labs/dbt-adapters/issues/5273))
- [@leahwicz](https://github.com/leahwicz) ([#219](https://github.com/dbt-labs/dbt-adapters/issues/219))


## dbt-adapters 1.4.0 - July 30, 2024

### Features
Expand Down
2 changes: 1 addition & 1 deletion dbt-tests-adapter/dbt/tests/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "1.9.2"
version = "1.10.2"
8 changes: 5 additions & 3 deletions dbt-tests-adapter/dbt/tests/adapter/hooks/test_run_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from dbt_common.exceptions import DbtDatabaseError
from dbt.tests.adapter.hooks import fixtures
from dbt.tests.util import check_table_does_not_exist, run_dbt

Expand All @@ -11,8 +12,8 @@ class BasePrePostRunHooks:
@pytest.fixture(scope="function")
def setUp(self, project):
project.run_sql_file(project.test_data_dir / Path("seed_run.sql"))
project.run_sql(f"drop table if exists { project.test_schema }.schemas")
project.run_sql(f"drop table if exists { project.test_schema }.db_schemas")
project.run_sql(f"drop table if exists {project.test_schema}.schemas")
project.run_sql(f"drop table if exists {project.test_schema}.db_schemas")
os.environ["TERM_TEST"] = "TESTING"

@pytest.fixture(scope="class")
Expand Down Expand Up @@ -158,7 +159,8 @@ def project_config_update(self):
}

def test_missing_column_pre_hook(self, project):
run_dbt(["run"], expect_pass=False)
with pytest.raises(DbtDatabaseError):
run_dbt(["run"], expect_pass=False)


class TestAfterRunHooks(BaseAfterRunHooks):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import os
from pprint import pformat
from unittest import mock

import pytest

from dbt.tests.util import relation_from_name, run_dbt

try:
# patch_microbatch_end_time introduced in dbt 1.9.0
from dbt.tests.util import patch_microbatch_end_time
except ImportError:
from freezegun import freeze_time as patch_microbatch_end_time

_input_model_sql = """
{{ config(materialized='table', event_time='event_time') }}
select 1 as id, TIMESTAMP '2020-01-01 00:00:00-0' as event_time
union all
select 2 as id, TIMESTAMP '2020-01-02 00:00:00-0' as event_time
union all
select 3 as id, TIMESTAMP '2020-01-03 00:00:00-0' as event_time
"""

_microbatch_model_sql = """
{{ config(materialized='incremental', incremental_strategy='microbatch', unique_key='id', event_time='event_time', batch_size='day', begin=modules.datetime.datetime(2020, 1, 1, 0, 0, 0)) }}
select * from {{ ref('input_model') }}
"""


class BaseMicrobatch:
@pytest.fixture(scope="class")
def microbatch_model_sql(self) -> str:
"""
This is the SQL that defines the microbatch model, including any {{ config(..) }}
"""
return _microbatch_model_sql

@pytest.fixture(scope="class")
def input_model_sql(self) -> str:
"""
This is the SQL that defines the input model to the microbatch model, including any {{ config(..) }}.
event_time is a required configuration of this input
"""
return _input_model_sql

@pytest.fixture(scope="class")
def insert_two_rows_sql(self, project) -> str:
test_schema_relation = project.adapter.Relation.create(
database=project.database, schema=project.test_schema
)
return f"insert into {test_schema_relation}.input_model (id, event_time) values (4, TIMESTAMP '2020-01-04 00:00:00-0'), (5, TIMESTAMP '2020-01-05 00:00:00-0')"

@pytest.fixture(scope="class")
def models(self, microbatch_model_sql, input_model_sql):
return {
"input_model.sql": input_model_sql,
"microbatch_model.sql": microbatch_model_sql,
}

def assert_row_count(self, project, relation_name: str, expected_row_count: int):
relation = relation_from_name(project.adapter, relation_name)
result = project.run_sql(f"select * from {relation}", fetch="all")

assert len(result) == expected_row_count, f"{relation_name}:{pformat(result)}"

@mock.patch.dict(os.environ, {"DBT_EXPERIMENTAL_MICROBATCH": "True"})
def test_run_with_event_time(self, project, insert_two_rows_sql):
# initial run -- backfills all data
with patch_microbatch_end_time("2020-01-03 13:57:00"):
run_dbt(["run"])
self.assert_row_count(project, "microbatch_model", 3)

# our partition grain is "day" so running the same day without new data should produce the same results
with patch_microbatch_end_time("2020-01-03 14:57:00"):
run_dbt(["run"])
self.assert_row_count(project, "microbatch_model", 3)

# add next two days of data
project.run_sql(insert_two_rows_sql)

self.assert_row_count(project, "input_model", 5)

# re-run without changing current time => no insert
with patch_microbatch_end_time("2020-01-03 14:57:00"):
run_dbt(["run", "--select", "microbatch_model"])
self.assert_row_count(project, "microbatch_model", 3)

# re-run by advancing time by one day changing current time => insert 1 row
with patch_microbatch_end_time("2020-01-04 14:57:00"):
run_dbt(["run", "--select", "microbatch_model"])
self.assert_row_count(project, "microbatch_model", 4)

# re-run by advancing time by one more day changing current time => insert 1 more row
with patch_microbatch_end_time("2020-01-05 14:57:00"):
run_dbt(["run", "--select", "microbatch_model"])
self.assert_row_count(project, "microbatch_model", 5)
1 change: 1 addition & 0 deletions dbt-tests-adapter/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies = [
# `dbt-core` takes the packages below as dependencies, so they are unpinned to avoid conflicts
"dbt-adapters",
"pyyaml",
"freezegun",
]
[project.urls]
Homepage = "https://github.com/dbt-labs/dbt-adapters"
Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "1.4.1"
version = "1.7.0"
37 changes: 19 additions & 18 deletions dbt/adapters/base/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Union,
TYPE_CHECKING,
)

import os
import pytz
from dbt_common.behavior_flags import Behavior, BehaviorFlag
from dbt_common.clients.jinja import CallableMacroGenerator
Expand Down Expand Up @@ -55,7 +55,7 @@
BaseConnectionManager,
Connection,
)
from dbt.adapters.base.meta import AdapterMeta, available
from dbt.adapters.base.meta import AdapterMeta, available, available_property
from dbt.adapters.base.relation import (
BaseRelation,
ComponentName,
Expand Down Expand Up @@ -83,7 +83,6 @@
QuoteConfigTypeError,
RelationReturnedMultipleResultsError,
RenameToNoneAttemptedError,
SnapshotTargetIncompleteError,
SnapshotTargetNotSnapshotTableError,
UnexpectedNonTimestampError,
)
Expand Down Expand Up @@ -272,7 +271,8 @@ def __init__(self, config, mp_context: SpawnContext) -> None:
self.connections = self.ConnectionManager(config, mp_context)
self._macro_resolver: Optional[MacroResolverProtocol] = None
self._macro_context_generator: Optional[MacroContextGeneratorCallable] = None
self.behavior = [] # this will be updated to include global behavior flags once they exist
# this will be updated to include global behavior flags once they exist
self.behavior = [] # type: ignore

###
# Methods to set / access a macro resolver
Expand All @@ -293,11 +293,11 @@ def set_macro_context_generator(
) -> None:
self._macro_context_generator = macro_context_generator

@property
@available_property
def behavior(self) -> Behavior:
return self._behavior

@behavior.setter
@behavior.setter # type: ignore
def behavior(self, flags: List[BehaviorFlag]) -> None:
flags.extend(self._behavior_flags)
try:
Expand Down Expand Up @@ -763,7 +763,9 @@ def get_missing_columns(
return [col for (col_name, col) in from_columns.items() if col_name in missing_columns]

@available.parse_none
def valid_snapshot_target(self, relation: BaseRelation) -> None:
def valid_snapshot_target(
self, relation: BaseRelation, column_names: Optional[Dict[str, str]] = None
) -> None:
"""Ensure that the target relation is valid, by making sure it has the
expected columns.
Expand All @@ -781,21 +783,16 @@ def valid_snapshot_target(self, relation: BaseRelation) -> None:

columns = self.get_columns_in_relation(relation)
names = set(c.name.lower() for c in columns)
expanded_keys = ("scd_id", "valid_from", "valid_to")
extra = []
missing = []
for legacy in expanded_keys:
desired = "dbt_" + legacy
# Note: we're not checking dbt_updated_at here because it's not
# always present.
for column in ("dbt_scd_id", "dbt_valid_from", "dbt_valid_to"):
desired = column_names[column] if column_names else column
if desired not in names:
missing.append(desired)
if legacy in names:
extra.append(legacy)

if missing:
if extra:
raise SnapshotTargetIncompleteError(extra, missing)
else:
raise SnapshotTargetNotSnapshotTableError(missing)
raise SnapshotTargetNotSnapshotTableError(missing)

@available.parse_none
def expand_target_column_types(
Expand Down Expand Up @@ -1572,7 +1569,11 @@ def valid_incremental_strategies(self):
return ["append"]

def builtin_incremental_strategies(self):
return ["append", "delete+insert", "merge", "insert_overwrite"]
builtin_strategies = ["append", "delete+insert", "merge", "insert_overwrite"]
if os.environ.get("DBT_EXPERIMENTAL_MICROBATCH"):
builtin_strategies.append("microbatch")

return builtin_strategies

@available.parse_none
def get_incremental_strategy_macro(self, model_context, strategy: str):
Expand Down
19 changes: 19 additions & 0 deletions dbt/adapters/base/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ def parse_list(self, func: Callable) -> Callable:
available = _Available()


class available_property(property):
"""
This supports making dynamic properties (`@property`) available in the jinja context.
We use `@available` to make methods available in the jinja context, but this mechanism relies on the method being callable.
Intuitively, we should be able to use both `@available` and `@property` to create a dynamic property that's available in the jinja context.
Using the `@property` decorator as the inner decorator supplies `@available` with something that is not callable.
Instead of returning the method, `@property` returns the value itself, not the method that is called to create the value.
Using the `@available` decorator as the inner decorator adds `_is_available_ = True` to the function.
However, when the `@property` decorator executes, it returns a `property` object which does not have the `_is_available_` attribute.
This decorator solves this problem by simply adding `_is_available_ = True` as an attribute on the `property` built-in.
"""

_is_available_ = True


class AdapterMeta(abc.ABCMeta):
_available_: FrozenSet[str]
_parse_replacements_: Dict[str, Callable]
Expand Down
Loading

0 comments on commit d14192c

Please sign in to comment.