risingwave_common/types/
postgres_type.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::DataType;

/// `DataType` information extracted from PostgreSQL `pg_type`
///
/// ```sql
/// select oid, typarray, typname, typinput, typlen from pg_type
/// where oid in (16, 21, 23, 20, 1700, 700, 701, 1043, 17, 1082, 1114, 1184, 1083, 1186, 3802);
/// ```
///
/// See also:
/// * <https://www.postgresql.org/docs/15/catalog-pg-type.html>
/// * <https://github.com/postgres/postgres/blob/REL_15_4/src/include/catalog/pg_type.dat>
#[macro_export]
macro_rules! for_all_base_types {
    ($macro:ident $(, $x:tt)*) => {
        $macro! {
            $($x, )*
            { Boolean     |   16 |     1000 | bool        | boolin         |      1 }
            { Bytea       |   17 |     1001 | bytea       | byteain        |     -1 }
            { Int64       |   20 |     1016 | int8        | int8in         |      8 }
            { Int16       |   21 |     1005 | int2        | int2in         |      2 }
            { Int32       |   23 |     1007 | int4        | int4in         |      4 }
            { Float32     |  700 |     1021 | float4      | float4in       |      4 }
            { Float64     |  701 |     1022 | float8      | float8in       |      8 }
            { Varchar     | 1043 |     1015 | varchar     | varcharin      |     -1 }
            { Date        | 1082 |     1182 | date        | date_in        |      4 }
            { Time        | 1083 |     1183 | time        | time_in        |      8 }
            { Timestamp   | 1114 |     1115 | timestamp   | timestamp_in   |      8 }
            { Timestamptz | 1184 |     1185 | timestamptz | timestamptz_in |      8 }
            { Interval    | 1186 |     1187 | interval    | interval_in    |     16 }
            { Decimal     | 1700 |     1231 | numeric     | numeric_in     |     -1 }
            { Jsonb       | 3802 |     3807 | jsonb       | jsonb_in       |     -1 }
        }
    };
}

#[derive(Debug, thiserror::Error)]
#[error("Unsupported oid {0}")]
pub struct UnsupportedOid(i32);

/// Get type information compatible with Postgres type, such as oid, type length.
impl DataType {
    /// For a fixed-size type, typlen is the number of bytes in the internal representation of the type.
    /// But for a variable-length type, typlen is negative.
    /// -1 indicates a “varlena” type (one that has a length word),
    /// -2 indicates a null-terminated C string.
    ///
    /// <https://www.postgresql.org/docs/15/catalog-pg-type.html#:~:text=of%20the%20type-,typlen,-int2>
    pub fn type_len(&self) -> i16 {
        macro_rules! impl_type_len {
            ($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $input:ident | $len:literal } )*) => {
                match self {
                    $(
                    DataType::$enum => $len,
                    )*
                    DataType::Serial => 8,
                    DataType::Int256 => -1,
                    DataType::List(_) | DataType::Struct(_) | DataType::Map(_) => -1,
                }
            }
        }
        for_all_base_types! { impl_type_len }
    }

    // NOTE:
    // Refer https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat when add new TypeOid.
    // Be careful to distinguish oid from array_type_oid.
    // Such as:
    //  https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat#L347
    //  For Numeric(aka Decimal): oid = 1700, array_type_oid = 1231
    pub fn from_oid(oid: i32) -> Result<Self, UnsupportedOid> {
        macro_rules! impl_from_oid {
            ($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $input:ident | $len:literal } )*) => {
                match oid {
                    $(
                    $oid => Ok(DataType::$enum),
                    )*
                    $(
                    $oid_array => Ok(DataType::List(Box::new(DataType::$enum))),
                    )*
                    // workaround to support text in extended mode.
                    25 => Ok(DataType::Varchar),
                    1009 => Ok(DataType::List(Box::new(DataType::Varchar))),
                    _ => Err(UnsupportedOid(oid)),
                }
            }
        }
        for_all_base_types! { impl_from_oid }
    }

    /// Refer to [`Self::from_oid`]
    pub fn to_oid(&self) -> i32 {
        macro_rules! impl_to_oid {
            ($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $input:ident | $len:literal } )*) => {
                match self {
                    $(
                    DataType::$enum => $oid,
                    )*
                    DataType::List(inner) => match inner.unnest_list() {
                        $(
                        DataType::$enum => $oid_array,
                        )*
                        DataType::Int256 => 1302,
                        DataType::Serial => 1016,
                        DataType::Struct(_) => 2287, // pseudo-type of array[struct] (see `pg_type.dat`)
                        DataType::List { .. } => unreachable!("Never reach here!"),
                        DataType::Map(_) => 1304,
                    }
                    DataType::Serial => 20,
                    // XXX: what does the oid mean here? Why we don't have from_oid for them?
                    DataType::Int256 => 1301,
                    DataType::Map(_) => 1303,
                    // TODO: Support to give a new oid for custom struct type. #9434
                    DataType::Struct(_) => 2249,  // pseudo-type of struct (see `pg_type.dat`)
                }
            }
        }
        for_all_base_types! { impl_to_oid }
    }

    pub fn pg_name(&self) -> &'static str {
        macro_rules! impl_pg_name {
            ($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $input:ident | $len:literal } )*) => {
                match self {
                    $(
                    DataType::$enum => stringify!($name),
                    )*
                    DataType::Struct(_) => "struct",
                    DataType::List(_) => "list",
                    DataType::Serial => "serial",
                    DataType::Int256 => "rw_int256",
                    DataType::Map(_) => "map",
                }
            }
        }
        for_all_base_types! { impl_pg_name }
    }
}