You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/10/20 08:29:07 UTC

(datafusion) 01/01: feat: preserve ordering through `to_timestamp_seconds/millis/micros/nanos` and `to_unixtime` (#26111)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-26111-d89a85d3e340062a9efad3d8c4b32f10a7b139c5
in repository https://gitbox.apache.org/repos/asf/datafusion.git

commit 3ee3e394c19c97ccc7cca7ed3becb75cf364746a
Author: Huaijin <ha...@gmail.com>
AuthorDate: Tue Oct 20 08:28:42 2026 +0000

    feat: preserve ordering through `to_timestamp_seconds/millis/micros/nanos` and `to_unixtime` (#26111)
    
    ## Which issue does this PR close?
    
    - Closes #26108
    
    ## Rationale for this change
    
    Queries over ordered data can perform unnecessary sorts after timestamp
    conversions. Propagating ordering through these conversions lets the
    optimizer remove redundant sorts, including the `to_timestamp_micros` →
    `date_bin` → `to_unixtime` chain.
    
    ## What changes are included in this PR?
    
    - Add ordering propagation for single-argument `to_timestamp_seconds`,
    `to_timestamp_millis`, `to_timestamp_micros`, and `to_timestamp_nanos`
    calls with integer input.
    - Add ordering propagation for single-argument `to_unixtime` calls with
    integer, Date32, Date64, or timestamp input.
    - Preserve input sort direction and NULL placement. Overflow continues
    to report an error.
    - Keep strict-ordering metadata conservative so precision reduction does
    not incorrectly eliminate sorting by subsequent keys.
    
    ## What is the testing strategy for this PR?
    
    `integer_timestamp_ordering` checks all four timestamp functions across
    integer types, sort directions, NULL placement, and unsupported inputs.
    `ordering_matches_runtime_conversion` checks `to_unixtime` ordering and
    temporal runtime conversions across date types, timestamp units, and
    timezones.
    
    Tests in the existing `datetime/timestamps.slt` verify plans without
    redundant `SortExec`, conversion results and boundaries, the full
    conversion chain, and a case where precision reduction creates ties and
    a subsequent sorting key still requires `SortExec`.
    
    Validation passed:
    
    - Both added unit tests.
    - `cargo test -p datafusion-sqllogictest --test sqllogictests --
    datetime/timestamps.slt`
    - `cargo fmt --all -- --check` and `git diff --check`
    - `cargo clippy --all-targets --all-features -- -D warnings`
    - `uv run ./dev/rust_lint.sh`
    
    ## Are there any user-facing changes?
    
    Yes. Queries using these conversions on ordered input can avoid
    redundant sorts. Function results and public APIs are unchanged.
---
 datafusion/functions/src/datetime/to_timestamp.rs  |  76 +++++
 datafusion/functions/src/datetime/to_unixtime.rs   |  37 ++
 .../test_files/datetime/timestamps.slt             | 371 +++++++++++++++++++++
 3 files changed, 484 insertions(+)

diff --git a/datafusion/functions/src/datetime/to_timestamp.rs b/datafusion/functions/src/datetime/to_timestamp.rs
index 6720152cbd..5331db2d41 100644
--- a/datafusion/functions/src/datetime/to_timestamp.rs
+++ b/datafusion/functions/src/datetime/to_timestamp.rs
@@ -31,6 +31,7 @@ use arrow::datatypes::{
 };
 use datafusion_common::config::ConfigOptions;
 use datafusion_common::{Result, ScalarType, ScalarValue, exec_datafusion_err, exec_err};
+use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
 use datafusion_expr::{
     ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl,
     Signature, Volatility,
@@ -581,6 +582,23 @@ impl ScalarUDFImpl for ToTimestampSecondsFunc {
         }
     }
 
+    fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
+        Ok(if is_integer_input(input) {
+            input[0].sort_properties
+        } else {
+            SortProperties::Unordered
+        })
+    }
+
+    fn preserves_lex_ordering(&self, input: &[ExprProperties]) -> Result<bool> {
+        Ok(is_integer_input(input))
+    }
+
+    fn strictly_order_preserving(&self, input: &[ExprProperties]) -> Result<bool> {
+        // A lossless cast cannot restore strictness lost by its input.
+        Ok(is_integer_input(input) && input[0].strictly_order_preserving)
+    }
+
     fn documentation(&self) -> Option<&Documentation> {
         self.doc()
     }
@@ -650,6 +668,23 @@ impl ScalarUDFImpl for ToTimestampMillisFunc {
         }
     }
 
