datafusion_python/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18// Re-export Apache Arrow DataFusion dependencies
19pub use datafusion;
20pub use datafusion::{
21    common as datafusion_common, logical_expr as datafusion_expr, optimizer, sql as datafusion_sql,
22};
23#[cfg(feature = "substrait")]
24pub use datafusion_substrait;
25#[cfg(feature = "mimalloc")]
26use mimalloc::MiMalloc;
27use pyo3::prelude::*;
28
29#[allow(clippy::borrow_deref_ref)]
30pub mod catalog;
31pub mod common;
32
33#[allow(clippy::borrow_deref_ref)]
34mod config;
35#[allow(clippy::borrow_deref_ref)]
36pub mod context;
37#[allow(clippy::borrow_deref_ref)]
38pub mod dataframe;
39mod dataset;
40mod dataset_exec;
41pub mod errors;
42#[allow(clippy::borrow_deref_ref)]
43pub mod expr;
44#[allow(clippy::borrow_deref_ref)]
45mod functions;
46pub mod physical_plan;
47mod pyarrow_filter_expression;
48pub mod pyarrow_util;
49mod record_batch;
50pub mod sql;
51pub mod store;
52pub mod table;
53pub mod unparser;
54
55#[cfg(feature = "substrait")]
56pub mod substrait;
57#[allow(clippy::borrow_deref_ref)]
58mod udaf;
59#[allow(clippy::borrow_deref_ref)]
60mod udf;
61pub mod udtf;
62mod udwf;
63pub mod utils;
64
65#[cfg(feature = "mimalloc")]
66#[global_allocator]
67static GLOBAL: MiMalloc = MiMalloc;
68
69// Used to define Tokio Runtime as a Python module attribute
70pub(crate) struct TokioRuntime(tokio::runtime::Runtime);
71
72/// Low-level DataFusion internal package.
73///
74/// The higher-level public API is defined in pure python files under the
75/// datafusion directory.
76#[pymodule]
77fn _internal(py: Python, m: Bound<'_, PyModule>) -> PyResult<()> {
78    // Initialize logging
79    pyo3_log::init();
80
81    // Register the python classes
82    m.add_class::<context::PyRuntimeEnvBuilder>()?;
83    m.add_class::<context::PySessionConfig>()?;
84    m.add_class::<context::PySessionContext>()?;
85    m.add_class::<context::PySQLOptions>()?;
86    m.add_class::<dataframe::PyDataFrame>()?;
87    m.add_class::<dataframe::PyInsertOp>()?;
88    m.add_class::<dataframe::PyDataFrameWriteOptions>()?;
89    m.add_class::<dataframe::PyParquetColumnOptions>()?;
90    m.add_class::<dataframe::PyParquetWriterOptions>()?;
91    m.add_class::<udf::PyScalarUDF>()?;
92    m.add_class::<udaf::PyAggregateUDF>()?;
93    m.add_class::<udwf::PyWindowUDF>()?;
94    m.add_class::<udtf::PyTableFunction>()?;
95    m.add_class::<config::PyConfig>()?;
96    m.add_class::<sql::logical::PyLogicalPlan>()?;
97    m.add_class::<physical_plan::PyExecutionPlan>()?;
98    m.add_class::<record_batch::PyRecordBatch>()?;
99    m.add_class::<record_batch::PyRecordBatchStream>()?;
100
101    let catalog = PyModule::new(py, "catalog")?;
102    catalog::init_module(&catalog)?;
103    m.add_submodule(&catalog)?;
104
105    // Register `common` as a submodule. Matching `datafusion-common` https://docs.rs/datafusion-common/latest/datafusion_common/
106    let common = PyModule::new(py, "common")?;
107    common::init_module(&common)?;
108    m.add_submodule(&common)?;
109
110    // Register `expr` as a submodule. Matching `datafusion-expr` https://docs.rs/datafusion-expr/latest/datafusion_expr/
111    let expr = PyModule::new(py, "expr")?;
112    expr::init_module(&expr)?;
113    m.add_submodule(&expr)?;
114
115    let unparser = PyModule::new(py, "unparser")?;
116    unparser::init_module(&unparser)?;
117    m.add_submodule(&unparser)?;
118
119    // Register the functions as a submodule
120    let funcs = PyModule::new(py, "functions")?;
121    functions::init_module(&funcs)?;
122    m.add_submodule(&funcs)?;
123
124    let store = PyModule::new(py, "object_store")?;
125    store::init_module(&store)?;
126    m.add_submodule(&store)?;
127
128    // Register substrait as a submodule
129    #[cfg(feature = "substrait")]
130    setup_substrait_module(py, &m)?;
131
132    Ok(())
133}
134
135#[cfg(feature = "substrait")]
136fn setup_substrait_module(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
137    let substrait = PyModule::new(py, "substrait")?;
138    substrait::init_module(&substrait)?;
139    m.add_submodule(&substrait)?;
140    Ok(())
141}