Repository navigation
perf: Optimize lpad(), rpad() for scalar args - #20657
Merged
Merged
Conversation
Contributor
Author
|
Benchmarks: |
Contributor
Author
|
Commenting to bring this back to the top of the Pending PRs list 😊 |
martin-g
self-requested a review
March 18, 2026 21:04
martin-g
reviewed
Mar 18, 2026
…alars # Conflicts: # datafusion/functions/src/unicode/lpad.rs # datafusion/functions/src/unicode/rpad.rs
Contributor
Author
|
BTW, I debated whether it is actually worth the trouble to cap the padding buffer's size. For small inputs with very large target lengths, the padding buffer's relative memory use will be high, but it should be fine in most other cases. If anyone has a view, lmk. |
lpad(), rpad() for scalar args
martin-g
approved these changes
Mar 20, 2026
timsaucer
approved these changes
Mar 20, 2026
timsaucer
left a comment
Member
There was a problem hiding this comment.
Thanks for another excellent PR!
haohuaijin
pushed a commit
to haohuaijin/arrow-datafusion
that referenced
this pull request
Mar 21, 2026
## Which issue does this PR close? - Closes apache#20655. ## Rationale for this change `lpad` and `rpad` are commonly called with scalar length and fill arguments, e.g. `lpad(column, 20, '0')`. We can special-case this scenario to improve performance by avoiding the overhead of `make_scalar_function` and by precomputing the padding buffer and reusing it for each row. For scalar args, this improves performance by ~3x for ASCII inputs and ~1.6x for Unicode inputs. ## What changes are included in this PR? - Add benchmarks for padding with scalar length and fill. - Add a scalar fast path for `lpad` and `rpad` that precomputes a padding buffer. We only use the fast path if the pad length is reasonably small (<= 16KB), to avoid using too much memory on a scratch buffer. - Code cleanup: extract and use `try_as_scalar_str` and `try_as_scalar_i64` helpers. - Code cleanup: use `target_len` consistently instead of `length`, because the latter is ambiguous. - Code cleanup: make `rpad` and `lpad` more similar by removing needless and probably unintended differences between the two functions. We could go further and refactor them to remove the redundancy but I won't attempt that for now. ## Are these changes tested? Yes; covered by existing tests. Added new benchmarks. ## Are there any user-facing changes? No. ## AI usage Multiple AI tools were used to iterate on this PR. I have reviewed and understand the resulting code.
de-bgunter
pushed a commit
to de-bgunter/datafusion
that referenced
this pull request
Mar 24, 2026
## Which issue does this PR close? - Closes apache#20655. ## Rationale for this change `lpad` and `rpad` are commonly called with scalar length and fill arguments, e.g. `lpad(column, 20, '0')`. We can special-case this scenario to improve performance by avoiding the overhead of `make_scalar_function` and by precomputing the padding buffer and reusing it for each row. For scalar args, this improves performance by ~3x for ASCII inputs and ~1.6x for Unicode inputs. ## What changes are included in this PR? - Add benchmarks for padding with scalar length and fill. - Add a scalar fast path for `lpad` and `rpad` that precomputes a padding buffer. We only use the fast path if the pad length is reasonably small (<= 16KB), to avoid using too much memory on a scratch buffer. - Code cleanup: extract and use `try_as_scalar_str` and `try_as_scalar_i64` helpers. - Code cleanup: use `target_len` consistently instead of `length`, because the latter is ambiguous. - Code cleanup: make `rpad` and `lpad` more similar by removing needless and probably unintended differences between the two functions. We could go further and refactor them to remove the redundancy but I won't attempt that for now. ## Are these changes tested? Yes; covered by existing tests. Added new benchmarks. ## Are there any user-facing changes? No. ## AI usage Multiple AI tools were used to iterate on this PR. I have reviewed and understand the resulting code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
lpadandrpadare commonly called with scalar length and fill arguments, e.g.lpad(column, 20, '0'). We can special-case this scenario to improve performance by avoiding the overhead ofmake_scalar_functionand by precomputing the padding buffer and reusing it for each row.For scalar args, this improves performance by ~3x for ASCII inputs and ~1.6x for Unicode inputs.
What changes are included in this PR?
lpadandrpadthat precomputes a padding buffer. We only use the fast path if the pad length is reasonably small (<= 16KB), to avoid using too much memory on a scratch buffer.try_as_scalar_strandtry_as_scalar_i64helpers.target_lenconsistently instead oflength, because the latter is ambiguous.rpadandlpadmore similar by removing needless and probably unintended differences between the two functions. We could go further and refactor them to remove the redundancy but I won't attempt that for now.Are these changes tested?
Yes; covered by existing tests. Added new benchmarks.
Are there any user-facing changes?
No.
AI usage
Multiple AI tools were used to iterate on this PR. I have reviewed and understand the resulting code.