-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sample Mode Alpha #11247
Merged
Merged
Sample Mode Alpha #11247
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1faa8b7
Add `--sample` flag to `run` command
QMalcolm 2394402
Remove no longer needed `if` statement around EventTimeFilter creatio…
QMalcolm 25fb1e5
Get sample mode working with `--event-time-start/end`
QMalcolm 6f68797
Begin using `--sample-window` for sample mode instead of `--event-tim…
QMalcolm 6bcb328
Move `SampleWindow` class to `sample_window.py` in `event_time` submo…
QMalcolm d6f113f
Create an `offset_timestamp` separate from MicrobatchBuilder
QMalcolm c547617
Add `types-python-dateutil` to dev requirements
QMalcolm cf5bfd6
Begin supporting microbatch models in sample mode
QMalcolm a8cbec2
Move parsing logic of `SampleWindowType` to `SampleWindow`
QMalcolm fb64b00
Allow for specificaion of "specific" sample windows
QMalcolm 10674f7
Fix tests of `BaseResolver.resolve_event_time_filter` for sample mode…
QMalcolm 7e86080
Add `--no-sample` as it's necessary for retry
QMalcolm 2942861
Add guards to accessing of `sample` and `sample_window`
QMalcolm 1811754
Gate sample mode functionality via env var `DBT_EXPERIMENTAL_SAMPLE_M…
QMalcolm 719b550
Add sample mode tests for incremental models
QMalcolm 01ae64f
Add changie doc for sample mode initial implementation
QMalcolm 5c51a6f
Fixup sample mode functional tests
QMalcolm 3c5539b
Ensure microbatch creates correct number of batches when sample mode …
QMalcolm 6d001d7
Correct comment in SampleWindow post serialization method
QMalcolm 1ef8b3f
Hide CLI sample mode options
QMalcolm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: Initial implementation of sample mode | ||
time: 2025-02-02T14:00:54.074209-06:00 | ||
custom: | ||
Author: QMalcolm | ||
Issue: 11227 11230 11231 11248 11252 11254 11258 |
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
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
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
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
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
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,40 @@ | ||
from datetime import datetime | ||
|
||
from dateutil.relativedelta import relativedelta | ||
|
||
from dbt.artifacts.resources.types import BatchSize | ||
from dbt_common.exceptions import DbtRuntimeError | ||
|
||
|
||
def offset_timestamp(timestamp=datetime, batch_size=BatchSize, offset=int) -> datetime: | ||
"""Offsets the passed in timestamp based on the batch_size and offset. | ||
|
||
Note: THIS IS DIFFERENT FROM MicrobatchBuilder.offset_timestamp. That function first | ||
`truncates` the timestamp, and then does delta addition subtraction from there. This | ||
function _doesn't_ truncate the timestamp and uses `relativedelta` for specific edge | ||
case handling (months, years), which may produce different results than the delta math | ||
done in `MicrobatchBuilder.offset_timestamp` | ||
|
||
Examples | ||
2024-09-17 16:06:00 + Batchsize.hour -1 -> 2024-09-17 15:06:00 | ||
2024-09-17 16:06:00 + Batchsize.hour +1 -> 2024-09-17 17:06:00 | ||
2024-09-17 16:06:00 + Batchsize.day -1 -> 2024-09-16 16:06:00 | ||
2024-09-17 16:06:00 + Batchsize.day +1 -> 2024-09-18 16:06:00 | ||
2024-09-17 16:06:00 + Batchsize.month -1 -> 2024-08-17 16:06:00 | ||
2024-09-17 16:06:00 + Batchsize.month +1 -> 2024-10-17 16:06:00 | ||
2024-09-17 16:06:00 + Batchsize.year -1 -> 2023-09-17 16:06:00 | ||
2024-09-17 16:06:00 + Batchsize.year +1 -> 2025-09-17 16:06:00 | ||
2024-01-31 16:06:00 + Batchsize.month +1 -> 2024-02-29 16:06:00 | ||
2024-02-29 16:06:00 + Batchsize.year +1 -> 2025-02-28 16:06:00 | ||
""" | ||
|
||
if batch_size == BatchSize.hour: | ||
return timestamp + relativedelta(hours=offset) | ||
elif batch_size == BatchSize.day: | ||
return timestamp + relativedelta(days=offset) | ||
elif batch_size == BatchSize.month: | ||
return timestamp + relativedelta(months=offset) | ||
elif batch_size == BatchSize.year: | ||
return timestamp + relativedelta(years=offset) | ||
else: | ||
raise DbtRuntimeError(f"Unhandled batch_size '{batch_size}'") | ||
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,60 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import datetime | ||
|
||
import pytz | ||
from attr import dataclass | ||
|
||
from dbt.artifacts.resources.types import BatchSize | ||
from dbt.event_time.event_time import offset_timestamp | ||
from dbt_common.dataclass_schema import dbtClassMixin | ||
from dbt_common.exceptions import DbtRuntimeError | ||
|
||
|
||
@dataclass | ||
class SampleWindow(dbtClassMixin): | ||
start: datetime | ||
end: datetime | ||
|
||
def __post_serialize__(self, data, context): | ||
# This is insane, but necessary, I apologize. Mashumaro handles the | ||
# dictification of this class via a compile time generated `to_dict` | ||
# method based off of the _typing_ of th class. By default `datetime` | ||
# types are converted to strings. We don't want that, we want them to | ||
# stay datetimes. | ||
# Note: This is safe because the `BatchContext` isn't part of the artifact | ||
# and thus doesn't get written out. | ||
new_data = super().__post_serialize__(data, context) | ||
new_data["start"] = self.start | ||
new_data["end"] = self.end | ||
return new_data | ||
|
||
@classmethod | ||
def from_relative_string(cls, relative_string: str) -> SampleWindow: | ||
end = datetime.now(tz=pytz.UTC) | ||
|
||
relative_window = relative_string.split(" ") | ||
if len(relative_window) != 2: | ||
raise DbtRuntimeError( | ||
f"Cannot load SAMPLE_WINDOW from '{relative_string}'. Must be of form 'DAYS_INT GRAIN_SIZE'." | ||
) | ||
|
||
try: | ||
lookback = int(relative_window[0]) | ||
except Exception: | ||
raise DbtRuntimeError(f"Unable to convert '{relative_window[0]}' to an integer.") | ||
|
||
try: | ||
batch_size_string = relative_window[1].lower().rstrip("s") | ||
batch_size = BatchSize[batch_size_string] | ||
except Exception: | ||
grains = [size.value for size in BatchSize] | ||
grain_plurals = [BatchSize.plural(size) for size in BatchSize] | ||
valid_grains = grains + grain_plurals | ||
raise DbtRuntimeError( | ||
f"Invalid grain size '{relative_window[1]}'. Must be one of {valid_grains}." | ||
) | ||
|
||
start = offset_timestamp(timestamp=end, batch_size=batch_size, offset=-1 * lookback) | ||
|
||
return cls(start=start, end=end) |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hide, and later unhide. I.e.
hidden=True
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For clarification, we want to hide these while we're in alpha / gated behind an environment variable. Once we remove the environment variable, we'll unhide these