Skip to main content

datafusion_functions/datetime/
from_unixtime.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::sync::Arc;
19
20use arrow::datatypes::DataType::{Int64, Timestamp, Utf8};
21use arrow::datatypes::TimeUnit::Second;
22use arrow::datatypes::{DataType, Field, FieldRef};
23use datafusion_common::{Result, ScalarValue, exec_err, internal_err};
24use datafusion_expr::TypeSignature::Exact;
25use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
26use datafusion_expr::{
27    ColumnarValue, Documentation, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl,
28    Signature, Volatility,
29};
30use datafusion_macros::user_doc;
31
32#[user_doc(
33    doc_section(label = "Time and Date Functions"),
34    description = "Converts an integer to RFC3339 timestamp format (`YYYY-MM-DDT00:00:00.000000000Z`). Integers and unsigned integers are interpreted as seconds since the unix epoch (`1970-01-01T00:00:00Z`) return the corresponding timestamp.",
35    syntax_example = "from_unixtime(expression[, timezone])",
36    sql_example = r#"```sql
37> select from_unixtime(1599572549, 'America/New_York');
38+-----------------------------------------------------------+
39| from_unixtime(Int64(1599572549),Utf8("America/New_York")) |
40+-----------------------------------------------------------+
41| 2020-09-08T09:42:29-04:00                                 |
42+-----------------------------------------------------------+
43```"#,
44    standard_argument(name = "expression",),
45    argument(
46        name = "timezone",
47        description = "Optional timezone to use when converting the integer to a timestamp. If not provided, the default timezone is UTC."
48    )
49)]
50#[derive(Debug, PartialEq, Eq, Hash)]
51pub struct FromUnixtimeFunc {
52    signature: Signature,
53}
54
55impl Default for FromUnixtimeFunc {
56    fn default() -> Self {
57        Self::new()
58    }
59}
60
61impl FromUnixtimeFunc {
62    pub fn new() -> Self {
63        Self {
64            signature: Signature::one_of(
65                vec![Exact(vec![Int64, Utf8]), Exact(vec![Int64])],
66                Volatility::Immutable,
67            ),
68        }
69    }
70}
71
72impl ScalarUDFImpl for FromUnixtimeFunc {
73    fn name(&self) -> &str {
74        "from_unixtime"
75    }
76
77    fn signature(&self) -> &Signature {
78        &self.signature
79    }
80
81    fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
82        // Length check handled in the signature
83        debug_assert!(matches!(args.scalar_arguments.len(), 1 | 2));
84
85        if args.scalar_arguments.len() == 1 {
86            Ok(Field::new(self.name(), Timestamp(Second, None), true).into())
87        } else {
88            args.scalar_arguments[1]
89                .and_then(|sv| {
90                    sv.try_as_str()
91                        .flatten()
92                        .filter(|s| !s.is_empty())
93                        .map(|tz| {
94                            Field::new(
95                                self.name(),
96                                Timestamp(Second, Some(Arc::from(tz.to_string()))),
97                                true,
98                            )
99                        })
100                })
101                .map(Arc::new)
102                .map_or_else(
103                    || {
104                        exec_err!(
105                            "{} requires its second argument to be a constant string",
106                            self.name()
107                        )
108                    },
109                    Ok,
110                )
111        }
112    }
113
114    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
115        internal_err!("call return_field_from_args instead")
116    }
117
118    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
119        let args = args.args;
120        let len = args.len();
121        if len != 1 && len != 2 {
122            return exec_err!(
123                "from_unixtime function requires 1 or 2 argument, got {}",
124                args.len()
125            );
126        }
127
128        if args[0].data_type() != Int64 {
129            return exec_err!(
130                "Unsupported data type {} for function from_unixtime",
131                args[0].data_type()
132            );
133        }
134
135        match len {
136            1 => args[0].cast_to(&Timestamp(Second, None), None),
137            2 => match &args[1] {
138                ColumnarValue::Scalar(ScalarValue::Utf8(Some(tz))) => args[0]
139                    .cast_to(&Timestamp(Second, Some(Arc::from(tz.to_string()))), None),
140                _ => {
141                    exec_err!(
142                        "Unsupported data type {} for function from_unixtime",
143                        args[1].data_type()
144                    )
145                }
146            },
147            _ => unreachable!(),
148        }
149    }
150
151    fn output_ordering(&self, inputs: &[ExprProperties]) -> Result<SortProperties> {
152        // The optional timezone argument must be a constant string and only
153        // affects the display metadata, not the stored epoch value, so the
154        // output ordering follows the first argument.
155        Ok(inputs[0].sort_properties)
156    }
157
158    fn preserves_lex_ordering(&self, _inputs: &[ExprProperties]) -> Result<bool> {
159        Ok(true)
160    }
161
162    fn strictly_order_preserving(&self, _inputs: &[ExprProperties]) -> Result<bool> {
163        // `from_unixtime` stores the input's exact `Int64` value as a
164        // `Timestamp(Second)`: the mapping is one-to-one, order-preserving,
165        // and maps nulls to nulls.
166        Ok(true)
167    }
168
169    fn documentation(&self) -> Option<&Documentation> {
170        self.doc()
171    }
172}
173
174#[cfg(test)]
175mod test {
176    use crate::datetime::from_unixtime::FromUnixtimeFunc;
177    use arrow::datatypes::TimeUnit::Second;
178    use arrow::datatypes::{DataType, Field};
179    use datafusion_common::ScalarValue;
180    use datafusion_common::ScalarValue::Int64;
181    use datafusion_common::config::ConfigOptions;
182    use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl};
183    use std::sync::Arc;
184
185    #[test]
186    fn test_without_timezone() {
187        let arg_field = Arc::new(Field::new("a", DataType::Int64, true));
188        let args = ScalarFunctionArgs {
189            args: vec![ColumnarValue::Scalar(Int64(Some(1729900800)))],
190            arg_fields: vec![arg_field],
191            number_rows: 1,
192            return_field: Field::new("f", DataType::Timestamp(Second, None), true).into(),
193            config_options: Arc::new(ConfigOptions::default()),
194        };
195        let result = FromUnixtimeFunc::new().invoke_with_args(args).unwrap();
196
197        match result {
198            ColumnarValue::Scalar(ScalarValue::TimestampSecond(Some(sec), None)) => {
199                assert_eq!(sec, 1729900800);
200            }
201            _ => panic!("Expected scalar value"),
202        }
203    }
204
205    #[test]
206    fn test_with_timezone() {
207        let arg_fields = vec![
208            Field::new("a", DataType::Int64, true).into(),
209            Field::new("a", DataType::Utf8, true).into(),
210        ];
211        let args = ScalarFunctionArgs {
212            args: vec![
213                ColumnarValue::Scalar(Int64(Some(1729900800))),
214                ColumnarValue::Scalar(ScalarValue::Utf8(Some(
215                    "America/New_York".to_string(),
216                ))),
217            ],
218            arg_fields,
219            number_rows: 2,
220            return_field: Field::new(
221                "f",
222                DataType::Timestamp(Second, Some(Arc::from("America/New_York"))),
223                true,
224            )
225            .into(),
226            config_options: Arc::new(ConfigOptions::default()),
227        };
228        let result = FromUnixtimeFunc::new().invoke_with_args(args).unwrap();
229
230        match result {
231            ColumnarValue::Scalar(ScalarValue::TimestampSecond(Some(sec), Some(tz))) => {
232                assert_eq!(sec, 1729900800);
233                assert_eq!(tz.to_string(), "America/New_York");
234            }
235            _ => panic!("Expected scalar value"),
236        }
237    }
238}