Skip to main content

datafusion_ffi/
table_source.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::{TableProviderFilterPushDown, TableType};
19
20/// FFI safe version of [`TableProviderFilterPushDown`].
21#[expect(non_camel_case_types)]
22#[repr(u8)]
23pub enum FFI_TableProviderFilterPushDown {
24    Unsupported,
25    Inexact,
26    Exact,
27}
28
29impl From<&FFI_TableProviderFilterPushDown> for TableProviderFilterPushDown {
30    fn from(value: &FFI_TableProviderFilterPushDown) -> Self {
31        match value {
32            FFI_TableProviderFilterPushDown::Unsupported => {
33                TableProviderFilterPushDown::Unsupported
34            }
35            FFI_TableProviderFilterPushDown::Inexact => {
36                TableProviderFilterPushDown::Inexact
37            }
38            FFI_TableProviderFilterPushDown::Exact => TableProviderFilterPushDown::Exact,
39        }
40    }
41}
42
43impl From<&TableProviderFilterPushDown> for FFI_TableProviderFilterPushDown {
44    fn from(value: &TableProviderFilterPushDown) -> Self {
45        match value {
46            TableProviderFilterPushDown::Unsupported => {
47                FFI_TableProviderFilterPushDown::Unsupported
48            }
49            TableProviderFilterPushDown::Inexact => {
50                FFI_TableProviderFilterPushDown::Inexact
51            }
52            TableProviderFilterPushDown::Exact => FFI_TableProviderFilterPushDown::Exact,
53        }
54    }
55}
56
57/// FFI safe version of [`TableType`].
58#[repr(C)]
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum FFI_TableType {
61    Base,
62    View,
63    Temporary,
64}
65
66impl From<FFI_TableType> for TableType {
67    fn from(value: FFI_TableType) -> Self {
68        match value {
69            FFI_TableType::Base => TableType::Base,
70            FFI_TableType::View => TableType::View,
71            FFI_TableType::Temporary => TableType::Temporary,
72        }
73    }
74}
75
76impl From<TableType> for FFI_TableType {
77    fn from(value: TableType) -> Self {
78        match value {
79            TableType::Base => FFI_TableType::Base,
80            TableType::View => FFI_TableType::View,
81            TableType::Temporary => FFI_TableType::Temporary,
82        }
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use datafusion::error::Result;
89
90    use super::*;
91
92    fn round_trip_filter_pushdown(pushdown: TableProviderFilterPushDown) -> Result<()> {
93        let ffi_pushdown: FFI_TableProviderFilterPushDown = (&pushdown).into();
94        let round_trip: TableProviderFilterPushDown = (&ffi_pushdown).into();
95
96        assert_eq!(pushdown, round_trip);
97        Ok(())
98    }
99
100    #[test]
101    fn round_trip_all_filter_pushdowns() -> Result<()> {
102        round_trip_filter_pushdown(TableProviderFilterPushDown::Exact)?;
103        round_trip_filter_pushdown(TableProviderFilterPushDown::Inexact)?;
104        round_trip_filter_pushdown(TableProviderFilterPushDown::Unsupported)?;
105
106        Ok(())
107    }
108
109    fn round_trip_table_type(table_type: TableType) -> Result<()> {
110        let ffi_type: FFI_TableType = table_type.into();
111        let round_trip_type: TableType = ffi_type.into();
112
113        assert_eq!(table_type, round_trip_type);
114        Ok(())
115    }
116
117    #[test]
118    fn test_round_all_trip_table_type() -> Result<()> {
119        round_trip_table_type(TableType::Base)?;
120        round_trip_table_type(TableType::Temporary)?;
121        round_trip_table_type(TableType::View)?;
122
123        Ok(())
124    }
125}