catalog/system_schema/pg_catalog/
pg_catalog_memory_table.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
15use std::sync::Arc;
16
17use datatypes::schema::{ColumnSchema, Schema, SchemaRef};
18use datatypes::vectors::{Int16Vector, StringVector, UInt32Vector, VectorRef};
19
20use crate::memory_table_cols;
21use crate::system_schema::pg_catalog::oid_column;
22use crate::system_schema::pg_catalog::table_names::PG_TYPE;
23use crate::system_schema::utils::tables::{i16_column, string_column};
24
25fn pg_type_schema_columns() -> (Vec<ColumnSchema>, Vec<VectorRef>) {
26    // TODO(j0hn50n133): acquire this information from `DataType` instead of hardcoding it to avoid regression.
27    memory_table_cols!(
28        [oid, typname, typlen],
29        [
30            (1, "String", -1),
31            (2, "Binary", -1),
32            (3, "Int8", 1),
33            (4, "Int16", 2),
34            (5, "Int32", 4),
35            (6, "Int64", 8),
36            (7, "UInt8", 1),
37            (8, "UInt16", 2),
38            (9, "UInt32", 4),
39            (10, "UInt64", 8),
40            (11, "Float32", 4),
41            (12, "Float64", 8),
42            (13, "Decimal", 16),
43            (14, "Date", 4),
44            (15, "DateTime", 8),
45            (16, "Timestamp", 8),
46            (17, "Time", 8),
47            (18, "Duration", 8),
48            (19, "Interval", 16),
49            (20, "List", -1),
50        ]
51    );
52    (
53        // not quiet identical with pg, we only follow the definition in pg
54        vec![oid_column(), string_column("typname"), i16_column("typlen")],
55        vec![
56            Arc::new(UInt32Vector::from_vec(oid)), // oid
57            Arc::new(StringVector::from(typname)),
58            Arc::new(Int16Vector::from_vec(typlen)), // typlen in bytes
59        ],
60    )
61}
62
63pub(super) fn get_schema_columns(table_name: &str) -> (SchemaRef, Vec<VectorRef>) {
64    let (column_schemas, columns): (_, Vec<VectorRef>) = match table_name {
65        PG_TYPE => pg_type_schema_columns(),
66        _ => unreachable!("Unknown table in pg_catalog: {}", table_name),
67    };
68    (Arc::new(Schema::new(column_schemas)), columns)
69}