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