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