1717
1818//! Structs and traits to provide the information needed for expression simplification.
1919
20+ use std:: sync:: Arc ;
21+
2022use arrow:: datatypes:: DataType ;
23+ use chrono:: { DateTime , Utc } ;
24+ use datafusion_common:: config:: ConfigOptions ;
2125use datafusion_common:: { DFSchemaRef , Result , internal_datafusion_err} ;
2226
2327use crate :: { Expr , ExprSchemable , execution_props:: ExecutionProps } ;
@@ -35,43 +39,85 @@ pub trait SimplifyInfo {
3539 /// Returns true of this expr is nullable (could possibly be NULL)
3640 fn nullable ( & self , expr : & Expr ) -> Result < bool > ;
3741
38- /// Returns details needed for partial expression evaluation
39- fn execution_props ( & self ) -> & ExecutionProps ;
40-
4142 /// Returns data type of this expr needed for determining optimized int type of a value
4243 fn get_data_type ( & self , expr : & Expr ) -> Result < DataType > ;
44+
45+ /// Returns the time at which the query execution started.
46+ /// If `None`, time-dependent functions like `now()` will not be simplified.
47+ fn query_execution_start_time ( & self ) -> Option < DateTime < Utc > > ;
48+
49+ /// Returns the configuration options for the session.
50+ fn config_options ( & self ) -> Option < & Arc < ConfigOptions > > ;
4351}
4452
45- /// Provides simplification information based on DFSchema and
46- /// [`ExecutionProps`]. This is the default implementation used by DataFusion
53+ /// Provides simplification information based on schema, query execution time,
54+ /// and configuration options.
4755///
4856/// # Example
4957/// See the `simplify_demo` in the [`expr_api` example]
5058///
5159/// [`expr_api` example]: https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/query_planning/expr_api.rs
5260#[ derive( Debug , Clone ) ]
53- pub struct SimplifyContext < ' a > {
61+ pub struct SimplifyContext {
5462 schema : Option < DFSchemaRef > ,
55- props : & ' a ExecutionProps ,
63+ query_execution_start_time : Option < DateTime < Utc > > ,
64+ config_options : Option < Arc < ConfigOptions > > ,
5665}
5766
58- impl < ' a > SimplifyContext < ' a > {
59- /// Create a new SimplifyContext
60- pub fn new ( props : & ' a ExecutionProps ) -> Self {
67+ impl SimplifyContext {
68+ /// Create a new SimplifyContext from [`ExecutionProps`].
69+ ///
70+ /// This constructor extracts `query_execution_start_time` and `config_options`
71+ /// from the provided `ExecutionProps`.
72+ pub fn new ( props : & ExecutionProps ) -> Self {
73+ Self {
74+ schema : None ,
75+ query_execution_start_time : props. query_execution_start_time ,
76+ config_options : props. config_options . clone ( ) ,
77+ }
78+ }
79+
80+ /// Create a new empty SimplifyContext.
81+ ///
82+ /// This is useful when you don't need time-dependent simplification
83+ /// (like `now()`) or config-based simplification.
84+ pub fn new_empty ( ) -> Self {
6185 Self {
6286 schema : None ,
63- props,
87+ query_execution_start_time : None ,
88+ config_options : None ,
6489 }
6590 }
6691
92+ /// Set the query execution start time
93+ pub fn with_query_execution_start_time (
94+ mut self ,
95+ query_execution_start_time : Option < DateTime < Utc > > ,
96+ ) -> Self {
97+ self . query_execution_start_time = query_execution_start_time;
98+ self
99+ }
100+
101+ /// Set the configuration options
102+ pub fn with_config_options ( mut self , config_options : Arc < ConfigOptions > ) -> Self {
103+ self . config_options = Some ( config_options) ;
104+ self
105+ }
106+
67107 /// Register a [`DFSchemaRef`] with this context
68108 pub fn with_schema ( mut self , schema : DFSchemaRef ) -> Self {
69109 self . schema = Some ( schema) ;
70110 self
71111 }
72112}
73113
74- impl SimplifyInfo for SimplifyContext < ' _ > {
114+ impl Default for SimplifyContext {
115+ fn default ( ) -> Self {
116+ Self :: new_empty ( )
117+ }
118+ }
119+
120+ impl SimplifyInfo for SimplifyContext {
75121 /// Returns true if this Expr has boolean type
76122 fn is_boolean_type ( & self , expr : & Expr ) -> Result < bool > {
77123 if let Some ( schema) = & self . schema
@@ -99,8 +145,12 @@ impl SimplifyInfo for SimplifyContext<'_> {
99145 expr. get_type ( schema)
100146 }
101147
102- fn execution_props ( & self ) -> & ExecutionProps {
103- self . props
148+ fn query_execution_start_time ( & self ) -> Option < DateTime < Utc > > {
149+ self . query_execution_start_time
150+ }
151+
152+ fn config_options ( & self ) -> Option < & Arc < ConfigOptions > > {
153+ self . config_options . as_ref ( )
104154 }
105155}
106156
0 commit comments