datafusion_functions/datetime/
now.rs1use arrow::datatypes::DataType::Timestamp;
19use arrow::datatypes::TimeUnit::Nanosecond;
20use arrow::datatypes::{DataType, Field, FieldRef};
21use std::any::Any;
22
23use datafusion_common::{internal_err, Result, ScalarValue};
24use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyInfo};
25use datafusion_expr::{
26 ColumnarValue, Documentation, Expr, ReturnFieldArgs, ScalarUDFImpl, Signature,
27 Volatility,
28};
29use datafusion_macros::user_doc;
30
31#[user_doc(
32 doc_section(label = "Time and Date Functions"),
33 description = r#"
34Returns the current UTC timestamp.
35
36The `now()` return value is determined at query time and will return the same timestamp, no matter when in the query plan the function executes.
37"#,
38 syntax_example = "now()"
39)]
40#[derive(Debug)]
41pub struct NowFunc {
42 signature: Signature,
43 aliases: Vec<String>,
44}
45
46impl Default for NowFunc {
47 fn default() -> Self {
48 Self::new()
49 }
50}
51
52impl NowFunc {
53 pub fn new() -> Self {
54 Self {
55 signature: Signature::nullary(Volatility::Stable),
56 aliases: vec!["current_timestamp".to_string()],
57 }
58 }
59}
60
61impl ScalarUDFImpl for NowFunc {
68 fn as_any(&self) -> &dyn Any {
69 self
70 }
71
72 fn name(&self) -> &str {
73 "now"
74 }
75
76 fn signature(&self) -> &Signature {
77 &self.signature
78 }
79
80 fn return_field_from_args(&self, _args: ReturnFieldArgs) -> Result<FieldRef> {
81 Ok(Field::new(
82 self.name(),
83 Timestamp(Nanosecond, Some("+00:00".into())),
84 false,
85 )
86 .into())
87 }
88
89 fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
90 internal_err!("return_field_from_args should be called instead")
91 }
92
93 fn invoke_with_args(
94 &self,
95 _args: datafusion_expr::ScalarFunctionArgs,
96 ) -> Result<ColumnarValue> {
97 internal_err!("invoke should not be called on a simplified now() function")
98 }
99
100 fn simplify(
101 &self,
102 _args: Vec<Expr>,
103 info: &dyn SimplifyInfo,
104 ) -> Result<ExprSimplifyResult> {
105 let now_ts = info
106 .execution_props()
107 .query_execution_start_time
108 .timestamp_nanos_opt();
109 Ok(ExprSimplifyResult::Simplified(Expr::Literal(
110 ScalarValue::TimestampNanosecond(now_ts, Some("+00:00".into())),
111 None,
112 )))
113 }
114
115 fn aliases(&self) -> &[String] {
116 &self.aliases
117 }
118
119 fn documentation(&self) -> Option<&Documentation> {
120 self.doc()
121 }
122}