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