perf: Improve rename performace for Lazy API #18890
Merged
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.
Closes #17336
For the python rename API, if we pass a dictionary arguments, we convert to LazyFrame and internally call the rename operation; the issue is that we need to get index number for all the columns we are renaming; this is an O(n^2) operation.
If we pass a function as argument to rename, we convert to LazyFrame and internally call the select operation; here we get the index for each column from the Schema, which is an IndexMap and hence overall the operation becomes O(n).
Have tried to fix this issue in a similar fashion by constructing the schema first and then getting index from it.
After the fix :
Time for rename(): 0.041061488911509514 | Version: 1.7.1
Time for rename(): 0.06575666600838304 | Version: 1.7.1
In the Eager API for Rust, this was fixed in #1028 by implementing a hash map.