Skip to content
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
53 changes: 48 additions & 5 deletions datafusion/functions/src/datetime/now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ impl Default for NowFunc {

impl NowFunc {
#[deprecated(since = "50.2.0", note = "use `new_with_config` instead")]
/// Deprecated constructor retained for backwards compatibility.
///
/// Prefer [`NowFunc::new_with_config`] which allows specifying the
/// timezone via [`ConfigOptions`]. This helper now mirrors the
/// canonical default offset (`"+00:00"`) provided by
/// `ConfigOptions::default()`.
pub fn new() -> Self {
Self {
signature: Signature::nullary(Volatility::Stable),
aliases: vec!["current_timestamp".to_string()],
timezone: Some(Arc::from("+00")),
}
Self::new_with_config(&ConfigOptions::default())
}

pub fn new_with_config(config: &ConfigOptions) -> Self {
Expand Down Expand Up @@ -138,3 +140,44 @@ impl ScalarUDFImpl for NowFunc {
self.doc()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[allow(deprecated)]
#[test]
fn now_func_default_matches_config() {
let default_config = ConfigOptions::default();

let legacy_now = NowFunc::new();
let configured_now = NowFunc::new_with_config(&default_config);

let empty_fields: [FieldRef; 0] = [];
let empty_scalars: [Option<&ScalarValue>; 0] = [];

let legacy_field = legacy_now
.return_field_from_args(ReturnFieldArgs {
arg_fields: &empty_fields,
scalar_arguments: &empty_scalars,
})
.expect("legacy now() return field");

let configured_field = configured_now
.return_field_from_args(ReturnFieldArgs {
arg_fields: &empty_fields,
scalar_arguments: &empty_scalars,
})
.expect("configured now() return field");

assert_eq!(legacy_field.as_ref(), configured_field.as_ref());

let legacy_scalar =
ScalarValue::TimestampNanosecond(None, legacy_now.timezone.clone());
let configured_scalar =
ScalarValue::TimestampNanosecond(None, configured_now.timezone.clone());

assert_eq!(legacy_scalar, configured_scalar);
assert_eq!(Some("+00:00"), legacy_now.timezone.as_deref());
}
}