55from typing import TYPE_CHECKING , Any , ClassVar , Optional
66
77import polars as pl
8- from dagster import InputContext , MetadataValue , MultiPartitionKey , OutputContext
8+ from dagster import (
9+ InputContext ,
10+ MetadataValue ,
11+ MultiPartitionKey ,
12+ MultiPartitionsDefinition ,
13+ OutputContext ,
14+ TimeWindowPartitionsDefinition ,
15+ )
916from dagster ._core .errors import DagsterInvariantViolationError
1017from dagster ._core .storage .upath_io_manager import is_dict_type
1118from packaging .version import parse as parse_version
@@ -484,7 +491,18 @@ def get_predicate(
484491 f"Invalid context type: { type (context )} "
485492 )
486493
487- def key_to_predicate (key ):
494+ def key_to_predicate (key : str , dim : str | None = None ) -> str :
495+ partitions_def = context .asset_partitions_def
496+ if dim is not None and isinstance (
497+ partitions_def , MultiPartitionsDefinition
498+ ):
499+ dim_partitions_def = partitions_def .get_partitions_def_for_dimension (
500+ dim
501+ )
502+ if isinstance (dim_partitions_def , TimeWindowPartitionsDefinition ):
503+ return f"DATE '{ key } '"
504+ elif isinstance (partitions_def , TimeWindowPartitionsDefinition ):
505+ return f"DATE '{ key } '"
488506 return f"'{ key } '"
489507
490508 if partition_by is None or not context .has_asset_partitions :
@@ -502,7 +520,7 @@ def key_to_predicate(key):
502520
503521 predicate = " AND " .join (
504522 [
505- f"{ partition_by [dim ]} in ({ ', ' .join (map ( key_to_predicate , keys ) )} )"
523+ f"{ partition_by [dim ]} in ({ ', ' .join (key_to_predicate ( key , dim ) for key in keys )} )"
506524 for dim , keys in all_keys_by_dim .items ()
507525 ]
508526 )
@@ -514,7 +532,7 @@ def key_to_predicate(key):
514532 )
515533
516534 if len (context .asset_partition_keys ) == 1 :
517- predicate = f"{ partition_by } = ' { context .asset_partition_keys [0 ]} ' "
535+ predicate = f"{ partition_by } = { key_to_predicate ( context .asset_partition_keys [0 ]) } "
518536 else :
519537 predicate = f"{ partition_by } in ({ ', ' .join (map (key_to_predicate , context .asset_partition_keys ))} )"
520538
0 commit comments