Skip to content

Apache DataFusion SQL Query Engine

perf: preserve scalar UDF output types in property analysis (#25668)

Malingshuafe4a63d0d08
diff --git a/datafusion/expr/src/udf.rs b/datafusion/expr/src/udf.rs
index 3a79792eb6..fd2e7b96be 100644
--- a/datafusion/expr/src/udf.rs
+++ b/datafusion/expr/src/udf.rs
@@ -908,15 +908,15 @@ pub trait ScalarUDFImpl: Debug + DynEq + DynHash + Send + Sync + Any {
/// # Output type
///
/// The default implementation returns an unbounded [`DataType::Null`]
- /// interval because the output type cannot be inferred generically.
- /// Implementations should override this method when they can determine the
- /// output type, even if they cannot compute precise bounds. Returning an
- /// unbounded interval with the correct type preserves information used by
- /// downstream physical property analysis.
- ///
- /// If the output type depends on argument values, only report it when those
- /// values are known, such as from singleton input intervals. Otherwise,
- /// keep the output type unknown.
+ /// interval because the output type cannot be inferred from input intervals
+ /// alone. During physical property analysis, `ScalarFunctionExpr` uses its
+ /// resolved return type to replace this default with a typed unbounded
+ /// interval when supported. UDFs need not override this method solely to
+ /// preserve their output type for that analysis.
+ ///
+ /// Override this method to provide more precise bounds. Explicit bounds
+ /// are preserved. The fallback does not apply to direct bounds evaluation
+ /// by the constraint solver.
fn evaluate_bounds(&self, _input: &[&Interval]) -> Result<Interval> {
// We cannot assume the input datatype is the same of output type.
Interval::make_unbounded(&DataType::Null)
diff --git a/datafusion/functions/src/datetime/date_bin.rs b/datafusion/functions/src/datetime/date_bin.rs
index 2696916b06..dc3da309da 100644
--- a/datafusion/functions/src/datetime/date_bin.rs
+++ b/datafusion/functions/src/datetime/date_bin.rs
@@ -38,7 +38,6 @@ use datafusion_common::{
Result, ScalarValue, exec_datafusion_err, exec_err, not_impl_err, plan_err,
};
use datafusion_expr::TypeSignature::Exact;
-use datafusion_expr::interval_arithmetic::Interval as ExprInterval;
use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
use datafusion_expr::{
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
@@ -294,11 +293,6 @@ impl ScalarUDFImpl for DateBinFunc {
}
}
- fn evaluate_bounds(&self, inputs: &[&ExprInterval]) -> Result<ExprInterval> {
- // DATE_BIN returns the same type as its source argument.
- ExprInterval::make_unbounded(&inputs[1].data_type())
- }
-
fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
@@ -829,11 +823,10 @@ mod tests {
use arrow::array::types::TimestampNanosecondType;
use arrow::array::{Array, IntervalDayTimeArray, TimestampNanosecondArray};
use arrow::compute::kernels::cast_utils::string_to_timestamp_nanos;
- use arrow::datatypes::{DataType, Field, FieldRef, IntervalUnit, TimeUnit};
+ use arrow::datatypes::{DataType, Field, FieldRef, TimeUnit};
use arrow_buffer::{IntervalDayTime, IntervalMonthDayNano};
use datafusion_common::{DataFusionError, ScalarValue};
- use datafusion_expr::interval_arithmetic::Interval as ExprInterval;
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl};
use chrono::TimeDelta;
@@ -884,28 +877,6 @@ mod tests {
);
}
- #[test]
- fn evaluate_bounds_preserves_source_type() {
- let stride =
- ExprInterval::make_unbounded(&DataType::Interval(IntervalUnit::DayTime))
- .unwrap();
- let source_types = [
- DataType::Timestamp(TimeUnit::Second, None),
- DataType::Timestamp(TimeUnit::Nanosecond, Some("UTC".into())),
- DataType::Time32(TimeUnit::Millisecond),
- DataType::Time64(TimeUnit::Microsecond),
- ];
- let function = DateBinFunc::new();
-
- for source_type in source_types {
- let source = ExprInterval::make_unbounded(&source_type).unwrap();
- let bounds = function.evaluate_bounds(&[&stride, &source]).unwrap();
-
- assert!(bounds.is_unbounded());
- assert_eq!(bounds.data_type(), source_type);
- }
- }
-
#[test]
fn test_date_bin() {
let return_field = &Arc::new(Field::new(
diff --git a/datafusion/functions/src/datetime/from_unixtime.rs b/datafusion/functions/src/datetime/from_unixtime.rs
index 53a170c5c8..d6aa09fb31 100644
--- a/datafusion/functions/src/datetime/from_unixtime.rs
+++ b/datafusion/functions/src/datetime/from_unixtime.rs
@@ -28,7 +28,6 @@ use chrono::{DateTime, NaiveDateTime, Offset, TimeDelta, TimeZone};
use datafusion_common::config::ConfigOptions;
use datafusion_common::{Result, ScalarValue, exec_err, internal_err};
use datafusion_expr::TypeSignature::Exact;
-use datafusion_expr::interval_arithmetic::Interval;
use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
use datafusion_expr::{
ColumnarValue, Documentation, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDF,
@@ -220,22 +219,6 @@ impl ScalarUDFImpl for FromUnixtimeFunc {
Ok(true)
}
- fn evaluate_bounds(&self, inputs: &[&Interval]) -> Result<Interval> {
- let data_type = match inputs {
- [_] => Timestamp(Second, self.timezone.clone()),
- // Literal properties encode a constant timezone as a singleton interval.
- [_, timezone] if timezone.lower() == timezone.upper() => {
- match timezone_from_scalar(timezone.lower()) {
- Some(timezone) => Timestamp(Second, Some(timezone)),
- None => DataType::Null,
- }
- }
- _ => DataType::Null,
- };
-
- Interval::make_unbounded(&data_type)
- }
-
fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
@@ -351,7 +334,6 @@ mod test {
use datafusion_common::ScalarValue;
use datafusion_common::ScalarValue::Int64;
use datafusion_common::config::ConfigOptions;
- use datafusion_expr::interval_arithmetic::Interval;
use datafusion_expr::{
ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl,
};
@@ -364,71 +346,6 @@ mod test {
/// represent (`-262143-01-01T00:00:00`).
const MIN_UTC_SECONDS: i64 = -8334601228800;
- #[test]
- fn evaluate_bounds_preserves_output_type() {
- let seconds = Interval::make_unbounded(&DataType::Int64).unwrap();
-
- let function = FromUnixtimeFunc::default();
- let bounds = function.evaluate_bounds(&[&seconds]).unwrap();
- assert!(bounds.is_unbounded());
- assert_eq!(bounds.data_type(), DataType::Timestamp(Second, None));
-
- let mut options = ConfigOptions::default();
- options.execution.time_zone = Some("America/Denver".to_string());
- let function = FromUnixtimeFunc::new_with_config(&options);
- let bounds = function.evaluate_bounds(&[&seconds]).unwrap();
- assert_eq!(
- bounds.data_type(),
- DataType::Timestamp(Second, Some("America/Denver".into()))
- );
-
- let timezone = ScalarValue::Utf8(Some("+08:00".to_string()));
- let timezone = Interval::try_new(timezone.clone(), timezone).unwrap();
- let bounds = function.evaluate_bounds(&[&seconds, &timezone]).unwrap();
- assert_eq!(
- bounds.data_type(),
- DataType::Timestamp(Second, Some("+08:00".into()))
- );
- }
-
- #[test]
- fn evaluate_bounds_keeps_unknown_timezone_unknown() {
- let seconds = Interval::make_unbounded(&DataType::Int64).unwrap();
- let function = FromUnixtimeFunc::default();
-
- let unknown = Interval::make_unbounded(&DataType::Utf8).unwrap();
- assert_eq!(
- function
- .evaluate_bounds(&[&seconds, &unknown])
- .unwrap()
- .data_type(),
- DataType::Null
- );
-
- let varying = Interval::try_new(
- ScalarValue::Utf8(Some("+08:00".to_string())),
- ScalarValue::Utf8(Some("+09:00".to_string())),
- )
- .unwrap();
- assert_eq!(
- function
- .evaluate_bounds(&[&seconds, &varying])
- .unwrap()
- .data_type(),
- DataType::Null
- );
-
- let empty = ScalarValue::Utf8(Some(String::new()));
- let empty = Interval::try_new(empty.clone(), empty).unwrap();
- assert_eq!(
- function
- .evaluate_bounds(&[&seconds, &empty])
- .unwrap()
- .data_type(),
- DataType::Null
- );
- }
-
/// Invoke the single argument form with `datafusion.execution.time_zone`
/// set to `timezone`.
fn from_unixtime_session_tz(
diff --git a/datafusion/physical-expr/src/scalar_function.rs b/datafusion/physical-expr/src/scalar_function.rs
index 6a5ab219aa..2245f63eef 100644
--- a/datafusion/physical-expr/src/scalar_function.rs
+++ b/datafusion/physical-expr/src/scalar_function.rs
@@ -322,6 +322,13 @@ impl PhysicalExpr for ScalarFunctionExpr {
.map(|props| &props.range)
.collect::<Vec<_>>();
let range = self.fun().evaluate_bounds(&children_range)?;
+ // The default UDF bounds carry no type information. Recover the resolved
+ // output type for property inference without discarding explicit bounds.
+ let range = if range.data_type() == DataType::Null && range.is_unbounded() {
+ Interval::make_unbounded(self.return_type()).unwrap_or(range)
+ } else {
+ range
+ };
Ok(ExprProperties {
sort_properties,
@@ -357,14 +364,16 @@ impl PhysicalExpr for ScalarFunctionExpr {
mod tests {
use super::*;
use crate::expressions::Column;
- use arrow::datatypes::Field;
+ use arrow::datatypes::{Field, TimeUnit};
+ use datafusion_expr::sort_properties::SortProperties;
use datafusion_expr::{ScalarUDFImpl, Signature};
use datafusion_physical_expr_common::physical_expr::is_volatile;
- /// Test helper to create a mock UDF with a specific volatility
+ /// Test UDF with configurable volatility and return type, using default bounds.
#[derive(Debug, PartialEq, Eq, Hash)]
struct MockScalarUDF {
signature: Signature,
+ return_type: DataType,
}
impl ScalarUDFImpl for MockScalarUDF {
@@ -377,11 +386,226 @@ mod tests {
}
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
- Ok(DataType::Int32)
+ Ok(self.return_type.clone())
}
fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> {
- Ok(ColumnarValue::Scalar(ScalarValue::Int32(Some(42))))
+ Ok(ColumnarValue::Scalar(ScalarValue::try_from(
+ &self.return_type,
+ )?))
+ }
+ }
+
+ fn default_bounds_expr(return_type: DataType) -> ScalarFunctionExpr {
+ ScalarFunctionExpr::try_new(
+ Arc::new(ScalarUDF::from(MockScalarUDF {
+ signature: Signature::exact(vec![], Volatility::Immutable),
+ return_type,
+ })),
+ vec![],
+ &Schema::empty(),
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap()
+ }
+
+ #[test]
+ fn properties_recover_return_type_without_changing_bounds_evaluation() {
+ for data_type in [
+ DataType::Null,
+ DataType::Boolean,
+ DataType::Int32,
+ DataType::UInt64,
+ DataType::Float64,
+ DataType::Utf8,
+ DataType::Timestamp(TimeUnit::Second, None),
+ DataType::Timestamp(TimeUnit::Nanosecond, Some("America/Goose_Bay".into())),
+ DataType::Time32(TimeUnit::Millisecond),
+ DataType::Time64(TimeUnit::Microsecond),
+ DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
+ ] {
+ let expr = default_bounds_expr(data_type.clone());
+ let properties = expr.get_properties(&[]).unwrap();
+ assert_eq!(
+ properties.range,
+ Interval::make_unbounded(&data_type).unwrap()
+ );
+ assert_eq!(properties.sort_properties, SortProperties::Unordered);
+ assert!(!properties.preserves_lex_ordering);
+ assert!(!properties.strictly_order_preserving);
+ // The constraint solver calls this method directly, bypassing the fallback.
+ assert_eq!(
+ expr.evaluate_bounds(&[]).unwrap(),
+ Interval::make_unbounded(&DataType::Null).unwrap()
+ );
+ }
+ }
+
+ #[test]
+ fn properties_keep_unknown_bounds_for_unsupported_return_type() {
+ // This unit is not supported for Time32 by ScalarValue/Interval.
+ let data_type = DataType::Time32(TimeUnit::Nanosecond);
+ assert!(Interval::make_unbounded(&data_type).is_err());
+ let properties = default_bounds_expr(data_type).get_properties(&[]).unwrap();
+ assert_eq!(
+ properties.range,
+ Interval::make_unbounded(&DataType::Null).unwrap()
+ );
+ }
+
+ #[test]
+ fn datetime_properties_use_resolved_return_types() {
+ use datafusion_functions::datetime::date_bin::DateBinFunc;
+ use datafusion_functions::datetime::from_unixtime::FromUnixtimeFunc;
+
+ for source_type in [
+ DataType::Timestamp(TimeUnit::Second, None),
+ DataType::Timestamp(TimeUnit::Nanosecond, Some("UTC".into())),
+ DataType::Time32(TimeUnit::Millisecond),
+ DataType::Time64(TimeUnit::Microsecond),
+ ] {
+ let expr = ScalarFunctionExpr::try_new(
+ Arc::new(ScalarUDF::from(DateBinFunc::new())),
+ vec![
+ Arc::new(Literal::new(ScalarValue::new_interval_mdn(
+ 0,
+ 0,
+ 60_000_000_000,
+ ))),
+ Arc::new(Column::new("ts", 0)),
+ ],
+ &Schema::new(vec![Field::new("ts", source_type.clone(), true)]),
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap();
+ let properties = expr
+ .get_properties(&[
+ ExprProperties::new_unknown().with_order(SortProperties::Singleton),
+ ExprProperties::new_unknown()
+ .with_range(Interval::make_unbounded(&source_type).unwrap()),
+ ])
+ .unwrap();
+ assert_eq!(
+ properties.range,
+ Interval::make_unbounded(&source_type).unwrap()
+ );
+ }
+
+ for (session_tz, explicit_tz, expected_tz) in [
+ (None, None, None),
+ (Some("America/Denver"), None, Some("America/Denver")),
+ (
+ Some("America/Denver"),
+ Some("America/Goose_Bay"),
+ Some("America/Goose_Bay"),
+ ),
+ ] {
+ let mut config = ConfigOptions::default();
+ config.execution.time_zone = session_tz.map(str::to_owned);
+ let udf = FromUnixtimeFunc::new_with_config(&config);
+ let mut args: Vec<Arc<dyn PhysicalExpr>> =
+ vec![Arc::new(Column::new("c", 0))];
+ if let Some(tz) = explicit_tz {
+ args.push(Arc::new(Literal::new(ScalarValue::from(tz))));
+ }
+ let children = vec![ExprProperties::new_unknown(); args.len()];
+ let expr = ScalarFunctionExpr::try_new(
+ Arc::new(ScalarUDF::from(udf)),
+ args,
+ &Schema::new(vec![Field::new("c", DataType::Int64, true)]),
+ Arc::new(config),
+ )
+ .unwrap();
+ // The literal's timezone was resolved during construction, even
+ // though the child intervals here contain no value information.
+ let expected =
+ DataType::Timestamp(TimeUnit::Second, expected_tz.map(Into::into));
+ assert_eq!(
+ expr.get_properties(&children).unwrap().range,
+ Interval::make_unbounded(&expected).unwrap()
+ );
+ }
+ }
+
+ #[derive(Debug, PartialEq, Eq, Hash)]
+ struct BoundsUDF {
+ inner: MockScalarUDF,
+ fail: bool,
+ }
+
+ impl ScalarUDFImpl for BoundsUDF {
+ fn name(&self) -> &str {
+ "bounds_function"
+ }
+
+ fn signature(&self) -> &Signature {
+ self.inner.signature()
+ }
+
+ fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+ self.inner.return_type(arg_types)
+ }
+
+ fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
+ self.inner.invoke_with_args(args)
+ }
+
+ fn evaluate_bounds(&self, inputs: &[&Interval]) -> Result<Interval> {
+ if self.fail {
+ return internal_err!("bounds evaluation failed");
+ }
+ Ok(inputs[0].clone())
+ }
+ }
+
+ #[test]
+ fn properties_preserve_udf_bounds_and_errors() {
+ for fail in [false, true] {
+ let expr = ScalarFunctionExpr::try_new(
+ Arc::new(ScalarUDF::from(BoundsUDF {
+ inner: MockScalarUDF {
+ signature: Signature::exact(
+ vec![DataType::Int32],
+ Volatility::Immutable,
+ ),
+ return_type: DataType::Int32,
+ },
+ fail,
+ })),
+ vec![Arc::new(Column::new("a", 0))],
+ &Schema::new(vec![Field::new("a", DataType::Int32, true)]),
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap();
+ for (lower, upper) in [
+ (Some(0), Some(100)),
+ (Some(1), Some(1)),
+ (None, Some(100)),
+ (Some(0), None),
+ (None, None),
+ ] {
+ let bounds = Interval::make::<i32>(lower, upper).unwrap();
+ let child = ExprProperties::new_unknown().with_range(bounds.clone());
+ let properties = expr.get_properties(&[child]);
+ let direct = expr.evaluate_bounds(&[&bounds]);
+ if fail {
+ assert!(
+ properties
+ .unwrap_err()
+ .to_string()
+ .contains("bounds evaluation failed")
+ );
+ assert!(
+ direct
+ .unwrap_err()
+ .to_string()
+ .contains("bounds evaluation failed")
+ );
+ } else {
+ assert_eq!(properties.unwrap().range, bounds);
+ assert_eq!(direct.unwrap(), bounds);
+ }
+ }
}
}
@@ -389,6 +613,7 @@ mod tests {
fn test_scalar_function_volatile_node() {
// Create a volatile UDF
let volatile_udf = Arc::new(ScalarUDF::from(MockScalarUDF {
+ return_type: DataType::Int32,
signature: Signature::uniform(
1,
vec![DataType::Float32],
@@ -398,6 +623,7 @@ mod tests {
// Create a non-volatile UDF
let stable_udf = Arc::new(ScalarUDF::from(MockScalarUDF {
+ return_type: DataType::Int32,
signature: Signature::uniform(1, vec![DataType::Float32], Volatility::Stable),
}));
diff --git a/datafusion/sqllogictest/test_files/cast.slt b/datafusion/sqllogictest/test_files/cast.slt
index 1fd3851b81..e94869e830 100644
--- a/datafusion/sqllogictest/test_files/cast.slt
+++ b/datafusion/sqllogictest/test_files/cast.slt
@@ -180,6 +180,42 @@ physical_plan
statement ok
SET datafusion.explain.physical_plan_only = false;
+# Numeric UDFs with default bounds retain their output type through a cast.
+statement ok
+CREATE VIEW numeric_udf_ordering AS
+SELECT column1 AS x
+FROM (VALUES (2.5::DOUBLE), (-1.5::DOUBLE), (NULL::DOUBLE))
+ORDER BY x
+LIMIT 3;
+
+query R
+SELECT CAST(trunc(x) AS REAL) AS truncated
+FROM numeric_udf_ordering
+ORDER BY truncated;
+----
+-1
+2
+NULL
+
+statement ok
+SET datafusion.explain.physical_plan_only = true;
+
+query TT
+EXPLAIN SELECT CAST(trunc(x) AS REAL) AS truncated
+FROM numeric_udf_ordering
+ORDER BY truncated;
+----
+physical_plan
+01)ProjectionExec: expr=[CAST(trunc(column1@0) AS Float32) as truncated]
+02)--SortExec: TopK(fetch=3), expr=[column1@0 ASC NULLS LAST], preserve_partitioning=[false]
+03)----DataSourceExec: partitions=1, partition_sizes=[1]
+
+statement ok
+RESET datafusion.explain.physical_plan_only;
+
+statement ok
+DROP VIEW numeric_udf_ordering;
+
# Regression for https://github.com/apache/datafusion/issues/25523.
statement ok
SET datafusion.execution.target_partitions = 1;
diff --git a/datafusion/sqllogictest/test_files/datetime/arith_time_interval.slt b/datafusion/sqllogictest/test_files/datetime/arith_time_interval.slt
index 73eee70268..f9cc5858dd 100644
--- a/datafusion/sqllogictest/test_files/datetime/arith_time_interval.slt
+++ b/datafusion/sqllogictest/test_files/datetime/arith_time_interval.slt
@@ -158,3 +158,58 @@ NULL
statement ok
DROP TABLE time_vals
+
+# Time arithmetic can wrap even when its input is an ordered function result.
+statement ok
+SET datafusion.explain.physical_plan_only = true;
+
+statement ok
+CREATE VIEW function_time_ordering AS
+SELECT column1 AS t
+FROM (VALUES (TIME '23:30:00'), (TIME '00:30:00'))
+ORDER BY t
+LIMIT 2;
+
+query D
+SELECT date_trunc('hour', t) + INTERVAL '2 hours' AS shifted
+FROM function_time_ordering
+ORDER BY shifted;
+----
+01:00:00
+02:00:00
+
+query TT
+EXPLAIN SELECT date_trunc('hour', t) + INTERVAL '2 hours' AS shifted
+FROM function_time_ordering
+ORDER BY shifted;
+----
+physical_plan
+01)SortExec: expr=[shifted@0 ASC NULLS LAST], preserve_partitioning=[false]
+02)--ProjectionExec: expr=[date_trunc(hour, column1@0) + IntervalMonthDayNano { months: 0, days: 0, nanoseconds: 7200000000000 } as shifted]
+03)----SortExec: TopK(fetch=2), expr=[column1@0 ASC NULLS LAST], preserve_partitioning=[false]
+04)------DataSourceExec: partitions=1, partition_sizes=[1]
+
+query D
+SELECT date_trunc('hour', t) - INTERVAL '2 hours' AS shifted
+FROM function_time_ordering
+ORDER BY shifted;
+----
+21:00:00
+22:00:00
+
+query TT
+EXPLAIN SELECT date_trunc('hour', t) - INTERVAL '2 hours' AS shifted
+FROM function_time_ordering
+ORDER BY shifted;
+----
+physical_plan
+01)SortExec: expr=[shifted@0 ASC NULLS LAST], preserve_partitioning=[false]
+02)--ProjectionExec: expr=[date_trunc(hour, column1@0) - IntervalMonthDayNano { months: 0, days: 0, nanoseconds: 7200000000000 } as shifted]
+03)----SortExec: TopK(fetch=2), expr=[column1@0 ASC NULLS LAST], preserve_partitioning=[false]
+04)------DataSourceExec: partitions=1, partition_sizes=[1]
+
+statement ok
+DROP VIEW function_time_ordering;
+
+statement ok
+RESET datafusion.explain.physical_plan_only;
diff --git a/datafusion/sqllogictest/test_files/datetime/timestamps.slt b/datafusion/sqllogictest/test_files/datetime/timestamps.slt
index cc1b65db46..c11f4ea04f 100644
--- a/datafusion/sqllogictest/test_files/datetime/timestamps.slt
+++ b/datafusion/sqllogictest/test_files/datetime/timestamps.slt
@@ -1796,6 +1796,59 @@ ORDER BY b ASC NULLS LAST
NULL
NULL
+# Default bounds from nested functions retain their resolved output type.
+statement ok
+SET datafusion.explain.physical_plan_only = true;
+
+statement ok
+CREATE VIEW nested_trunc_ordering AS
+SELECT arrow_cast(column1, 'Timestamp(Second)') AS ts
+FROM (VALUES (90001::bigint), (3601::bigint))
+ORDER BY ts
+LIMIT 2;
+
+query I
+SELECT arrow_cast(date_trunc('day', date_trunc('hour', ts)), 'Int64') AS bucket
+FROM nested_trunc_ordering
+ORDER BY bucket;
+----
+0
+86400
+
+query TT
+EXPLAIN SELECT date_trunc('day', date_trunc('hour', ts)) AS bucket
+FROM nested_trunc_ordering
+ORDER BY bucket;
+----
+physical_plan
+01)ProjectionExec: expr=[date_trunc(day, date_trunc(hour, ts@0)) as bucket]
+02)--SortExec: TopK(fetch=2), expr=[ts@0 ASC NULLS LAST], preserve_partitioning=[false]
+03)----ProjectionExec: expr=[CAST(column1@0 AS Timestamp(s)) as ts]
+04)------DataSourceExec: partitions=1, partition_sizes=[1]
+
+statement ok
+DROP VIEW nested_trunc_ordering;
+
+# Recovering a named timezone must still retain the outer sort.
+query TT
+EXPLAIN SELECT date_trunc('hour', from_unixtime(c, 'America/Goose_Bay')) AS bucket
+FROM (
+ SELECT column1 AS c
+ FROM (VALUES (562129260::bigint), (562129259::bigint))
+ ORDER BY c
+ LIMIT 2
+)
+ORDER BY bucket;
+----
+physical_plan
+01)SortExec: expr=[bucket@0 ASC NULLS LAST], preserve_partitioning=[false]
+02)--ProjectionExec: expr=[date_trunc(hour, from_unixtime(column1@0, America/Goose_Bay)) as bucket]
+03)----SortExec: TopK(fetch=2), expr=[column1@0 ASC NULLS LAST], preserve_partitioning=[false]
+04)------DataSourceExec: partitions=1, partition_sizes=[1]
+
+statement ok
+RESET datafusion.explain.physical_plan_only;
+
statement ok
CREATE TABLE timestamp_strings(ts varchar)
AS VALUES