Skip to main content

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::{
20    self, common as datafusion_common, logical_expr as datafusion_expr, optimizer,
21    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;
46mod options;
47pub mod physical_plan;
48mod pyarrow_filter_expression;
49pub mod pyarrow_util;
50mod record_batch;
51pub mod sql;
52pub mod store;
53pub mod table;
54pub mod unparser;
55
56mod array;
57#[cfg(feature = "substrait")]
58pub mod substrait;
59#[allow(clippy::borrow_deref_ref)]
60mod udaf;
61#[allow(clippy::borrow_deref_ref)]
62mod udf;
63pub mod udtf;
64mod udwf;
65
66#[cfg(feature = "mimalloc")]
67#[global_allocator]
68static GLOBAL: MiMalloc = MiMalloc;
69
70/// Low-level DataFusion internal package.
71///
72/// The higher-level public API is defined in pure python files under the
73/// datafusion directory.
74#[pymodule]
75fn _internal(py: Python, m: Bound<'_, PyModule>) -> PyResult<()> {
76    // Initialize logging
77    pyo3_log::init();
78
79    // Register the python classes
80    m.add_class::<context::PyRuntimeEnvBuilder>()?;
81    m.add_class::<context::PySessionConfig>()?;
82    m.add_class::<context::PySessionContext>()?;
83    m.add_class::<context::PySQLOptions>()?;
84    m.add_class::<dataframe::PyDataFrame>()?;
85    m.add_class::<dataframe::PyInsertOp>()?;
86    m.add_class::<dataframe::PyDataFrameWriteOptions>()?;
87    m.add_class::<dataframe::PyParquetColumnOptions>()?;
88    m.add_class::<dataframe::PyParquetWriterOptions>()?;
89    m.add_class::<udf::PyScalarUDF>()?;
90    m.add_class::<udaf::PyAggregateUDF>()?;
91    m.add_class::<udwf::PyWindowUDF>()?;
92    m.add_class::<udtf::PyTableFunction>()?;
93    m.add_class::<config::PyConfig>()?;
94    m.add_class::<sql::logical::PyLogicalPlan>()?;
95    m.add_class::<physical_plan::PyExecutionPlan>()?;
96    m.add_class::<record_batch::PyRecordBatch>()?;
97    m.add_class::<record_batch::PyRecordBatchStream>()?;
98
99    let catalog = PyModule::new(py, "catalog")?;
100    catalog::init_module(&catalog)?;
101    m.add_submodule(&catalog)?;
102
103    // Register `common` as a submodule. Matching `datafusion-common` https://docs.rs/datafusion-common/latest/datafusion_common/
104    let common = PyModule::new(py, "common")?;
105    common::init_module(&common)?;
106    m.add_submodule(&common)?;
107
108    // Register `expr` as a submodule. Matching `datafusion-expr` https://docs.rs/datafusion-expr/latest/datafusion_expr/
109    let expr = PyModule::new(py, "expr")?;
110    expr::init_module(&expr)?;
111    m.add_submodule(&expr)?;
112
113    let unparser = PyModule::new(py, "unparser")?;
114    unparser::init_module(&unparser)?;
115    m.add_submodule(&unparser)?;
116
117    // Register the functions as a submodule
118    let funcs = PyModule::new(py, "functions")?;
119    functions::init_module(&funcs)?;
120    m.add_submodule(&funcs)?;
121
122    let store = PyModule::new(py, "object_store")?;
123    store::init_module(&store)?;
124    m.add_submodule(&store)?;
125
126    let options = PyModule::new(py, "options")?;
127    options::init_module(&options)?;
128    m.add_submodule(&options)?;
129
130    // Register substrait as a submodule
131    #[cfg(feature = "substrait")]
132    setup_substrait_module(py, &m)?;
133
134    Ok(())
135}
136
137#[cfg(feature = "substrait")]
138fn setup_substrait_module(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
139    let substrait = PyModule::new(py, "substrait")?;
140    substrait::init_module(&substrait)?;
141    m.add_submodule(&substrait)?;
142    Ok(())
143}