Skip to content

Commit

Permalink
Add NamespaceIdent.parent()
Browse files Browse the repository at this point in the history
  • Loading branch information
c-thiel committed Sep 22, 2024
1 parent 12e12e2 commit e8eeb9a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions crates/iceberg/src/catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ 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> {
if self.0.len() <= 1 {
None
} else {
Some(Self(self.0[..self.0.len() - 1].to_vec()))
}
}
}

impl AsRef<Vec<String>> for NamespaceIdent {
Expand Down Expand Up @@ -480,6 +490,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

0 comments on commit e8eeb9a

Please # to comment.