This is an automated email from the ASF dual-hosted git repository.

github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 62740802f6 Update `to_unixtime` udf function to support a consistent 
set of argument types (#19442)
62740802f6 is described below

commit 62740802f648aa3d425bdd201ceb8d6216acaf60
Author: Kumar Ujjawal <[email protected]>
AuthorDate: Sat Dec 27 05:21:31 2025 +0530

    Update `to_unixtime` udf function to support a consistent set of argument 
types (#19442)
    
    ## Which issue does this PR close?
    
    <!--
    We generally require a GitHub issue to be filed for all bug fixes and
    enhancements and this helps us generate change logs for our releases.
    You can link an issue to this PR using the GitHub syntax. For example
    `Closes #123` indicates that this PR will close issue #123.
    -->
    
    - Closes #19119
    
    ## Rationale for this change
    
    to_unixtime lacks the support for several data types.
    
    <!--
    Why are you proposing this change? If this is already explained clearly
    in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand
    your changes and offer better suggestions for fixes.
    -->
    
    ## What changes are included in this PR?
    
    Expanded `to_unixtime` support to all signed ints (Int8/16/32/64), all
    unsigned ints (UInt8/16/32/64), all floats (Float16/32/64), all UTF8
    variants (Utf8/Utf8View/LargeUtf8),
    
    <!--
    There is no need to duplicate the description in the issue here but it
    is sometimes worth providing a summary of the individual changes in this
    PR.
    -->
    
    ## Are these changes tested?
    
    Added sqllogictest
    
    <!--
    We typically require tests for all PRs in order to:
    1. Prevent the code from being accidentally broken by subsequent changes
    2. Serve as another way to document the expected behavior of the code
    
    If tests are not included in your PR, please explain why (for example,
    are they covered by existing tests)?
    -->
    
    ## Are there any user-facing changes?
    
    <!--
    If there are user-facing changes then we may require documentation to be
    updated before approving the PR.
    -->
    
    <!--
    If there are any breaking changes to public APIs, please add the `api
    change` label.
    -->
    
    ---------
    
    Co-authored-by: Martin Grigorov <[email protected]>
---
 datafusion/functions/src/datetime/mod.rs          |  2 +-
 datafusion/functions/src/datetime/to_unixtime.rs  | 38 +++++++++++----
 datafusion/sqllogictest/test_files/timestamps.slt | 58 +++++++++++++++++++++++
 docs/source/user-guide/sql/scalar_functions.md    |  2 +-
 4 files changed, 90 insertions(+), 10 deletions(-)

diff --git a/datafusion/functions/src/datetime/mod.rs 
b/datafusion/functions/src/datetime/mod.rs
index f9b4b5a3c9..6ebea3c1a5 100644
--- a/datafusion/functions/src/datetime/mod.rs
+++ b/datafusion/functions/src/datetime/mod.rs
@@ -108,7 +108,7 @@ pub mod expr_fn {
     ),
     (
         to_unixtime,
-        "converts a string and optional formats to a Unixtime",
+        "converts a value to seconds since the unix epoch",
         args,
     ),(
         to_timestamp,
diff --git a/datafusion/functions/src/datetime/to_unixtime.rs 
b/datafusion/functions/src/datetime/to_unixtime.rs
index a62ace9ccb..fb6315d8f0 100644
--- a/datafusion/functions/src/datetime/to_unixtime.rs
+++ b/datafusion/functions/src/datetime/to_unixtime.rs
@@ -27,7 +27,7 @@ use std::any::Any;
 
 #[user_doc(
     doc_section(label = "Time and Date Functions"),
-    description = "Converts a value to seconds since the unix epoch 
(`1970-01-01T00:00:00Z`). Supports strings, dates, timestamps and double types 
as input. Strings are parsed as RFC3339 (e.g. '2023-07-20T05:44:00') if no 
[Chrono 
formats](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) are 
provided.",
+    description = "Converts a value to seconds since the unix epoch 
(`1970-01-01T00:00:00`). Supports strings, dates, timestamps, integer, unsigned 
integer, and float types as input. Strings are parsed as RFC3339 (e.g. 
'2023-07-20T05:44:00') if no [Chrono 
formats](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) are 
provided. Integers, unsigned integers, and floats are interpreted as seconds 
since the unix epoch (`1970-01-01T00:00:00`).",
     syntax_example = "to_unixtime(expression[, ..., format_n])",
     sql_example = r#"
 ```sql
@@ -101,22 +101,44 @@ impl ScalarUDFImpl for ToUnixtimeFunc {
 
         // validate that any args after the first one are Utf8
         if arg_args.len() > 1 {
-            validate_data_types(arg_args, "to_unixtime")?;
+            // Format arguments only make sense for string inputs
+            match arg_args[0].data_type() {
+                DataType::Utf8View | DataType::LargeUtf8 | DataType::Utf8 => {
+                    validate_data_types(arg_args, "to_unixtime")?;
+                }
+                _ => {
+                    return exec_err!(
+                        "to_unixtime function only accepts format arguments 
with string input, got {} arguments",
+                        arg_args.len()
+                    );
+                }
+            }
         }
 
         match arg_args[0].data_type() {
-            DataType::Int32 | DataType::Int64 | DataType::Null | 
DataType::Float64 => {
-                arg_args[0].cast_to(&DataType::Int64, None)
-            }
+            DataType::Int8
+            | DataType::Int16
+            | DataType::Int32
+            | DataType::Int64
+            | DataType::UInt8
+            | DataType::UInt16
+            | DataType::UInt32
+            | DataType::UInt64
+            | DataType::Float16
+            | DataType::Float32
+            | DataType::Float64
+            | DataType::Null => arg_args[0].cast_to(&DataType::Int64, None),
             DataType::Date64 | DataType::Date32 => arg_args[0]
                 .cast_to(&DataType::Timestamp(TimeUnit::Second, None), None)?
                 .cast_to(&DataType::Int64, None),
             DataType::Timestamp(_, tz) => arg_args[0]
                 .cast_to(&DataType::Timestamp(TimeUnit::Second, tz), None)?
                 .cast_to(&DataType::Int64, None),
-            DataType::Utf8 => ToTimestampSecondsFunc::new()
-                .invoke_with_args(args)?
-                .cast_to(&DataType::Int64, None),
+            DataType::Utf8View | DataType::LargeUtf8 | DataType::Utf8 => {
+                ToTimestampSecondsFunc::new()
+                    .invoke_with_args(args)?
+                    .cast_to(&DataType::Int64, None)
+            }
             other => {
                 exec_err!("Unsupported data type {} for function to_unixtime", 
other)
             }
diff --git a/datafusion/sqllogictest/test_files/timestamps.slt 
b/datafusion/sqllogictest/test_files/timestamps.slt
index 67f44312e5..90accbce38 100644
--- a/datafusion/sqllogictest/test_files/timestamps.slt
+++ b/datafusion/sqllogictest/test_files/timestamps.slt
@@ -3526,6 +3526,64 @@ select to_unixtime(arrow_cast(1599523200.414, 
'Float64'));
 ----
 1599523200
 
+query I
+select to_unixtime(arrow_cast(-1, 'Int8'));
+----
+-1
+
+query I
+select to_unixtime(arrow_cast(null, 'Int8'));
+----
+NULL
+
+query I
+select to_unixtime(arrow_cast(1000, 'Int16'));
+----
+1000
+
+query I
+select to_unixtime(arrow_cast(255, 'UInt8'));
+----
+255
+
+query I
+select to_unixtime(arrow_cast(65535, 'UInt16'));
+----
+65535
+
+query I
+select to_unixtime(arrow_cast(1599523200, 'UInt32'));
+----
+1599523200
+
+query I
+select to_unixtime(arrow_cast(1599523200, 'UInt64'));
+----
+1599523200
+
+query error DataFusion error: Arrow error: Cast error: Can't cast value 
18446744073709551615 to type Int64
+select to_unixtime(arrow_cast(18446744073709551615, 'UInt64'));
+
+query I
+select to_unixtime(arrow_cast(1000.12, 'Float16'));
+----
+1000
+
+query I
+select to_unixtime(arrow_cast(1000.414, 'Float32'));
+----
+1000
+
+query I
+select to_unixtime(arrow_cast('2020-09-08T12:00:00+00:00', 'Utf8View'));
+----
+1599566400
+
+query I
+select to_unixtime(arrow_cast('2020-09-08T12:00:00+00:00', 'LargeUtf8'));
+----
+1599566400
+
 ##########
 ## Tests for the "AT TIME ZONE" clause
 ##########
diff --git a/docs/source/user-guide/sql/scalar_functions.md 
b/docs/source/user-guide/sql/scalar_functions.md
index e454788003..cf35b9f3c3 100644
--- a/docs/source/user-guide/sql/scalar_functions.md
+++ b/docs/source/user-guide/sql/scalar_functions.md
@@ -2982,7 +2982,7 @@ Additional examples can be found 
[here](https://github.com/apache/datafusion/blo
 
 ### `to_unixtime`
 
-Converts a value to seconds since the unix epoch (`1970-01-01T00:00:00Z`). 
Supports strings, dates, timestamps and double types as input. Strings are 
parsed as RFC3339 (e.g. '2023-07-20T05:44:00') if no [Chrono 
formats](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) are 
provided.
+Converts a value to seconds since the unix epoch (`1970-01-01T00:00:00`). 
Supports strings, dates, timestamps, integer, unsigned integer, and float types 
as input. Strings are parsed as RFC3339 (e.g. '2023-07-20T05:44:00') if no 
[Chrono 
formats](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) are 
provided. Integers, unsigned integers, and floats are interpreted as seconds 
since the unix epoch (`1970-01-01T00:00:00`).
 
 ```sql
 to_unixtime(expression[, ..., format_n])


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to