|
1 | | -import asyncio |
2 | 1 | import functools |
3 | 2 | from collections.abc import Awaitable, Iterable |
4 | 3 | from datetime import datetime, timedelta |
|
22 | 21 | from dagster._core.definitions.freshness import FreshnessState |
23 | 22 | from dagster._core.definitions.partitions.context import ( |
24 | 23 | PartitionLoadingContext, |
| 24 | + partition_loading_context, |
25 | 25 | use_partition_loading_context, |
26 | 26 | ) |
27 | 27 | from dagster._core.definitions.partitions.definition import ( |
@@ -564,9 +564,12 @@ async def compute_subset_with_status( |
564 | 564 | """Returns the subset of an asset check that matches a given status.""" |
565 | 565 | from dagster._core.storage.event_log.base import AssetCheckSummaryRecord |
566 | 566 |
|
567 | | - # Handle partitioned asset checks with partition-level granularity |
| 567 | + # Handle partitioned asset checks |
568 | 568 | if self._get_partitions_def(key): |
569 | | - return await self._get_partitioned_check_subset_with_status(key, status, from_subset) |
| 569 | + with partition_loading_context(new_ctx=self._partition_loading_context): |
| 570 | + return await self._get_partitioned_check_subset_with_status( |
| 571 | + key, status, from_subset |
| 572 | + ) |
570 | 573 |
|
571 | 574 | # Handle non-partitioned asset checks with existing logic |
572 | 575 | summary = await AssetCheckSummaryRecord.gen(self, key) |
@@ -628,35 +631,39 @@ async def _compute_missing_check_subset( |
628 | 631 | ) -> EntitySubset[AssetCheckKey]: |
629 | 632 | return await self.compute_subset_with_status(key, None, from_subset) |
630 | 633 |
|
631 | | - async def _get_asset_check_partition_status( |
632 | | - self, key: AssetCheckKey, partition: str |
633 | | - ) -> Optional["AssetCheckExecutionResolvedStatus"]: |
634 | | - # NOTE: we should add a LoadingContext-native version of this |
635 | | - record = self.instance.event_log_storage.get_latest_asset_check_execution_by_key( |
636 | | - [key], partition |
637 | | - ).get(key) |
638 | | - if record: |
639 | | - targets_latest = await record.targets_latest_materialization(self) |
640 | | - return await record.resolve_status(self) if targets_latest else None |
641 | | - else: |
642 | | - return None |
643 | | - |
| 634 | + @use_partition_loading_context |
644 | 635 | async def _get_partitioned_check_subset_with_status( |
645 | 636 | self, |
646 | 637 | key: AssetCheckKey, |
647 | 638 | status: Optional["AssetCheckExecutionResolvedStatus"], |
648 | 639 | from_subset: EntitySubset, |
649 | 640 | ) -> EntitySubset[AssetCheckKey]: |
| 641 | + from dagster._core.storage.asset_check_status_cache import ( |
| 642 | + get_updated_asset_check_status_cache_value, |
| 643 | + ) |
| 644 | + |
650 | 645 | check_node = self.asset_graph.get(key) |
651 | 646 | if not check_node or not check_node.partitions_def: |
652 | 647 | check.failed(f"Asset check {key} not found or not partitioned.") |
653 | 648 |
|
654 | | - partitions = list(from_subset.expensively_compute_partition_keys()) |
655 | | - statuses = await asyncio.gather( |
656 | | - *(self._get_asset_check_partition_status(key, p) for p in partitions) |
| 649 | + cache_value = get_updated_asset_check_status_cache_value( |
| 650 | + key, check_node.partitions_def, self |
657 | 651 | ) |
658 | | - matching_partitions = {p for p, s in zip(partitions, statuses) if s == status} |
659 | | - return from_subset.compute_intersection_with_partition_keys(matching_partitions) |
| 652 | + |
| 653 | + if status is None: |
| 654 | + known_statuses = self.get_empty_subset(key=key) |
| 655 | + for serializable_subset in cache_value.subsets.values(): |
| 656 | + subset = self.get_subset_from_serializable_subset(serializable_subset) |
| 657 | + if subset: |
| 658 | + known_statuses = known_statuses.compute_union(subset) |
| 659 | + return from_subset.compute_difference(known_statuses) or self.get_empty_subset(key=key) |
| 660 | + else: |
| 661 | + serializable_subset = cache_value.subsets.get(status) |
| 662 | + if serializable_subset is None: |
| 663 | + return self.get_empty_subset(key=key) |
| 664 | + return self.get_subset_from_serializable_subset( |
| 665 | + serializable_subset |
| 666 | + ) or self.get_empty_subset(key=key) |
660 | 667 |
|
661 | 668 | async def _compute_run_in_progress_asset_subset(self, key: AssetKey) -> EntitySubset[AssetKey]: |
662 | 669 | from dagster._core.storage.partition_status_cache import AssetStatusCacheValue |
|
0 commit comments