diff --git a/core/dbt/materializations/incremental/microbatch.py b/core/dbt/materializations/incremental/microbatch.py index 6de6945704c..370082461e6 100644 --- a/core/dbt/materializations/incremental/microbatch.py +++ b/core/dbt/materializations/incremental/microbatch.py @@ -194,13 +194,21 @@ def truncate_timestamp(timestamp: datetime, batch_size: BatchSize) -> datetime: @staticmethod def batch_id(start_time: datetime, batch_size: BatchSize) -> str: - return MicrobatchBuilder.format_batch_start(start_time, batch_size).replace("-", "") + return MicrobatchBuilder.format_batch_start(start_time, batch_size) @staticmethod def format_batch_start(batch_start: datetime, batch_size: BatchSize) -> str: - return str( - batch_start.date() if (batch_start and batch_size != BatchSize.hour) else batch_start - ) + """Format the passed in datetime based on the batch_size. + + 2024-09-17 16:06:00 + Batchsize.day -> 20240917 + 2024-09-17 16:06:00 + Batchsize.hour -> 20240917T16 + """ + # If we want a date only + if batch_size != BatchSize.hour: + return batch_start.strftime("%Y%m%d") + + # If we want date + time + return batch_start.strftime("%Y%m%dT%H") @staticmethod def ceiling_timestamp(timestamp: datetime, batch_size: BatchSize) -> datetime: diff --git a/tests/unit/materializations/incremental/test_microbatch.py b/tests/unit/materializations/incremental/test_microbatch.py index 3d827a79975..ed5415c0f0b 100644 --- a/tests/unit/materializations/incremental/test_microbatch.py +++ b/tests/unit/materializations/incremental/test_microbatch.py @@ -602,13 +602,27 @@ def test_offset_timestamp(self, timestamp, batch_size, offset, expected_timestam def test_truncate_timestamp(self, timestamp, batch_size, expected_timestamp): assert MicrobatchBuilder.truncate_timestamp(timestamp, batch_size) == expected_timestamp + @pytest.mark.parametrize( + "batch_size,start_time,expected_formatted_start_time", + [ + (BatchSize.year, None, ""), + (BatchSize.year, datetime(2020, 1, 1, 1), "20200101"), + (BatchSize.month, datetime(2020, 1, 1, 1), "20200101"), + (BatchSize.day, datetime(2020, 1, 1, 1), "20200101"), + (BatchSize.hour, datetime(2020, 1, 1, 1), "20200101T01"), + ], + ) + def test_batch_id(self, batch_size, start_time, expected_formatted_start_time): + assert MicrobatchBuilder.batch_id(start_time, batch_size) == expected_formatted_start_time + @pytest.mark.parametrize( "batch_size,batch_start,expected_formatted_batch_start", [ - (BatchSize.year, datetime(2020, 1, 1, 1), "2020-01-01"), - (BatchSize.month, datetime(2020, 1, 1, 1), "2020-01-01"), - (BatchSize.day, datetime(2020, 1, 1, 1), "2020-01-01"), - (BatchSize.hour, datetime(2020, 1, 1, 1), "2020-01-01 01:00:00"), + (BatchSize.year, None, ""), + (BatchSize.year, datetime(2020, 1, 1, 1), "20200101"), + (BatchSize.month, datetime(2020, 1, 1, 1), "20200101"), + (BatchSize.day, datetime(2020, 1, 1, 1), "20200101"), + (BatchSize.hour, datetime(2020, 1, 1, 1), "20200101T01"), ], ) def test_format_batch_start(self, batch_size, batch_start, expected_formatted_batch_start):