Skip to content

Commit de389be

Browse files
committed
rebase_main
1 parent c3e66f7 commit de389be

20 files changed

Lines changed: 103 additions & 111 deletions

datafusion/ffi/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ challenges for our use case:
9191
function pointers that reference `&Self`, which complicates `IStable`
9292
implementations.
9393

94-
3. **FFI_Option and FFI_Result**: For similar reasons, we provide our own
95-
`FFI_Option<T>` and `FFI_Result<T, E>` types using `#[repr(C, u8)]` instead
94+
3. **FFI_Option and FFIResult**: For similar reasons, we provide our own
95+
`FFI_Option<T>` and `FFIResult<T>` types using `#[repr(C, u8)]` instead
9696
of stabby's `Option` and `Result`, which require inner types to be `IStable`.
9797

9898
This hybrid approach gives us stabby's maintained, ABI-stable collection types

datafusion/ffi/src/catalog_provider.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use tokio::runtime::Handle;
3030
use crate::execution::FFI_TaskContextProvider;
3131
use crate::proto::logical_extension_codec::FFI_LogicalExtensionCodec;
3232
use crate::schema_provider::{FFI_SchemaProvider, ForeignSchemaProvider};
33-
use crate::util::{FFI_Option, FFI_Result, FFIResult};
33+
use crate::util::{FFI_Option, FFIResult};
3434
use crate::{df_result, sresult_return};
3535

3636
/// A stable struct for sharing [`CatalogProvider`] across FFI boundaries.
@@ -152,7 +152,7 @@ unsafe extern "C" fn register_schema_fn_wrapper(
152152
})
153153
.into();
154154

155-
FFI_Result::Ok(returned_schema)
155+
FFIResult::Ok(returned_schema)
156156
}
157157
}
158158

