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