You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "lyne7-sc (via GitHub)" <gi...@apache.org> on 2026/02/15 05:41:17 UTC

[PR] perf: optimize `array_distinct` with batched row conversion [datafusion]

lyne7-sc opened a new pull request, #20364:
URL: https://github.com/apache/datafusion/pull/20364

   ## 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.  
   -->
   
   This PR optimizes the `array_distinct` function by batching value conversions and utilizing a `HashSet` for deduplication. 
   It is a follow-up to https://github.com/apache/datafusion/pull/20243.
   
   ## What changes are included in this PR?
   
   This PR optimizes `array_distinct` by:
   1. Converting all values to rows in a single batch rather than individually.
   2. Using a HashSet to deduplicate values for each list.
   
   ### Benchmark
   ```
   group                                main                                   optimized
   -----                                ----                                   ---------
   array_distinct/high_duplicate/10     2.66   855.1±28.18µs        ? ?/sec    1.00    321.9±8.70µs        ? ?/sec
   array_distinct/high_duplicate/100    2.21      6.4±0.13ms        ? ?/sec    1.00      2.9±0.09ms        ? ?/sec
   array_distinct/high_duplicate/50     2.14      3.2±0.05ms        ? ?/sec    1.00  1478.3±41.90µs        ? ?/sec
   array_distinct/low_duplicate/10      2.73  1017.3±44.67µs        ? ?/sec    1.00   372.5±17.33µs        ? ?/sec
   array_distinct/low_duplicate/100     1.32      4.4±0.13ms        ? ?/sec    1.00      3.3±0.15ms        ? ?/sec
   array_distinct/low_duplicate/50      1.55      2.6±0.06ms        ? ?/sec    1.00  1689.0±94.15µs        ? ?/sec
   ```
   
   <!--
   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?
   
   <!--
   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)?
   -->
   
   Yes, unit tests exist and pass.
   
   ## Are there any user-facing changes?
   
   Yes, there is a slight change in the output order. This new behavior is consistent with `array_union` and `array_intersect`, where the output order is more intuitive as it preserves the original order of elements in the array.
   
   <!--
   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 `api 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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: optimize `array_distinct` with batched row conversion [datafusion]

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on code in PR #20364:
URL: https://github.com/apache/datafusion/pull/20364#discussion_r2809091594


##########
datafusion/functions-nested/src/set_ops.rs:
##########
@@ -527,42 +531,52 @@ fn general_array_distinct<OffsetSize: OffsetSizeTrait>(
     if array.is_empty() {
         return Ok(Arc::new(array.clone()) as ArrayRef);
     }
+    let value_offsets = array.value_offsets();
     let dt = array.value_type();
-    let mut offsets = Vec::with_capacity(array.len());
+    let mut offsets = Vec::with_capacity(array.len() + 1);
     offsets.push(OffsetSize::usize_as(0));
-    let mut new_arrays = Vec::with_capacity(array.len());
-    let converter = RowConverter::new(vec![SortField::new(dt)])?;
-    // distinct for each list in ListArray
-    for arr in array.iter() {
-        let last_offset: OffsetSize = offsets.last().copied().unwrap();
-        let Some(arr) = arr else {
-            // Add same offset for null
+
+    // Convert all values to row format in a single batch for performance
+    let converter = RowConverter::new(vec![SortField::new(dt.clone())])?;
+    let rows = converter.convert_columns(&[Arc::clone(array.values())])?;

Review Comment:
   I think as a follow-up, it might reuse the `Rows` and `HashSet` allocations between batches.



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: optimize `array_distinct` with batched row conversion [datafusion]

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan merged PR #20364:
URL: https://github.com/apache/datafusion/pull/20364


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: optimize `array_distinct` with batched row conversion [datafusion]

Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #20364:
URL: https://github.com/apache/datafusion/pull/20364#discussion_r2810352004


##########
datafusion/functions-nested/src/set_ops.rs:
##########
@@ -527,42 +531,52 @@ fn general_array_distinct<OffsetSize: OffsetSizeTrait>(
     if array.is_empty() {
         return Ok(Arc::new(array.clone()) as ArrayRef);
     }
+    let value_offsets = array.value_offsets();
     let dt = array.value_type();
-    let mut offsets = Vec::with_capacity(array.len());
+    let mut offsets = Vec::with_capacity(array.len() + 1);
     offsets.push(OffsetSize::usize_as(0));
-    let mut new_arrays = Vec::with_capacity(array.len());
-    let converter = RowConverter::new(vec![SortField::new(dt)])?;
-    // distinct for each list in ListArray
-    for arr in array.iter() {
-        let last_offset: OffsetSize = offsets.last().copied().unwrap();
-        let Some(arr) = arr else {
-            // Add same offset for null
+
+    // Convert all values to row format in a single batch for performance
+    let converter = RowConverter::new(vec![SortField::new(dt.clone())])?;
+    let rows = converter.convert_columns(&[Arc::clone(array.values())])?;

Review Comment:
   Thanks for the suggestion! I'll look into it and try reusing the allocations. Not sure if thread_local will help, but any tips or better approaches you'd recommend?



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org