1515// specific language governing permissions and limitations
1616// under the License.
1717
18- //! SessionContext contains methods for registering data sources and executing queries
18+ //! [` SessionContext`] contains methods for registering data sources and executing queries
1919use crate :: {
2020 catalog:: catalog:: { CatalogList , MemoryCatalogList } ,
2121 datasource:: {
@@ -158,11 +158,15 @@ where
158158 }
159159}
160160
161- /// SessionContext is the main interface for executing queries with DataFusion. It stands for
162- /// the connection between user and DataFusion/Ballista cluster.
163- /// The context provides the following functionality
161+ /// Main interface for executing queries with DataFusion. Maintains
162+ /// the state of the connection between a user and an instance of the
163+ /// DataFusion engine.
164164///
165- /// * Create DataFrame from a CSV or Parquet data source.
165+ /// # Overview
166+ ///
167+ /// [`SessionContext`] provides the following functionality:
168+ ///
169+ /// * Create a DataFrame from a CSV or Parquet data source.
166170/// * Register a CSV or Parquet data source as a table that can be referenced from a SQL query.
167171/// * Register a custom data source that can be referenced from a SQL query.
168172/// * Execution a SQL query
@@ -199,6 +203,20 @@ where
199203/// # Ok(())
200204/// # }
201205/// ```
206+ ///
207+ /// # `SessionContext`, `SessionState`, and `TaskContext`
208+ ///
209+ /// A [`SessionContext`] can be created from a [`SessionConfig`] and
210+ /// stores the state for a particular query session. A single
211+ /// [`SessionContext`] can run multiple queries.
212+ ///
213+ /// [`SessionState`] contains information available during query
214+ /// planning (creating [`LogicalPlan`]s and [`ExecutionPlan`]s).
215+ ///
216+ /// [`TaskContext`] contains the state available during query
217+ /// execution [`ExecutionPlan::execute`]. It contains a subset of the
218+ /// information in[`SessionState`] and is created from a
219+ /// [`SessionContext`] or a [`SessionState`].
202220#[ derive( Clone ) ]
203221pub struct SessionContext {
204222 /// UUID for the session
@@ -216,7 +234,7 @@ impl Default for SessionContext {
216234}
217235
218236impl SessionContext {
219- /// Creates a new execution context using a default session configuration .
237+ /// Creates a new `SessionContext` using the default [`SessionConfig`] .
220238 pub fn new ( ) -> Self {
221239 Self :: with_config ( SessionConfig :: new ( ) )
222240 }
@@ -241,19 +259,35 @@ impl SessionContext {
241259 Ok ( ( ) )
242260 }
243261
244- /// Creates a new session context using the provided session configuration.
262+ /// Creates a new `SessionContext` using the provided
263+ /// [`SessionConfig`] and a new [`RuntimeEnv`].
264+ ///
265+ /// See [`Self::with_config_rt`] for more details on resource
266+ /// limits.
245267 pub fn with_config ( config : SessionConfig ) -> Self {
246268 let runtime = Arc :: new ( RuntimeEnv :: default ( ) ) ;
247269 Self :: with_config_rt ( config, runtime)
248270 }
249271
250- /// Creates a new session context using the provided configuration and [`RuntimeEnv`].
272+ /// Creates a new `SessionContext` using the provided
273+ /// [`SessionConfig`] and a [`RuntimeEnv`].
274+ ///
275+ /// # Resource Limits
276+ ///
277+ /// By default, each new `SessionContext` creates a new
278+ /// `RuntimeEnv`, and therefore will not enforce memory or disk
279+ /// limits for queries run on different `SessionContext`s.
280+ ///
281+ /// To enforce resource limits (e.g. to limit the total amount of
282+ /// memory used) across all DataFusion queries in a process,
283+ /// all `SessionContext`'s should be configured with the
284+ /// same `RuntimeEnv`.
251285 pub fn with_config_rt ( config : SessionConfig , runtime : Arc < RuntimeEnv > ) -> Self {
252286 let state = SessionState :: with_config_rt ( config, runtime) ;
253287 Self :: with_state ( state)
254288 }
255289
256- /// Creates a new session context using the provided session state.
290+ /// Creates a new `SessionContext` using the provided [`SessionState`]
257291 pub fn with_state ( state : SessionState ) -> Self {
258292 Self {
259293 session_id : state. session_id . clone ( ) ,
@@ -262,7 +296,7 @@ impl SessionContext {
262296 }
263297 }
264298
265- /// Returns the time this session was created
299+ /// Returns the time this `SessionContext` was created
266300 pub fn session_start_time ( & self ) -> DateTime < Utc > {
267301 self . session_start_time
268302 }
@@ -282,12 +316,12 @@ impl SessionContext {
282316 )
283317 }
284318
285- /// Return the [RuntimeEnv] used to run queries with this [ SessionContext]
319+ /// Return the [RuntimeEnv] used to run queries with this ` SessionContext`
286320 pub fn runtime_env ( & self ) -> Arc < RuntimeEnv > {
287321 self . state . read ( ) . runtime_env . clone ( )
288322 }
289323
290- /// Return the `session_id` of this Session
324+ /// Returns an id that uniquely identifies this `SessionContext`.
291325 pub fn session_id ( & self ) -> String {
292326 self . session_id . clone ( )
293327 }
@@ -1205,7 +1239,7 @@ impl QueryPlanner for DefaultQueryPlanner {
12051239/// Execution context for registering data sources and executing queries
12061240#[ derive( Clone ) ]
12071241pub struct SessionState {
1208- /// UUID for the session
1242+ /// A unique UUID that identifies the session
12091243 session_id : String ,
12101244 /// Responsible for analyzing and rewrite a logical plan before optimization
12111245 analyzer : Analyzer ,
@@ -1252,7 +1286,8 @@ pub fn default_session_builder(config: SessionConfig) -> SessionState {
12521286}
12531287
12541288impl SessionState {
1255- /// Returns new SessionState using the provided configuration and runtime
1289+ /// Returns new [`SessionState`] using the provided
1290+ /// [`SessionConfig`] and [`RuntimeEnv`].
12561291 pub fn with_config_rt ( config : SessionConfig , runtime : Arc < RuntimeEnv > ) -> Self {
12571292 let catalog_list = Arc :: new ( MemoryCatalogList :: new ( ) ) as Arc < dyn CatalogList > ;
12581293 Self :: with_config_rt_and_catalog_list ( config, runtime, catalog_list)
0 commit comments