You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Rich-T-kid (via GitHub)" <gi...@apache.org> on 2026/08/15 02:37:30 UTC

[PR] implement retain on metadata [arrow-rs]

Rich-T-kid opened a new pull request, #10695:
URL: https://github.com/apache/arrow-rs/pull/10695

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax.
   -->
   
   - Closes #10683.
   
   # Rationale for this change
   
   see #10683
   
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   # What changes are included in this PR?
   implements `retain` on metadata. The underlying BTreeMap is allocation free unless the `Arc` pointer is shared
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are these changes tested?
   yes
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   
   If this PR claims a performance improvement, please include evidence such as benchmark results.
   -->
   
   # Are there any user-facing changes?
   yes, new `retain` method on the metadata struct
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   
   If there are any breaking changes to public APIs, please call them out.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] implement `Metadata::retain` [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #10695:
URL: https://github.com/apache/arrow-rs/pull/10695


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] implement retain on metadata [arrow-rs]

Posted by "Rich-T-kid (via GitHub)" <gi...@apache.org>.
Rich-T-kid commented on code in PR #10695:
URL: https://github.com/apache/arrow-rs/pull/10695#discussion_r3789599853


##########
arrow-schema/src/metadata.rs:
##########
@@ -146,6 +146,19 @@ impl Metadata {
     pub fn values(&self) -> impl Iterator<Item = &String> {
         self.iter().map(|(_, value)| value)
     }
+
+    /// Retains only the entries for which `f(&key, &mut value)` returns `true`.
+    ///
+    /// Entries for which `f` returns `false` are removed. Retained entries
+    /// remain in their original (sorted) order.
+    ///
+    /// Clones the underlying map if (and only if) it is shared.
+    pub fn retain<F>(&mut self, eq: F)
+    where
+        F: FnMut(&String, &mut String) -> bool,
+    {
+        Arc::make_mut(self.0.get_or_insert_default()).retain(eq);

Review Comment:
   ah good catch!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] implement retain on metadata [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #10695:
URL: https://github.com/apache/arrow-rs/pull/10695#discussion_r3789217137


##########
arrow-schema/src/metadata.rs:
##########
@@ -146,6 +146,19 @@ impl Metadata {
     pub fn values(&self) -> impl Iterator<Item = &String> {
         self.iter().map(|(_, value)| value)
     }
+
+    /// Retains only the entries for which `f(&key, &mut value)` returns `true`.
+    ///
+    /// Entries for which `f` returns `false` are removed. Retained entries
+    /// remain in their original (sorted) order.
+    ///
+    /// Clones the underlying map if (and only if) it is shared.
+    pub fn retain<F>(&mut self, eq: F)
+    where
+        F: FnMut(&String, &mut String) -> bool,
+    {
+        Arc::make_mut(self.0.get_or_insert_default()).retain(eq);

Review Comment:
   Does calling get_or_insert_default mean this method would turn a metadata that had a None inner map into aSome(empty map)?
   
   I think it would make more sense to do nothing if `self.0` is None 



##########
arrow-schema/src/metadata.rs:
##########
@@ -445,6 +458,35 @@ mod tests {
         assert_eq!(format!("{metadata:?}"), r#"{"a": "1", "b": "2"}"#);
         assert_eq!(format!("{:?}", Metadata::new()), "{}");
     }
+    #[test]
+    fn test_retain() {
+        let mut metadata =
+            Metadata::from([("a", "1"), ("b", "2"), ("c", "3"), ("d", "4"), ("e", "5")]);
+
+        metadata.retain(|k, _| -> bool { k >= &String::from("c") });
+
+        let result_map = Metadata::from([("c", "3"), ("d", "4"), ("e", "5")]);
+        assert_eq!(metadata, result_map)
+    }
+
+    #[test]

Review Comment:
   Can you please add a test that shows what happens when retain is called on a empty Metadata (`None`)? It should I think still be `None`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] implement retain on metadata [arrow-rs]

Posted by "Rich-T-kid (via GitHub)" <gi...@apache.org>.
Rich-T-kid commented on code in PR #10695:
URL: https://github.com/apache/arrow-rs/pull/10695#discussion_r3789604765


##########
arrow-schema/src/metadata.rs:
##########
@@ -445,6 +458,35 @@ mod tests {
         assert_eq!(format!("{metadata:?}"), r#"{"a": "1", "b": "2"}"#);
         assert_eq!(format!("{:?}", Metadata::new()), "{}");
     }
+    #[test]
+    fn test_retain() {
+        let mut metadata =
+            Metadata::from([("a", "1"), ("b", "2"), ("c", "3"), ("d", "4"), ("e", "5")]);
+
+        metadata.retain(|k, _| -> bool { k >= &String::from("c") });
+
+        let result_map = Metadata::from([("c", "3"), ("d", "4"), ("e", "5")]);
+        assert_eq!(metadata, result_map)
+    }
+
+    #[test]

Review Comment:
   also added a test for when `retain()` clears all the maps contents. the metadata becomes `None`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org