datafusion_ffi/
util.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 std::sync::Arc;
19
20use abi_stable::std_types::{RResult, RString, RVec};
21use arrow::datatypes::{DataType, Field};
22use arrow::ffi::FFI_ArrowSchema;
23use arrow_schema::FieldRef;
24
25use crate::arrow_wrappers::WrappedSchema;
26
27/// Convenience type for results passed through the FFI boundary. Since the
28/// `DataFusionError` enum is complex and little value is gained from creating
29/// a FFI safe variant of it, we convert errors to strings when passing results
30/// back. These are converted back and forth using the `df_result`, `rresult`,
31/// and `rresult_return` macros.
32pub type FFIResult<T> = RResult<T, RString>;
33
34/// This macro is a helpful conversion utility to convert from an abi_stable::RResult to a
35/// DataFusion result.
36#[macro_export]
37macro_rules! df_result {
38    ( $x:expr ) => {
39        match $x {
40            abi_stable::std_types::RResult::ROk(v) => Ok(v),
41            abi_stable::std_types::RResult::RErr(err) => {
42                datafusion_common::ffi_err!("{err}")
43            }
44        }
45    };
46}
47
48/// This macro is a helpful conversion utility to convert from a DataFusion Result to an abi_stable::RResult
49#[macro_export]
50macro_rules! rresult {
51    ( $x:expr ) => {
52        match $x {
53            Ok(v) => abi_stable::std_types::RResult::ROk(v),
54            Err(e) => abi_stable::std_types::RResult::RErr(
55                abi_stable::std_types::RString::from(e.to_string()),
56            ),
57        }
58    };
59}
60
61/// This macro is a helpful conversion utility to convert from a DataFusion Result to an abi_stable::RResult
62/// and to also call return when it is an error. Since you cannot use `?` on an RResult, this is designed
63/// to mimic the pattern.
64#[macro_export]
65macro_rules! rresult_return {
66    ( $x:expr ) => {
67        match $x {
68            Ok(v) => v,
69            Err(e) => {
70                return abi_stable::std_types::RResult::RErr(
71                    abi_stable::std_types::RString::from(e.to_string()),
72                )
73            }
74        }
75    };
76}
77
78/// This is a utility function to convert a slice of [`Field`] to its equivalent
79/// FFI friendly counterpart, [`WrappedSchema`]
80pub fn vec_fieldref_to_rvec_wrapped(
81    fields: &[FieldRef],
82) -> Result<RVec<WrappedSchema>, arrow::error::ArrowError> {
83    Ok(fields
84        .iter()
85        .map(FFI_ArrowSchema::try_from)
86        .collect::<Result<Vec<_>, arrow::error::ArrowError>>()?
87        .into_iter()
88        .map(WrappedSchema)
89        .collect())
90}
91
92/// This is a utility function to convert an FFI friendly vector of [`WrappedSchema`]
93/// to their equivalent [`Field`].
94pub fn rvec_wrapped_to_vec_fieldref(
95    fields: &RVec<WrappedSchema>,
96) -> Result<Vec<FieldRef>, arrow::error::ArrowError> {
97    fields
98        .iter()
99        .map(|d| Field::try_from(&d.0).map(Arc::new))
100        .collect()
101}
102
103/// This is a utility function to convert a slice of [`DataType`] to its equivalent
104/// FFI friendly counterpart, [`WrappedSchema`]
105pub fn vec_datatype_to_rvec_wrapped(
106    data_types: &[DataType],
107) -> Result<RVec<WrappedSchema>, arrow::error::ArrowError> {
108    Ok(data_types
109        .iter()
110        .map(FFI_ArrowSchema::try_from)
111        .collect::<Result<Vec<_>, arrow::error::ArrowError>>()?
112        .into_iter()
113        .map(WrappedSchema)
114        .collect())
115}
116
117/// This is a utility function to convert an FFI friendly vector of [`WrappedSchema`]
118/// to their equivalent [`DataType`].
119pub fn rvec_wrapped_to_vec_datatype(
120    data_types: &RVec<WrappedSchema>,
121) -> Result<Vec<DataType>, arrow::error::ArrowError> {
122    data_types
123        .iter()
124        .map(|d| DataType::try_from(&d.0))
125        .collect()
126}
127
128#[cfg(test)]
129pub(crate) mod tests {
130    use std::sync::Arc;
131
132    use abi_stable::std_types::{RResult, RString};
133    use datafusion::error::DataFusionError;
134    use datafusion::prelude::SessionContext;
135    use datafusion_execution::TaskContextProvider;
136
137    use crate::execution::FFI_TaskContextProvider;
138    use crate::util::FFIResult;
139
140    pub(crate) fn test_session_and_ctx() -> (Arc<SessionContext>, FFI_TaskContextProvider)
141    {
142        let ctx = Arc::new(SessionContext::new());
143        let task_ctx_provider = Arc::clone(&ctx) as Arc<dyn TaskContextProvider>;
144        let task_ctx_provider = FFI_TaskContextProvider::from(&task_ctx_provider);
145
146        (ctx, task_ctx_provider)
147    }
148
149    fn wrap_result(result: Result<String, DataFusionError>) -> FFIResult<String> {
150        RResult::ROk(rresult_return!(result))
151    }
152
153    #[test]
154    fn test_conversion() {
155        const VALID_VALUE: &str = "valid_value";
156        const ERROR_VALUE: &str = "error_value";
157
158        let ok_r_result: FFIResult<RString> =
159            RResult::ROk(VALID_VALUE.to_string().into());
160        let err_r_result: FFIResult<RString> =
161            RResult::RErr(ERROR_VALUE.to_string().into());
162
163        let returned_ok_result = df_result!(ok_r_result);
164        assert!(returned_ok_result.is_ok());
165        assert!(returned_ok_result.unwrap().to_string() == VALID_VALUE);
166
167        let returned_err_result = df_result!(err_r_result);
168        assert!(returned_err_result.is_err());
169        assert!(
170            returned_err_result.unwrap_err().strip_backtrace()
171                == format!("FFI error: {ERROR_VALUE}")
172        );
173
174        let ok_result: Result<String, DataFusionError> = Ok(VALID_VALUE.to_string());
175        let err_result: Result<String, DataFusionError> =
176            datafusion_common::ffi_err!("{ERROR_VALUE}");
177
178        let returned_ok_r_result = wrap_result(ok_result);
179        assert!(returned_ok_r_result == RResult::ROk(VALID_VALUE.into()));
180
181        let returned_err_r_result = wrap_result(err_result);
182        assert!(returned_err_r_result.is_err());
183        assert!(
184            returned_err_r_result
185                .unwrap_err()
186                .starts_with(format!("FFI error: {ERROR_VALUE}").as_str())
187        );
188    }
189}