datafusion_functions/datetime/
current_time.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use 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
58/// Create an implementation of `current_time()` that always returns the
59/// specified current time.
60///
61/// The semantics of `current_time()` require it to return the same value
62/// wherever it appears within a single statement. This value is
63/// chosen during planning time.
64impl 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}