Skip to main content

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 arrow::error::ArrowError;
21use arrow::ffi::FFI_ArrowSchema;
22use arrow_schema::FieldRef;
23use datafusion_common::{DataFusionError, Result};
24use datafusion_expr::function::PartitionEvaluatorArgs;
25use datafusion_physical_plan::PhysicalExpr;
26use stabby::vec::Vec as SVec;
27
28use crate::arrow_wrappers::WrappedSchema;
29use crate::physical_expr::FFI_PhysicalExpr;
30use crate::util::rvec_wrapped_to_vec_fieldref;
31
32/// A stable struct for sharing [`PartitionEvaluatorArgs`] across FFI boundaries.
33/// For an explanation of each field, see the corresponding function
34/// defined in [`PartitionEvaluatorArgs`].
35#[repr(C)]
36#[derive(Debug)]
37pub struct FFI_PartitionEvaluatorArgs {
38    input_exprs: SVec<FFI_PhysicalExpr>,
39    input_fields: SVec<WrappedSchema>,
40    is_reversed: bool,
41    ignore_nulls: bool,
42}
43
44impl TryFrom<PartitionEvaluatorArgs<'_>> for FFI_PartitionEvaluatorArgs {
45    type Error = DataFusionError;
46
47    fn try_from(args: PartitionEvaluatorArgs) -> Result<Self, DataFusionError> {
48        let input_exprs = args
49            .input_exprs()
50            .iter()
51            .map(Arc::clone)
52            .map(FFI_PhysicalExpr::from)
53            .collect();
54
55        let input_fields = args
56            .input_fields()
57            .iter()
58            .map(|input_type| FFI_ArrowSchema::try_from(input_type).map(WrappedSchema))
59            .collect::<Result<Vec<_>, ArrowError>>()?
60            .into_iter()
61            .collect();
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 {}