datafusion_python/
utils.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
18use crate::errors::{PyDataFusionError, PyDataFusionResult};
19use crate::TokioRuntime;
20use datafusion::execution::context::SessionContext;
21use datafusion::logical_expr::Volatility;
22use pyo3::exceptions::PyValueError;
23use pyo3::prelude::*;
24use pyo3::types::PyCapsule;
25use std::future::Future;
26use std::sync::OnceLock;
27use tokio::runtime::Runtime;
28
29/// Utility to get the Tokio Runtime from Python
30#[inline]
31pub(crate) fn get_tokio_runtime() -> &'static TokioRuntime {
32    // NOTE: Other pyo3 python libraries have had issues with using tokio
33    // behind a forking app-server like `gunicorn`
34    // If we run into that problem, in the future we can look to `delta-rs`
35    // which adds a check in that disallows calls from a forked process
36    // https://github.com/delta-io/delta-rs/blob/87010461cfe01563d91a4b9cd6fa468e2ad5f283/python/src/utils.rs#L10-L31
37    static RUNTIME: OnceLock<TokioRuntime> = OnceLock::new();
38    RUNTIME.get_or_init(|| TokioRuntime(tokio::runtime::Runtime::new().unwrap()))
39}
40
41/// Utility to get the Global Datafussion CTX
42#[inline]
43pub(crate) fn get_global_ctx() -> &'static SessionContext {
44    static CTX: OnceLock<SessionContext> = OnceLock::new();
45    CTX.get_or_init(SessionContext::new)
46}
47
48/// Utility to collect rust futures with GIL released
49pub fn wait_for_future<F>(py: Python, f: F) -> F::Output
50where
51    F: Future + Send,
52    F::Output: Send,
53{
54    let runtime: &Runtime = &get_tokio_runtime().0;
55    py.allow_threads(|| runtime.block_on(f))
56}
57
58pub(crate) fn parse_volatility(value: &str) -> PyDataFusionResult<Volatility> {
59    Ok(match value {
60        "immutable" => Volatility::Immutable,
61        "stable" => Volatility::Stable,
62        "volatile" => Volatility::Volatile,
63        value => {
64            return Err(PyDataFusionError::Common(format!(
65                "Unsupportad volatility type: `{value}`, supported \
66                 values are: immutable, stable and volatile."
67            )))
68        }
69    })
70}
71
72pub(crate) fn validate_pycapsule(capsule: &Bound<PyCapsule>, name: &str) -> PyResult<()> {
73    let capsule_name = capsule.name()?;
74    if capsule_name.is_none() {
75        return Err(PyValueError::new_err(
76            "Expected schema PyCapsule to have name set.",
77        ));
78    }
79
80    let capsule_name = capsule_name.unwrap().to_str()?;
81    if capsule_name != name {
82        return Err(PyValueError::new_err(format!(
83            "Expected name '{}' in PyCapsule, instead got '{}'",
84            name, capsule_name
85        )));
86    }
87
88    Ok(())
89}