alamb commented on code in PR #10075:
URL: https://github.com/apache/arrow-rs/pull/10075#discussion_r3364769886


##########
arrow-schema/src/extension/canonical/variable_shape_tensor.rs:
##########
@@ -632,20 +621,16 @@ mod tests {
             ],
             false,
         )
-        .with_metadata(
-            [
-                (
-                    EXTENSION_TYPE_NAME_KEY.to_owned(),
-                    VariableShapeTensor::NAME.to_owned(),
-                ),
-                (
-                    EXTENSION_TYPE_METADATA_KEY.to_owned(),
-                    r#"{ "dim_names": [1, null, 3, 4] }"#.to_owned(),
-                ),
-            ]
-            .into_iter()
-            .collect(),
-        );
+        .with_metadata([
+            (
+                EXTENSION_TYPE_NAME_KEY,
+                VariableShapeTensor::NAME.to_owned(),

Review Comment:
   You may not need the `to_owned()`:
   
   ```rust
           .with_metadata([
               (
                   EXTENSION_TYPE_NAME_KEY,
                   VariableShapeTensor::NAME,
               ),
               (
                   EXTENSION_TYPE_METADATA_KEY,
                   r#"{ "dim_names": [1, null, 3, 4] }"#,
               ),
           ]);
   ```



##########
arrow-schema/src/metadata.rs:
##########
@@ -0,0 +1,454 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::{BTreeMap, HashMap, btree_map};
+use std::fmt;
+use std::ops::Index;
+use std::sync::Arc;
+
+/// A cheaply clonable map of key-value metadata, used by
+/// [`Field`](crate::Field) and [`Schema`](crate::Schema).
+///
+/// Cloning a `Metadata` is always cheap, as the underlying map is
+/// reference-counted. Mutating a shared `Metadata` (e.g. via
+/// [`Metadata::insert`]) will clone the underlying map if (and only if)
+/// it is shared (copy-on-write).
+///
+/// The entries are stored in a [`BTreeMap`], so iteration order is
+/// deterministic (sorted by key).
+///
+/// # Example
+/// ```
+/// # use arrow_schema::Metadata;
+/// let mut metadata = Metadata::new();
+/// metadata.insert("key", "value");
+///
+/// let clone = metadata.clone(); // cheap
+/// assert_eq!(clone.get("key"), Some(&"value".to_string()));
+///
+/// // Mutating one does not affect the other:

Review Comment:
   As a follow on PR it might be cool to add a builder style API too:
   ```rust
   let metadata = Metadata::builder()
     .with("key3", "value3")
     .build()
   ```
   
   That way then we could write
   
   ```rust
    .with_metadata([
               (
                   EXTENSION_TYPE_NAME_KEY,
                   VariableShapeTensor::NAME.to_owned(),
               ),
               (
                   EXTENSION_TYPE_METADATA_KEY,
                   r#"{ "dim_names": [1, null, 3, 4] }"#.to_owned(),
               ),
   ```
    something like
   
   ```rust
    .with_metadata(Metadata::builder()
       .with(EXTENSION_TYPE_NAME_KEY, VariableShapeTensor::NAME),
       .with(EXTENSION_TYPE_METADATA_KEY)
       .build()
     )
   ```
   



##########
arrow-schema/src/metadata.rs:
##########
@@ -0,0 +1,454 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::{BTreeMap, HashMap, btree_map};
+use std::fmt;
+use std::ops::Index;
+use std::sync::Arc;
+
+/// A cheaply clonable map of key-value metadata, used by
+/// [`Field`](crate::Field) and [`Schema`](crate::Schema).
+///
+/// Cloning a `Metadata` is always cheap, as the underlying map is
+/// reference-counted. Mutating a shared `Metadata` (e.g. via
+/// [`Metadata::insert`]) will clone the underlying map if (and only if)
+/// it is shared (copy-on-write).
+///
+/// The entries are stored in a [`BTreeMap`], so iteration order is
+/// deterministic (sorted by key).
+///
+/// # Example
+/// ```
+/// # use arrow_schema::Metadata;
+/// let mut metadata = Metadata::new();
+/// metadata.insert("key", "value");
+///
+/// let clone = metadata.clone(); // cheap
+/// assert_eq!(clone.get("key"), Some(&"value".to_string()));
+///
+/// // Mutating one does not affect the other:
+/// metadata.insert("key2", "value2");
+/// assert_eq!(metadata.len(), 2);
+/// assert_eq!(clone.len(), 1);
+/// ```
+#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub struct Metadata(
+    // Invariant: the inner map is never empty (`None` encodes the empty map).
+    // This ensures the derived implementations of `PartialEq`, `Ord`, `Hash`, 
…
+    // treat an empty `Metadata` consistently, and the empty case never 
allocates.
+    Option<Arc<BTreeMap<String, String>>>,

Review Comment:
   It might also be mentioning again here that it uses a BTreeMap to ensure 
consistent iteration order



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to