datafusion_ffi/expr/
interval.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 abi_stable::StableAbi;
19use datafusion_common::DataFusionError;
20use datafusion_expr::interval_arithmetic::Interval;
21
22use crate::arrow_wrappers::WrappedArray;
23
24/// A stable struct for sharing [`Interval`] across FFI boundaries.
25/// See [`Interval`] for the meaning of each field. Scalar values
26/// are passed as Arrow arrays of length 1.
27#[repr(C)]
28#[derive(Debug, StableAbi)]
29pub struct FFI_Interval {
30    lower: WrappedArray,
31    upper: WrappedArray,
32}
33
34impl TryFrom<&Interval> for FFI_Interval {
35    type Error = DataFusionError;
36    fn try_from(value: &Interval) -> Result<Self, Self::Error> {
37        let upper = value.upper().try_into()?;
38        let lower = value.lower().try_into()?;
39
40        Ok(FFI_Interval { upper, lower })
41    }
42}
43impl TryFrom<Interval> for FFI_Interval {
44    type Error = DataFusionError;
45    fn try_from(value: Interval) -> Result<Self, Self::Error> {
46        FFI_Interval::try_from(&value)
47    }
48}
49
50impl TryFrom<FFI_Interval> for Interval {
51    type Error = DataFusionError;
52    fn try_from(value: FFI_Interval) -> Result<Self, Self::Error> {
53        let upper = value.upper.try_into()?;
54        let lower = value.lower.try_into()?;
55
56        Interval::try_new(lower, upper)
57    }
58}