Skip to content

Commit

Permalink
[task apache#5568] add_to_unixtime_function
Browse files Browse the repository at this point in the history
Signed-off-by: tangruilin <[email protected]>
  • Loading branch information
Tangruilin committed Mar 1, 2024
1 parent 10d5f2d commit b46f0d8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions datafusion/expr/src/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ pub enum BuiltinScalarFunction {
FromUnixtime,
/// to_date
ToDate,
/// to_unixtime
ToUnixtime,
///now
Now,
///current_date
Expand Down Expand Up @@ -475,6 +477,7 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::Struct => Volatility::Immutable,
BuiltinScalarFunction::FromUnixtime => Volatility::Immutable,
BuiltinScalarFunction::ToDate => Volatility::Immutable,
BuiltinScalarFunction::ToUnixtime => Volatility::Immutable,
BuiltinScalarFunction::ArrowTypeof => Volatility::Immutable,
BuiltinScalarFunction::OverLay => Volatility::Immutable,
BuiltinScalarFunction::Levenshtein => Volatility::Immutable,
Expand Down Expand Up @@ -785,6 +788,7 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::ToTimestampSeconds => Ok(Timestamp(Second, None)),
BuiltinScalarFunction::FromUnixtime => Ok(Timestamp(Second, None)),
BuiltinScalarFunction::ToDate => Ok(Date32),
BuiltinScalarFunction::ToUnixtime => Ok(Int64),
BuiltinScalarFunction::Now => {
Ok(Timestamp(Nanosecond, Some("+00:00".into())))
}
Expand Down Expand Up @@ -1063,6 +1067,9 @@ impl BuiltinScalarFunction {
Signature::uniform(1, vec![Int64], self.volatility())
}
BuiltinScalarFunction::ToDate => Signature::variadic_any(self.volatility()),
BuiltinScalarFunction::ToUnixtime => {
Signature::variadic_any(self.volatility())
}
BuiltinScalarFunction::Digest => Signature::one_of(
vec![
Exact(vec![Utf8, Utf8]),
Expand Down Expand Up @@ -1496,6 +1503,7 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::ToTimestampNanos => &["to_timestamp_nanos"],
BuiltinScalarFunction::FromUnixtime => &["from_unixtime"],
BuiltinScalarFunction::ToDate => &["to_date"],
BuiltinScalarFunction::ToUnixtime => &["to_unixtime"],

// hashing functions
BuiltinScalarFunction::Digest => &["digest"],
Expand Down
26 changes: 26 additions & 0 deletions datafusion/physical-expr/src/datetime_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ use datafusion_common::{
ScalarValue,
};
use datafusion_expr::ColumnarValue;
use petgraph::visit::Data;
use uuid::timestamp;

use crate::expressions::cast_column;

Expand Down Expand Up @@ -1878,6 +1880,30 @@ pub fn from_unixtime_invoke(args: &[ColumnarValue]) -> Result<ColumnarValue> {
}
}

/// to_unixtime() SQL function implementation
pub fn to_unixtime_invoke(args: &[ColumnarValue]) -> Result<ColumnarValue> {
if args.len() != 1 {
return exec_err!(
"to_unixtime function require 1 argument, get {}",
args.len()
);
}

match args[0].data_type() {
DataType::Null | DataType::Int32 | DataType::Int64 => Ok(args[0].clone()),
DataType::Timestamp(_, None) => cast_column(&args[0], &DataType::Int64, None),
DataType::Utf8 => {
cast_column(&to_timestamp_seconds(args)?, &DataType::Int64, None)
}
other => {
exec_err!(
"Unsupported data type {:?} for function to_unixtime function",
other
)
}
}
}

#[cfg(test)]
mod tests {
use std::sync::Arc;
Expand Down
3 changes: 3 additions & 0 deletions datafusion/physical-expr/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ pub fn create_physical_fun(
BuiltinScalarFunction::FromUnixtime => {
Arc::new(datetime_expressions::from_unixtime_invoke)
}
BuiltinScalarFunction::ToUnixtime => {
Arc::new(datetime_expressions::to_unixtime_invoke)
}
BuiltinScalarFunction::ToDate => Arc::new(datetime_expressions::to_date_invoke),
BuiltinScalarFunction::InitCap => Arc::new(|args| match args[0].data_type() {
DataType::Utf8 => {
Expand Down
2 changes: 2 additions & 0 deletions datafusion/proto/proto/datafusion.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand Down Expand Up @@ -684,6 +685,7 @@ enum ScalarFunction {
RegexpLike = 135;
ToChar = 136;
ToDate = 137;
ToUnixtime = 138;
}

message ScalarFunctionNode {
Expand Down

0 comments on commit b46f0d8

Please sign in to comment.