Skip to main content

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