datafusion_functions/datetime/
current_time.rs1use arrow::datatypes::DataType;
19use arrow::datatypes::DataType::Time64;
20use arrow::datatypes::TimeUnit::Nanosecond;
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, ScalarUDFImpl, Signature, Volatility,
27};
28use datafusion_macros::user_doc;
29
30#[user_doc(
31 doc_section(label = "Time and Date Functions"),
32 description = r#"
33Returns the current UTC time.
34
35The `current_time()` return value is determined at query time and will return the same time, no matter when in the query plan the function executes.
36"#,
37 syntax_example = "current_time()"
38)]
39#[derive(Debug)]
40pub struct CurrentTimeFunc {
41 signature: Signature,
42}
43
44impl Default for CurrentTimeFunc {
45 fn default() -> Self {
46 Self::new()
47 }
48}
49
50impl CurrentTimeFunc {
51 pub fn new() -> Self {
52 Self {
53 signature: Signature::nullary(Volatility::Stable),
54 }
55 }
56}
57
58impl ScalarUDFImpl for CurrentTimeFunc {
65 fn as_any(&self) -> &dyn Any {
66 self
67 }
68
69 fn name(&self) -> &str {
70 "current_time"
71 }
72
73 fn signature(&self) -> &Signature {
74 &self.signature
75 }
76
77 fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
78 Ok(Time64(Nanosecond))
79 }
80
81 fn invoke_with_args(
82 &self,
83 _args: datafusion_expr::ScalarFunctionArgs,
84 ) -> Result<ColumnarValue> {
85 internal_err!(
86 "invoke should not be called on a simplified current_time() function"
87 )
88 }
89
90 fn simplify(
91 &self,
92 _args: Vec<Expr>,
93 info: &dyn SimplifyInfo,
94 ) -> Result<ExprSimplifyResult> {
95 let now_ts = info.execution_props().query_execution_start_time;
96 let nano = now_ts.timestamp_nanos_opt().map(|ts| ts % 86400000000000);
97 Ok(ExprSimplifyResult::Simplified(Expr::Literal(
98 ScalarValue::Time64Nanosecond(nano),
99 )))
100 }
101
102 fn documentation(&self) -> Option<&Documentation> {
103 self.doc()
104 }
105}