Skip to content
Open
Show file tree
Hide file tree
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
101 changes: 101 additions & 0 deletions datafusion/functions/src/datetime/to_timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use arrow::datatypes::{
};
use datafusion_common::config::ConfigOptions;
use datafusion_common::{Result, ScalarType, ScalarValue, exec_datafusion_err, exec_err};
use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
use datafusion_expr::{
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl,
Signature, Volatility,
Expand Down Expand Up @@ -581,6 +582,16 @@ impl ScalarUDFImpl for ToTimestampSecondsFunc {
}
}

fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
if let [value] = input
&& value.range.data_type().is_integer()
{
Ok(value.sort_properties)
} else {
Ok(SortProperties::Unordered)
}
}

fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
Expand Down Expand Up @@ -650,6 +661,16 @@ impl ScalarUDFImpl for ToTimestampMillisFunc {
}
}

fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
if let [value] = input
&& value.range.data_type().is_integer()
{
Ok(value.sort_properties)
} else {
Ok(SortProperties::Unordered)
}
}

fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
Expand Down Expand Up @@ -719,6 +740,16 @@ impl ScalarUDFImpl for ToTimestampMicrosFunc {
}
}

fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
if let [value] = input
&& value.range.data_type().is_integer()
{
Ok(value.sort_properties)
} else {
Ok(SortProperties::Unordered)
}
}

fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
Expand Down Expand Up @@ -788,6 +819,16 @@ impl ScalarUDFImpl for ToTimestampNanosFunc {
}
}

fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
if let [value] = input
&& value.range.data_type().is_integer()
{
Ok(value.sort_properties)
} else {
Ok(SortProperties::Unordered)
}
}

fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
Expand Down Expand Up @@ -852,6 +893,66 @@ mod tests {

use super::*;

#[test]
fn integer_timestamp_ordering() -> Result<()> {
use arrow::compute::SortOptions;
use datafusion_expr::interval_arithmetic::Interval;

let config = ConfigOptions::default();
let functions: [Box<dyn ScalarUDFImpl>; 4] = [
Box::new(ToTimestampSecondsFunc::new_with_config(&config)),
Box::new(ToTimestampMillisFunc::new_with_config(&config)),
Box::new(ToTimestampMicrosFunc::new_with_config(&config)),
Box::new(ToTimestampNanosFunc::new_with_config(&config)),
];
for function in functions {
for descending in [false, true] {
for nulls_first in [false, true] {
let ordering = SortProperties::Ordered(SortOptions {
descending,
nulls_first,
});
for (data_type, supported) in [
(Int8, true),
(Int16, true),
(Int32, true),
(Int64, true),
(UInt8, true),
(UInt16, true),
(UInt32, true),
(UInt64, true),
(Float64, false),
(Decimal128(20, 0), false),
(Date32, false),
(Date64, false),
(Utf8, false),
(Null, false),
(Timestamp(Microsecond, None), false),
] {
let input = ExprProperties::new_unknown()
.with_order(ordering)
.with_range(Interval::make_unbounded(&data_type)?);
let expected = if supported {
ordering
} else {
SortProperties::Unordered
};
assert_eq!(
function.output_ordering(std::slice::from_ref(&input))?,
expected
);
assert_eq!(
function.output_ordering(&[input.clone(), input])?,
SortProperties::Unordered
);
}
}
}
assert_eq!(function.output_ordering(&[])?, SortProperties::Unordered);
}
Ok(())
}

fn to_timestamp(args: &[ColumnarValue]) -> Result<ColumnarValue> {
let timezone: Option<Arc<str>> = Some("UTC".into());
to_timestamp_impl::<TimestampNanosecondType>(
Expand Down
129 changes: 129 additions & 0 deletions datafusion/functions/src/datetime/to_unixtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::to_timestamp::ToTimestampSecondsFunc;
use crate::datetime::common::*;
use arrow::datatypes::{DataType, TimeUnit};
use datafusion_common::{Result, exec_err};
use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
use datafusion_expr::{
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
Volatility,
Expand Down Expand Up @@ -143,7 +144,135 @@ impl ScalarUDFImpl for ToUnixtimeFunc {
}
}

fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
if let [value] = input
&& (value.range.data_type().is_integer()
|| matches!(
value.range.data_type(),
DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _)
))
{
// Conversions preserve epoch order and report overflow instead of adding NULLs.
Ok(value.sort_properties)
} else {
Ok(SortProperties::Unordered)
}
}

fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
}

#[cfg(test)]
mod tests {
use std::sync::Arc;

use arrow::array::{Array, Int64Array};
use arrow::compute::{SortOptions, sort};
use arrow::datatypes::DataType::*;
use datafusion_common::{config::ConfigOptions, datatype::DataTypeExt};
use datafusion_expr::interval_arithmetic::Interval;

use super::*;

#[test]
fn ordering_matches_runtime_conversion() -> Result<()> {
let function = ToUnixtimeFunc::new();
let mut cases = vec![
(Int8, true),
(Int16, true),
(Int32, true),
(Int64, true),
(UInt8, true),
(UInt16, true),
(UInt32, true),
(UInt64, true),
(Date32, true),
(Date64, true),
(Float64, false),
(Decimal128(20, 0), false),
(Utf8, false),
(Null, false),
];
for unit in [
TimeUnit::Second,
TimeUnit::Millisecond,
TimeUnit::Microsecond,
TimeUnit::Nanosecond,
] {
for timezone in [
None,
Some("UTC".into()),
Some("+05:30".into()),
Some("America/New_York".into()),
] {
cases.push((Timestamp(unit, timezone), true));
}
}
for (data_type, supported) in cases {
for (descending, nulls_first) in
[(false, false), (false, true), (true, false), (true, true)]
{
let options = SortOptions {
descending,
nulls_first,
};
let ordering = SortProperties::Ordered(options);
let input = ExprProperties::new_unknown()
.with_order(ordering)
.with_range(Interval::make_unbounded(&data_type)?);
assert_eq!(
function.output_ordering(&[input])?,
if supported {
ordering
} else {
SortProperties::Unordered
}
);
if !supported || data_type.is_integer() {
continue;
}

let (min, max) = if data_type == Date32 {
(i32::MIN as i64, i32::MAX as i64)
} else {
(i64::MIN, i64::MAX)
};
let values = Int64Array::from(vec![
Some(min),
Some(-1),
Some(0),
Some(1),
Some(max),
None,
]);
let argument = ColumnarValue::Array(sort(&values, Some(options))?)
.cast_to(&data_type, None)?;
let result = function
.invoke_with_args(ScalarFunctionArgs {
args: vec![argument],
arg_fields: vec![data_type.clone().into_nullable_field_ref()],
number_rows: values.len(),
return_field: Int64.into_nullable_field_ref(),
config_options: Arc::new(ConfigOptions::default()),
})?
.to_array(values.len())?;
assert_eq!(
result.as_ref(),
sort(result.as_ref(), Some(options))?.as_ref()
);
assert_eq!(result.null_count(), values.null_count());
}
}
let input = ExprProperties::new_unknown()
.with_order(SortProperties::Ordered(SortOptions::default()))
.with_range(Interval::make_unbounded(&Int64)?);
assert_eq!(
function.output_ordering(&[input.clone(), input])?,
SortProperties::Unordered
);
assert_eq!(function.output_ordering(&[])?, SortProperties::Unordered);
Ok(())
}
}
Loading
Loading