This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-rust.git
The following commit(s) were added to refs/heads/main by this push:
new b3709baa feat: Add NamespaceIdent.parent() (#641)
b3709baa is described below
commit b3709baaabf1260571f0b7d3e54ea258f4baa357
Author: Christian <[email protected]>
AuthorDate: Tue Sep 24 04:47:04 2024 +0200
feat: Add NamespaceIdent.parent() (#641)
* Add NamespaceIdent.parent()
* Use split_last
---
crates/iceberg/src/catalog/mod.rs | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/crates/iceberg/src/catalog/mod.rs
b/crates/iceberg/src/catalog/mod.rs
index aa2311b4..54abe808 100644
--- a/crates/iceberg/src/catalog/mod.rs
+++ b/crates/iceberg/src/catalog/mod.rs
@@ -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 {
@@ -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 {