Struct SessionState

Source
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

Source

pub fn new_with_config_rt( config: SessionConfig, runtime: Arc<RuntimeEnv>, ) -> Self

👎Deprecated since 41.0.0: Use SessionStateBuilder

Returns new SessionState using the provided SessionConfig and RuntimeEnv.

Source

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.

Source

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 AnalyzerRules used to rewrite queries.

Source

pub fn set_function_factory( &mut self, function_factory: Arc<dyn FunctionFactory>, )

Registers a FunctionFactory to handle CREATE FUNCTION statements

Source

pub fn function_factory(&self) -> Option<&Arc<dyn FunctionFactory>>

Get the function factory

Source

pub fn table_factories(&self) -> &HashMap<String, Arc<dyn TableProviderFactory>>

Get the table factories

Source

pub fn table_factories_mut( &mut self, ) -> &mut HashMap<String, Arc<dyn TableProviderFactory>>

Get the table factories

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub async fn statement_to_plan( &self, statement: Statement, ) -> Result<LogicalPlan>

Convert an AST Statement into a LogicalPlan

Source

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.

Source

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

Source

pub fn analyzer(&self) -> &Analyzer

Returns the Analyzer for this session

Source

pub fn optimizer(&self) -> &Optimizer

Returns the Optimizer for this session

Source

pub fn query_planner(&self) -> &Arc<dyn QueryPlanner + Send + Sync>

Returns the QueryPlanner for this session

Source

pub fn optimize(&self, plan: &LogicalPlan) -> Result<LogicalPlan>

Optimizes the logical plan by applying optimizer rules.

Source

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 LogicalPlans such as catalog DDL like CREATE TABLE, which do not have corresponding physical plans and must be handled by another layer, typically SessionContext.

Source

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:
Source

pub fn session_id(&self) -> &str

Return the session ID

Source

pub fn runtime_env(&self) -> &Arc<RuntimeEnv>

Return the runtime env

Source

pub fn execution_props(&self) -> &ExecutionProps

Return the execution properties

Source

pub fn execution_props_mut(&mut self) -> &mut ExecutionProps

Return mutable execution properties

Source

pub fn config(&self) -> &SessionConfig

Return the SessionConfig

Source

pub fn config_mut(&mut self) -> &mut SessionConfig

Return the mutable SessionConfig.

Source

pub fn optimizers(&self) -> &[Arc<dyn OptimizerRule + Send + Sync>]

Return the logical optimizers

Source

pub fn physical_optimizers( &self, ) -> &[Arc<dyn PhysicalOptimizerRule + Send + Sync>]

Return the physical optimizers

Source

pub fn config_options(&self) -> &ConfigOptions

return the configuration options

Source

pub fn table_options(&self) -> &TableOptions

Return the table options

Source

pub fn default_table_options(&self) -> TableOptions

return the TableOptions options with its extensions

Source

pub fn table_options_mut(&mut self) -> &mut TableOptions

Returns a mutable reference to TableOptions

Source

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.

Source

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.

Source

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.

Source

pub fn task_ctx(&self) -> Arc<TaskContext>

Get a new TaskContext to run in this session

Source

pub fn catalog_list(&self) -> &Arc<dyn CatalogProviderList>

Return catalog list

Source

pub fn scalar_functions(&self) -> &HashMap<String, Arc<ScalarUDF>>

Return reference to scalar_functions

Source

pub fn aggregate_functions(&self) -> &HashMap<String, Arc<AggregateUDF>>

Return reference to aggregate_functions

Source

pub fn window_functions(&self) -> &HashMap<String, Arc<WindowUDF>>

Return reference to window functions

Source

pub fn table_functions(&self) -> &HashMap<String, Arc<TableFunction>>

Return reference to table_functions

Source

pub fn serializer_registry(&self) -> &Arc<dyn SerializerRegistry>

Return SerializerRegistry for extensions

Source

pub fn version(&self) -> &str

Return version of the cargo package that produced this query

Source

pub fn register_udtf(&mut self, name: &str, fun: Arc<dyn TableFunctionImpl>)

Register a user defined table function

Source

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

Source§

fn clone(&self) -> SessionState

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SessionState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Prefer having short fields at the top and long vector fields near the end Group fields by

Source§

impl From<&SessionState> for TaskContext

Create a new task context instance from SessionState

Source§

fn from(state: &SessionState) -> Self

Converts to this type from the input type.
Source§

impl From<SessionState> for SessionContext

