You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/03/10 11:24:22 UTC

[GitHub] [arrow-rs] alamb commented on a diff in pull request #3831: Make dictionary preservation optional in row encoding

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


##########
arrow-row/src/lib.rs:
##########
@@ -561,7 +603,28 @@ impl SortField {
 
     /// Create a new column with the given data type and [`SortOptions`]
     pub fn new_with_options(data_type: DataType, options: SortOptions) -> Self {
-        Self { options, data_type }
+        Self {
+            options,
+            data_type,
+            preserve_dictionaries: true,
+        }
+    }
+
+    /// By default dictionaries are preserved as described on [`RowConverter`]
+    ///
+    /// However, this process requires maintaining and incrementally updating
+    /// an order-preserving mapping of dictionary values which is relatively expensive

Review Comment:
   ```suggestion
       /// an order-preserving mapping of dictionary values which is relatively expensive
       /// computationally but minimizes memory used.
   ```



##########
arrow-row/src/lib.rs:
##########
@@ -1557,8 +1643,25 @@ mod tests {
         assert_eq!(&cols[0], &col);
     }
 
+    /// If `exact` is false performs a logical comparison between a and dictionary-encoded b
+    fn dictionary_eq(exact: bool, a: &dyn Array, b: &dyn Array) {
+        match b.data_type() {
+            DataType::Dictionary(_, v) if !exact => {
+                assert_eq!(a.data_type(), v.as_ref());
+                let b = arrow_cast::cast(b, v).unwrap();
+                assert_eq!(a.data(), b.data())
+            }
+            _ => assert_eq!(a.data(), b.data()),
+        }
+    }
+
     #[test]
     fn test_string_dictionary() {
+        test_string_dictionary_preserve(false);
+        test_string_dictionary_preserve(true);
+    }
+
+    fn test_string_dictionary_preserve(preserve: bool) {

Review Comment:
   the repeated `preserve` was a little confusing at first -- as I would expect `test_string_dictionary_preserve` to mean testing preserve = true.
   
   totally minor nitpick but maybe it could be renamed to `test_string_dictionary`



##########
arrow/benches/row_format.rs:
##########
@@ -57,42 +65,44 @@ fn do_bench(c: &mut Criterion, name: &str, cols: Vec<ArrayRef>) {
 
 fn row_bench(c: &mut Criterion) {
     let cols = vec![Arc::new(create_primitive_array::<UInt64Type>(4096, 0.)) as ArrayRef];
-    do_bench(c, "4096 u64(0)", cols);
+    do_bench(c, "4096 u64(0)", cols, true);
 
     let cols = vec![Arc::new(create_primitive_array::<Int64Type>(4096, 0.)) as ArrayRef];
-    do_bench(c, "4096 i64(0)", cols);
+    do_bench(c, "4096 i64(0)", cols, true);
 
     let cols =
         vec![Arc::new(create_string_array_with_len::<i32>(4096, 0., 10)) as ArrayRef];
-    do_bench(c, "4096 string(10, 0)", cols);
+    do_bench(c, "4096 string(10, 0)", cols, true);
 
     let cols =
         vec![Arc::new(create_string_array_with_len::<i32>(4096, 0., 30)) as ArrayRef];
-    do_bench(c, "4096 string(30, 0)", cols);
+    do_bench(c, "4096 string(30, 0)", cols, true);
 
     let cols =
         vec![Arc::new(create_string_array_with_len::<i32>(4096, 0., 100)) as ArrayRef];
-    do_bench(c, "4096 string(100, 0)", cols);
+    do_bench(c, "4096 string(100, 0)", cols, true);
 
     let cols =
         vec![Arc::new(create_string_array_with_len::<i32>(4096, 0.5, 100)) as ArrayRef];
-    do_bench(c, "4096 string(100, 0.5)", cols);
+    do_bench(c, "4096 string(100, 0.5)", cols, true);
 
     let cols =
         vec![Arc::new(create_string_dict_array::<Int32Type>(4096, 0., 10)) as ArrayRef];
-    do_bench(c, "4096 string_dictionary(10, 0)", cols);
+    do_bench(c, "4096 string_dictionary(10, 0)", cols, true);
 
     let cols =
         vec![Arc::new(create_string_dict_array::<Int32Type>(4096, 0., 30)) as ArrayRef];
-    do_bench(c, "4096 string_dictionary(30, 0)", cols);
+    do_bench(c, "4096 string_dictionary(30, 0)", cols, true);
 
     let cols =
         vec![Arc::new(create_string_dict_array::<Int32Type>(4096, 0., 100)) as ArrayRef];
-    do_bench(c, "4096 string_dictionary(100, 0)", cols);
+    do_bench(c, "4096 string_dictionary(100, 0)", cols.clone(), true);
+    do_bench(c, "4096 string_dictionary_hydrate(100, 0)", cols, false);
 
     let cols =
         vec![Arc::new(create_string_dict_array::<Int32Type>(4096, 0.5, 100)) as ArrayRef];
-    do_bench(c, "4096 string_dictionary(100, 0.5)", cols);
+    do_bench(c, "4096 string_dictionary(100, 0.5)", cols.clone(), true);
+    do_bench(c, "4096 string_dictionary_hydrate(100, 0.5)", cols, false);

Review Comment:
   rather than using `hydrate` which I think is a somewhat non standard term, maybe this could be `non_preserve` to match the parameter name



##########
arrow-row/src/lib.rs:
##########
@@ -2160,9 +2277,9 @@ mod tests {
             }
 
             let back = converter.convert_rows(&rows).unwrap();
-            for (actual, expected) in back.iter().zip(&arrays) {
+            for ((actual, expected), preserve) in back.iter().zip(&arrays).zip(preserve) {
                 actual.data().validate_full().unwrap();
-                assert_eq!(actual, expected)
+                dictionary_eq(preserve, actual, expected)
             }
         }
     }

Review Comment:
   All these tests seem to verify that the values are the same. I wonder if it would be valuable to cover space efficiency too in a test (like encode using preserved and non preserved dictionaries and show them taking up different sizes 🤔 )



-- 
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