datafusion_ffi/
insert_op.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::logical_expr::logical_plan::dml::InsertOp;
20
21/// FFI safe version of [`InsertOp`].
22#[repr(C)]
23#[derive(StableAbi)]
24#[allow(non_camel_case_types)]
25pub enum FFI_InsertOp {
26    Append,
27    Overwrite,
28    Replace,
29}
30
31impl From<FFI_InsertOp> for InsertOp {
32    fn from(value: FFI_InsertOp) -> Self {
33        match value {
34            FFI_InsertOp::Append => InsertOp::Append,
35            FFI_InsertOp::Overwrite => InsertOp::Overwrite,
36            FFI_InsertOp::Replace => InsertOp::Replace,
37        }
38    }
39}
40
41impl From<InsertOp> for FFI_InsertOp {
42    fn from(value: InsertOp) -> Self {
43        match value {
44            InsertOp::Append => FFI_InsertOp::Append,
45            InsertOp::Overwrite => FFI_InsertOp::Overwrite,
46            InsertOp::Replace => FFI_InsertOp::Replace,
47        }
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use datafusion::logical_expr::dml::InsertOp;
54
55    use super::FFI_InsertOp;
56
57    fn test_round_trip_insert_op(insert_op: InsertOp) {
58        let ffi_insert_op: FFI_InsertOp = insert_op.into();
59        let round_trip: InsertOp = ffi_insert_op.into();
60
61        assert_eq!(insert_op, round_trip);
62    }
63
64    /// This test ensures we have not accidentally mapped the FFI
65    /// enums to the wrong internal enums values.
66    #[test]
67    fn test_all_round_trip_insert_ops() {
68        test_round_trip_insert_op(InsertOp::Append);
69        test_round_trip_insert_op(InsertOp::Overwrite);
70        test_round_trip_insert_op(InsertOp::Replace);
71    }
72}