You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "neilconway (via GitHub)" <gi...@apache.org> on 2026/04/25 22:29:02 UTC

[PR] perf: Add `BulkNullStringArrayBuilder` trait, use in `repeat` [datafusion]

neilconway opened a new pull request, #21854:
URL: https://github.com/apache/datafusion/pull/21854

   Add a DataFusion-side trait that abstracts over the bulk-NULL string array builders (GenericStringArrayBuilder<O> and StringViewArrayBuilder), so that functions which dispatch over Utf8/LargeUtf8/Utf8View can adopt the new builders without giving up their single-bodied generic implementation.
   
   Convert `repeat` as the first call site. The output is null iff either input is null, so the per-row null match becomes a single NullBuffer::union over the input null buffers, evaluated once before the loop.
   
   Also mark the inherent append_value/append_placeholder methods on the new builders as #[inline]; without this, calls through the trait wrapper end up going through a non-inlined inherent and slow down small-output paths.
   
   ## Which issue does this PR close?
   
   - Closes #21853.
   
   ## Rationale for this change
   
   Optimize NULL handling in `repeat` using the bulk-NULL string builders that have recently been added. This requires adding `BulkNullStringArrayBuilder`, a trait that is similar in spirit to Arrow's `StringLikeArrayBuilder`.
   
   Benchmarks:
   
     - repeat_string overflow [size=1024, repeat_times=1073741824]: 1022.5ns → 1054.5ns (+3.13%)
     - repeat_string overflow [size=4096, repeat_times=1073741824]: 1016.6ns → 1055.3ns (+3.81%)
     - repeat_large_string [size=1024, repeat_times=3]: 32.4µs → 26.6µs (−17.90%)
     - repeat_large_string [size=4096, repeat_times=3]: 127.4µs → 104.0µs (−18.37%)
     - repeat_string [size=1024, repeat_times=3]: 32.6µs → 26.8µs (−17.79%)
     - repeat_string [size=4096, repeat_times=3]: 127.4µs → 105.5µs (−17.19%)
     - repeat_string_view [size=1024, repeat_times=3]: 37.3µs → 31.7µs (−15.01%)
     - repeat_string_view [size=4096, repeat_times=3]: 146.5µs → 124.5µs (−15.02%)
     - repeat_large_string [size=1024, repeat_times=30]: 82.0µs → 80.4µs (−1.95%)
     - repeat_large_string [size=4096, repeat_times=30]: 344.2µs → 338.7µs (−1.60%)
     - repeat_string [size=1024, repeat_times=30]: 81.7µs → 79.7µs (−2.45%)
     - repeat_string [size=4096, repeat_times=30]: 352.2µs → 334.7µs (−4.97%)
     - repeat_string_view [size=1024, repeat_times=30]: 88.1µs → 83.1µs (−5.68%)
     - repeat_string_view [size=4096, repeat_times=30]: 368.8µs → 342.6µs (−7.10%)
     - repeat/scalar_utf8: 174.7ns → 179.2ns (+2.58%)
     - repeat/scalar_utf8view: 174.5ns → 180.5ns (+3.44%)
   
   ## What changes are included in this PR?
   
   * Add `BulkNullStringArrayBuilder`
   * Optimize `repeat` using `BulkNullStringArrayBuilder`
   * Inline some functions in GenericStringBuilder; benchmarking suggests this is a win
   
   ## Are these changes tested?
   
   Yes.
   
   ## Are there any user-facing changes?
   
   No.
   


-- 
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: Add `BulkNullStringArrayBuilder` trait, use in `repeat` [datafusion]

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


##########
datafusion/functions/src/string/repeat.rs:
##########
@@ -301,12 +285,18 @@ where
         }
     }
 