+    fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
+        Ok(if is_integer_input(input) {
+            input[0].sort_properties
+        } else {
+            SortProperties::Unordered
+        })
+    }
+
+    fn preserves_lex_ordering(&self, input: &[ExprProperties]) -> Result<bool> {
+        Ok(is_integer_input(input))
+    }
+
+    fn strictly_order_preserving(&self, input: &[ExprProperties]) -> Result<bool> {
+        // A lossless cast cannot restore strictness lost by its input.
+        Ok(is_integer_input(input) && input[0].strictly_order_preserving)
+    }
+
     fn documentation(&self) -> Option<&Documentation> {
         self.doc()
     }
@@ -719,6 +754,23 @@ impl ScalarUDFImpl for ToTimestampMicrosFunc {
         }
     }
 
+    fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
+        Ok(if is_integer_input(input) {
+            input[0].sort_properties
+        } else {
+            SortProperties::Unordered
+        })
+    }
+
+    fn preserves_lex_ordering(&self, input: &[ExprProperties]) -> Result<bool> {
+        Ok(is_integer_input(input))
+    }
+
+    fn strictly_order_preserving(&self, input: &[ExprProperties]) -> Result<bool> {
+        // A lossless cast cannot restore strictness lost by its input.
+        Ok(is_integer_input(input) && input[0].strictly_order_preserving)
+    }
+
     fn documentation(&self) -> Option<&Documentation> {
         self.doc()
     }
@@ -788,11 +840,35 @@ impl ScalarUDFImpl for ToTimestampNanosFunc {
         }
     }
 
+    fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
+        Ok(if is_integer_input(input) {
+            input[0].sort_properties
+        } else {
+            SortProperties::Unordered
+        })
+    }
+
+    fn preserves_lex_ordering(&self, input: &[ExprProperties]) -> Result<bool> {
+        Ok(is_integer_input(input))
+    }
+
+    fn strictly_order_preserving(&self, input: &[ExprProperties]) -> Result<bool> {
+        // A lossless cast cannot restore strictness lost by its input.
+        Ok(is_integer_input(input) && input[0].strictly_order_preserving)
+    }
+
     fn documentation(&self) -> Option<&Documentation> {
         self.doc()
     }
 }
 
+/// The single-argument integer form casts the value to `Timestamp(unit, tz)`
+/// unchanged: one-to-one, order-preserving, and nulls map to nulls.
+/// Strict ordering of the full expression still depends on the input expression.
+fn is_integer_input(input: &[ExprProperties]) -> bool {
+    matches!(input, [value] if value.range.data_type().is_integer())
+}
+
 fn to_timestamp_impl<T: ArrowTimestampType + ScalarType<i64>>(
     args: &[ColumnarValue],
     name: &str,
diff --git a/datafusion/functions/src/datetime/to_unixtime.rs b/datafusion/functions/src/datetime/to_unixtime.rs
index 5b9734c05d..553935b3cb 100644
--- a/datafusion/functions/src/datetime/to_unixtime.rs
+++ b/datafusion/functions/src/datetime/to_unixtime.rs
@@ -19,6 +19,7 @@ use super::to_timestamp::ToTimestampSecondsFunc;
 use crate::datetime::common::*;
 use arrow::datatypes::{DataType, TimeUnit};
 use datafusion_common::{Result, exec_err};
+use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
 use datafusion_expr::{
     ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
     Volatility,
@@ -143,6 +144,42 @@ impl ScalarUDFImpl for ToUnixtimeFunc {
         }
     }
 
+    fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
+        if self.preserves_lex_ordering(input)? {
+            // Conversions preserve epoch order and report overflow instead of adding NULLs.
+            Ok(input[0].sort_properties)
+        } else {
+            Ok(SortProperties::Unordered)
+        }
+    }
+
+    fn preserves_lex_ordering(&self, input: &[ExprProperties]) -> Result<bool> {
+        let [value] = input else {
+            return Ok(false);
+        };
+        let data_type = value.range.data_type();
+        Ok(data_type.is_integer()
+            || matches!(
+                data_type,
+                DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _)
+            ))
+    }
+
+    fn strictly_order_preserving(&self, input: &[ExprProperties]) -> Result<bool> {
+        let [value] = input else {
+            return Ok(false);
+        };
+        let data_type = value.range.data_type();
+        // Exact or scaled-by-constant conversions are one-to-one; Date64 and
+        // sub-second timestamps truncate to seconds.
+        Ok(value.strictly_order_preserving
+            && (data_type.is_integer()
+                || matches!(
+                    data_type,
+                    DataType::Date32 | DataType::Timestamp(TimeUnit::Second, _)
+                )))
+    }
+
     fn documentation(&self) -> Option<&Documentation> {
         self.doc()
     }
