|
16 | 16 | // under the License. |
17 | 17 |
|
18 | 18 | use std::ops::ControlFlow; |
| 19 | +use std::sync::Arc; |
19 | 20 |
|
20 | 21 | use arrow::datatypes::{DataType, TimeUnit}; |
21 | 22 | use datafusion_expr::planner::{ |
@@ -750,24 +751,12 @@ impl<S: ContextProvider> SqlToRel<'_, S> { |
750 | 751 | SQLExpr::AtTimeZone { |
751 | 752 | timestamp, |
752 | 753 | time_zone, |
753 | | - } => Ok(Expr::Cast(Cast::new( |
754 | | - Box::new(self.sql_expr_to_logical_expr_internal( |
755 | | - *timestamp, |
756 | | - schema, |
757 | | - planner_context, |
758 | | - )?), |
759 | | - match *time_zone { |
760 | | - SQLExpr::Value(ValueWithSpan { |
761 | | - value: Value::SingleQuotedString(s), |
762 | | - span: _, |
763 | | - }) => DataType::Timestamp(TimeUnit::Nanosecond, Some(s.into())), |
764 | | - _ => { |
765 | | - return not_impl_err!( |
766 | | - "Unsupported ast node in sqltorel: {time_zone:?}" |
767 | | - ); |
768 | | - } |
769 | | - }, |
770 | | - ))), |
| 754 | + } => self.sql_at_time_zone_to_expr( |
| 755 | + *timestamp, |
| 756 | + *time_zone, |
| 757 | + schema, |
| 758 | + planner_context, |
| 759 | + ), |
771 | 760 | SQLExpr::Dictionary(fields) => { |
772 | 761 | self.try_plan_dictionary_literal(fields, schema, planner_context) |
773 | 762 | } |
@@ -905,6 +894,86 @@ impl<S: ContextProvider> SqlToRel<'_, S> { |
905 | 894 | } |
906 | 895 | } |
907 | 896 |
|
| 897 | + /// Plan `<timestamp> AT TIME ZONE '<tz>'`. |
| 898 | + /// |
| 899 | + /// The meaning of `AT TIME ZONE` depends on whether its input carries a |
| 900 | + /// timezone, and it always returns the *other* kind of timestamp. This |
| 901 | + /// follows PostgreSQL (and DuckDB): |
| 902 | + /// |
| 903 | + /// * a timezone-**naive** input is read as a wall clock in `tz`, and the |
| 904 | + /// result is the corresponding timezone-**aware** instant. That is a |
| 905 | + /// plain `CAST(expr AS Timestamp(unit, Some(tz)))`, because arrow's |
| 906 | + /// `Timestamp(_, None) -> Timestamp(_, Some(tz))` cast interprets the |
| 907 | + /// naive value as local time in `tz`. |
| 908 | + /// * a timezone-**aware** input is an instant, and the result is the wall |
| 909 | + /// clock that instant has in `tz`, as a timezone-**naive** timestamp. |
| 910 | + /// The same cast is still the first half of that (casting between two |
| 911 | + /// aware types preserves the instant and only relabels the zone); the |
| 912 | + /// second half — dropping the zone while keeping the displayed value — |
| 913 | + /// is delegated to [`ExprPlanner::plan_at_time_zone`], which |
| 914 | + /// `datafusion-functions` implements with `to_local_time`. |
| 915 | + /// |
| 916 | + /// Anything that is not a timestamp (a string literal, for instance) takes |
| 917 | + /// the naive path, since a `CAST` to a timezone-aware timestamp is the |
| 918 | + /// natural reading of `AT TIME ZONE` for it. |
| 919 | + /// |
| 920 | + /// [`ExprPlanner::plan_at_time_zone`]: datafusion_expr::planner::ExprPlanner::plan_at_time_zone |
| 921 | + fn sql_at_time_zone_to_expr( |
| 922 | + &self, |
| 923 | + timestamp: SQLExpr, |
| 924 | + time_zone: SQLExpr, |
| 925 | + schema: &DFSchema, |
| 926 | + planner_context: &mut PlannerContext, |
| 927 | + ) -> Result<Expr> { |
| 928 | + let tz: Arc<str> = match time_zone { |
| 929 | + SQLExpr::Value(ValueWithSpan { |
| 930 | + value: Value::SingleQuotedString(s), |
| 931 | + span: _, |
| 932 | + }) => s.into(), |
| 933 | + _ => { |
| 934 | + return not_impl_err!("Unsupported ast node in sqltorel: {time_zone:?}"); |
| 935 | + } |
| 936 | + }; |
| 937 | + |
| 938 | + let expr = |
| 939 | + self.sql_expr_to_logical_expr_internal(timestamp, schema, planner_context)?; |
| 940 | + |
| 941 | + // `AT TIME ZONE` does not change the precision of its input, so keep |
| 942 | + // the input's `TimeUnit` when it has one. |
| 943 | + let (unit, input_is_tz_aware) = match expr.get_type(schema)? { |
| 944 | + DataType::Timestamp(unit, tz) => (unit, tz.is_some()), |
| 945 | + _ => (TimeUnit::Nanosecond, false), |
| 946 | + }; |
| 947 | + |
| 948 | + // Instant-preserving relabel into `tz` for an aware input; local-time |
| 949 | + // interpretation for a naive one. |
| 950 | + let relabeled = Expr::Cast(Cast::new( |
| 951 | + Box::new(expr), |
| 952 | + DataType::Timestamp(unit, Some(tz)), |
| 953 | + )); |
| 954 | + |
| 955 | + if !input_is_tz_aware { |
| 956 | + return Ok(relabeled); |
| 957 | + } |
| 958 | + |
| 959 | + let mut args = vec![relabeled]; |
| 960 | + for planner in self.context_provider.get_expr_planners() { |
| 961 | + match planner.plan_at_time_zone(args)? { |
| 962 | + PlannerResult::Planned(expr) => return Ok(expr), |
| 963 | + PlannerResult::Original(original) => { |
| 964 | + args = original; |
| 965 | + } |
| 966 | + } |
| 967 | + } |
| 968 | + |
| 969 | + plan_err!( |
| 970 | + "AT TIME ZONE on a timezone-aware timestamp is not supported by any \ |
| 971 | + ExprPlanner. It needs the `to_local_time` function; register \ |
| 972 | + `datafusion_functions::datetime` (or its `DatetimeFunctionPlanner`) \ |
| 973 | + with the session" |
| 974 | + ) |
| 975 | + } |
| 976 | + |
908 | 977 | fn sql_position_to_expr( |
909 | 978 | &self, |
910 | 979 | substr_expr: SQLExpr, |
|
0 commit comments