datafusion_functions/datetime/
to_unixtime.rs1use 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, ScalarFunctionArgs, ScalarUDFImpl, Signature,
24 Volatility,
25};
26use datafusion_macros::user_doc;
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 name(&self) -> &str {
83 "to_unixtime"
84 }
85
86 fn signature(&self) -> &Signature {
87 &self.signature
88 }
89
90 fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
91 Ok(DataType::Int64)
92 }
93
94 fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
95 let arg_args = &args.args;
96 if arg_args.is_empty() {
97 return exec_err!("to_unixtime function requires 1 or more arguments, got 0");
98 }
99
100 if arg_args.len() > 1 {
102 match arg_args[0].data_type() {
104 DataType::Utf8View | DataType::LargeUtf8 | DataType::Utf8 => {
105 validate_data_types(arg_args, "to_unixtime")?;
106 }
107 _ => {
108 return exec_err!(
109 "to_unixtime function only accepts format arguments with string input, got {} arguments",
110 arg_args.len()
111 );
112 }
113 }
114 }
115
116 match arg_args[0].data_type() {
117 DataType::Int8
118 | DataType::Int16
119 | DataType::Int32
120 | DataType::Int64
121 | DataType::UInt8
122 | DataType::UInt16
123 | DataType::UInt32
124 | DataType::UInt64
125 | DataType::Float16
126 | DataType::Float32
127 | DataType::Float64
128 | DataType::Null => arg_args[0].cast_to(&DataType::Int64, None),
129 DataType::Date64 | DataType::Date32 => arg_args[0]
130 .cast_to(&DataType::Timestamp(TimeUnit::Second, None), None)?
131 .cast_to(&DataType::Int64, None),
132 DataType::Timestamp(_, tz) => arg_args[0]
133 .cast_to(&DataType::Timestamp(TimeUnit::Second, tz), None)?
134 .cast_to(&DataType::Int64, None),
135 DataType::Utf8View | DataType::LargeUtf8 | DataType::Utf8 => {
136 ToTimestampSecondsFunc::new_with_config(args.config_options.as_ref())
137 .invoke_with_args(args)?
138 .cast_to(&DataType::Int64, None)
139 }
140 other => {
141 exec_err!("Unsupported data type {} for function to_unixtime", other)
142 }
143 }
144 }
145
146 fn documentation(&self) -> Option<&Documentation> {
147 self.doc()
148 }
149}