1use std::sync::Arc;
14
15use datafusion::catalog::DynamicFileCatalog;
16use datafusion::prelude::{SessionConfig, SessionContext};
17
18mod blocking;
19pub mod database;
20mod duckdb_functions;
21pub mod error;
22mod file_table;
23pub mod graph;
24pub mod notices;
25pub mod options;
26pub mod paths;
27pub mod read_functions;
28pub mod serial;
29pub mod stores;
30
31pub use blocking::{run_blocking, BlockingError};
32pub use error::user_message;
33pub use file_table::{open_file_table, open_files, FileFormat, FileOptions};
34pub use stores::{ensure_object_store, ensure_object_store_for};
35
36pub fn new_session() -> SessionContext {
44 let mut config = SessionConfig::new();
45 config.options_mut().execution.parquet.skip_metadata = false;
46 new_session_with_config(config)
47}
48
49pub fn new_session_with_config(config: SessionConfig) -> SessionContext {
57 #[cfg(target_arch = "wasm32")]
58 let config = config.with_target_partitions(1);
59 let config = graph::with_algorithms(graph::with_registry(config.with_information_schema(true)));
60 let config = database::with_database_slot(options::with_options(config));
61 let config = notices::with_notice_slot(config);
62 let config = read_functions::with_read_tables(config);
63 let ctx = SessionContext::new_with_config(config);
64 graph::registry_of(&ctx).set_catalogs(Arc::clone(ctx.state().catalog_list()));
66 let factory = Arc::new(file_table::FileTableFactory::default());
69 let catalogs = Arc::new(DynamicFileCatalog::new(
70 Arc::clone(ctx.state().catalog_list()),
71 Arc::clone(&factory) as _,
72 ));
73 let session_id = ctx.session_id();
74 let ctx = ctx
75 .into_state_builder()
76 .with_session_id(session_id)
77 .with_catalog_list(catalogs)
78 .with_query_planner(graph::query_planner())
79 .with_physical_optimizer_rule(graph::edge_order_rule());
80 #[cfg(target_arch = "wasm32")]
83 let ctx = ctx.with_physical_optimizer_rule(Arc::new(serial::SingleTask));
84 let ctx = ctx.with_physical_optimizer_rule(graph::collapse_held_rule());
86 let mut state = ctx.build();
87 state
90 .register_file_format(
91 Arc::new(ursa_io::parquet_footer::ValidParquetFactory::new()),
92 true,
93 )
94 .expect("registering a file format cannot fail");
95 if let Some(parquet) = state.table_factories_mut().get_mut("PARQUET") {
98 *parquet = Arc::new(file_table::CheckedParquetTables(Arc::clone(parquet)));
99 }
100 for ext in ["jsonl", "ndjson"] {
101 state
102 .register_file_format(Arc::new(file_table::JsonLines(ext)), false)
103 .expect("registering a file format cannot fail");
104 }
105 let ctx: SessionContext = state.into();
106 graph::register_functions(&ctx).expect("registering a relation planner cannot fail");
107 duckdb_functions::register(&ctx);
109 let reads = read_functions::ReadFunctions::new(ctx.state().config())
110 .expect("the session config has a place for read_* tables");
111 ctx.register_relation_planner(Arc::new(reads))
112 .expect("registering a relation planner cannot fail");
113 factory.attach(ctx.state_weak_ref());
114 ctx
115}