Skip to main content

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