use crate::db::{CellInput, Db};
use crate::engine::{DataType, EvalContext, PrimaryKeySpec, Value};
use crate::session::SessionTimeZone;
use crate::txn::SYSTEM_TXID;
#[derive(Clone, Copy)]
struct PgTypeRow {
oid: i32,
typname: &'static str,
typnamespace: i32,
typtype: &'static str,
typcategory: &'static str,
typdelim: &'static str,
typinput: &'static str,
typoutput: &'static str,
typreceive: &'static str,
typsend: &'static str,
typalign: &'static str,
typstorage: &'static str,
typlen: i32,
typbyval: bool,
typdefault: Option<&'static str>,
typnotnull: bool,
typbasetype: i32,
typtypmod: i32,
typrelid: i32,
typelem: i32,
typarray: i32,
typcollation: i32,
}
impl PgTypeRow {
fn to_cells(self) -> Vec<CellInput> {
vec![
CellInput::Value(Value::Int64(self.oid as i64)),
CellInput::Value(Value::Text(self.typname.to_string())),
CellInput::Value(Value::Int64(self.typnamespace as i64)),
CellInput::Value(Value::Bytes(self.typtype.as_bytes().to_vec())),
CellInput::Value(Value::Text(self.typcategory.to_string())),
CellInput::Value(Value::Text(self.typdelim.to_string())),
CellInput::Value(Value::Text(self.typinput.to_string())),
CellInput::Value(Value::Text(self.typoutput.to_string())),
CellInput::Value(Value::Text(self.typreceive.to_string())),
CellInput::Value(Value::Text(self.typsend.to_string())),
CellInput::Value(Value::Text(self.typalign.to_string())),
CellInput::Value(Value::Text(self.typstorage.to_string())),
CellInput::Value(Value::Int64(self.typlen as i64)),
CellInput::Value(Value::Bool(self.typbyval)),
match self.typdefault {
Some(s) => CellInput::Value(Value::Text(s.to_string())),
None => CellInput::Value(Value::Null),
},
CellInput::Value(Value::Bool(self.typnotnull)),
CellInput::Value(Value::Int64(self.typbasetype as i64)),
CellInput::Value(Value::Int64(self.typtypmod as i64)),
CellInput::Value(Value::Int64(self.typrelid as i64)),
CellInput::Value(Value::Int64(self.typelem as i64)),
CellInput::Value(Value::Int64(self.typarray as i64)),
CellInput::Value(Value::Int64(self.typcollation as i64)),
]
}
}
const PG_CATALOG_OID: i32 = 11;
const BUILTIN_TYPES: &[PgTypeRow] = &[
PgTypeRow {
oid: 16,
typname: "bool",
typlen: 1,
typbyval: true,
typcategory: "B",
typtype: "b",
typdelim: ",",
typinput: "boolin",
typoutput: "boolout",
typreceive: "boolrecv",
typsend: "boolsend",
typalign: "c",
typstorage: "p",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1000,
typcollation: 0,
},
PgTypeRow {
oid: 17,
typname: "bytea",
typlen: -1,
typbyval: false,
typcategory: "U",
typtype: "b",
typdelim: ",",
typinput: "byteain",
typoutput: "byteaout",
typreceive: "bytearecv",
typsend: "byteasend",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1001,
typcollation: 0,
},
PgTypeRow {
oid: 19,
typname: "name",
typlen: 64,
typbyval: false,
typcategory: "S",
typtype: "b",
typdelim: ",",
typinput: "namein",
typoutput: "nameout",
typreceive: "namerecv",
typsend: "namesend",
typalign: "c",
typstorage: "p",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1003,
typcollation: 0,
},
PgTypeRow {
oid: 20,
typname: "int8",
typlen: 8,
typbyval: true,
typcategory: "N",
typtype: "b",
typdelim: ",",
typinput: "int8in",
typoutput: "int8out",
typreceive: "int8recv",
typsend: "int8send",
typalign: "d",
typstorage: "p",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1016,
typcollation: 0,
},
PgTypeRow {
oid: 23,
typname: "int4",
typlen: 4,
typbyval: true,
typcategory: "N",
typtype: "b",
typdelim: ",",
typinput: "int4in",
typoutput: "int4out",
typreceive: "int4recv",
typsend: "int4send",
typalign: "i",
typstorage: "p",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1007,
typcollation: 0,
},
PgTypeRow {
oid: 25,
typname: "text",
typlen: -1,
typbyval: false,
typcategory: "S",
typtype: "b",
typdelim: ",",
typinput: "textin",
typoutput: "textout",
typreceive: "textrecv",
typsend: "textsend",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1009,
typcollation: 100,
},
PgTypeRow {
oid: 26,
typname: "oid",
typlen: 4,
typbyval: true,
typcategory: "N",
typtype: "b",
typdelim: ",",
typinput: "oidin",
typoutput: "oidout",
typreceive: "oidrecv",
typsend: "oidsend",
typalign: "i",
typstorage: "p",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1028,
typcollation: 0,
},
PgTypeRow {
oid: 114,
typname: "json",
typlen: -1,
typbyval: false,
typcategory: "U",
typtype: "b",
typdelim: ",",
typinput: "json_in",
typoutput: "json_out",
typreceive: "json_recv",
typsend: "json_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 199,
typcollation: 0,
},
PgTypeRow {
oid: 701,
typname: "float8",
typlen: 8,
typbyval: true,
typcategory: "N",
typtype: "b",
typdelim: ",",
typinput: "float8in",
typoutput: "float8out",
typreceive: "float8recv",
typsend: "float8send",
typalign: "d",
typstorage: "p",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1022,
typcollation: 0,
},
PgTypeRow {
oid: 1043,
typname: "varchar",
typlen: -1,
typbyval: false,
typcategory: "S",
typtype: "b",
typdelim: ",",
typinput: "varcharin",
typoutput: "varcharout",
typreceive: "varcharrecv",
typsend: "varcharsend",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 1043,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1015,
typcollation: 100,
},
PgTypeRow {
oid: 1082,
typname: "date",
typlen: 4,
typbyval: true,
typcategory: "D",
typtype: "b",
typdelim: ",",
typinput: "date_in",
typoutput: "date_out",
typreceive: "date_recv",
typsend: "date_send",
typalign: "i",
typstorage: "p",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1182,
typcollation: 0,
},
PgTypeRow {
oid: 1114,
typname: "timestamp",
typlen: 8,
typbyval: true,
typcategory: "D",
typtype: "b",
typdelim: ",",
typinput: "timestamp_in",
typoutput: "timestamp_out",
typreceive: "timestamp_recv",
typsend: "timestamp_send",
typalign: "d",
typstorage: "p",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1115,
typcollation: 0,
},
PgTypeRow {
oid: 1184,
typname: "timestamptz",
typlen: 8,
typbyval: true,
typcategory: "D",
typtype: "b",
typdelim: ",",
typinput: "timestamptz_in",
typoutput: "timestamptz_out",
typreceive: "timestamptz_recv",
typsend: "timestamptz_send",
typalign: "d",
typstorage: "p",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1185,
typcollation: 0,
},
PgTypeRow {
oid: 1186,
typname: "interval",
typlen: 16,
typbyval: false,
typcategory: "T",
typtype: "b",
typdelim: ",",
typinput: "interval_in",
typoutput: "interval_out",
typreceive: "interval_recv",
typsend: "interval_send",
typalign: "d",
typstorage: "m",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 1187,
typcollation: 0,
},
PgTypeRow {
oid: 3802,
typname: "jsonb",
typlen: -1,
typbyval: false,
typcategory: "U",
typtype: "b",
typdelim: ",",
typinput: "jsonb_in",
typoutput: "jsonb_out",
typreceive: "jsonb_recv",
typsend: "jsonb_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 3807,
typcollation: 0,
},
PgTypeRow {
oid: 2206,
typname: "regtype",
typlen: 4,
typbyval: true,
typcategory: "N",
typtype: "b",
typdelim: ",",
typinput: "regtypein",
typoutput: "regtypeout",
typreceive: "regtyperecv",
typsend: "regtypesend",
typalign: "i",
typstorage: "p",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 0,
typarray: 2211,
typcollation: 0,
},
PgTypeRow {
oid: 199,
typname: "_json",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 114,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 1000,
typname: "_bool",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 16,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 1001,
typname: "_bytea",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 17,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 1003,
typname: "_name",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 19,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 1007,
typname: "_int4",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 23,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 1009,
typname: "_text",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 25,
typarray: 0,
typcollation: 100,
},
PgTypeRow {
oid: 1015,
typname: "_varchar",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 1043,
typarray: 0,
typcollation: 100,
},
PgTypeRow {
oid: 1016,
typname: "_int8",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 20,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 1022,
typname: "_float8",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 701,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 1028,
typname: "_oid",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 26,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 1115,
typname: "_timestamp",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 1114,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 1182,
typname: "_date",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 1082,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 1185,
typname: "_timestamptz",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 1184,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 1187,
typname: "_interval",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 1186,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 2211,
typname: "_regtype",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 2206,
typarray: 0,
typcollation: 0,
},
PgTypeRow {
oid: 3807,
typname: "_jsonb",
typlen: -1,
typbyval: false,
typcategory: "A",
typtype: "b",
typdelim: ",",
typinput: "array_in",
typoutput: "array_out",
typreceive: "array_recv",
typsend: "array_send",
typalign: "i",
typstorage: "x",
typnamespace: PG_CATALOG_OID,
typdefault: None,
typnotnull: false,
typbasetype: 0,
typtypmod: -1,
typrelid: 0,
typelem: 3802,
typarray: 0,
typcollation: 0,
},
];
pub(super) fn init_pg_type(db: &mut Db) {
let cols = vec![
("oid".to_string(), DataType::Int4, false, None, None),
("typname".to_string(), DataType::Text, false, None, None),
(
"typnamespace".to_string(),
DataType::Int4,
false,
None,
None,
),
("typtype".to_string(), DataType::Bytea, false, None, None),
("typcategory".to_string(), DataType::Text, false, None, None),
("typdelim".to_string(), DataType::Text, false, None, None),
("typinput".to_string(), DataType::Text, false, None, None),
("typoutput".to_string(), DataType::Text, false, None, None),
("typreceive".to_string(), DataType::Text, false, None, None),
("typsend".to_string(), DataType::Text, false, None, None),
("typalign".to_string(), DataType::Text, false, None, None),
("typstorage".to_string(), DataType::Text, false, None, None),
("typlen".to_string(), DataType::Int4, false, None, None),
("typbyval".to_string(), DataType::Bool, false, None, None),
("typdefault".to_string(), DataType::Text, true, None, None),
("typnotnull".to_string(), DataType::Bool, false, None, None),
("typbasetype".to_string(), DataType::Int4, false, None, None),
("typtypmod".to_string(), DataType::Int4, false, None, None),
("typrelid".to_string(), DataType::Int4, false, None, None),
("typelem".to_string(), DataType::Int4, false, None, None),
("typarray".to_string(), DataType::Int4, false, None, None),
(
"typcollation".to_string(),
DataType::Int4,
false,
None,
None,
),
];
let pk = PrimaryKeySpec {
name: Some("pg_type_oid_pkey".to_string()),
columns: vec!["oid".to_string()],
};
db.create_table("pg_catalog", "pg_type", cols, Some(pk), Vec::new(), &[])
.expect("create pg_catalog.pg_type");
let ctx = EvalContext::new(SessionTimeZone::Utc);
let rows: Vec<Vec<CellInput>> = BUILTIN_TYPES.iter().map(|row| row.to_cells()).collect();
db.insert_full_rows(
"pg_catalog",
"pg_type",
rows,
false,
SYSTEM_TXID,
&[],
&ctx,
None,
)
.unwrap_or_else(|e| panic!("seed pg_catalog.pg_type: {e}"));
}