Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add NamespaceIdent.parent() #641

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/iceberg/src/catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ impl NamespaceIdent {
pub fn inner(self) -> Vec<String> {
self.0
}

/// Get the parent of this namespace.
/// Returns None if this namespace only has a single element and thus has no parent.
pub fn parent(&self) -> Option<Self> {
self.0.split_last().and_then(|(_, parent)| {
if parent.is_empty() {
None
} else {
Some(Self(parent.to_vec()))
}
})
}
}

impl AsRef<Vec<String>> for NamespaceIdent {
Expand Down Expand Up @@ -480,6 +492,17 @@ mod tests {
};
use crate::{NamespaceIdent, TableCreation, TableIdent, TableRequirement, TableUpdate};

#[test]
fn test_parent_namespace() {
let ns1 = NamespaceIdent::from_strs(vec!["ns1"]).unwrap();
let ns2 = NamespaceIdent::from_strs(vec!["ns1", "ns2"]).unwrap();
let ns3 = NamespaceIdent::from_strs(vec!["ns1", "ns2", "ns3"]).unwrap();

assert_eq!(ns1.parent(), None);
assert_eq!(ns2.parent(), Some(ns1.clone()));
assert_eq!(ns3.parent(), Some(ns2.clone()));
}

#[test]
fn test_create_table_id() {
let table_id = TableIdent {
Expand Down
Loading