@@ -40,6 +40,14 @@ pub struct SimplifyContext {
4040 config_options : Arc < ConfigOptions > ,
4141}
4242
43+ /// Builder for [`SimplifyContext`].
44+ #[ derive( Debug , Default ) ]
45+ pub struct SimplifyContextBuilder {
46+ schema : Option < DFSchemaRef > ,
47+ query_execution_start_time : Option < DateTime < Utc > > ,
48+ config_options : Option < Arc < ConfigOptions > > ,
49+ }
50+
4351impl Default for SimplifyContext {
4452 fn default ( ) -> Self {
4553 Self {
@@ -51,6 +59,11 @@ impl Default for SimplifyContext {
5159}
5260
5361impl SimplifyContext {
62+ /// Returns a builder for [`SimplifyContext`].
63+ pub fn builder ( ) -> SimplifyContextBuilder {
64+ SimplifyContextBuilder :: default ( )
65+ }
66+
5467 /// Set the [`ConfigOptions`] for this context
5568 pub fn with_config_options ( mut self , config_options : Arc < ConfigOptions > ) -> Self {
5669 self . config_options = config_options;
@@ -110,6 +123,46 @@ impl SimplifyContext {
110123 }
111124}
112125
126+ impl SimplifyContextBuilder {
127+ /// Set the [`ConfigOptions`] for this context.
128+ pub fn with_config_options ( mut self , config_options : Arc < ConfigOptions > ) -> Self {
129+ self . config_options = Some ( config_options) ;
130+ self
131+ }
132+
133+ /// Set the schema for this context.
134+ pub fn with_schema ( mut self , schema : DFSchemaRef ) -> Self {
135+ self . schema = Some ( schema) ;
136+ self
137+ }
138+
139+ /// Set the query execution start time.
140+ pub fn with_query_execution_start_time (
141+ mut self ,
142+ query_execution_start_time : Option < DateTime < Utc > > ,
143+ ) -> Self {
144+ self . query_execution_start_time = query_execution_start_time;
145+ self
146+ }
147+
148+ /// Set the query execution start to the current time.
149+ pub fn with_current_time ( mut self ) -> Self {
150+ self . query_execution_start_time = Some ( Utc :: now ( ) ) ;
151+ self
152+ }
153+
154+ /// Build a [`SimplifyContext`], filling in any unspecified fields with defaults.
155+ pub fn build ( self ) -> SimplifyContext {
156+ SimplifyContext {
157+ schema : self . schema . unwrap_or_else ( || Arc :: new ( DFSchema :: empty ( ) ) ) ,
158+ query_execution_start_time : self . query_execution_start_time ,
159+ config_options : self
160+ . config_options
161+ . unwrap_or_else ( || Arc :: new ( ConfigOptions :: default ( ) ) ) ,
162+ }
163+ }
164+ }
165+
113166/// Was the expression simplified?
114167#[ derive( Debug ) ]
115168pub enum ExprSimplifyResult {
@@ -119,3 +172,38 @@ pub enum ExprSimplifyResult {
119172 /// are return unmodified.
120173 Original ( Vec < Expr > ) ,
121174}
175+
176+ #[ cfg( test) ]
177+ mod tests {
178+ use super :: * ;
179+
180+ #[ test]
181+ fn simplify_context_builder_builds_default_context ( ) {
182+ let context = SimplifyContext :: builder ( ) . build ( ) ;
183+ let default_options = ConfigOptions :: default ( ) ;
184+
185+ assert_eq ! ( context. schema( ) . as_ref( ) , & DFSchema :: empty( ) ) ;
186+ assert_eq ! ( context. query_execution_start_time( ) , None ) ;
187+ assert_eq ! (
188+ context. config_options( ) . optimizer. max_passes,
189+ default_options. optimizer. max_passes
190+ ) ;
191+ }
192+
193+ #[ test]
194+ fn simplify_context_builder_uses_overrides ( ) {
195+ let schema = Arc :: new ( DFSchema :: empty ( ) ) ;
196+ let config_options = Arc :: new ( ConfigOptions :: default ( ) ) ;
197+ let current_time = Utc :: now ( ) ;
198+
199+ let context = SimplifyContext :: builder ( )
200+ . with_schema ( Arc :: clone ( & schema) )
201+ . with_config_options ( Arc :: clone ( & config_options) )
202+ . with_query_execution_start_time ( Some ( current_time) )
203+ . build ( ) ;
204+
205+ assert_eq ! ( context. schema( ) . as_ref( ) , schema. as_ref( ) ) ;
206+ assert_eq ! ( context. query_execution_start_time( ) , Some ( current_time) ) ;
207+ assert ! ( Arc :: ptr_eq( context. config_options( ) , & config_options) ) ;
208+ }
209+ }
0 commit comments