@@ -168,7 +168,7 @@ unsafe extern "C" fn deregister_schema_fn_wrapper(
168168
let maybe_schema =
169169
sresult_return!(inner_provider.deregister_schema(name.as_str(), cascade));
170170

171-
FFI_Result::Ok(
171+
FFIResult::Ok(
172172
maybe_schema
173173
.map(|schema| {
174174
FFI_SchemaProvider::new_with_ffi_codec(

datafusion/ffi/src/config/extension_options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use stabby::string::String as SString;
2727
use stabby::vec::Vec as SVec;
2828

2929
use crate::df_result;
30-
use crate::util::{FFI_Result, FFIResult};
30+
use crate::util::{FFIResult};
3131

3232
/// A stable struct for sharing [`ExtensionOptions`] across FFI boundaries.
3333
///
@@ -99,7 +99,7 @@ unsafe extern "C" fn set_fn_wrapper(
9999
let _ = options
100100
.inner_mut()
101101
.insert(key.as_str().into(), value.as_str().into());
102-
FFI_Result::Ok(())
102+
FFIResult::Ok(())
103103
}
104104

105105
unsafe extern "C" fn entries_fn_wrapper(

datafusion/ffi/src/execution_plan.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ unsafe extern "C" fn with_new_children_fn_wrapper(
137137
let children = sresult_return!(children);
138138
let new_plan = sresult_return!(inner_plan.with_new_children(children));
139139

140-
crate::ffi_option::FFI_Result::Ok(FFI_ExecutionPlan::new(new_plan, runtime))
140+
FFIResult::Ok(FFI_ExecutionPlan::new(new_plan, runtime))
141141
}
142142

143143
unsafe extern "C" fn execute_fn_wrapper(

datafusion/ffi/src/ffi_option.rs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
//! pointers and cannot implement `IStable`. These simple `#[repr(C)]` types
2323
//! provide the same FFI-safe semantics without that constraint.
2424
25+
use stabby::string::String as SString;
26+
2527
/// An FFI-safe option type.
2628
#[repr(C, u8)]
2729
#[derive(Debug, Clone)]
@@ -68,65 +70,65 @@ impl<T> FFI_Option<T> {
6870
}
6971
}
7072

71-
/// An FFI-safe result type.
73+
/// An FFI-safe result type with SString as the error type.
7274
#[repr(C, u8)]
7375
#[derive(Debug, Clone)]
74-
pub enum FFI_Result<T, E> {
76+
pub enum FFIResult<T> {
7577
Ok(T),
76-
Err(E),
77-
}
78-
79-
impl<T, E> From<Result<T, E>> for FFI_Result<T, E> {
80-
fn from(res: Result<T, E>) -> Self {
81-
match res {
82-
Ok(v) => FFI_Result::Ok(v),
83-
Err(e) => FFI_Result::Err(e),
84-
}
85-
}
86-
}
87-
88-
impl<T, E> From<FFI_Result<T, E>> for Result<T, E> {
89-
fn from(res: FFI_Result<T, E>) -> Self {
90-
match res {
91-
FFI_Result::Ok(v) => Ok(v),
92-
FFI_Result::Err(e) => Err(e),
93-
}
94-
}
78+
Err(SString),
9579
}
9680

97-
impl<T, E> FFI_Result<T, E> {
81+
impl<T> FFIResult<T> {
9882
pub fn is_ok(&self) -> bool {
99-
matches!(self, FFI_Result::Ok(_))
83+
matches!(self, FFIResult::Ok(_))
10084
}
10185

10286
pub fn is_err(&self) -> bool {
103-
matches!(self, FFI_Result::Err(_))
87+
matches!(self, FFIResult::Err(_))
10488
}
10589

106-
pub fn unwrap_err(self) -> E {
90+
pub fn unwrap_err(self) -> SString {
10791
match self {
108-
FFI_Result::Err(e) => e,
109-
FFI_Result::Ok(_) => panic!("called unwrap_err on Ok"),
92+
FFIResult::Err(e) => e,
93+
FFIResult::Ok(_) => panic!("called unwrap_err on Ok"),
11094
}
11195
}
11296

113-
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> FFI_Result<U, E> {
97+
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> FFIResult<U> {
11498
match self {
115-
FFI_Result::Ok(v) => FFI_Result::Ok(f(v)),
116-
FFI_Result::Err(e) => FFI_Result::Err(e),
99+
FFIResult::Ok(v) => FFIResult::Ok(f(v)),
100+
FFIResult::Err(e) => FFIResult::Err(e),
117101
}
118102
}
119103

120-
pub fn into_result(self) -> Result<T, E> {
104+
pub fn into_result(self) -> Result<T, SString> {
121105
self.into()
122106
}
123107
}
124108

125-
impl<T: PartialEq, E: PartialEq> PartialEq for FFI_Result<T, E> {
109+
impl<T> From<FFIResult<T>> for Result<T, SString> {
110+
fn from(res: FFIResult<T>) -> Self {
111+
match res {
112+
FFIResult::Ok(v) => Ok(v),
113+
FFIResult::Err(e) => Err(e),
114+
}
115+
}
116+
}
117+
118+
impl<T, E: ToString> From<Result<T, E>> for FFIResult<T> {
119+
fn from(res: Result<T, E>) -> Self {
120+
match res {
121+
Ok(v) => FFIResult::Ok(v),
122+
Err(e) => FFIResult::Err(SString::from(e.to_string().as_str())),
123+
}
124+
}
125+
}
126+
127+
impl<T: PartialEq> PartialEq for FFIResult<T> {
126128
fn eq(&self, other: &Self) -> bool {
127129
match (self, other) {
128-
(FFI_Result::Ok(a), FFI_Result::Ok(b)) => a == b,
129-
(FFI_Result::Err(a), FFI_Result::Err(b)) => a == b,
130+
(FFIResult::Ok(a), FFIResult::Ok(b)) => a == b,
131+
(FFIResult::Err(a), FFIResult::Err(b)) => a == b,
130132
_ => false,
131133
}
132134
}

datafusion/ffi/src/physical_expr/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::expr::interval::FFI_Interval;
4646
use crate::record_batch_stream::{
4747
record_batch_to_wrapped_array, wrapped_array_to_record_batch,
4848
};
49-
use crate::util::{FFI_Option, FFI_Result, FFIResult};
49+
use crate::util::{FFI_Option, FFIResult};
5050
use crate::{df_result, sresult, sresult_return};
5151

5252
#[repr(C)]
@@ -290,7 +290,7 @@ unsafe extern "C" fn propagate_constraints_fn_wrapper(
290290
.transpose()
291291
);
292292

293-
FFI_Result::Ok(result.into())
293+
FFIResult::Ok(result.into())
294294
}
295295

296296
unsafe extern "C" fn evaluate_statistics_fn_wrapper(
@@ -336,7 +336,7 @@ unsafe extern "C" fn propagate_statistics_fn_wrapper(
336336
.transpose()
337337
);
338338

339-
FFI_Result::Ok(result.into())
339+
FFIResult::Ok(result.into())
340340
}
341341

342342
unsafe extern "C" fn get_properties_fn_wrapper(
@@ -359,7 +359,7 @@ unsafe extern "C" fn get_properties_fn_wrapper(
359359
unsafe extern "C" fn fmt_sql_fn_wrapper(expr: &FFI_PhysicalExpr) -> FFIResult<SString> {
360360
let expr = expr.inner();
361361
let result = fmt_sql(expr.as_ref()).to_string();
362-
FFI_Result::Ok(result.into())
362+
FFIResult::Ok(result.into())
363363
}
364364

365365
unsafe extern "C" fn snapshot_fn_wrapper(
@@ -683,8 +683,8 @@ impl PhysicalExpr for ForeignPhysicalExpr {
683683
fn fmt_sql(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
684684
unsafe {
685685
match (self.expr.fmt_sql)(&self.expr) {
686-
FFI_Result::Ok(sql) => write!(f, "{sql}"),
687-
FFI_Result::Err(_) => Err(std::fmt::Error),
686+
FFIResult::Ok(sql) => write!(f, "{sql}"),
687+
FFIResult::Err(_) => Err(std::fmt::Error),
688688
}
689689
}
690690
}

datafusion/ffi/src/physical_optimizer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use tokio::runtime::Handle;
2828

2929
use crate::config::FFI_ConfigOptions;
3030
use crate::execution_plan::FFI_ExecutionPlan;
31-
use crate::util::{FFI_Result, FFIResult};
31+
use crate::util::{FFIResult};
3232
use crate::{df_result, sresult_return};
3333

3434
/// A stable struct for sharing [`PhysicalOptimizerRule`] across FFI boundaries.
@@ -95,7 +95,7 @@ unsafe extern "C" fn optimize_fn_wrapper(
9595
let config = sresult_return!(ConfigOptions::try_from(config));
9696
let optimized_plan = sresult_return!(rule.optimize(plan, &config));
9797

98-
FFI_Result::Ok(FFI_ExecutionPlan::new(optimized_plan, runtime))
98+
FFIResult::Ok(FFI_ExecutionPlan::new(optimized_plan, runtime))
9999
}
100100

101101
unsafe extern "C" fn name_fn_wrapper(rule: &FFI_PhysicalOptimizerRule) -> SString {

datafusion/ffi/src/proto/logical_extension_codec.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::table_provider::FFI_TableProvider;
4444
use crate::udaf::FFI_AggregateUDF;
4545
use crate::udf::FFI_ScalarUDF;
4646
use crate::udwf::FFI_WindowUDF;
47-
use crate::util::{FFI_Result, FFIResult};
47+
use crate::util::{FFIResult};
4848
use crate::{df_result, sresult_return};
4949

5050
/// A stable struct for sharing [`LogicalExtensionCodec`] across FFI boundaries.
@@ -163,7 +163,7 @@ unsafe extern "C" fn try_decode_table_provider_fn_wrapper(
163163
ctx.as_ref()
164164
));
165165

166-
FFI_Result::Ok(FFI_TableProvider::new_with_ffi_codec(
166+
FFIResult::Ok(FFI_TableProvider::new_with_ffi_codec(
167167
table_provider,
168168
true,
169169
runtime,
@@ -187,7 +187,7 @@ unsafe extern "C" fn try_encode_table_provider_fn_wrapper(
187187
&mut bytes
188188
));
189189

190-
FFI_Result::Ok(bytes.into_iter().collect())
190+
FFIResult::Ok(bytes.into_iter().collect())
191191
}
192192

193193
unsafe extern "C" fn try_decode_udf_fn_wrapper(
@@ -200,7 +200,7 @@ unsafe extern "C" fn try_decode_udf_fn_wrapper(
200200
let udf = sresult_return!(codec.try_decode_udf(name.as_str(), buf.as_ref()));
201201
let udf = FFI_ScalarUDF::from(udf);
202202

203-
FFI_Result::Ok(udf)
203+
FFIResult::Ok(udf)
204204
}
205205

206206
unsafe extern "C" fn try_encode_udf_fn_wrapper(
@@ -214,7 +214,7 @@ unsafe extern "C" fn try_encode_udf_fn_wrapper(
214214
let mut bytes = Vec::new();
215215
sresult_return!(codec.try_encode_udf(&node, &mut bytes));
216216

217-
FFI_Result::Ok(bytes.into_iter().collect())
217+
FFIResult::Ok(bytes.into_iter().collect())
218218
}
219219

220220
unsafe extern "C" fn try_decode_udaf_fn_wrapper(
@@ -226,7 +226,7 @@ unsafe extern "C" fn try_decode_udaf_fn_wrapper(
226226
let udaf = sresult_return!(codec_inner.try_decode_udaf(name.into(), buf.as_ref()));
227227
let udaf = FFI_AggregateUDF::from(udaf);
228228

229-
FFI_Result::Ok(udaf)
229+
FFIResult::Ok(udaf)
230230
}
231231

232232
unsafe extern "C" fn try_encode_udaf_fn_wrapper(
@@ -240,7 +240,7 @@ unsafe extern "C" fn try_encode_udaf_fn_wrapper(
240240
let mut bytes = Vec::new();
241241
sresult_return!(codec.try_encode_udaf(&udaf, &mut bytes));
242242

243-
FFI_Result::Ok(bytes.into_iter().collect())
243+
FFIResult::Ok(bytes.into_iter().collect())
244244
}
245245

246246
unsafe extern "C" fn try_decode_udwf_fn_wrapper(
@@ -252,7 +252,7 @@ unsafe extern "C" fn try_decode_udwf_fn_wrapper(
252252
let udwf = sresult_return!(codec.try_decode_udwf(name.into(), buf.as_ref()));
253253
let udwf = FFI_WindowUDF::from(udwf);
254254

255-
FFI_Result::Ok(udwf)
255+
FFIResult::Ok(udwf)
256256
}
257257

258258
unsafe extern "C" fn try_encode_udwf_fn_wrapper(
@@ -266,7 +266,7 @@ unsafe extern "C" fn try_encode_udwf_fn_wrapper(
266266
let mut bytes = Vec::new();
267267
sresult_return!(codec.try_encode_udwf(&udwf, &mut bytes));
268268

269-
FFI_Result::Ok(bytes.into_iter().collect())
269+
FFIResult::Ok(bytes.into_iter().collect())
270270
}
271271

272272
unsafe extern "C" fn release_fn_wrapper(provider: &mut FFI_LogicalExtensionCodec) {

0 commit comments

Comments
 (0)