Skip to main content

uqa_sql/catalog/
oids.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Stable identities for SQL catalog objects.
8
9/// The first OID a user-created object receives (`FirstNormalObjectId`); lower OIDs belong to the system catalogs.
10pub const FIRST_NORMAL_OBJECT_ID: u32 = 16_384;
11
12pub fn stable_oid(kind: &str, name: &str) -> i64 {
13    let mut hash = 14_695_981_039_346_656_037_u64;
14    for byte in kind.bytes().chain(*b":").chain(name.bytes()) {
15        hash ^= u64::from(byte);
16        hash = hash.wrapping_mul(1_099_511_628_211);
17    }
18    10_000 + i64::try_from(hash % 2_000_000_000).unwrap_or(0)
19}
20
21pub fn stable_object_oid(kind: &str, object_id: &[u8; 16]) -> i64 {
22    let mut hash = 14_695_981_039_346_656_037_u64;
23    for byte in kind.bytes().chain(*b":").chain(object_id.iter().copied()) {
24        hash ^= u64::from(byte);
25        hash = hash.wrapping_mul(1_099_511_628_211);
26    }
27    10_000 + i64::try_from(hash % 2_000_000_000).unwrap_or(0)
28}
29
30pub fn schema_oid(schema: &str) -> i64 {
31    match schema {
32        "pg_catalog" => 11,
33        "public" => 2200,
34        "information_schema" => 13_293,
35        other => stable_oid("namespace", other),
36    }
37}
38
39pub fn relation_oid(kind: &str, schema: &str, name: &str) -> i64 {
40    stable_oid(kind, &format!("{schema}.{name}"))
41}
42
43pub fn current_user_oid() -> i64 {
44    10
45}
46
47pub fn current_user_name() -> &'static str {
48    "uqa"
49}