uqa_sql/semantics/key_identity.rs
1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! The physical document identity an integer primary-key value names.
8
9use uqa_core::{DocId, Value};
10
11/// A table whose single primary-key column is an integer names each row whose key lies in `0..KEY_IDENTITY_LIMIT` by the document identity equal to the key, so a key lookup reads that one identity. Its other rows, with negative keys or keys at or above the limit, take identities the table generates at or above the limit, where no key value names one; a key lookup reaches them through the key's index.
12pub const KEY_IDENTITY_LIMIT: DocId = 1 << 62;
13
14/// The document identity an integer primary-key value names, or `None` for a value no identity is named by.
15#[must_use]
16pub fn key_document_id(value: &Value) -> Option<DocId> {
17 match value {
18 Value::Int(key) => DocId::try_from(*key)
19 .ok()
20 .filter(|doc_id| *doc_id < KEY_IDENTITY_LIMIT),
21 _ => None,
22 }
23}
24
25/// Whether `doc_id` is an identity an integer primary-key value names.
26#[must_use]
27pub const fn is_key_document_id(doc_id: DocId) -> bool {
28 doc_id < KEY_IDENTITY_LIMIT
29}