Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor: Avoid some repetition in to_timestamp #11116

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 16 additions & 30 deletions datafusion/functions/src/datetime/to_timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use std::any::Any;
use arrow::datatypes::DataType::Timestamp;
use arrow::datatypes::TimeUnit::{Microsecond, Millisecond, Nanosecond, Second};
use arrow::datatypes::{
ArrowTimestampType, DataType, TimestampMicrosecondType, TimestampMillisecondType,
TimestampNanosecondType, TimestampSecondType,
ArrowTimestampType, DataType, TimeUnit, TimestampMicrosecondType,
TimestampMillisecondType, TimestampNanosecondType, TimestampSecondType,
};

use datafusion_common::{exec_err, Result, ScalarType};
Expand Down Expand Up @@ -144,12 +144,7 @@ impl ScalarUDFImpl for ToTimestampFunc {
}

fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
match &arg_types[0] {
DataType::Timestamp(_, Some(tz)) => {
Ok(Timestamp(Nanosecond, Some(tz.clone())))
}
_ => Ok(Timestamp(Nanosecond, None)),
}
Ok(return_type_for(&arg_types[0], Nanosecond))
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
Expand Down Expand Up @@ -202,10 +197,7 @@ impl ScalarUDFImpl for ToTimestampSecondsFunc {
}

fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
match &arg_types[0] {
DataType::Timestamp(_, Some(tz)) => Ok(Timestamp(Second, Some(tz.clone()))),
_ => Ok(Timestamp(Second, None)),
}
Ok(return_type_for(&arg_types[0], Second))
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
Expand Down Expand Up @@ -255,12 +247,7 @@ impl ScalarUDFImpl for ToTimestampMillisFunc {
}

fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
match &arg_types[0] {
DataType::Timestamp(_, Some(tz)) => {
Ok(Timestamp(Millisecond, Some(tz.clone())))
}
_ => Ok(Timestamp(Millisecond, None)),
}
Ok(return_type_for(&arg_types[0], Millisecond))
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
Expand Down Expand Up @@ -310,12 +297,7 @@ impl ScalarUDFImpl for ToTimestampMicrosFunc {
}

fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
match &arg_types[0] {
DataType::Timestamp(_, Some(tz)) => {
Ok(Timestamp(Microsecond, Some(tz.clone())))
}
_ => Ok(Timestamp(Microsecond, None)),
}
Ok(return_type_for(&arg_types[0], Microsecond))
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
Expand Down Expand Up @@ -365,12 +347,7 @@ impl ScalarUDFImpl for ToTimestampNanosFunc {
}

fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
match &arg_types[0] {
DataType::Timestamp(_, Some(tz)) => {
Ok(Timestamp(Nanosecond, Some(tz.clone())))
}
_ => Ok(Timestamp(Nanosecond, None)),
}
Ok(return_type_for(&arg_types[0], Nanosecond))
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
Expand Down Expand Up @@ -406,6 +383,15 @@ impl ScalarUDFImpl for ToTimestampNanosFunc {
}
}

/// Returns the return type for the to_timestamp_* function, preserving
/// the timezone if it exists.
fn return_type_for(arg: &DataType, unit: TimeUnit) -> DataType {
match arg {
Timestamp(_, Some(tz)) => Timestamp(unit, Some(tz.clone())),
_ => Timestamp(unit, None),
}
}

fn to_timestamp_impl<T: ArrowTimestampType + ScalarType<i64>>(
args: &[ColumnarValue],
name: &str,
Expand Down