-    // Fast path: no nulls in either array
-    if string_array.null_count() == 0 && number_array.null_count() == 0 {
+    // Output is null IFF either input is null
+    let nulls = NullBuffer::union(string_array.nulls(), number_array.nulls());

Review Comment:
   It is a very widespread assumption that all of the Array args to a UDF will be the same length; if that wasn't the case, a _lot_ of things would break in all sorts of unsightly ways. If we think there's a reason to program defensively around that assumption, we'd need to change a _lot_ of places to validate that condition consistently; doing it in just one place seems hard to justify IMO.



-- 
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: Add `BulkNullStringArrayBuilder` trait, use in `repeat` [datafusion]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on PR #21854:
URL: https://github.com/apache/datafusion/pull/21854#issuecomment-4350737527

   Thank you, @neilconway & @Weijun-H !


-- 
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: Add `BulkNullStringArrayBuilder` trait, use in `repeat` [datafusion]

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


-- 
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: Add `BulkNullStringArrayBuilder` trait, use in `repeat` [datafusion]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #21854:
URL: https://github.com/apache/datafusion/pull/21854#discussion_r3157008581


##########
datafusion/functions/src/string/repeat.rs:
##########
@@ -301,12 +285,18 @@ where
         }
     }
 
-    // Fast path: no nulls in either array
-    if string_array.null_count() == 0 && number_array.null_count() == 0 {
+    // Output is null IFF either input is null
+    let nulls = NullBuffer::union(string_array.nulls(), number_array.nulls());

Review Comment:
   `NullBuffer::union()` could panic if the buffers have different lengths - https://docs.rs/arrow-buffer/58.1.0/src/arrow_buffer/buffer/boolean.rs.html#642
   
   Since the method returns Result I wonder whether it would be better to check manually the lengths and return a plan_err! ?!



-- 
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: Add `BulkNullStringArrayBuilder` trait, use in `repeat` [datafusion]

Posted by "Weijun-H (via GitHub)" <gi...@apache.org>.
Weijun-H commented on code in PR #21854:
URL: https://github.com/apache/datafusion/pull/21854#discussion_r3142922040


##########
datafusion/functions/src/string/repeat.rs:
##########
@@ -301,12 +285,18 @@ where
         }
     }
 
-    // Fast path: no nulls in either array
-    if string_array.null_count() == 0 && number_array.null_count() == 0 {
+    // Output is null IFF either input is null
+    let nulls = NullBuffer::union(string_array.nulls(), number_array.nulls());

Review Comment:
   Could we add array-level tests for this path? The existing repeat tests mostly cover scalar inputs, while this change primarily affects array execution and bulk null handling.
   
   It would be useful to cover Utf8/LargeUtf8/Utf8View arrays with interleaved nulls, one-sided nulls, and a sliced array with a non-zero null bitmap offset.
   



-- 
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: Add `BulkNullStringArrayBuilder` trait, use in `repeat` [datafusion]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #21854:
URL: https://github.com/apache/datafusion/pull/21854#discussion_r3157008581


##########
datafusion/functions/src/string/repeat.rs:
##########
@@ -301,12 +285,18 @@ where
         }
     }
 
-    // Fast path: no nulls in either array
-    if string_array.null_count() == 0 && number_array.null_count() == 0 {
+    // Output is null IFF either input is null
+    let nulls = NullBuffer::union(string_array.nulls(), number_array.nulls());

Review Comment:
   `NullBuffer::union()` could panic if the buffers have different lengths - https://docs.rs/arrow-buffer/58.1.0/src/arrow_buffer/buffer/boolean.rs.html#642
   
   Since the method returns Result I wonder whether it would be better to check manually the lengths and return an exec_err! ?!



-- 
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: Add `BulkNullStringArrayBuilder` trait, use in `repeat` [datafusion]

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


##########
datafusion/functions/src/string/repeat.rs:
##########
@@ -301,12 +285,18 @@ where
         }
     }
 
-    // Fast path: no nulls in either array
-    if string_array.null_count() == 0 && number_array.null_count() == 0 {
+    // Output is null IFF either input is null
+    let nulls = NullBuffer::union(string_array.nulls(), number_array.nulls());

Review Comment:
   Thanks, good suggestion! Done.



-- 
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: Add `BulkNullStringArrayBuilder` trait, use in `repeat` [datafusion]

Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on PR #21854:
URL: https://github.com/apache/datafusion/pull/21854#issuecomment-4335813436

   @martin-g If you have a sec, can you take a look at this PR as well?


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