Toby1009 commented on code in PR #25526:
URL: https://github.com/apache/datafusion/pull/25526#discussion_r4056976150


##########
datafusion/physical-expr/src/expressions/cast.rs:
##########
@@ -283,26 +283,57 @@ impl CastExpr {
     }
 }
 
-pub(crate) fn is_order_preserving_cast_family(
-    source_type: &DataType,
-    target_type: &DataType,
-) -> bool {
-    (source_type.is_numeric() || *source_type == Boolean) && 
target_type.is_numeric()
-        || source_type.is_temporal() && target_type.is_temporal()
-        || source_type.eq(target_type)
+/// Whether successful casts preserve order when conversion failures return 
errors.
+/// Unlike `check_bigger_cast`, this allows precision loss and is not 
sufficient
+/// for propagating distinct counts or the ordering of subsequent sort keys.
+fn is_order_preserving_cast(source_type: &DataType, target_type: &DataType) -> 
bool {
+    use arrow::datatypes::TimeUnit::*;
+    if source_type == target_type
+        || (source_type.is_numeric() || *source_type == Boolean)
+            && target_type.is_numeric()
+    {
+        return true;
+    }
+    // Temporal casts are not generally monotonic: extracting time-of-day wraps
+    // at midnight, and timezone transitions can reverse the local date.
+    match (source_type, target_type) {
+        (Date32 | Date64, Date32 | Date64)
+        | (Date32 | Date64, Timestamp(_, None))
+        | (Timestamp(_, None), Date32) => true,

Review Comment:
   Thanks, applied this suggestion. UTC and fixed-offset timezones now preserve 
ordering for timestamp-to-Date32 and naive-to-fixed-offset timestamp casts. I 
added the three requested unit cases and verified the EXPLAIN example no longer 
contains the outer SortExec. The full required local test suite also passes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to