datafusion_ffi/udwf/
partition_evaluator_args.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::StableAbi;
21use abi_stable::std_types::RVec;
22use arrow::error::ArrowError;
23use arrow::ffi::FFI_ArrowSchema;
24use arrow_schema::FieldRef;
25use datafusion_common::{DataFusionError, Result};
26use datafusion_expr::function::PartitionEvaluatorArgs;
27use datafusion_physical_plan::PhysicalExpr;
28
29use crate::arrow_wrappers::WrappedSchema;
30use crate::physical_expr::FFI_PhysicalExpr;
31use crate::util::rvec_wrapped_to_vec_fieldref;
32
33/// A stable struct for sharing [`PartitionEvaluatorArgs`] across FFI boundaries.
34/// For an explanation of each field, see the corresponding function
35/// defined in [`PartitionEvaluatorArgs`].
36#[repr(C)]
37#[derive(Debug, StableAbi)]
38pub struct FFI_PartitionEvaluatorArgs {
39    input_exprs: RVec<FFI_PhysicalExpr>,
40    input_fields: RVec<WrappedSchema>,
41    is_reversed: bool,
42    ignore_nulls: bool,
43}
44
45impl TryFrom<PartitionEvaluatorArgs<'_>> for FFI_PartitionEvaluatorArgs {
46    type Error = DataFusionError;
47
48    fn try_from(args: PartitionEvaluatorArgs) -> Result<Self, DataFusionError> {
49        let input_exprs = args
50            .input_exprs()
51            .iter()
52            .map(Arc::clone)
53            .map(FFI_PhysicalExpr::from)
54            .collect();
55
56        let input_fields = args
57            .input_fields()
58            .iter()
59            .map(|input_type| FFI_ArrowSchema::try_from(input_type).map(WrappedSchema))
60            .collect::<Result<Vec<_>, ArrowError>>()?
61            .into();
62
63        Ok(Self {
64            input_exprs,
65            input_fields,
66            is_reversed: args.is_reversed(),
67            ignore_nulls: args.ignore_nulls(),
68        })
69    }
70}
71
72/// This struct mirrors PartitionEvaluatorArgs except that it contains owned data.
73/// It is necessary to create this struct so that we can parse the protobuf
74/// data across the FFI boundary and turn it into owned data that
75/// PartitionEvaluatorArgs can then reference.
76pub struct ForeignPartitionEvaluatorArgs {
77    input_exprs: Vec<Arc<dyn PhysicalExpr>>,
78    input_fields: Vec<FieldRef>,
79    is_reversed: bool,
80    ignore_nulls: bool,
81}
82
83impl TryFrom<FFI_PartitionEvaluatorArgs> for ForeignPartitionEvaluatorArgs {
84    type Error = DataFusionError;
85
86    fn try_from(value: FFI_PartitionEvaluatorArgs) -> Result<Self> {
87        let input_exprs = value.input_exprs.iter().map(Into::into).collect();
88
89        let input_fields = rvec_wrapped_to_vec_fieldref(&value.input_fields)?;
90
91        Ok(Self {
92            input_exprs,
93            input_fields,
94            is_reversed: value.is_reversed,
95            ignore_nulls: value.ignore_nulls,
96        })
97    }
98}
99
100impl<'a> From<&'a ForeignPartitionEvaluatorArgs> for PartitionEvaluatorArgs<'a> {
101    fn from(value: &'a ForeignPartitionEvaluatorArgs) -> Self {
102        PartitionEvaluatorArgs::new(
103            &value.input_exprs,
104            &value.input_fields,
105            value.is_reversed,
106            value.ignore_nulls,
107        )
108    }
109}
110
111#[cfg(test)]
112mod tests {}