From e8eeb9abe096b6a979e35b70ab85b218ebdcef63 Mon Sep 17 00:00:00 2001 From: Christian Thiel Date: Sun, 22 Sep 2024 16:10:01 +0200 Subject: [PATCH 1/2] Add NamespaceIdent.parent() --- crates/iceberg/src/catalog/mod.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/iceberg/src/catalog/mod.rs b/crates/iceberg/src/catalog/mod.rs index aa2311b4a..63be3a69c 100644 --- a/crates/iceberg/src/catalog/mod.rs +++ b/crates/iceberg/src/catalog/mod.rs @@ -133,6 +133,16 @@ impl NamespaceIdent { pub fn inner(self) -> Vec { 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 { + if self.0.len() <= 1 { + None + } else { + Some(Self(self.0[..self.0.len() - 1].to_vec())) + } + } } impl AsRef> for NamespaceIdent { @@ -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 { From 0a4584575b89d37485e74e98dd72667b51ca0e4e Mon Sep 17 00:00:00 2001 From: Christian Thiel Date: Mon, 23 Sep 2024 21:55:37 +0200 Subject: [PATCH 2/2] Use split_last --- crates/iceberg/src/catalog/mod.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/iceberg/src/catalog/mod.rs b/crates/iceberg/src/catalog/mod.rs index 63be3a69c..54abe8083 100644 --- a/crates/iceberg/src/catalog/mod.rs +++ b/crates/iceberg/src/catalog/mod.rs @@ -137,11 +137,13 @@ impl NamespaceIdent { /// 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 { - if self.0.len() <= 1 { - None - } else { - Some(Self(self.0[..self.0.len() - 1].to_vec())) - } + self.0.split_last().and_then(|(_, parent)| { + if parent.is_empty() { + None + } else { + Some(Self(parent.to_vec())) + } + }) } }