datafusion_functions/datetime/
current_date.rs1use std::any::Any;
19
20use arrow::datatypes::DataType;
21use arrow::datatypes::DataType::Date32;
22use chrono::{Datelike, NaiveDate};
23
24use datafusion_common::{internal_err, Result, ScalarValue};
25use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyInfo};
26use datafusion_expr::{
27 ColumnarValue, Documentation, Expr, ScalarUDFImpl, Signature, 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 date.
35
36The `current_date()` return value is determined at query time and will return the same date, no matter when in the query plan the function executes.
37"#,
38 syntax_example = "current_date()"
39)]
40#[derive(Debug)]
41pub struct CurrentDateFunc {
42 signature: Signature,
43 aliases: Vec<String>,
44}
45
46impl Default for CurrentDateFunc {
47 fn default() -> Self {
48 Self::new()
49 }
50}
51
52impl CurrentDateFunc {
53 pub fn new() -> Self {
54 Self {
55 signature: Signature::nullary(Volatility::Stable),
56 aliases: vec![String::from("today")],
57 }
58 }
59}
60
61impl ScalarUDFImpl for CurrentDateFunc {
68 fn as_any(&self) -> &dyn Any {
69 self
70 }
71
72 fn name(&self) -> &str {
73 "current_date"
74 }
75
76 fn signature(&self) -> &Signature {
77 &self.signature
78 }
79
80 fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
81 Ok(Date32)
82 }
83
84 fn invoke_with_args(
85 &self,
86 _args: datafusion_expr::ScalarFunctionArgs,
87 ) -> Result<ColumnarValue> {
88 internal_err!(
89 "invoke should not be called on a simplified current_date() function"
90 )
91 }
92
93 fn aliases(&self) -> &[String] {
94 &self.aliases
95 }
96
97 fn simplify(
98 &self,
99 _args: Vec<Expr>,
100 info: &dyn SimplifyInfo,
101 ) -> Result<ExprSimplifyResult> {
102 let now_ts = info.execution_props().query_execution_start_time;
103 let days = Some(
104 now_ts.num_days_from_ce()
105 - NaiveDate::from_ymd_opt(1970, 1, 1)
106 .unwrap()
107 .num_days_from_ce(),
108 );
109 Ok(ExprSimplifyResult::Simplified(Expr::Literal(
110 ScalarValue::Date32(days),
111 )))
112 }
113
114 fn documentation(&self) -> Option<&Documentation> {
115 self.doc()
116 }
117}