datafusion_functions/datetime/
current_time.rsuse arrow::datatypes::DataType;
use arrow::datatypes::DataType::Time64;
use arrow::datatypes::TimeUnit::Nanosecond;
use std::any::Any;
use std::sync::OnceLock;
use datafusion_common::{internal_err, Result, ScalarValue};
use datafusion_expr::scalar_doc_sections::DOC_SECTION_DATETIME;
use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyInfo};
use datafusion_expr::{
ColumnarValue, Documentation, Expr, ScalarUDFImpl, Signature, Volatility,
};
#[derive(Debug)]
pub struct CurrentTimeFunc {
signature: Signature,
}
impl Default for CurrentTimeFunc {
fn default() -> Self {
Self::new()
}
}
impl CurrentTimeFunc {
pub fn new() -> Self {
Self {
signature: Signature::uniform(0, vec![], Volatility::Stable),
}
}
}
impl ScalarUDFImpl for CurrentTimeFunc {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"current_time"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(Time64(Nanosecond))
}
fn invoke(&self, _args: &[ColumnarValue]) -> Result<ColumnarValue> {
internal_err!(
"invoke should not be called on a simplified current_time() function"
)
}
fn simplify(
&self,
_args: Vec<Expr>,
info: &dyn SimplifyInfo,
) -> Result<ExprSimplifyResult> {
let now_ts = info.execution_props().query_execution_start_time;
let nano = now_ts.timestamp_nanos_opt().map(|ts| ts % 86400000000000);
Ok(ExprSimplifyResult::Simplified(Expr::Literal(
ScalarValue::Time64Nanosecond(nano),
)))
}
fn documentation(&self) -> Option<&Documentation> {
Some(get_current_time_doc())
}
}
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
fn get_current_time_doc() -> &'static Documentation {
DOCUMENTATION.get_or_init(|| {
Documentation::builder()
.with_doc_section(DOC_SECTION_DATETIME)
.with_description(r#"
Returns the current UTC time.
The `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.
"#)
.with_syntax_example("current_time()")
.build()
.unwrap()
})
}