1use std::sync::Arc;
14
15use datafusion::catalog::DynamicFileCatalog;
16use datafusion::prelude::{SessionConfig, SessionContext};
17
18mod blocking;
19pub mod database;
20pub mod error;
21mod file_table;
22pub mod graph;
23pub mod options;
24pub mod paths;
25pub mod read_functions;
26pub mod serial;
27pub mod stores;
28
29pub use blocking::{run_blocking, BlockingError};
30pub use error::user_message;
31pub use file_table::{open_file_table, open_files, FileFormat, FileOptions};
32pub use stores::{ensure_object_store, ensure_object_store_for};
33
34pub fn new_session() -> SessionContext {
36 new_session_with_config(SessionConfig::new())
37}
38
39pub fn new_session_with_config(config: SessionConfig) -> SessionContext {
47 #[cfg(target_arch = "wasm32")]
48 let config = config.with_target_partitions(1);
49 let config = graph::with_algorithms(graph::with_registry(config.with_information_schema(true)));
50 let config = database::with_database_slot(options::with_options(config));
51 let config = read_functions::with_read_tables(config);
52 let ctx = SessionContext::new_with_config(config);
53 graph::registry_of(&ctx).set_catalogs(Arc::clone(ctx.state().catalog_list()));
55 let factory = Arc::new(file_table::FileTableFactory::default());
58 let catalogs = Arc::new(DynamicFileCatalog::new(
59 Arc::clone(ctx.state().catalog_list()),
60 Arc::clone(&factory) as _,
61 ));
62 let session_id = ctx.session_id();
63 let ctx = ctx
64 .into_state_builder()
65 .with_session_id(session_id)
66 .with_catalog_list(catalogs)
67 .with_query_planner(graph::query_planner())
68 .with_physical_optimizer_rule(graph::edge_order_rule());
69 #[cfg(target_arch = "wasm32")]
71 let ctx = ctx.with_physical_optimizer_rule(Arc::new(serial::SingleTask));
72 let mut state = ctx.build();
73 for ext in ["jsonl", "ndjson"] {
74 state
75 .register_file_format(Arc::new(file_table::JsonLines(ext)), false)
76 .expect("registering a file format cannot fail");
77 }
78 let ctx: SessionContext = state.into();
79 graph::register_functions(&ctx).expect("registering a relation planner cannot fail");
80 let reads = read_functions::ReadFunctions::new(ctx.state().config())
81 .expect("the session config has a place for read_* tables");
82 ctx.register_relation_planner(Arc::new(reads))
83 .expect("registering a relation planner cannot fail");
84 factory.attach(ctx.state_weak_ref());
85 ctx
86}