Skip to main content

datafusion_functions/datetime/
to_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 super::to_timestamp::ToTimestampSecondsFunc;
19use crate::datetime::common::*;
20use arrow::datatypes::{DataType, TimeUnit};
21use datafusion_common::{Result, exec_err};
22use datafusion_expr::{
23    ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
24};
25use datafusion_macros::user_doc;
26use std::any::Any;
27
28#[user_doc(
29    doc_section(label = "Time and Date Functions"),
30    description = r#"
31Converts a value to seconds since the unix epoch (`1970-01-01T00:00:00`).
32Supports strings, dates, timestamps, integer, unsigned integer, and float types as input.
33Strings are parsed as RFC3339 (e.g. '2023-07-20T05:44:00')
34if no [Chrono formats](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) are provided.
35Integers, unsigned integers, and floats are interpreted as seconds since the unix epoch (`1970-01-01T00:00:00`)."#,
36    syntax_example = "to_unixtime(expression[, ..., format_n])",
37    sql_example = r#"
38```sql
39> select to_unixtime('2020-09-08T12:00:00+00:00');
40+------------------------------------------------+
41| to_unixtime(Utf8("2020-09-08T12:00:00+00:00")) |
42+------------------------------------------------+
43| 1599566400                                     |
44+------------------------------------------------+
45> select to_unixtime('01-14-2023 01:01:30+05:30', '%q', '%d-%m-%Y %H/%M/%S', '%+', '%m-%d-%Y %H:%M:%S%#z');
46+-----------------------------------------------------------------------------------------------------------------------------+
47| to_unixtime(Utf8("01-14-2023 01:01:30+05:30"),Utf8("%q"),Utf8("%d-%m-%Y %H/%M/%S"),Utf8("%+"),Utf8("%m-%d-%Y %H:%M:%S%#z")) |
48+-----------------------------------------------------------------------------------------------------------------------------+
49| 1673638290                                                                                                                  |
50+-----------------------------------------------------------------------------------------------------------------------------+
51```
52"#,
53    argument(
54        name = "expression",
55        description = "Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators."
56    ),
57    argument(
58        name = "format_n",
59        description = "Optional [Chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) strings to use to parse the expression. Formats will be tried in the order they appear with the first successful one being returned. If none of the formats successfully parse the expression an error will be returned."
60    )
61)]
62#[derive(Debug, PartialEq, Eq, Hash)]
63pub struct ToUnixtimeFunc {
64    signature: Signature,
65}
66
67impl Default for ToUnixtimeFunc {
68    fn default() -> Self {
69        Self::new()
70    }
71}
72
73impl ToUnixtimeFunc {
74    pub fn new() -> Self {
75        Self {
76            signature: Signature::variadic_any(Volatility::Immutable),
77        }
78    }
79}
80
81impl ScalarUDFImpl for ToUnixtimeFunc {
82    fn as_any(&self) -> &dyn Any {
83        self
84    }
85
86    fn name(&self) -> &str {
87        "to_unixtime"
88    }
89
90    fn signature(&self) -> &Signature {
91        &self.signature
92    }
93
94    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
95        Ok(DataType::Int64)
96    }
97
98    fn invoke_with_args(
99        &self,
100        args: datafusion_expr::ScalarFunctionArgs,
101    ) -> Result<ColumnarValue> {
102        let arg_args = &args.args;
103        if arg_args.is_empty() {
104            return exec_err!("to_unixtime function requires 1 or more arguments, got 0");
105        }
106
107        // validate that any args after the first one are Utf8
108        if arg_args.len() > 1 {
109            // Format arguments only make sense for string inputs
110            match arg_args[0].data_type() {
111                DataType::Utf8View | DataType::LargeUtf8 | DataType::Utf8 => {
112                    validate_data_types(arg_args, "to_unixtime")?;
113                }
114                _ => {
115                    return exec_err!(
116                        "to_unixtime function only accepts format arguments with string input, got {} arguments",
117                        arg_args.len()
118                    );
119                }
120            }
121        }
122
123        match arg_args[0].data_type() {
124            DataType::Int8
125            | DataType::Int16
126            | DataType::Int32
127            | DataType::Int64
128            | DataType::UInt8
129            | DataType::UInt16
130            | DataType::UInt32
131            | DataType::UInt64
132            | DataType::Float16
133            | DataType::Float32
134            | DataType::Float64
135            | DataType::Null => arg_args[0].cast_to(&DataType::Int64, None),
136            DataType::Date64 | DataType::Date32 => arg_args[0]
137                .cast_to(&DataType::Timestamp(TimeUnit::Second, None), None)?
138                .cast_to(&DataType::Int64, None),
139            DataType::Timestamp(_, tz) => arg_args[0]
140                .cast_to(&DataType::Timestamp(TimeUnit::Second, tz), None)?
141                .cast_to(&DataType::Int64, None),
142            DataType::Utf8View | DataType::LargeUtf8 | DataType::Utf8 => {
143                ToTimestampSecondsFunc::new_with_config(args.config_options.as_ref())
144                    .invoke_with_args(args)?
145                    .cast_to(&DataType::Int64, None)
146            }
147            other => {
148                exec_err!("Unsupported data type {} for function to_unixtime", other)
149            }
150        }
151    }
152
153    fn documentation(&self) -> Option<&Documentation> {
154        self.doc()
155    }
156}