15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
- //! SessionContext contains methods for registering data sources and executing queries
18
+ //! [` SessionContext`] contains methods for registering data sources and executing queries
19
19
use crate :: {
20
20
catalog:: catalog:: { CatalogList , MemoryCatalogList } ,
21
21
datasource:: {
@@ -158,11 +158,15 @@ where
158
158
}
159
159
}
160
160
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.
164
164
///
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.
166
170
/// * Register a CSV or Parquet data source as a table that can be referenced from a SQL query.
167
171
/// * Register a custom data source that can be referenced from a SQL query.
168
172
/// * Execution a SQL query
@@ -199,6 +203,20 @@ where
199
203
/// # Ok(())
200
204
/// # }
201
205
/// ```
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`].
202
220
#[ derive( Clone ) ]
203
221
pub struct SessionContext {
204
222
/// UUID for the session
@@ -216,7 +234,7 @@ impl Default for SessionContext {
216
234
}
217
235
218
236
impl SessionContext {
219
- /// Creates a new execution context using a default session configuration .
237
+ /// Creates a new `SessionContext` using the default [`SessionConfig`] .
220
238
pub fn new ( ) -> Self {
221
239
Self :: with_config ( SessionConfig :: new ( ) )
222
240
}
@@ -241,19 +259,35 @@ impl SessionContext {
241
259
Ok ( ( ) )
242
260
}
243
261
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.
245
267
pub fn with_config ( config : SessionConfig ) -> Self {
246
268
let runtime = Arc :: new ( RuntimeEnv :: default ( ) ) ;
247
269
Self :: with_config_rt ( config, runtime)
248
270
}
249
271
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`.
251
285
pub fn with_config_rt ( config : SessionConfig , runtime : Arc < RuntimeEnv > ) -> Self {
252
286
let state = SessionState :: with_config_rt ( config, runtime) ;
253
287
Self :: with_state ( state)
254
288
}
255
289
256
- /// Creates a new session context using the provided session state.
290
+ /// Creates a new `SessionContext` using the provided [`SessionState`]
257
291
pub fn with_state ( state : SessionState ) -> Self {
258
292
Self {
259
293
session_id : state. session_id . clone ( ) ,
@@ -262,7 +296,7 @@ impl SessionContext {
262
296
}
263
297
}
264
298
265
- /// Returns the time this session was created
299
+ /// Returns the time this `SessionContext` was created
266
300
pub fn session_start_time ( & self ) -> DateTime < Utc > {
267
301
self . session_start_time
268
302
}
@@ -282,12 +316,12 @@ impl SessionContext {
282
316
)
283
317
}
284
318
285
- /// Return the [RuntimeEnv] used to run queries with this [ SessionContext]
319
+ /// Return the [RuntimeEnv] used to run queries with this ` SessionContext`
286
320
pub fn runtime_env ( & self ) -> Arc < RuntimeEnv > {
287
321
self . state . read ( ) . runtime_env . clone ( )
288
322
}
289
323
290
- /// Return the `session_id` of this Session
324
+ /// Returns an id that uniquely identifies this `SessionContext`.
291
325
pub fn session_id ( & self ) -> String {
292
326
self . session_id . clone ( )
293
327
}
@@ -1205,7 +1239,7 @@ impl QueryPlanner for DefaultQueryPlanner {
1205
1239
/// Execution context for registering data sources and executing queries
1206
1240
#[ derive( Clone ) ]
1207
1241
pub struct SessionState {
1208
- /// UUID for the session
1242
+ /// A unique UUID that identifies the session
1209
1243
session_id : String ,
1210
1244
/// Responsible for analyzing and rewrite a logical plan before optimization
1211
1245
analyzer : Analyzer ,
@@ -1252,7 +1286,8 @@ pub fn default_session_builder(config: SessionConfig) -> SessionState {
1252
1286
}
1253
1287
1254
1288
impl SessionState {
1255
- /// Returns new SessionState using the provided configuration and runtime
1289
+ /// Returns new [`SessionState`] using the provided
1290
+ /// [`SessionConfig`] and [`RuntimeEnv`].
1256
1291
pub fn with_config_rt ( config : SessionConfig , runtime : Arc < RuntimeEnv > ) -> Self {
1257
1292
let catalog_list = Arc :: new ( MemoryCatalogList :: new ( ) ) as Arc < dyn CatalogList > ;
1258
1293
Self :: with_config_rt_and_catalog_list ( config, runtime, catalog_list)
0 commit comments