Apache DataFusion SQL Query Engine
fix: preserve input strictness in `from_unixtime` (#26414)
Huaijin5b5edc7f3cbc
diff --git a/datafusion/functions/src/datetime/from_unixtime.rs b/datafusion/functions/src/datetime/from_unixtime.rsindex 89a7e807e3..c1914d1f48 100644--- a/datafusion/functions/src/datetime/from_unixtime.rs+++ b/datafusion/functions/src/datetime/from_unixtime.rs@@ -212,11 +212,9 @@ impl ScalarUDFImpl for FromUnixtimeFunc {Ok(true)}- fn strictly_order_preserving(&self, _inputs: &[ExprProperties]) -> Result<bool> {- // `from_unixtime` stores the input's exact `Int64` value as a- // `Timestamp(Second)`: the mapping is one-to-one, order-preserving,- // and maps nulls to nulls.- Ok(true)+ fn strictly_order_preserving(&self, inputs: &[ExprProperties]) -> Result<bool> {+ // A lossless cast cannot restore strictness lost by its input.+ Ok(inputs[0].strictly_order_preserving)}fn documentation(&self) -> Option<&Documentation> {diff --git a/datafusion/sqllogictest/test_files/datetime/timestamps.slt b/datafusion/sqllogictest/test_files/datetime/timestamps.sltindex 162846779a..a0f4af894a 100644--- a/datafusion/sqllogictest/test_files/datetime/timestamps.slt+++ b/datafusion/sqllogictest/test_files/datetime/timestamps.slt@@ -460,6 +460,47 @@ SELECT from_unixtime(ts / 1000000000, 'UTC') FROM ts_data LIMIT 3;2020-09-08T12:42:29Z2020-09-08T11:42:29Z+# LIMIT makes the inner sort necessary; the lossy cast creates tied seconds.+statement ok+CREATE VIEW fractional_epochs AS+SELECT * FROM (VALUES (1.1::DOUBLE, 1), (1.9, 0), (NULL, 2), (2.1, 3)) AS v(seconds, id)+ORDER BY seconds ASC NULLS FIRST, id LIMIT 3;++query PI+SELECT from_unixtime(CAST(seconds AS BIGINT)) AS t, id+FROM fractional_epochs ORDER BY t ASC NULLS FIRST, id;+----+NULL 2+1970-01-01T00:00:01 0+1970-01-01T00:00:01 1++query PI+SELECT from_unixtime(CAST(seconds AS BIGINT), 'UTC') AS t, id+FROM fractional_epochs ORDER BY t ASC NULLS FIRST, id;+----+NULL 2+1970-01-01T00:00:01Z 0+1970-01-01T00:00:01Z 1++statement ok+SET datafusion.explain.physical_plan_only = true;++# A direct integer input still preserves the ordering of subsequent keys.+query TT+EXPLAIN SELECT from_unixtime(seconds, 'UTC') AS t, id+FROM (+ SELECT * FROM (VALUES (1::BIGINT, 1), (2, 0), (3, 2)) AS v(seconds, id)+ ORDER BY seconds, id LIMIT 2+) ORDER BY t, id;+----+physical_plan+01)ProjectionExec: expr=[from_unixtime(column1@0, UTC) as t, column2@1 as id]+02)--SortExec: TopK(fetch=2), expr=[column1@0 ASC NULLS LAST, column2@1 ASC NULLS LAST], preserve_partitioning=[false]+03)----DataSourceExec: partitions=1, partition_sizes=[1]++statement ok+RESET datafusion.explain.physical_plan_only;+# to_timestampquery I