Source§

fn from(state: SessionState) -> Self

Converts to this type from the input type.
Source§

impl From<SessionState> for SessionStateBuilder

Source§

fn from(state: SessionState) -> Self

Converts to this type from the input type.
Source§

impl FunctionRegistry for SessionState

Source§

fn udfs(&self) -> HashSet<String>

Set of all available udfs.
Source§

fn udf(&self, name: &str) -> Result<Arc<ScalarUDF>>

Returns a reference to the user defined scalar function (udf) named name.
Source§

fn udaf(&self, name: &str) -> Result<Arc<AggregateUDF>>

Returns a reference to the user defined aggregate function (udaf) named name.
Source§

fn udwf(&self, name: &str) -> Result<Arc<WindowUDF>>

Returns a reference to the user defined window function (udwf) named name.
Source§

fn register_udf( &mut self, udf: Arc<ScalarUDF>, ) -> Result<Option<Arc<ScalarUDF>>>

Registers a new ScalarUDF, returning any previously registered implementation. Read more
Source§

fn register_udaf( &mut self, udaf: Arc<AggregateUDF>, ) -> Result<Option<Arc<AggregateUDF>>>

Registers a new AggregateUDF, returning any previously registered implementation. Read more
Source§

fn register_udwf( &mut self, udwf: Arc<WindowUDF>, ) -> Result<Option<Arc<WindowUDF>>>

Registers a new WindowUDF, returning any previously registered implementation. Read more
Source§

fn deregister_udf(&mut self, name: &str) -> Result<Option<Arc<ScalarUDF>>>

Deregisters a ScalarUDF, returning the implementation that was deregistered. Read more
Source§

fn deregister_udaf(&mut self, name: &str) -> Result<Option<Arc<AggregateUDF>>>

Deregisters a AggregateUDF, returning the implementation that was deregistered. Read more
Source§

fn deregister_udwf(&mut self, name: &str) -> Result<Option<Arc<WindowUDF>>>

Deregisters a WindowUDF, returning the implementation that was deregistered. Read more
Source§

fn register_function_rewrite( &mut self, rewrite: Arc<dyn FunctionRewrite + Send + Sync>, ) -> Result<()>

Registers a new FunctionRewrite with the registry. Read more
Source§

fn expr_planners(&self) -> Vec<Arc<dyn ExprPlanner>>

Set of all registered ExprPlanners
Source§

fn register_expr_planner( &mut self, expr_planner: Arc<dyn ExprPlanner>, ) -> Result<()>

Registers a new ExprPlanner with the registry.
Source§

impl OptimizerConfig for SessionState

Source§

fn query_execution_start_time(&self) -> DateTime<Utc>

Return the time at which the query execution started. This time is used as the value for now()
Source§

fn alias_generator(&self) -> &Arc<AliasGenerator>

Return alias generator used to generate unique aliases for subqueries
Source§

fn options(&self) -> &ConfigOptions

Source§

fn function_registry(&self) -> Option<&dyn FunctionRegistry>

Source§

impl Session for SessionState

Source§

fn session_id(&self) -> &str

Return the session ID
Source§

fn config(&self) -> &SessionConfig

Return the 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,

Creates a physical ExecutionPlan plan from a LogicalPlan. Read more
Source§

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. Read more
Source§

fn scalar_functions(&self) -> &HashMap<String, Arc<ScalarUDF>>

Return reference to scalar_functions
Source§

fn aggregate_functions(&self) -> &HashMap<String, Arc<AggregateUDF>>

Return reference to aggregate_functions
Source§

fn window_functions(&self) -> &HashMap<String, Arc<WindowUDF>>

Return reference to window functions
Source§

fn runtime_env(&self) -> &Arc<RuntimeEnv>

Return the runtime env
Source§

fn execution_props(&self) -> &ExecutionProps

Return the execution properties
Source§

fn as_any(&self) -> &dyn Any

Source§

fn table_options(&self) -> &TableOptions

Return the table options
Source§

fn table_options_mut(&mut self) -> &mut TableOptions

Returns a mutable reference to TableOptions
Source§

fn task_ctx(&self) -> Arc<TaskContext>

Get a new TaskContext to run in this session
Source§

fn config_options(&self) -> &ConfigOptions

return the ConfigOptions
Source§

fn default_table_options(&self) -> TableOptions

return the TableOptions options with its extensions

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T

Source§

impl<T> Ungil for T
where T: Send,