Omega359 commented on code in PR #11403:
URL: https://github.com/apache/datafusion/pull/11403#discussion_r1675942907


##########
datafusion/core/src/execution/session_state.rs:
##########
@@ -976,6 +837,482 @@ impl SessionState {
     }
 }
 
+/// A builder to be used for building [`SessionState`]'s. Defaults will be 
used for all values
+/// unless explicitly provided. Note that there is no `Default` or `new()` for 
SessionState,
+/// to avoid accidentally running queries or other operations without passing 
through
+/// the [`SessionConfig`] or [`RuntimeEnv`].
+pub struct SessionStateBuilder {
+    state: SessionState,
+    use_defaults: bool,
+}
+
+impl SessionStateBuilder {
+    /// Returns new [`SessionStateBuilder`] using the provided
+    /// [`SessionConfig`] and [`RuntimeEnv`].
+    pub fn new_with_config_rt(
+        config: SessionConfig,
+        runtime_env: Arc<RuntimeEnv>,
+    ) -> Self {
+        let session_id = Uuid::new_v4().to_string();
+        let catalog_list =
+            Arc::new(MemoryCatalogProviderList::new()) as Arc<dyn 
CatalogProviderList>;
+
+        Self {
+            state: SessionState {
+                session_id,
+                analyzer: Analyzer::new(),
+                expr_planners: vec![],
+                optimizer: Optimizer::new(),
+                physical_optimizers: PhysicalOptimizer::new(),
+                query_planner: Arc::new(DefaultQueryPlanner {}),
+                catalog_list,
+                table_functions: HashMap::new(),
+                scalar_functions: HashMap::new(),
+                aggregate_functions: HashMap::new(),
+                window_functions: HashMap::new(),
+                serializer_registry: Arc::new(EmptySerializerRegistry),
+                file_formats: HashMap::new(),
+                table_options: TableOptions::default_from_session_config(
+                    config.options(),
+                ),
+                config,
+                execution_props: ExecutionProps::new(),
+                table_factories: HashMap::new(),
+                runtime_env,
+                function_factory: None,
+            },
+            use_defaults: true,
+        }
+    }
+
+    /// Returns a new [SessionStateBuilder] based on an existing [SessionState]
+    /// The session id for the new builder will be set to a unique value; all
+    /// other fields will be cloned from what is set in the provided session 
state
+    pub fn new_from_existing(existing: &SessionState) -> Self {
+        let session_id = Uuid::new_v4().to_string();
+
+        Self {
+            state: SessionState {
+                session_id,
+                ..existing.clone()
+            },
+            use_defaults: true,
+        }
+    }
+
+    /// Set to true (default = true) if defaults for table_factories, 
expr_planners, file formats

Review Comment:
   I like .with_default_feature() myself.



##########
datafusion/core/src/execution/session_state.rs:
##########
@@ -976,6 +837,482 @@ impl SessionState {
     }
 }
 
+/// A builder to be used for building [`SessionState`]'s. Defaults will be 
used for all values
+/// unless explicitly provided. Note that there is no `Default` or `new()` for 
SessionState,
+/// to avoid accidentally running queries or other operations without passing 
through
+/// the [`SessionConfig`] or [`RuntimeEnv`].
+pub struct SessionStateBuilder {
+    state: SessionState,
+    use_defaults: bool,
+}
+
+impl SessionStateBuilder {
+    /// Returns new [`SessionStateBuilder`] using the provided
+    /// [`SessionConfig`] and [`RuntimeEnv`].
+    pub fn new_with_config_rt(
+        config: SessionConfig,
+        runtime_env: Arc<RuntimeEnv>,
+    ) -> Self {
+        let session_id = Uuid::new_v4().to_string();
+        let catalog_list =
+            Arc::new(MemoryCatalogProviderList::new()) as Arc<dyn 
CatalogProviderList>;
+
+        Self {
+            state: SessionState {
+                session_id,
+                analyzer: Analyzer::new(),
+                expr_planners: vec![],
+                optimizer: Optimizer::new(),
+                physical_optimizers: PhysicalOptimizer::new(),
+                query_planner: Arc::new(DefaultQueryPlanner {}),
+                catalog_list,
+                table_functions: HashMap::new(),
+                scalar_functions: HashMap::new(),
+                aggregate_functions: HashMap::new(),
+                window_functions: HashMap::new(),
+                serializer_registry: Arc::new(EmptySerializerRegistry),
+                file_formats: HashMap::new(),
+                table_options: TableOptions::default_from_session_config(
+                    config.options(),
+                ),
+                config,
+                execution_props: ExecutionProps::new(),
+                table_factories: HashMap::new(),
+                runtime_env,
+                function_factory: None,
+            },
+            use_defaults: true,
+        }
+    }
+
+    /// Returns a new [SessionStateBuilder] based on an existing [SessionState]
+    /// The session id for the new builder will be set to a unique value; all
+    /// other fields will be cloned from what is set in the provided session 
state
+    pub fn new_from_existing(existing: &SessionState) -> Self {
+        let session_id = Uuid::new_v4().to_string();
+
+        Self {
+            state: SessionState {
+                session_id,
+                ..existing.clone()
+            },
+            use_defaults: true,
+        }
+    }
+
+    /// Set to true (default = true) if defaults for table_factories, 
expr_planners, file formats

Review Comment:
   I like .with_default_features() myself.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to