datafusion_ffi/
volatility.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_expr::Volatility;
20
21#[repr(C)]
22#[derive(Debug, StableAbi, Clone)]
23pub enum FFI_Volatility {
24    Immutable,
25    Stable,
26    Volatile,
27}
28
29impl From<Volatility> for FFI_Volatility {
30    fn from(value: Volatility) -> Self {
31        match value {
32            Volatility::Immutable => Self::Immutable,
33            Volatility::Stable => Self::Stable,
34            Volatility::Volatile => Self::Volatile,
35        }
36    }
37}
38
39impl From<&FFI_Volatility> for Volatility {
40    fn from(value: &FFI_Volatility) -> Self {
41        match value {
42            FFI_Volatility::Immutable => Self::Immutable,
43            FFI_Volatility::Stable => Self::Stable,
44            FFI_Volatility::Volatile => Self::Volatile,
45        }
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use datafusion::logical_expr::Volatility;
52
53    use super::FFI_Volatility;
54
55    fn test_round_trip_volatility(volatility: Volatility) {
56        let ffi_volatility: FFI_Volatility = volatility.into();
57        let round_trip: Volatility = (&ffi_volatility).into();
58
59        assert_eq!(volatility, round_trip);
60    }
61
62    #[test]
63    fn test_all_round_trip_volatility() {
64        test_round_trip_volatility(Volatility::Immutable);
65        test_round_trip_volatility(Volatility::Stable);
66        test_round_trip_volatility(Volatility::Volatile);
67    }
68}