datatypes/
macros.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Some helper macros for datatypes, copied from databend.
16
17/// Apply the macro rules to all primitive types.
18#[macro_export]
19macro_rules! for_all_primitive_types {
20    ($macro:tt $(, $x:tt)*) => {
21        $macro! {
22            [$($x),*],
23            { i8 },
24            { i16 },
25            { i32 },
26            { i64 },
27            { u8 },
28            { u16 },
29            { u32 },
30            { u64 },
31            { f32 },
32            { f64 }
33        }
34    };
35}
36
37/// Match the logical type and apply `$body` to all primitive types and
38/// `nbody` to other types.
39#[macro_export]
40macro_rules! with_match_primitive_type_id {
41    ($key_type:expr, | $_:tt $T:ident | $body:tt, $nbody:tt) => {{
42        macro_rules! __with_ty__ {
43            ( $_ $T:ident ) => {
44                $body
45            };
46        }
47
48        use $crate::type_id::LogicalTypeId;
49        use $crate::types::{
50            Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type,
51            UInt32Type, UInt64Type, UInt8Type,
52        };
53        match $key_type {
54            LogicalTypeId::Int8 => __with_ty__! { Int8Type },
55            LogicalTypeId::Int16 => __with_ty__! { Int16Type },
56            LogicalTypeId::Int32 => __with_ty__! { Int32Type },
57            LogicalTypeId::Int64 => __with_ty__! { Int64Type },
58            LogicalTypeId::UInt8 => __with_ty__! { UInt8Type },
59            LogicalTypeId::UInt16 => __with_ty__! { UInt16Type },
60            LogicalTypeId::UInt32 => __with_ty__! { UInt32Type },
61            LogicalTypeId::UInt64 => __with_ty__! { UInt64Type },
62            LogicalTypeId::Float32 => __with_ty__! { Float32Type },
63            LogicalTypeId::Float64 => __with_ty__! { Float64Type },
64
65            _ => $nbody,
66        }
67    }};
68}