pub struct SessionState { /* private fields */ }
Expand description
SessionState
contains all the necessary state to plan and execute queries,
such as configuration, functions, and runtime environment. Please see the
documentation on SessionContext
for more information.
§Example: SessionState
from a SessionContext
use datafusion::prelude::*;
let ctx = SessionContext::new();
let state = ctx.state();
§Example: SessionState
via SessionStateBuilder
You can also use SessionStateBuilder
to build a SessionState
object
directly:
use datafusion::prelude::*;
let state = SessionStateBuilder::new()
.with_config(SessionConfig::new())
.with_runtime_env(Arc::new(RuntimeEnv::default()))
.with_default_features()
.build();
Ok(())
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
. See SessionStateBuilder
and
SessionContext
.
Implementations§
Source§impl SessionState
impl SessionState
Sourcepub fn new_with_config_rt(
config: SessionConfig,
runtime: Arc<RuntimeEnv>,
) -> Self
👎Deprecated since 41.0.0: Use SessionStateBuilder
pub fn new_with_config_rt( config: SessionConfig, runtime: Arc<RuntimeEnv>, ) -> Self
Returns new SessionState
using the provided
SessionConfig
and RuntimeEnv
.
Sourcepub fn schema_for_ref(
&self,
table_ref: impl Into<TableReference>,
) -> Result<Arc<dyn SchemaProvider>>
pub fn schema_for_ref( &self, table_ref: impl Into<TableReference>, ) -> Result<Arc<dyn SchemaProvider>>
Retrieve the SchemaProvider
for a specific TableReference
, if it
exists.
Sourcepub fn add_analyzer_rule(
&mut self,
analyzer_rule: Arc<dyn AnalyzerRule + Send + Sync>,
) -> &Self
pub fn add_analyzer_rule( &mut self, analyzer_rule: Arc<dyn AnalyzerRule + Send + Sync>, ) -> &Self
Add analyzer_rule
to the end of the list of
AnalyzerRule
s used to rewrite queries.
Sourcepub fn set_function_factory(
&mut self,
function_factory: Arc<dyn FunctionFactory>,
)
pub fn set_function_factory( &mut self, function_factory: Arc<dyn FunctionFactory>, )
Registers a FunctionFactory
to handle CREATE FUNCTION
statements
Sourcepub fn function_factory(&self) -> Option<&Arc<dyn FunctionFactory>>
pub fn function_factory(&self) -> Option<&Arc<dyn FunctionFactory>>
Get the function factory
Sourcepub fn table_factories(&self) -> &HashMap<String, Arc<dyn TableProviderFactory>>
pub fn table_factories(&self) -> &HashMap<String, Arc<dyn TableProviderFactory>>
Get the table factories
Sourcepub fn table_factories_mut(
&mut self,
) -> &mut HashMap<String, Arc<dyn TableProviderFactory>>
pub fn table_factories_mut( &mut self, ) -> &mut HashMap<String, Arc<dyn TableProviderFactory>>
Get the table factories
Sourcepub fn sql_to_statement(&self, sql: &str, dialect: &str) -> Result<Statement>
pub fn sql_to_statement(&self, sql: &str, dialect: &str) -> Result<Statement>
Parse an SQL string into an DataFusion specific AST
Statement
. See SessionContext::sql
for running queries.
Sourcepub fn sql_to_expr(&self, sql: &str, dialect: &str) -> Result<SQLExpr>
pub fn sql_to_expr(&self, sql: &str, dialect: &str) -> Result<SQLExpr>
parse a sql string into a sqlparser-rs AST SQLExpr
.
See Self::create_logical_expr
for parsing sql to Expr
.
Sourcepub fn sql_to_expr_with_alias(
&self,
sql: &str,
dialect: &str,
) -> Result<SQLExprWithAlias>
pub fn sql_to_expr_with_alias( &self, sql: &str, dialect: &str, ) -> Result<SQLExprWithAlias>
parse a sql string into a sqlparser-rs AST SQLExprWithAlias
.
See Self::create_logical_expr
for parsing sql to Expr
.
Sourcepub fn resolve_table_references(
&self,
statement: &Statement,
) -> Result<Vec<TableReference>>
pub fn resolve_table_references( &self, statement: &Statement, ) -> Result<Vec<TableReference>>
Resolve all table references in the SQL statement. Does not include CTE references.
See datafusion_sql::resolve::resolve_table_references
for more information.
Sourcepub async fn statement_to_plan(
&self,
statement: Statement,
) -> Result<LogicalPlan>
pub async fn statement_to_plan( &self, statement: Statement, ) -> Result<LogicalPlan>
Convert an AST Statement into a LogicalPlan
Sourcepub async fn create_logical_plan(&self, sql: &str) -> Result<LogicalPlan>
pub async fn create_logical_plan(&self, sql: &str) -> Result<LogicalPlan>
Creates a LogicalPlan
from the provided SQL string. This
interface will plan any SQL DataFusion supports, including DML
like CREATE TABLE
, and COPY
(which can write to local
files.
See SessionContext::sql
and
SessionContext::sql_with_options
for a higher-level
interface that handles DDL and verification of allowed
statements.
Sourcepub fn create_logical_expr(
&self,
sql: &str,
df_schema: &DFSchema,
) -> Result<Expr>
pub fn create_logical_expr( &self, sql: &str, df_schema: &DFSchema, ) -> Result<Expr>
Creates a datafusion style AST Expr
from a SQL string.
See example on SessionContext::parse_sql_expr
Sourcepub fn query_planner(&self) -> &Arc<dyn QueryPlanner + Send + Sync>
pub fn query_planner(&self) -> &Arc<dyn QueryPlanner + Send + Sync>
Returns the QueryPlanner
for this session
Sourcepub fn optimize(&self, plan: &LogicalPlan) -> Result<LogicalPlan>
pub fn optimize(&self, plan: &LogicalPlan) -> Result<LogicalPlan>
Optimizes the logical plan by applying optimizer rules.
Sourcepub async fn create_physical_plan(
&self,
logical_plan: &LogicalPlan,
) -> Result<Arc<dyn ExecutionPlan>>
pub async fn create_physical_plan( &self, logical_plan: &LogicalPlan, ) -> Result<Arc<dyn ExecutionPlan>>
Creates a physical ExecutionPlan
plan from a LogicalPlan
.
Note: this first calls Self::optimize
on the provided
plan.
This function will error for LogicalPlan
s such as catalog DDL like
CREATE TABLE
, which do not have corresponding physical plans and must
be handled by another layer, typically SessionContext
.
Sourcepub fn create_physical_expr(
&self,
expr: Expr,
df_schema: &DFSchema,
) -> Result<Arc<dyn PhysicalExpr>>
pub fn create_physical_expr( &self, expr: Expr, df_schema: &DFSchema, ) -> Result<Arc<dyn PhysicalExpr>>
Create a PhysicalExpr
from an Expr
after applying type
coercion, and function rewrites.
Note: The expression is not simplified or otherwise optimized:
a = 1 + 2
will not be simplified to a = 3
as this is a more involved process.
See the expr_api example for how to simplify expressions.
§See Also:
SessionContext::create_physical_expr
for a higher-level APIcreate_physical_expr
for a lower-level API
Sourcepub fn session_id(&self) -> &str
pub fn session_id(&self) -> &str
Return the session ID
Sourcepub fn runtime_env(&self) -> &Arc<RuntimeEnv>
pub fn runtime_env(&self) -> &Arc<RuntimeEnv>
Return the runtime env
Sourcepub fn execution_props(&self) -> &ExecutionProps
pub fn execution_props(&self) -> &ExecutionProps
Return the execution properties
Sourcepub fn execution_props_mut(&mut self) -> &mut ExecutionProps
pub fn execution_props_mut(&mut self) -> &mut ExecutionProps
Return mutable execution properties
Sourcepub fn config(&self) -> &SessionConfig
pub fn config(&self) -> &SessionConfig
Return the SessionConfig
Sourcepub fn config_mut(&mut self) -> &mut SessionConfig
pub fn config_mut(&mut self) -> &mut SessionConfig
Return the mutable SessionConfig
.
Sourcepub fn optimizers(&self) -> &[Arc<dyn OptimizerRule + Send + Sync>]
pub fn optimizers(&self) -> &[Arc<dyn OptimizerRule + Send + Sync>]
Return the logical optimizers
Sourcepub fn physical_optimizers(
&self,
) -> &[Arc<dyn PhysicalOptimizerRule + Send + Sync>]
pub fn physical_optimizers( &self, ) -> &[Arc<dyn PhysicalOptimizerRule + Send + Sync>]
Return the physical optimizers
Sourcepub fn config_options(&self) -> &ConfigOptions
pub fn config_options(&self) -> &ConfigOptions
return the configuration options
Sourcepub fn table_options(&self) -> &TableOptions
pub fn table_options(&self) -> &TableOptions
Return the table options
Sourcepub fn default_table_options(&self) -> TableOptions
pub fn default_table_options(&self) -> TableOptions
return the TableOptions options with its extensions
Sourcepub fn table_options_mut(&mut self) -> &mut TableOptions
pub fn table_options_mut(&mut self) -> &mut TableOptions
Returns a mutable reference to TableOptions
Sourcepub fn register_table_options_extension<T: ConfigExtension>(
&mut self,
extension: T,
)
pub fn register_table_options_extension<T: ConfigExtension>( &mut self, extension: T, )
Registers a ConfigExtension
as a table option extension that can be
referenced from SQL statements executed against this context.
Sourcepub fn register_file_format(
&mut self,
file_format: Arc<dyn FileFormatFactory>,
overwrite: bool,
) -> Result<(), DataFusionError>
pub fn register_file_format( &mut self, file_format: Arc<dyn FileFormatFactory>, overwrite: bool, ) -> Result<(), DataFusionError>
Adds or updates a FileFormatFactory which can be used with COPY TO or CREATE EXTERNAL TABLE statements for reading and writing files of custom formats.
Sourcepub fn get_file_format_factory(
&self,
ext: &str,
) -> Option<Arc<dyn FileFormatFactory>>
pub fn get_file_format_factory( &self, ext: &str, ) -> Option<Arc<dyn FileFormatFactory>>
Retrieves a FileFormatFactory based on file extension which has been registered via SessionContext::register_file_format. Extensions are not case sensitive.
Sourcepub fn task_ctx(&self) -> Arc<TaskContext>
pub fn task_ctx(&self) -> Arc<TaskContext>
Get a new TaskContext to run in this session
Sourcepub fn catalog_list(&self) -> &Arc<dyn CatalogProviderList>
pub fn catalog_list(&self) -> &Arc<dyn CatalogProviderList>
Return catalog list
Sourcepub fn scalar_functions(&self) -> &HashMap<String, Arc<ScalarUDF>>
pub fn scalar_functions(&self) -> &HashMap<String, Arc<ScalarUDF>>
Return reference to scalar_functions
Sourcepub fn aggregate_functions(&self) -> &HashMap<String, Arc<AggregateUDF>>
pub fn aggregate_functions(&self) -> &HashMap<String, Arc<AggregateUDF>>
Return reference to aggregate_functions
Sourcepub fn window_functions(&self) -> &HashMap<String, Arc<WindowUDF>>
pub fn window_functions(&self) -> &HashMap<String, Arc<WindowUDF>>
Return reference to window functions
Sourcepub fn table_functions(&self) -> &HashMap<String, Arc<TableFunction>>
pub fn table_functions(&self) -> &HashMap<String, Arc<TableFunction>>
Return reference to table_functions
Sourcepub fn serializer_registry(&self) -> &Arc<dyn SerializerRegistry>
pub fn serializer_registry(&self) -> &Arc<dyn SerializerRegistry>
Return SerializerRegistry for extensions
Sourcepub fn register_udtf(&mut self, name: &str, fun: Arc<dyn TableFunctionImpl>)
pub fn register_udtf(&mut self, name: &str, fun: Arc<dyn TableFunctionImpl>)
Register a user defined table function
Sourcepub fn deregister_udtf(
&mut self,
name: &str,
) -> Result<Option<Arc<dyn TableFunctionImpl>>>
pub fn deregister_udtf( &mut self, name: &str, ) -> Result<Option<Arc<dyn TableFunctionImpl>>>
Deregister a user defined table function
Trait Implementations§
Source§impl Clone for SessionState
impl Clone for SessionState
Source§fn clone(&self) -> SessionState
fn clone(&self) -> SessionState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for SessionState
impl Debug for SessionState
Source§impl From<&SessionState> for TaskContext
Create a new task context instance from SessionState
impl From<&SessionState> for TaskContext
Create a new task context instance from SessionState
Source§fn from(state: &SessionState) -> Self
fn from(state: &SessionState) -> Self
Source§impl From<SessionState> for SessionContext
impl From<SessionState> for SessionContext
Source§fn from(state: SessionState) -> Self
fn from(state: SessionState) -> Self
Source§impl From<SessionState> for SessionStateBuilder
impl From<SessionState> for SessionStateBuilder
Source§fn from(state: SessionState) -> Self
fn from(state: SessionState) -> Self
Source§impl FunctionRegistry for SessionState
impl FunctionRegistry for SessionState
Source§fn udf(&self, name: &str) -> Result<Arc<ScalarUDF>>
fn udf(&self, name: &str) -> Result<Arc<ScalarUDF>>
name
.Source§fn udaf(&self, name: &str) -> Result<Arc<AggregateUDF>>
fn udaf(&self, name: &str) -> Result<Arc<AggregateUDF>>
name
.Source§fn udwf(&self, name: &str) -> Result<Arc<WindowUDF>>
fn udwf(&self, name: &str) -> Result<Arc<WindowUDF>>
name
.Source§fn register_udaf(
&mut self,
udaf: Arc<AggregateUDF>,
) -> Result<Option<Arc<AggregateUDF>>>
fn register_udaf( &mut self, udaf: Arc<AggregateUDF>, ) -> Result<Option<Arc<AggregateUDF>>>
AggregateUDF
, returning any previously registered
implementation. Read moreSource§fn deregister_udaf(&mut self, name: &str) -> Result<Option<Arc<AggregateUDF>>>
fn deregister_udaf(&mut self, name: &str) -> Result<Option<Arc<AggregateUDF>>>
AggregateUDF
, returning the implementation that was
deregistered. Read moreSource§fn register_function_rewrite(
&mut self,
rewrite: Arc<dyn FunctionRewrite + Send + Sync>,
) -> Result<()>
fn register_function_rewrite( &mut self, rewrite: Arc<dyn FunctionRewrite + Send + Sync>, ) -> Result<()>
FunctionRewrite
with the registry. Read moreSource§fn expr_planners(&self) -> Vec<Arc<dyn ExprPlanner>>
fn expr_planners(&self) -> Vec<Arc<dyn ExprPlanner>>
ExprPlanner
sSource§fn register_expr_planner(
&mut self,
expr_planner: Arc<dyn ExprPlanner>,
) -> Result<()>
fn register_expr_planner( &mut self, expr_planner: Arc<dyn ExprPlanner>, ) -> Result<()>
ExprPlanner
with the registry.Source§impl OptimizerConfig for SessionState
impl OptimizerConfig for SessionState
Source§fn query_execution_start_time(&self) -> DateTime<Utc>
fn query_execution_start_time(&self) -> DateTime<Utc>
Source§fn alias_generator(&self) -> &Arc<AliasGenerator>
fn alias_generator(&self) -> &Arc<AliasGenerator>
fn options(&self) -> &ConfigOptions
fn function_registry(&self) -> Option<&dyn FunctionRegistry>
Source§impl Session for SessionState
impl Session for SessionState
Source§fn session_id(&self) -> &str
fn session_id(&self) -> &str
Source§fn config(&self) -> &SessionConfig
fn config(&self) -> &SessionConfig
SessionConfig
Source§fn create_physical_plan<'life0, 'life1, 'async_trait>(
&'life0 self,
logical_plan: &'life1 LogicalPlan,
) -> Pin<Box<dyn Future<Output = Result<Arc<dyn ExecutionPlan>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn create_physical_plan<'life0, 'life1, 'async_trait>(
&'life0 self,
logical_plan: &'life1 LogicalPlan,
) -> Pin<Box<dyn Future<Output = Result<Arc<dyn ExecutionPlan>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn create_physical_expr(
&self,
expr: Expr,
df_schema: &DFSchema,
) -> Result<Arc<dyn PhysicalExpr>>
fn create_physical_expr( &self, expr: Expr, df_schema: &DFSchema, ) -> Result<Arc<dyn PhysicalExpr>>
Source§fn scalar_functions(&self) -> &HashMap<String, Arc<ScalarUDF>>
fn scalar_functions(&self) -> &HashMap<String, Arc<ScalarUDF>>
Source§fn aggregate_functions(&self) -> &HashMap<String, Arc<AggregateUDF>>
fn aggregate_functions(&self) -> &HashMap<String, Arc<AggregateUDF>>
Source§fn window_functions(&self) -> &HashMap<String, Arc<WindowUDF>>
fn window_functions(&self) -> &HashMap<String, Arc<WindowUDF>>
Source§fn runtime_env(&self) -> &Arc<RuntimeEnv>
fn runtime_env(&self) -> &Arc<RuntimeEnv>
Source§fn execution_props(&self) -> &ExecutionProps
fn execution_props(&self) -> &ExecutionProps
fn as_any(&self) -> &dyn Any
Source§fn table_options(&self) -> &TableOptions
fn table_options(&self) -> &TableOptions
Source§fn table_options_mut(&mut self) -> &mut TableOptions
fn table_options_mut(&mut self) -> &mut TableOptions
TableOptions
Source§fn task_ctx(&self) -> Arc<TaskContext>
fn task_ctx(&self) -> Arc<TaskContext>
Source§fn config_options(&self) -> &ConfigOptions
fn config_options(&self) -> &ConfigOptions
ConfigOptions
Source§fn default_table_options(&self) -> TableOptions
fn default_table_options(&self) -> TableOptions
Auto Trait Implementations§
impl Freeze for SessionState
impl !RefUnwindSafe for SessionState
impl Send for SessionState
impl Sync for SessionState
impl Unpin for SessionState
impl !UnwindSafe for SessionState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more