Skip to content

Commit 9d46886

Browse files
committed
refactor: simplify ToTimestamp* constructors using macros for consistency and maintainability
1 parent 72f2a97 commit 9d46886

File tree

1 file changed

+48
-153
lines changed

1 file changed

+48
-153
lines changed

datafusion/functions/src/datetime/to_timestamp.rs

Lines changed: 48 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -284,157 +284,62 @@ pub struct ToTimestampNanosFunc {
284284
timezone: Option<Arc<str>>,
285285
}
286286

287-
impl Default for ToTimestampFunc {
288-
fn default() -> Self {
289-
Self::new_with_config(&ConfigOptions::default())
290-
}
291-
}
292-
293-
impl ToTimestampFunc {
294-
#[deprecated(since = "52.0.0", note = "use `new_with_config` instead")]
295-
/// Deprecated constructor retained for backwards compatibility.
296-
///
297-
/// Prefer [`ToTimestampFunc::new_with_config`] which allows specifying the
298-
/// timezone via [`ConfigOptions`]. This helper now mirrors the
299-
/// canonical default offset (None) provided by `ConfigOptions::default()`.
300-
pub fn new() -> Self {
301-
Self::new_with_config(&ConfigOptions::default())
302-
}
303-
304-
pub fn new_with_config(config: &ConfigOptions) -> Self {
305-
Self {
306-
signature: Signature::variadic_any(Volatility::Immutable),
307-
timezone: config
308-
.execution
309-
.time_zone
310-
.as_ref()
311-
.map(|tz| Arc::from(tz.as_str())),
312-
}
313-
}
314-
}
315-
316-
impl Default for ToTimestampSecondsFunc {
317-
fn default() -> Self {
318-
Self::new_with_config(&ConfigOptions::default())
319-
}
320-
}
321-
322-
impl ToTimestampSecondsFunc {
323-
#[deprecated(since = "52.0.0", note = "use `new_with_config` instead")]
324-
/// Deprecated constructor retained for backwards compatibility.
325-
///
326-
/// Prefer [`ToTimestampSecondsFunc::new_with_config`] which allows specifying the
327-
/// timezone via [`ConfigOptions`]. This helper now mirrors the
328-
/// canonical default offset (None) provided by `ConfigOptions::default()`.
329-
pub fn new() -> Self {
330-
Self::new_with_config(&ConfigOptions::default())
331-
}
332-
333-
pub fn new_with_config(config: &ConfigOptions) -> Self {
334-
Self {
335-
signature: Signature::variadic_any(Volatility::Immutable),
336-
timezone: config
337-
.execution
338-
.time_zone
339-
.as_ref()
340-
.map(|tz| Arc::from(tz.as_str())),
341-
}
342-
}
343-
}
344-
345-
impl Default for ToTimestampMillisFunc {
346-
fn default() -> Self {
347-
Self::new_with_config(&ConfigOptions::default())
348-
}
349-
}
350-
351-
impl ToTimestampMillisFunc {
352-
#[deprecated(since = "52.0.0", note = "use `new_with_config` instead")]
353-
/// Deprecated constructor retained for backwards compatibility.
354-
///
355-
/// Prefer [`ToTimestampMillisFunc::new_with_config`] which allows specifying the
356-
/// timezone via [`ConfigOptions`]. This helper now mirrors the
357-
/// canonical default offset (None) provided by `ConfigOptions::default()`.
358-
pub fn new() -> Self {
359-
Self::new_with_config(&ConfigOptions::default())
360-
}
361-
362-
pub fn new_with_config(config: &ConfigOptions) -> Self {
363-
Self {
364-
signature: Signature::variadic_any(Volatility::Immutable),
365-
timezone: config
366-
.execution
367-
.time_zone
368-
.as_ref()
369-
.map(|tz| Arc::from(tz.as_str())),
287+
/// Macro to generate boilerplate constructors and config methods for ToTimestamp* functions.
288+
/// Generates: Default impl, deprecated new(), new_with_config(), and extracts timezone from ConfigOptions.
289+
macro_rules! impl_to_timestamp_constructors {
290+
($func:ty) => {
291+
impl Default for $func {
292+
fn default() -> Self {
293+
Self::new_with_config(&ConfigOptions::default())
294+
}
370295
}
371-
}
372-
}
373296

374-
impl Default for ToTimestampMicrosFunc {
375-
fn default() -> Self {
376-
Self::new_with_config(&ConfigOptions::default())
377-
}
378-
}
379-
380-
impl ToTimestampMicrosFunc {
381-
#[deprecated(since = "52.0.0", note = "use `new_with_config` instead")]
382-
/// Deprecated constructor retained for backwards compatibility.
383-
///
384-
/// Prefer [`ToTimestampMicrosFunc::new_with_config`] which allows specifying the
385-
/// timezone via [`ConfigOptions`]. This helper now mirrors the
386-
/// canonical default offset (None) provided by `ConfigOptions::default()`.
387-
pub fn new() -> Self {
388-
Self::new_with_config(&ConfigOptions::default())
389-
}
297+
impl $func {
298+
#[deprecated(since = "52.0.0", note = "use `new_with_config` instead")]
299+
/// Deprecated constructor retained for backwards compatibility.
300+
///
301+
/// Prefer [`new_with_config`] which allows specifying the
302+
/// timezone via [`ConfigOptions`]. This helper now mirrors the
303+
/// canonical default offset (None) provided by `ConfigOptions::default()`.
304+
pub fn new() -> Self {
305+
Self::new_with_config(&ConfigOptions::default())
306+
}
390307

391-
pub fn new_with_config(config: &ConfigOptions) -> Self {
392-
Self {
393-
signature: Signature::variadic_any(Volatility::Immutable),
394-
timezone: config
395-
.execution
396-
.time_zone
397-
.as_ref()
398-
.map(|tz| Arc::from(tz.as_str())),
308+
pub fn new_with_config(config: &ConfigOptions) -> Self {
309+
Self {
310+
signature: Signature::variadic_any(Volatility::Immutable),
311+
timezone: config
312+
.execution
313+
.time_zone
314+
.as_ref()
315+
.map(|tz| Arc::from(tz.as_str())),
316+
}
317+
}
399318
}
400-
}
401-
}
402-
403-
impl Default for ToTimestampNanosFunc {
404-
fn default() -> Self {
405-
Self::new_with_config(&ConfigOptions::default())
406-
}
319+
};
407320
}
408321

409-
impl ToTimestampNanosFunc {
410-
#[deprecated(since = "52.0.0", note = "use `new_with_config` instead")]
411-
/// Deprecated constructor retained for backwards compatibility.
412-
///
413-
/// Prefer [`ToTimestampNanosFunc::new_with_config`] which allows specifying the
414-
/// timezone via [`ConfigOptions`]. This helper now mirrors the
415-
/// canonical default offset (None) provided by `ConfigOptions::default()`.
416-
pub fn new() -> Self {
417-
Self::new_with_config(&ConfigOptions::default())
418-
}
419-
420-
pub fn new_with_config(config: &ConfigOptions) -> Self {
421-
Self {
422-
signature: Signature::variadic_any(Volatility::Immutable),
423-
timezone: config
424-
.execution
425-
.time_zone
426-
.as_ref()
427-
.map(|tz| Arc::from(tz.as_str())),
428-
}
429-
}
430-
}
322+
impl_to_timestamp_constructors!(ToTimestampFunc);
323+
impl_to_timestamp_constructors!(ToTimestampSecondsFunc);
324+
impl_to_timestamp_constructors!(ToTimestampMillisFunc);
325+
impl_to_timestamp_constructors!(ToTimestampMicrosFunc);
326+
impl_to_timestamp_constructors!(ToTimestampNanosFunc);
431327

432328
/// to_timestamp SQL function
433329
///
434330
/// Note: `to_timestamp` returns `Timestamp(Nanosecond)` though its arguments are interpreted as **seconds**.
435331
/// The supported range for integer input is between `-9223372037` and `9223372036`.
436332
/// Supported range for string input is between `1677-09-21T00:12:44.0` and `2262-04-11T23:47:16.0`.
437333
/// Please use `to_timestamp_seconds` for the input outside of supported bounds.
334+
/// Macro to generate the with_updated_config method for ToTimestamp* functions.
335+
macro_rules! impl_with_updated_config {
336+
() => {
337+
fn with_updated_config(&self, config: &ConfigOptions) -> Option<ScalarUDF> {
338+
Some(Self::new_with_config(config).into())
339+
}
340+
};
341+
}
342+
438343
impl ScalarUDFImpl for ToTimestampFunc {
439344
fn as_any(&self) -> &dyn Any {
440345
self
@@ -452,9 +357,7 @@ impl ScalarUDFImpl for ToTimestampFunc {
452357
Ok(Timestamp(Nanosecond, self.timezone.clone()))
453358
}
454359

455-
fn with_updated_config(&self, config: &ConfigOptions) -> Option<ScalarUDF> {
456-
Some(Self::new_with_config(config).into())
457-
}
360+
impl_with_updated_config!();
458361

459362
fn invoke_with_args(
460363
&self,
@@ -547,9 +450,7 @@ impl ScalarUDFImpl for ToTimestampSecondsFunc {
547450
Ok(Timestamp(Second, self.timezone.clone()))
548451
}
549452

550-
fn with_updated_config(&self, config: &ConfigOptions) -> Option<ScalarUDF> {
551-
Some(Self::new_with_config(config).into())
552-
}
453+
impl_with_updated_config!();
553454

554455
fn invoke_with_args(
555456
&self,
@@ -611,9 +512,7 @@ impl ScalarUDFImpl for ToTimestampMillisFunc {
611512
Ok(Timestamp(Millisecond, self.timezone.clone()))
612513
}
613514

614-
fn with_updated_config(&self, config: &ConfigOptions) -> Option<ScalarUDF> {
615-
Some(Self::new_with_config(config).into())
616-
}
515+
impl_with_updated_config!();
617516

618517
fn invoke_with_args(
619518
&self,
@@ -673,9 +572,7 @@ impl ScalarUDFImpl for ToTimestampMicrosFunc {
673572
Ok(Timestamp(Microsecond, self.timezone.clone()))
674573
}
675574

676-
fn with_updated_config(&self, config: &ConfigOptions) -> Option<ScalarUDF> {
677-
Some(Self::new_with_config(config).into())
678-
}
575+
impl_with_updated_config!();
679576

680577
fn invoke_with_args(
681578
&self,
@@ -735,9 +632,7 @@ impl ScalarUDFImpl for ToTimestampNanosFunc {
735632
Ok(Timestamp(Nanosecond, self.timezone.clone()))
736633
}
737634

738-
fn with_updated_config(&self, config: &ConfigOptions) -> Option<ScalarUDF> {
739-
Some(Self::new_with_config(config).into())
740-
}
635+
impl_with_updated_config!();
741636

742637
fn invoke_with_args(
743638
&self,

0 commit comments

Comments
 (0)