Skip to content

Commit b3709ba

Browse files
authored
feat: Add NamespaceIdent.parent() (apache#641)
* Add NamespaceIdent.parent() * Use split_last
1 parent 1533c43 commit b3709ba

File tree

1 file changed

+23
-0
lines changed
  • crates/iceberg/src/catalog

1 file changed

+23
-0
lines changed

crates/iceberg/src/catalog/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ impl NamespaceIdent {
133133
pub fn inner(self) -> Vec<String> {
134134
self.0
135135
}
136+
137+
/// Get the parent of this namespace.
138+
/// Returns None if this namespace only has a single element and thus has no parent.
139+
pub fn parent(&self) -> Option<Self> {
140+
self.0.split_last().and_then(|(_, parent)| {
141+
if parent.is_empty() {
142+
None
143+
} else {
144+
Some(Self(parent.to_vec()))
145+
}
146+
})
147+
}
136148
}
137149

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

495+
#[test]
496+
fn test_parent_namespace() {
497+
let ns1 = NamespaceIdent::from_strs(vec!["ns1"]).unwrap();
498+
let ns2 = NamespaceIdent::from_strs(vec!["ns1", "ns2"]).unwrap();
499+
let ns3 = NamespaceIdent::from_strs(vec!["ns1", "ns2", "ns3"]).unwrap();
500+
501+
assert_eq!(ns1.parent(), None);
502+
assert_eq!(ns2.parent(), Some(ns1.clone()));
503+
assert_eq!(ns3.parent(), Some(ns2.clone()));
504+
}
505+
483506
#[test]
484507
fn test_create_table_id() {
485508
let table_id = TableIdent {

0 commit comments

Comments
 (0)