datafusion_common/types/
builtin.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 arrow::datatypes::IntervalUnit::*;
19
20use crate::types::{LogicalTypeRef, NativeType};
21use std::sync::{Arc, LazyLock};
22
23/// Create a singleton and accompanying static variable for a [`LogicalTypeRef`]
24/// of a [`NativeType`].
25/// * `name`: name of the static variable, must be unique.
26/// * `getter`: name of the public function that will return the singleton instance
27///   of the static variable.
28/// * `ty`: the [`NativeType`].
29macro_rules! singleton {
30    ($name:ident, $getter:ident, $ty:ident) => {
31        static $name: LazyLock<LogicalTypeRef> =
32            LazyLock::new(|| Arc::new(NativeType::$ty));
33
34        #[doc = "Getter for singleton instance of a logical type representing"]
35        #[doc = concat!("[`NativeType::", stringify!($ty), "`].")]
36        pub fn $getter() -> LogicalTypeRef {
37            Arc::clone(&$name)
38        }
39    };
40}
41
42/// Similar to [`singleton`], but for native types that have variants, such as
43/// `NativeType::Interval(MonthDayNano)`.
44/// * `name`: name of the static variable, must be unique.
45/// * `getter`: name of the public function that will return the singleton instance
46///   of the static variable.
47/// * `ty`: the [`NativeType`].
48/// * `variant`: specific variant of the `ty`.
49macro_rules! singleton_variant {
50    ($name:ident, $getter:ident, $ty:ident, $variant:ident) => {
51        static $name: LazyLock<LogicalTypeRef> =
52            LazyLock::new(|| Arc::new(NativeType::$ty($variant)));
53
54        #[doc = "Getter for singleton instance of a logical type representing"]
55        #[doc = concat!("[`NativeType::", stringify!($ty), "`] of unit [`", stringify!($variant),"`].`")]
56        pub fn $getter() -> LogicalTypeRef {
57            Arc::clone(&$name)
58        }
59    };
60}
61
62singleton!(LOGICAL_NULL, logical_null, Null);
63singleton!(LOGICAL_BOOLEAN, logical_boolean, Boolean);
64singleton!(LOGICAL_INT8, logical_int8, Int8);
65singleton!(LOGICAL_INT16, logical_int16, Int16);
66singleton!(LOGICAL_INT32, logical_int32, Int32);
67singleton!(LOGICAL_INT64, logical_int64, Int64);
68singleton!(LOGICAL_UINT8, logical_uint8, UInt8);
69singleton!(LOGICAL_UINT16, logical_uint16, UInt16);
70singleton!(LOGICAL_UINT32, logical_uint32, UInt32);
71singleton!(LOGICAL_UINT64, logical_uint64, UInt64);
72singleton!(LOGICAL_FLOAT16, logical_float16, Float16);
73singleton!(LOGICAL_FLOAT32, logical_float32, Float32);
74singleton!(LOGICAL_FLOAT64, logical_float64, Float64);
75singleton!(LOGICAL_DATE, logical_date, Date);
76singleton!(LOGICAL_BINARY, logical_binary, Binary);
77singleton!(LOGICAL_STRING, logical_string, String);
78
79singleton_variant!(
80    LOGICAL_INTERVAL_MDN,
81    logical_interval_mdn,
82    Interval,
83    MonthDayNano
84);