diff --git a/datafusion/sqllogictest/test_files/datetime/timestamps.slt b/datafusion/sqllogictest/test_files/datetime/timestamps.slt
index bc6a6d07ad..d608d857ec 100644
--- a/datafusion/sqllogictest/test_files/datetime/timestamps.slt
+++ b/datafusion/sqllogictest/test_files/datetime/timestamps.slt
@@ -6807,3 +6807,374 @@ SELECT timezone(arrow_cast(b, 'Utf8'), aware) FROM at_tz_case
 
 statement ok
 DROP TABLE at_tz_case
+
+# Timestamp conversion ordering.
+statement ok
+SET datafusion.execution.target_partitions = 1;
+
+statement ok
+SET datafusion.explain.physical_plan_only = true;
+
+query I
+COPY (
+    SELECT column1::BIGINT AS micros, column2::INT AS id,
+           arrow_cast(column1, 'Timestamp(Microsecond, None)') AS ts,
+           column3::DOUBLE AS epoch_seconds,
+           arrow_cast(column1, 'Date32') AS d,
+           arrow_cast(column1, 'Timestamp(Second, Some("America/New_York"))') AS ts_seconds,
+           arrow_cast(column1, 'Timestamp(Millisecond, None)') AS ts_millis,
+           arrow_cast(column1, 'Timestamp(Nanosecond, None)') AS ts_nanos,
+           column4 AS date_text, arrow_cast(column5, 'UInt64') AS u
+    FROM (VALUES (-1, 1, 1.1, '01-01-1971', 0), (0, 0, 1.9, '12-31-1970', 1),
+                 (1000000, 2, 2.0, '12-31-1971', 9223372036854775807), (NULL, 3, NULL, NULL, NULL))
+    ORDER BY micros ASC NULLS LAST, id
+) TO 'test_files/scratch/timestamps/conversion_asc.parquet' STORED AS PARQUET;
+----
+4
+
+statement ok
+CREATE EXTERNAL TABLE ordered_timestamps STORED AS PARQUET
+WITH ORDER (micros ASC NULLS LAST, id ASC)
+WITH ORDER (ts ASC NULLS LAST, id ASC)
+WITH ORDER (epoch_seconds ASC NULLS LAST, id ASC)
+WITH ORDER (d ASC NULLS LAST, id ASC)
+WITH ORDER (ts_seconds ASC NULLS LAST, id ASC)
+WITH ORDER (ts_millis ASC NULLS LAST, id ASC)
+WITH ORDER (ts_nanos ASC NULLS LAST, id ASC)
+WITH ORDER (date_text ASC NULLS LAST, id ASC)
+WITH ORDER (u ASC NULLS LAST, id ASC)
+LOCATION 'test_files/scratch/timestamps/conversion_asc.parquet';
+
+# The conversion chain preserves order: no SortExec is needed.
+query TT
+EXPLAIN SELECT to_unixtime(date_bin(interval '1 second', to_timestamp_micros(micros), timestamp '1970-01-01')) AS bucket
+FROM ordered_timestamps ORDER BY bucket ASC NULLS LAST;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_unixtime(date_bin(IntervalMonthDayNano { months: 0, days: 0, nanoseconds: 1000000000 }, to_timestamp_micros(micros@0), 0)) as bucket], file_type=parquet
+
+query I
+SELECT to_unixtime(date_bin(interval '1 second', to_timestamp_micros(micros), timestamp '1970-01-01')) AS bucket
+FROM ordered_timestamps ORDER BY bucket ASC NULLS LAST;
+----
+-1
+0
+1
+NULL
+
+# Integer conversions preserve subsequent sort keys for all timestamp units.
+query TT
+EXPLAIN SELECT to_timestamp_seconds(micros) AS t, id
+FROM ordered_timestamps ORDER BY t ASC NULLS LAST, id;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_timestamp_seconds(micros@0) as t, id], file_type=parquet
+
+query TT
+EXPLAIN SELECT to_timestamp_millis(micros) AS t, id
+FROM ordered_timestamps ORDER BY t ASC NULLS LAST, id;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_timestamp_millis(micros@0) as t, id], file_type=parquet
+
+query TT
+EXPLAIN SELECT to_timestamp_micros(micros) AS t, id
+FROM ordered_timestamps ORDER BY t ASC NULLS LAST, id;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_timestamp_micros(micros@0) as t, id], file_type=parquet
+
+query TT
+EXPLAIN SELECT to_timestamp_nanos(micros) AS t, id
+FROM ordered_timestamps ORDER BY t ASC NULLS LAST, id;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_timestamp_nanos(micros@0) as t, id], file_type=parquet
+
+# Lossless Unix-time conversions preserve subsequent sort keys.
+query TT
+EXPLAIN SELECT to_unixtime(micros) AS seconds, id
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_unixtime(micros@0) as seconds, id], file_type=parquet
+
+query TT
+EXPLAIN SELECT to_unixtime(d) AS seconds, id
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_unixtime(d@4) as seconds, id], file_type=parquet
+
+query TT
+EXPLAIN SELECT to_unixtime(ts_seconds) AS seconds, id
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_unixtime(ts_seconds@5) as seconds, id], file_type=parquet
+
+# Include UInt64's largest valid value without formatting an out-of-range timestamp.
+query IIIIII
+SELECT to_unixtime(micros) AS seconds, to_unixtime(d), to_unixtime(ts_seconds),
+       arrow_cast(to_timestamp_micros(u), 'Int64'), to_unixtime(u), id
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+-1 -86400 -1 0 0 1
+0 0 0 1 1 0
+1000000 86400000000 1000000 9223372036854775807 9223372036854775807 2
+NULL NULL NULL NULL NULL 3
+
+query TT
+EXPLAIN SELECT to_unixtime(u) AS seconds, id
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_unixtime(u@9) as seconds, id], file_type=parquet
+
+# A lossy conversion still preserves ordering by its first key.
+query TT
+EXPLAIN SELECT to_unixtime(arrow_cast(micros, 'Date64')) AS seconds
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_unixtime(CAST(micros@0 AS Date64)) as seconds], file_type=parquet
+
+# A converted suffix key need not be independently ordered.
+query TT
+EXPLAIN SELECT micros, to_unixtime(id) AS seconds
+FROM ordered_timestamps ORDER BY micros ASC NULLS LAST, seconds;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[micros, to_unixtime(id@1) as seconds], output_ordering=[micros@0 ASC NULLS LAST], file_type=parquet
+
+query TT
+EXPLAIN SELECT micros, to_timestamp_micros(id) AS t
+FROM ordered_timestamps ORDER BY micros ASC NULLS LAST, t;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[micros, to_timestamp_micros(id@1) as t], output_ordering=[micros@0 ASC NULLS LAST], file_type=parquet
+
+# Truncation creates ties, so sorting by seconds then id still needs SortExec.
+query II
+SELECT to_unixtime(ts) AS seconds, id FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+0 0
+0 1
+1 2
+NULL 3
+
+# Date64 and the other sub-second units also merge -1 and 0 into one group.
+query II
+SELECT to_unixtime(arrow_cast(micros, 'Date64')) AS seconds, id
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+0 0
+0 1
+1000 2
+NULL 3
+
+query II
+SELECT to_unixtime(ts_millis) AS seconds, id
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+0 0
+0 1
+1000 2
+NULL 3
+
+query II
+SELECT to_unixtime(ts_nanos) AS seconds, id
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+0 0
+0 1
+0 2
+NULL 3
+
+# Float inputs create ties too, without an explicit cast in the query.
+query II
+SELECT to_unixtime(epoch_seconds) AS seconds, id
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+1 0
+1 1
+2 2
+NULL 3
+
+query TT
+EXPLAIN SELECT to_timestamp_seconds(epoch_seconds) AS t, id
+FROM ordered_timestamps ORDER BY t ASC NULLS LAST, id;
+----
+physical_plan
+01)SortExec: expr=[t@0 ASC NULLS LAST, id@1 ASC NULLS LAST], preserve_partitioning=[false]
+02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_timestamp_seconds(epoch_seconds@3) as t, id], file_type=parquet
+
+# Lexically sorted date strings are not necessarily sorted after parsing.
+# Format arguments must not enable ordering propagation.
+query II
+SELECT to_unixtime(date_text, '%m-%d-%Y') AS seconds, id
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+31449600 0
+31536000 1
+62985600 2
+NULL 3
+
+query TT
+EXPLAIN SELECT to_timestamp_seconds(date_text, '%m-%d-%Y') AS t, id
+FROM ordered_timestamps ORDER BY t ASC NULLS LAST, id;
+----
+physical_plan
+01)SortExec: expr=[t@0 ASC NULLS LAST, id@1 ASC NULLS LAST], preserve_partitioning=[false]
+02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_timestamp_seconds(date_text@8, %m-%d-%Y) as t, id], file_type=parquet
+
+# Casting fractional Unix seconds to BIGINT creates ties (1.1 and 1.9 -> 1).
+# The conversions must retain SortExec to reorder the tied ids from 1, 0.
+query II
+SELECT to_unixtime(CAST(epoch_seconds AS BIGINT)) AS seconds, id
+FROM ordered_timestamps ORDER BY seconds ASC NULLS LAST, id;
+----
+1 0
+1 1
+2 2
+NULL 3
+
+query TT
+EXPLAIN SELECT to_timestamp_millis(CAST(epoch_seconds AS BIGINT)) AS t, id
+FROM ordered_timestamps ORDER BY t ASC NULLS LAST, id;
+----
+physical_plan
+01)SortExec: expr=[t@0 ASC NULLS LAST, id@1 ASC NULLS LAST], preserve_partitioning=[false]
+02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_timestamp_millis(CAST(epoch_seconds@3 AS Int64)) as t, id], file_type=parquet
+
+query TT
+EXPLAIN SELECT to_timestamp_micros(CAST(epoch_seconds AS BIGINT)) AS t, id
+FROM ordered_timestamps ORDER BY t ASC NULLS LAST, id;
+----
+physical_plan
+01)SortExec: expr=[t@0 ASC NULLS LAST, id@1 ASC NULLS LAST], preserve_partitioning=[false]
+02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_timestamp_micros(CAST(epoch_seconds@3 AS Int64)) as t, id], file_type=parquet
+
+query TT
+EXPLAIN SELECT to_timestamp_nanos(CAST(epoch_seconds AS BIGINT)) AS t, id
+FROM ordered_timestamps ORDER BY t ASC NULLS LAST, id;
+----
+physical_plan
+01)SortExec: expr=[t@0 ASC NULLS LAST, id@1 ASC NULLS LAST], preserve_partitioning=[false]
+02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_asc.parquet]]}, projection=[to_timestamp_nanos(CAST(epoch_seconds@3 AS Int64)) as t, id], file_type=parquet
+
+query PI
+SELECT to_timestamp_seconds(CAST(epoch_seconds AS BIGINT)) AS t, id
+FROM ordered_timestamps ORDER BY t ASC NULLS LAST, id;
+----
+1970-01-01T00:00:01 0
+1970-01-01T00:00:01 1
+1970-01-01T00:00:02 2
+NULL 3
+
+# Descending order with duplicate values and NULLs preserves the second key.
+query I
+COPY (
+    SELECT column1::BIGINT AS value, column2::INT AS id,
+           arrow_cast(column1, 'Date32') AS d
+    FROM (VALUES (NULL, 1), (NULL, 0), (1, 1), (1, 0), (0, 0), (-1, 0))
+    ORDER BY value DESC NULLS FIRST, id
+) TO 'test_files/scratch/timestamps/conversion_desc.parquet' STORED AS PARQUET;
+----
+6
+
+statement ok
+CREATE EXTERNAL TABLE ordered_timestamps_desc STORED AS PARQUET
+WITH ORDER (value DESC NULLS FIRST, id ASC)
+WITH ORDER (d DESC NULLS FIRST, id ASC)
+LOCATION 'test_files/scratch/timestamps/conversion_desc.parquet';
+
+query TT
+EXPLAIN SELECT to_unixtime(value) AS seconds, id
+FROM ordered_timestamps_desc ORDER BY seconds DESC NULLS FIRST, id;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_desc.parquet]]}, projection=[to_unixtime(value@0) as seconds, id], file_type=parquet
+
+query TT
+EXPLAIN SELECT to_timestamp_micros(value) AS t, id
+FROM ordered_timestamps_desc ORDER BY t DESC NULLS FIRST, id;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_desc.parquet]]}, projection=[to_timestamp_micros(value@0) as t, id], file_type=parquet
+
+query TT
+EXPLAIN SELECT to_unixtime(d) AS seconds, id
+FROM ordered_timestamps_desc ORDER BY seconds DESC NULLS FIRST, id;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_desc.parquet]]}, projection=[to_unixtime(d@2) as seconds, id], file_type=parquet
+
+query II
+SELECT to_unixtime(d) AS seconds, id
+FROM ordered_timestamps_desc ORDER BY seconds DESC NULLS FIRST, id;
+----
+NULL 0
+NULL 1
+86400 0
+86400 1
+0 0
+-86400 0
+
+query IPI
+SELECT to_unixtime(value) AS seconds, to_timestamp_micros(value), id
+FROM ordered_timestamps_desc ORDER BY seconds DESC NULLS FIRST, id;
+----
+NULL NULL 0
+NULL NULL 1
+1 1970-01-01T00:00:00.000001 0
+1 1970-01-01T00:00:00.000001 1
+0 1970-01-01T00:00:00 0
+-1 1969-12-31T23:59:59.999999 0
+
+query error Can't cast value 9223372036854775808 to type Int64
+SELECT to_timestamp_micros(arrow_cast(9223372036854775808, 'UInt64'));
+
+query error Can't cast value 9223372036854775808 to type Int64
+SELECT to_unixtime(arrow_cast(9223372036854775808, 'UInt64'));
+
+# Exercise overflow in the array cast path after the redundant sort is removed.
+query I
+COPY (
+    SELECT arrow_cast(column1, 'UInt64') AS u
+    FROM (VALUES (0), (9223372036854775807), (9223372036854775808))
+    ORDER BY u ASC NULLS LAST
+) TO 'test_files/scratch/timestamps/conversion_overflow.parquet' STORED AS PARQUET;
+----
+3
+
+statement ok
+CREATE EXTERNAL TABLE ordered_timestamps_overflow STORED AS PARQUET
+WITH ORDER (u ASC NULLS LAST)
+LOCATION 'test_files/scratch/timestamps/conversion_overflow.parquet';
+
+query TT
+EXPLAIN SELECT to_timestamp_micros(u) AS t
+FROM ordered_timestamps_overflow ORDER BY t ASC NULLS LAST;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_overflow.parquet]]}, projection=[to_timestamp_micros(u@0) as t], file_type=parquet
+
+query error Can't cast value 9223372036854775808 to type Int64
+SELECT to_timestamp_micros(u) AS t
+FROM ordered_timestamps_overflow ORDER BY t ASC NULLS LAST;
+
+query TT
+EXPLAIN SELECT to_unixtime(u) AS seconds
+FROM ordered_timestamps_overflow ORDER BY seconds ASC NULLS LAST;
+----
+physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/timestamps/conversion_overflow.parquet]]}, projection=[to_unixtime(u@0) as seconds], file_type=parquet
+
+query error Can't cast value 9223372036854775808 to type Int64
+SELECT to_unixtime(u) AS seconds
+FROM ordered_timestamps_overflow ORDER BY seconds ASC NULLS LAST;
+
+# Epoch-unit conversions remain valid at the underlying integer boundaries.
+query IIIIII
+SELECT to_unixtime(arrow_cast(v, 'Timestamp(Second, None)')),
+       to_unixtime(arrow_cast(v, 'Timestamp(Millisecond, None)')),
+       to_unixtime(arrow_cast(v, 'Timestamp(Microsecond, None)')),
+       to_unixtime(arrow_cast(v, 'Timestamp(Nanosecond, None)')),
+       to_unixtime(arrow_cast(v, 'Date64')), to_unixtime(arrow_cast(d, 'Date32'))
+FROM (VALUES ('-9223372036854775808'::BIGINT, -2147483648),
+             (9223372036854775807::BIGINT, 2147483647), (NULL::BIGINT, NULL)) AS t(v, d)
+ORDER BY v ASC NULLS LAST;
+----
+-9223372036854775808 -9223372036854775 -9223372036854 -9223372036 -9223372036854775 -185542587187200
+9223372036854775807 9223372036854775 9223372036854 9223372036 9223372036854775 185542587100800
+NULL NULL NULL NULL NULL NULL
+
+statement ok
+RESET datafusion.explain.physical_plan_only;
+
+statement ok
+SET datafusion.execution.target_partitions = 4;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org