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

[GitHub] [arrow-rs] tustvold opened a new pull request, #3831: Make dictionary preservation optional in row encoding

tustvold opened a new pull request, #3831:
URL: https://github.com/apache/arrow-rs/pull/3831

   # 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. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #.
   
   # Rationale for this change
    
   <!--
   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.
   -->
   
   As discussed on https://github.com/apache/arrow-datafusion/issues/4973#issuecomment-1460990408 the dictionary preserving row encoding is particularly expensive to compute and may not be appropriate for all use-cases. This PR therefore adds an option to instead encode the dictionary values directly, instead of constructing an order preserving dictionary.
   
   ```
   convert_columns 4096 string_dictionary(100, 0)
                           time:   [637.45 µs 637.75 µs 638.08 µs]
   
   convert_columns_prepared 4096 string_dictionary(100, 0)
                           time:   [141.49 µs 141.54 µs 141.60 µs]
   
   convert_columns 4096 string_dictionary_hydrate(100, 0)
                           time:   [89.498 µs 89.546 µs 89.594 µs]
   
   convert_columns_prepared 4096 string_dictionary_hydrate(100, 0)
                           time:   [87.264 µs 87.292 µs 87.323 µs]
   ```
   
   This can be significantly faster, especially when seeing values for the first time (the unprepared case above), however, does come with the potential downside of a significantly less efficient encoding, which in turn needs more memory and may make comparisons slower.
   
   # What changes are included in this PR?
   
   <!--
   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 there any user-facing changes?
   
   
   <!--
   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 add the `breaking change` label.
   -->
   


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


[GitHub] [arrow-rs] tustvold merged pull request #3831: Make dictionary preservation optional in row encoding

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


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


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

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


##########
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:
   Heh, I have a very bad reason why I did this... Formatting :laughing: 



##########
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:
   Heh, I have a very bad reason why I did this... Formatting :laughing: I'll work around it differently
   



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


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

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

   Looks good to me -- nice work


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


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

Posted by "ursabot (via GitHub)" <gi...@apache.org>.
ursabot commented on PR #3831:
URL: https://github.com/apache/arrow-rs/pull/3831#issuecomment-1463834079

   Benchmark runs are scheduled for baseline = 61c4f12e84330db243789fc98375512d67628e57 and contender = 69c04db962b915b8a2d3783853da0ce95a94c0ef. 69c04db962b915b8a2d3783853da0ce95a94c0ef is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
   Conbench compare runs links:
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/6b9af55d74314e54b67c097c96f416d3...b4dae3560c2244aea4c171c25f446563/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/511b4189f35c41d39d0c18e8d86a9134...1288429c644c47e5b80daef2612e7363/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/96be0179aaca4ef2b06dba6000f5379b...54ded4dd4a504938bc2ad733773677ce/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/4aed29ee25f840c78bb9e1d5a714d671...652c17af1aa4494cb818fe717729e04f/)
   Buildkite builds:
   Supported benchmarks:
   ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
   test-mac-arm: Supported benchmark langs: C++, Python, R
   ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
   ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
   


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


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

Posted by "alamb (via GitHub)" <gi...@apache.org>.
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